I find myself constantly thinking if I should prepend self with ampersand or not, and I'm surprised it is sometimes a reference and sometimes not, depending on the signature:
fn this(&self) -> &Self {
self // why not &self
}
Looks like self
is not always of type Self
, but its type depends on the signature. Yet this is inconsistent with how pattern matching syntax or closure parameters work:
vec![1, 2, 3].into_iter().filter(|&num| num > 0) // here `num` is *not* a reference
I wonder - do other people also find it confusing?
Is there a reason it was designed that way?