? on a &Option<T>

    pub fn blah() -> Option<()> {
        let t: Option<usize> = Some(20);
        let y = t?;
        let r = &t;
        let z = r?;
    }

In the code above, all lines are fine until z. Is there anyway to make this work and have z have type &usize ?

Currently, I have having to write

match r {
  None => return None,
  Some(z) => ...
}

which is not elegant.

as_ref is the way to do that: https://doc.rust-lang.org/beta/std/option/enum.Option.html#method.as_ref

3 Likes