struct A<'a> {
v: &'a str,
}
impl<'a> A<'a> {
pub fn test(&'a mut self) {
self.trivial();
// Compiling error info:
// cannot borrow `*self` as mutable more than once at a time
// first mutable borrow occurs here (last called self.trivial())
self.trivial(); // Compiling error line.
}
// Trivial empty function.
fn trivial(&'a mut self) {}
}
编译错误发生在第二个调用self.trivial()
时,编译器提示这是第一个调用self.trivial
可变borrow *self
.如果平凡函数是通过go 掉生命周期 ‘a来定义的,那么一切都很好.
// ok then if no 'a specified.
fn trivial(&mut self) {}
我的问题是,为什么生命周期 'a
会影响借款?我认为函数trivial
有一个类型为&mut
的参数,它只接受实参,根本不接受借阅,第一个调用如何借阅?