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
I'm not sure if this is intentional with the new borrowing rules, but I don't think it should be. When calling a &mut self method on a struct, you can't pass in a field of the struct by value, nor can you express a computed value in the arguments.
Test case:
struct Foo { x: int }
impl Foo {
fn set_x(&mut self, new: int) { self.x = new }
}
fn main() {
let t = &mut Foo { x: 0 };
// compiles:
let tmpx = t.x;
t.set_x(tmpx + 1);
// borrow fails:
t.set_x(t.x + 1)
}
The borrow of t.x fails, claiming that t is already borrowed by t.setx, but I would expect t.x + 1 to be evaluated prior to the mutable borrow (at least conceptually), and to behave essentially like the pair of lines above which compile successfully.
t2.rs:13:11: 13:14 error: cannot use `t.x` because it was mutably borrowed
t2.rs:13 t.set_x(t.x + 1);
^~~
t2.rs:13:3: 13:4 note: borrow of `*t` occurs here
t2.rs:13 t.set_x(t.x + 1);
^
error: aborting due to previous error
The text was updated successfully, but these errors were encountered:
This was listed as a known drawback to the stricter rules on the commit message for commit d7de4e9
That message also describes the standard workaround (bind the field as a temporary first) and says that hopefully the problem will go away if #6268 is fixed.
I'm not sure if this is intentional with the new borrowing rules, but I don't think it should be. When calling a
&mut self
method on a struct, you can't pass in a field of the struct by value, nor can you express a computed value in the arguments.Test case:
The borrow of
t.x
fails, claiming thatt
is already borrowed byt.setx
, but I would expectt.x + 1
to be evaluated prior to the mutable borrow (at least conceptually), and to behave essentially like the pair of lines above which compile successfully.The text was updated successfully, but these errors were encountered: