std-method

Result::ok and err

Extract one side of Result as Option while discarding the other side. Both consume the result. Use this only when the discarded payload is not needed. Expected stdout (not executed): Some("unavailable") Common mistake: Calling ok() and later expecting to recover the original error.

Formal Syntax & Structure

rust syntaxrust
fn ok(self) -> Option<T>; fn err(self) -> Option<E>

Working Example

Example Snippetrust
fn main() {    let value: Result<u32, &str> = Err("unavailable");    println!("{:?}", value.err());}
Rust Reference