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,
}
}
}
如评论中所述,为什么item
是&Foo
类型?有什么规则表明item
将是&Foo
类型而不是Foo
类型?
我理解item
不能是Foo
类型;&self
表示self
是一个引用,因此没有意义从引用中移动值,但是否有任何规范能清晰地定义这些规则呢?