Rust by Example — страница 47 из 66

// Modify n if the value is valid, otherwise pass on the error.

fn multiply(first_number_str: &str, second_number_str: &str) -> Result {

first_number_str.parse::().and_then(|first_number| {

second_number_str.parse::().map(|second_number| first_number * second_number)

})

}

fn print(result: Result) {

match result {

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

Err(e) => println!("Error: {}", e),

}

}

fn main() {

// This still presents a reasonable answer.

let twenty = multiply("10", "2");

print(twenty);

// The following now provides a much more helpful error message.

let tt = multiply("t", "2");

print(tt);

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

aliases forResult

How about when we want to reuse a specific Result type many times? Recall that Rust allows us to create aliases. Conveniently, we can define one for the specific Result in question.

At a module level, creating aliases can be particularly helpful. Errors found in a specific module often have the same Err type, so a single alias can succinctly define all associated Results. This is so useful that the std library even supplies one: io::Result!

Here's a quick example to show off the syntax:

use std::num::ParseIntError;

// Define a generic alias for a `Result` with the error type `ParseIntError`.

type AliasedResult = Result;

// Use the above alias to refer to our specific `Result` type.

fn multiply(first_number_str: &str, second_number_str: &str) -> AliasedResult {

first_number_str.parse::().and_then(|first_number| {

second_number_str.parse::().map(|second_number| first_number * second_number)

})

}

// Here, the alias again allows us to save some space.

fn print(result: AliasedResult) {

match result {

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

Err(e) => println!("Error: {}", e),

}

}

fn main() {

print(multiply("10", "2"));

print(multiply("t", "2"));

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

See also:

io::Result

Early returns

In the previous example, we explicitly handled the errors using combinators. Another way to deal with this case analysis is to use a combination of match statements and early returns.

That is, we can simply stop executing the function and return the error if one occurs. For some, this form of code can be easier to both read and write. Consider this version of the previous example, rewritten using early returns:

use std::num::ParseIntError;

fn multiply(first_number_str: &str, second_number_str: &str) -> Result {

let first_number = match first_number_str.parse::() {

Ok(first_number)  => first_number,

Err(e) => return Err(e),

};

let second_number = match second_number_str.parse::() {

Ok(second_number)  => second_number,

Err(e) => return Err(e),

};

Ok(first_number * second_number)

}

fn print(result: Result) {

match result {

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

Err(e) => println!("Error: {}", e),

}

}

fn main() {

print(multiply("10", "2"));

print(multiply("t", "2"));

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

At this point, we've learned to explicitly handle errors using combinators and early returns. While we generally want to avoid panicking, explicitly handling all of our errors is cumbersome.

In the next section, we'll introduce ? for the cases where we simply need to unwrap without possibly inducing panic.

Introducing?

Sometimes we just want the simplicity of unwrap without the possibility of a panic. Until now, unwrap has forced us to nest deeper and deeper when what we really wanted was to get the variable out. This is exactly the purpose of ?.

Upon finding an Err, there are two valid actions to take:

   1. panic! which we already decided to try to avoid if possible

   2. return because an Err means it cannot be handled

? is almost exactly equivalent to an unwrap which returns instead of panicking on Errs. Let's see how we can simplify the earlier example that used combinators:

use std::num::ParseIntError;

fn multiply(first_number_str: &str, second_number_str: &str) -> Result {

let first_number = first_number_str.parse::()?;

let second_number = second_number_str.parse::()?;

Ok(first_number * second_number)

}

fn print(result: Result) {

match result {

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

Err(e) => println!("Error: {}", e),

}

}

fn main() {

print(multiply("10", "2"));

print(multiply("t", "2"));

}

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