2 ms·
The definition in Rust's documentation[0] was this: let x: Option<&str> = None; assert_eq!(x.unwrap(), "air"); // fails The equivalent Haskell if unwr
by codygman 11y ago
The definition in Rust's documentation[0] was this:
let x: Option<&str> = None;
assert_eq!(x.unwrap(), "air"); // fails
The equivalent Haskell if unwrap throws a panic as the docs[0] seem to imply:
λ> fromMaybe (error "panic!") Nothing
*** Exception: panic!
You could also do something like this if you don't mind dealing with the wrapped boolean value:
λ> fmap (== "air") (Just "air")
Just True
and the failure case:
λ> fmap (== "air") (Nothing)
Nothing
0: https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap https://doc.rust-lang.org/std/option/enum.Option.html#method...
- deleted 11y ago[deleted]
- steveklabnik 11y agoRight, but it's still not unsafe. The stack unwinds, destructors get called. And I meant some notion of 'safety', not fromMaybe/fromJust.