Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUST

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)
}
 
PREVIOUS NEXT
Tagged: #error #handling #rust
ADD COMMENT
Topic
Name
4+8 =