英文:
Why method with lifetime specified cannot be called twice?
问题
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
函数的生命周期 'a
移除,一切就会正常运行。
我的问题是为什么生命周期 'a
会影响借用?我认为 trivial
函数的参数类型是 &mut
,只接受参数而不进行借用,为什么第一次调用会借用?
<details>
<summary>英文:</summary>
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) {}
}
The compiling errors happen at the second called `self.trivial()` and compiler hints that it's the first calling `self.trivial` mutably borrow `*self`. And if the trivial function is defined by removing the lifetime 'a then everything is fine.
// ok then if no 'a specified.
fn trivial(&mut self) {}
My question is why does the lifetime `'a` influence the borrowing? I think function `trivial` has the parameter with type `&mut` which just accepts the argument and not borrow at all, how does the first calling borrow?
</details>
# 答案1
**得分**: 3
生命周期的名称很重要。当您将其指定为`'a`时,您是在说给定给`trivial`的`&mut self`引用必须保持在结构体本身的生命周期内可访问。这意味着在第一次调用`trivial`之后它不会被“释放”。如果您将其更改为`trivial`具有自己的生命周期:
```rust
fn trivial<'b>(&'b mut self) {}
现在它将正常工作,因为借用仅在函数调用的时间内持续。如果您完全删除生命周期注释,那么隐式地发生的情况也是一样的。
请注意,将&mut self
(或任何引用)传递到函数中实际上是一种借用——当该参数是引用时,“接受一个参数而不借用”并不真正有意义。
英文:
The name of the lifetime matters. When you give it 'a
as an explicit lifetime, you're saying the &mut self
ref given to trivial
must remain accessible for the lifetime of the struct itself. Which means it doesn't get "released" after the first call to trivial
. If you change that to trivial
having its own lifetime:
fn trivial<'b>(&'b mut self) {}
Now it'll work fine because the borrow only lasts as long as the function call. This is the same thing that's happening implicitly if you remove the lifetime annotation entirely.
Note that passing a &mut self
(or any reference) into a function is a borrow -- "accepts an argument and not borrow at all" doesn't really make sense when that argument is a ref.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论