Rust tips is a user on octodon.social. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here.
Rust tips @rust

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 ownership of `Foo`, and that's not allowed through a reference.

`option.as_ref()` flips it inside-out to become `Option<&Foo>`, and `unwrap()` will get a reference rather than ownership, which is fine.

Try it: is.gd/3lPJqk

· Web · 0 · 0