引入 ?

    1. panic!,不过我们已经决定要尽可能避免 panic 了。
    2. 返回它,因为 Err 就意味着它已经不能被处理了。
    1. use std::num::ParseIntError;
    2. fn multiply(first_number_str: &str, second_number_str: &str) -> Result<i32, ParseIntError> {
    3. let first_number = try!(first_number_str.parse::<i32>());
    4. Ok(first_number * second_number)
    5. fn print(result: Result<i32, ParseIntError>) {
    6. match result {
    7. Ok(n) => println!("n is {}", n),
    8. }
    9. }
    10. fn main() {
    11. print(multiply("10", "2"));
    12. print(multiply("t", "2"));
    13. }