#[derive(Debug)]
struct Foo;
impl Foo {
fn borrow(&mut self) -> &Self {
&*self
}
}
fn main() {
let mut foo = Foo;
let a = foo.borrow();
}
按照官方的文档,生命周期应该会被展开成以下的伪代码
#[derive(Debug)]
struct Foo;
impl Foo {
fn borrow<'a>(&'a mut self) -> &'a Self {
&'a *self
}
}
fn main() {
'b: {
let mut foo = Foo;
'c: {
let a = Foo::borrow::<'c>(&'c mut foo);
}
}
}
按照官方的说明,同一 Context 里面,不允许同事存在一个可变和不可变应用。在这个例子里面,&mut self 和&Self 都存在于同一个‘ c 生命周期作用的 Context 里面,这样不是违反了这个规则吗?但是事实是可以运行。 难道是因为 Self 和 self 不能表示为同一个变量吗?