Выбрать главу

// Supplying extra info requires adding more data to the type.

Parse(ParseIntError),

}

impl fmt::Display for DoubleError {

fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

match *self {

DoubleError::EmptyVec =>

write!(f, "please use a vector with at least one element"),

// The wrapped error contains additional information and is available

// via the source() method.

DoubleError::Parse(..) =>

write!(f, "the provided string could not be parsed as int"),

}

}

}

impl error::Error for DoubleError {

fn source(&self) -> Option<&(dyn error::Error + 'static)> {

match *self {

DoubleError::EmptyVec => None,

// The cause is the underlying implementation error type. Is implicitly

// cast to the trait object `&error::Error`. This works because the

// underlying type already implements the `Error` trait.

DoubleError::Parse(ref e) => Some(e),

}

}

}

// Implement the conversion from `ParseIntError` to `DoubleError`.

// This will be automatically called by `?` if a `ParseIntError`

// needs to be converted into a `DoubleError`.

impl From<ParseIntError> for DoubleError {

fn from(err: ParseIntError) -> DoubleError {

DoubleError::Parse(err)

}

}

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

let first = vec.first().ok_or(DoubleError::EmptyVec)?;

// Here we implicitly use the `ParseIntError` implementation of `From` (which

// we defined above) in order to create a `DoubleError`.

let parsed = first.parse::<i32>()?;

Ok(2 * parsed)

}

fn print(result: Result<i32>) {

match result {

Ok(n) => println!("The first doubled is {}", n),

Err(e) => {

println!("Error: {}", e);

if let Some(source) = e.source() {

println!(" Caused by: {}", source);

}

},

}

}

fn main() {

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

let empty = vec![];

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

print(double_first(numbers));

print(double_first(empty));

print(double_first(strings));

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

This adds a bit more boilerplate for handling errors and might not be needed in all applications. There are some libraries that can take care of the boilerplate for you.

From::from and Enums

An Iter::map operation might fail, for example:

fn main() {

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

let numbers: Vec<_> = strings

.into_iter()

.map(|s| s.parse::<i32>())

.collect();

println!("Results: {:?}", numbers);

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Let's step through strategies for handling this.

filter_map calls a function and filters out the results that are None.

fn main() {

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

let numbers: Vec<_> = strings

.into_iter()

.filter_map(|s| s.parse::<i32>().ok())

.collect();

println!("Results: {:?}", numbers);

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Result implements FromIter so that a vector of results (Vec<Result<T, E>>) can be turned into a result with a vector (Result<Vec<T>, E>). Once an Result::Err is found, the iteration will terminate.