Answers for "error handling rust"

0

rust error handling

match do_steps() {
    Ok(_) => (),
    _ => alert_user("Failed to perform necessary steps")
}

// Additional function:
fn do_steps() -> Result<(), Error>{
    do_step_1()?;
    do_step_2()?;
    do_step_3()?;
    // etc
    Ok(())
}
Posted by: Guest on March-18-2022
0

error handling rust

fn main() {
    let c = div(2.0,0.0);
    match c {
        Ok(t)   =>  println!("c = {t}"),
        Err(e)  =>  println!("{e}")
    }
}

fn div(a:f32, b:f32) -> Result<f32, std::string::String> {
    if b==0.0 {
        let e = std::string::String::from("ZeroDevisionError: float division by zero");
        return Err(e)
    }
    return Ok(a/b)
}
Posted by: Guest on May-05-2022

Browse Popular Code Answers by Language