Macro loop_unwrap::unwrap_break[][src]

macro_rules! unwrap_break {
    ($x:expr, $err_msg:expr) => { ... };
    ($x:expr) => { ... };
}

Almost the same as unwrap_continue()!, just breaks the loop instead. Works like .unwrap, if it’s an Err or None, it calls break on the loop. Prints an error message with println!() if provided.

Examples

loop {
        let input = "Not a number";
        let parsed_input: i32 = unwrap_break!(input.parse()); //parse returns Err for this input
    }
println!("This line will be reached.");
loop {
        let input = "Not a number";
        let parsed_input: i32 = unwrap_break!(input.parse(), "Please Enter a Number!");
        // "Please Enter a Number!" is printed in console with a `println!()`
        //loop breaks
    }
loop {
        let some_value: i32 = unwrap_break!(Some(32), "Please Enter a Number!");
        assert_eq!(some_value, 32_i32)
        //no breakage here.
    }