Hi. I've recently started to learn Rust.
Now, I'm having a hard time to understand lifetime parameters inside a impl
statement.
Here's a code I have:
struct A<'a> {
a: &'a i32
}
impl<'a> A<'a> {
fn setup(&'a mut self) {
self.a = &1
}
fn run(&'a self) {
println!("{}", self.a)
}
}
pub fn main() {
let mut a = A {a: &0};
a.setup();
a.run()
}
When I tried to compile the above code, I got the following error:
Compiling playground v0.0.1 (/playground)
error[E0502]: cannot borrow `a` as immutable because it is also borrowed as mutable
--> src/main.rs:18:5
|
17 | a.setup();
| --------- mutable borrow occurs here
18 | a.run()
| ^^^^^^^
| |
| immutable borrow occurs here
| mutable borrow later used here
For more information about this error, try `rustc --explain E0502`.
error: could not compile `playground` due to previous error
However, it worked just by rewriting the function setup
like this:
fn setup(&mut self) {
self.a = &1
}
What makes these two functions identical?
I thought that the latter setup
equals to fn setup<'b>(&'b mut self)
, and then the actual lifetime passed to 'b
would be the one that variable a
in main has.
And, I believe that same thing happens to the original definition of setup
.
Does anyone explain why the latter one is fine, but the former one is not?