? 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


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.