Here is a sample:
struct X(u32);
impl X {
fn f(&mut self, v: u32) {}
}
fn main() {
let mut x = X(42);
// works
let v = x.0;
x.f(v);
// cannot use `x.0` because it was mutably borrowed
x.f(x.0);
}
error[E0503]: cannot use `x.0` because it was mutably borrowed
--> src/main.rs:16:9
|
16 | x.f(x.0);
| - ^^^ use of borrowed `x`
| |
| borrow of `x` occurs here
What is the reason why x.f(x.0)
does not work? x.0
is passed as an argument, bound to the v
parameter, of type u32
: there is absolutely no possibility that the function body access x.0
through the parameter.
Moreover, it seems very weird to me that:
f(something);
doesn't work, while:
v = something;
f(v);
works.