mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-06-10 09:19:23 +00:00
27 lines
645 B
Rust
27 lines
645 B
Rust
// ./src/error/multiple_error_types.md
|
|
|
|
|
|
fn double_first(vec: Vec<&str>) -> i32 {
|
|
let first = vec.first().unwrap(); // Generate error 1
|
|
2 * first.parse::<i32>().unwrap() // Generate error 2
|
|
}
|
|
|
|
fn part0() {
|
|
let numbers = vec!["42", "93", "18"];
|
|
let empty = vec![];
|
|
let strings = vec!["tofu", "93", "18"];
|
|
|
|
println!("The first doubled is {}", double_first(numbers));
|
|
|
|
println!("The first doubled is {}", double_first(empty));
|
|
// Error 1: the input vector is empty
|
|
|
|
println!("The first doubled is {}", double_first(strings));
|
|
// Error 2: the element doesn't parse to a number
|
|
}
|
|
|
|
pub fn main() {
|
|
part0();
|
|
}
|
|
|