You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fnmain(){letmut a = A{};println!("{}", a.increment(a.zero()));}structA{}implA{fnzero(&mutself) -> i32{0}fnincrement(&mutself,i:i32) -> i32{
i + 1}}
I expected to see this happen: the number 1 is printed
Instead, this happened:
error[E0499]: cannot borrow `a` asmutable more than once at a time
--> src/main.rs:3:32
|
3 | println!("{}", a.increment(a.zero()));
| - --------- ^ second mutable borrow occurs here
| | |
| | first borrow later used by call
| first mutable borrow occurs here
If I rewrite it like so it will run fine:
fnmain(){letmut a = A{};let zero = a.zero();println!("{}", a.increment(zero));}
a.zero is called before a.increment, and will have finished execution by the time a.increment is called.
From my understanding there is no difference in behaviour between:
let a = 0;let b = a + 1;
and
let b = 0 + 1;
or the equivalent with function calls.
The compiler seems to think that the reference created by calling a.zero will still be active when a.increment is called, which it won't.
I tried this code:
I expected to see this happen: the number
1
is printedInstead, this happened:
If I rewrite it like so it will run fine:
Meta
rustc --version --verbose
:(Exists in stable, beta and nightly)
Playground demo
The text was updated successfully, but these errors were encountered: