use crate::List::{Cons, Nil};
#[derive(Debug)]
struct Foo {}
#[derive(Debug)]
enum List {
Cons(i32, Foo),
Nil,
}
impl List {
fn tail(&self) -> Option<&Foo> {
match self {
Cons(_, item) => Some(item), // why `item` is of type `&Foo`?
Nil => None,
}
}
}
As stated in the comment, why is item
of type &Foo
? What is the rule that says item
will be type &Foo
rather than Foo
?
I understand that it does not make sense for item to be Foo
; &self
says self
is a reference, so it does not make sense to move a value out of a reference, but are there any specifications that define the rules clearly?