Some time back, I came across Logan Smith’s Choose the Right Option video, and the other day I was trying to summarize it for a colleague.
I think it boils down to this:
If we assume T: ?Clone
, then
to convert… | to &Option<T> … |
to Option<&T> … |
---|---|---|
x: T |
&Some(x) (moves x ) |
Some(&x) |
x: &T |
not possible | Some(x) |
x: Option<T> |
&x |
x.as_ref() |
x: Option<Box<T>> |
not possible | x.as_deref() |
x: &Option<T> |
x |
x.as_ref() |
plus niche optimization.