Извлечение Result из Option

Наиболее простой способ обработки ошибок разных типов - это встраивание их друг в друга.

use std::num::ParseIntError;

fn double_first(vec: Vec<&str>) -> Option> {

vec.first().map(|first| {

first.parse::().map(|n| 2 * n)

})

}

fn main() {

let numbers = vec!["42", "93", "18"];

let empty = vec![];

let strings = vec!["tofu", "93", "18"];

println!("Первое удвоенное: {:?}", double_first(numbers));

println!("Первое удвоенное: {:?}", double_first(empty));

// Ошибка первая: исходный вектор пустой

println!("Первое удвоенное {:?}", double_first(strings));

// Ошибка вторая: элемент не переводится в число

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Бывает, мы хотим приостановить работу при ошибке (как при помощи оператора ?), но продолжать работать, если Option None. Есть пара комбинаторов, которые поменяют местами Result и Option.

use std::num::ParseIntError;

fn double_first(vec: Vec<&str>) -> Result, ParseIntError> {

let opt = vec.first().map(|first| {

first.parse::().map(|n| 2 * n)

});

opt.map_or(Ok(None), |r| r.map(Some))

}

fn main() {

let numbers = vec!["42", "93", "18"];

let empty = vec![];

let strings = vec!["tofu", "93", "18"];

println!("The first doubled is {:?}", double_first(numbers));

println!("The first doubled is {:?}", double_first(empty));

println!("The first doubled is {:?}", double_first(strings));

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Загрузка...