When Rust doesn't allow you to unwrap an Option
, use .as_ref()
.
Unwrapping &Option<Foo>
causes "cannot move out of borrowed content" error, because it assumes you want to take exclusive ownership of Foo
, destroying the Option
in the process, and that's not allowed through a reference.
option.as_ref()
flips it inside-out to become an owned option holding a reference Option<&Foo>
. Then unwrap()
will get a shared reference rather than take ownership, which is fine.
Try it: Rust Playground