-3

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);
}
ecorrales
  • 137
  • 11
  • 3
    The only difference between `.unwrap()` and `.expect()` is the ability to set a custom message. Both will panic all the same if there isn't a successful value. – kmdreko Aug 20 '22 at 00:32
  • @kmdreko Would you mind to post this as an actual answer, not a comment? – the busybee Aug 20 '22 at 12:32

1 Answers1

2

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 customized error message.

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

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77