mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-05-28 16:28:08 +00:00
54 lines
1.3 KiB
Rust
54 lines
1.3 KiB
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"));
|
|
}
|
|
|
|
// To compile and run this example without errors, while using Cargo, change the value
|
|
// of the `edition` field, in the `[package]` section of the `Cargo.toml` file, to "2015".
|
|
|
|
use std::num::ParseIntError;
|
|
|
|
fn multiply(first_number_str: &str, second_number_str: &str) -> Result<i32, ParseIntError> {
|
|
let first_number = try!(first_number_str.parse::<i32>());
|
|
let second_number = try!(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 part1() {
|
|
print(multiply("10", "2"));
|
|
print(multiply("t", "2"));
|
|
}
|
|
|
|
pub fn main() {
|
|
part0();
|
|
part1();
|
|
}
|
|
|