unwrap and expect

unwrap() and expect() are shortcuts for extracting values from Option and Result, but they panic on failure.

Using unwrap

let file = File::open("hello.txt").unwrap();

Panics if the file does not exist.

Using expect

let file = File::open("hello.txt").expect("Failed to open file");

expect allows you to provide a custom error message.

Only use these if you're sure the operation won't fail, or when panicking is acceptable.

← PrevNext →