rust-ape-example/src/bin/error_result_enter_question_mark.rs
2022-09-07 11:36:05 +05:30

29 lines
596 B
Rust

// ./src/error/result/enter_question_mark.md
use std::num::ParseIntError;
fn multiply(first_number_str: &str, second_number_str: &str) -> Result<i32, ParseIntError> {
let first_number = first_number_str.parse::<i32>()?;
let second_number = second_number_str.parse::<i32>()?;
Ok(first_number * second_number)
}
fn print(result: Result<i32, ParseIntError>) {
match result {
Ok(n) => println!("n is {}", n),
Err(e) => println!("Error: {}", e),
}
}
fn part0() {
print(multiply("10", "2"));
print(multiply("t", "2"));
}
pub fn main() {
part0();
}