To avoid the unnecessary overhead of formatting and allocating a String
in the case of an Ok
, you can convert the Option
to a Result
and then unwrap it:
fn main() {
let x: Option<&str> = None;
x.ok_or_else(|| format!("the world is ending: {}", "foo"))
.unwrap();
}