std-method

Option::unwrap and expect

Both consume Option and return the present value. If absent, they panic. expect includes a diagnostic message; use it for an invariant you deliberately require, not for routine missing user data. Expected stdout (not executed): 8080 Common mistake: Assuming expect is a recoverable error handler.

Formal Syntax & Structure

rust syntaxrust
fn unwrap(self) -> T; fn expect(self, msg: &str) -> T

Working Example

Example Snippetrust
fn main() {    let port = Some(8080u16);    println!("{}", port.expect("configured port should exist"));}
Rust Reference