-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to use match with a borrowed value which will be mutated and returned within the match statement #115390
Comments
This is a known limitation of the borrow checker. It's fixed by Polonius ( |
Thanks. I am not sure how hard it would be to achieve, but wouldn't it be good to adapt the error message in order to refer to the Polonius project? |
I had a similar issue, using What I'm really confused about is that matching on references isn't just a syntax sugar for |
Thank you. This is much more readable/intuitive than the if statement. None the less this issue should be resolved and apparently already is in the Polonius project (not implying that you do not want this to be solved). |
Polonius is still unstable and forcing people to use nightly is not nice, so using |
Definitely. I did not want to say to use nightly, only that a fix exists but the underlying project is still in the works. |
As far as I know, each match arm may have a different binding mode (move, ref, ref mut). If you borrow the scrutinee ( If you turn the |
Also compiles successfully with - let Some(content) = &self.item else { unreachable!() };
- content
+ self.item.as_ref().unwrap() I could close this as a duplicate of #54663 since |
I am not sure if those two are similar, since the issue #54663 has a problem with the borrow checker expanding behind the return and in this issue the borrow checker borrows the value even if there is nothing to borrow ( |
When trying to mutate a value within a match statement which is borrowed by the statement and returned as the result of the function, the compiler fails with the error
E0506
.I tried this code:
I expected to see this happen: That the code compiles and runs successfully.
Instead, this happened: The compilation failed with the following error:
Using
if
syntax the code compiles and runs successfully.Code
Meta
rustc --version --verbose
:Nigthly version with the same result:
The text was updated successfully, but these errors were encountered: