Rust-Programming-Cookbook/Chapter06/sample_early_ret.rs

40 wiersze
1.1 KiB
Rust

//-- #########################
//-- Task: Implementing early returns
//-- Author: Vigneshwer.D
//-- Version: 1.0.0
//-- Date: 26 March 17
//-- #########################
// Use `String` as our error type
type Result<T> = std::result::Result<T, String>;
fn double_first(vec: Vec<&str>) -> Result<i32> {
// Convert the `Option` to a `Result` if there is a value.
// Otherwise, provide an `Err` containing this `String`.
let first = match vec.first() {
Some(first) => first,
None => return Err("Please use a vector with at least one element.".to_owned())
};
// Double the number inside if `parse` works fine.
// Otherwise, map any errors that `parse` yields to `String`.
match first.parse::<i32>() {
Ok(i) => Ok(2 * i),
Err(e) => Err(e.to_string()),
}
}
fn print(result: Result<i32>) {
match result {
Ok(n) => println!("The first doubled is {}", n),
Err(e) => println!("Error: {}", e),
}
}
fn main() {
let empty = vec![];
let strings = vec!["tofu", "93", "18"];
print(double_first(empty));
print(double_first(strings));
}