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
If a borrowed value is used in a match statement and returned in a match arm, the borrow ends at the end of the function.
The following code illustrates this behaviour:
When I try to compile it, the compiler outputs this message:
test.rs:6:2: 6:3 error: cannot borrow `*x` as mutable more than once at a time
test.rs:6 x.as_mut().unwrap()
^
test.rs:2:8: 2:9 note: previous borrow of `*x` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `*x` until the borrow ends
test.rs:2 match x.as_mut() {
^
test.rs:7:2: 7:2 note: previous borrow ends here
test.rs:1 fn foo(x : &mut Result<usize, usize>) -> &mut usize {
...
test.rs:7 }
^
error: aborting due to previous error
(However, the code compiles if the return statement is removed.)
I think the borrow checker is too restrictive in this case, since the borrow of x in line 2 should end in line 5.
Thanks for the report! This is a feature known as "non lexical borrows" for us, and that's being tracked over at rust-lang/rfcs#811 so I'm gonna close this in favor of that.
If a borrowed value is used in a match statement and returned in a match arm, the borrow ends at the end of the function.
The following code illustrates this behaviour:
When I try to compile it, the compiler outputs this message:
(However, the code compiles if the return statement is removed.)
I think the borrow checker is too restrictive in this case, since the borrow of
x
in line 2 should end in line 5.I used the following compiler version:
The text was updated successfully, but these errors were encountered: