简体   繁体   中英

Why getting panic in Rust program with an expect

Beginner Rust fan - I am getting a panic for either one of these.
I expected it from the first part, but not the second.
What am I missing?

fn main() {

    //let age = "4a";
    //let age2: i32 = age.trim().parse().unwrap();
    //println!("{:?}", age2);

    let age = "4a";
    let age2: i32 = age.trim().parse().expect("What was this?");
    println!("{:?}", age2);
}

From expect() 's documentation :

Panics

Panics if the value is an Err , with a panic message including the passed message, and the content of the Err .

The only difference to unwrap() is the custom error message.

See also: When should we use unwrap vs expect in Rust .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM