When you create a Rust function expecting an optional reference, use Option<&Foo>
rather than &Option<Foo>
.
/// An optional shared reference to a `Maybe` type
/// A reference to an `Option` exclusively owning the `Maybe` instance
Both options happen to have the same representation in memory, but Option<&T>
is easier to work with, and is more universal. This is because Option<&T>
can be created on the fly from from T
, &T
, and Option<T>
or &Option<T>
(via .as_ref()
), but borrowing &Option<T>
requires having specifically Option<T>
.