How can the ref keyword be avoided when pattern matching in a function taking &self or &mut self?

Solution 1:

Since self is of type &mut Self, it is enough to match against itself, while omitting ref entirely. Either dereferencing it with *self or adding & to the match arm would cause an unwanted move.

fn ref_mut(&mut self) -> &mut i32 {
    match self {
        OwnBox(i) => i,
    }
}

For newtypes such as this one however, &mut self.0 would have been enough.

This is thanks to RFC 2005 — Match Ergonomics.