-
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
Compiler spuriously infers data needs to be borrowed as mutable #64261
Comments
@edwardw Thank you for filing this issue! If there is a rustacean out there who is willing to do a bit of hand-holding, I'd love to help on this issue. |
Is this really a bug? When you try to access collection[j] the compiler returns a &mut String because that's the type of the Vector. When you try to access collection[least_element] the borrow checker doesn't know if least_element != j, and having 2 &mut references of the same element would be UB. You can either use std::ops::Index which returns a &&mut String (and it's safe to have 2 immutable references to the same &mut reference), directly borrowing the elements (&collection[j] < &collection[least_element]) or if possible changing the type of collection to Vec<&String> or Vec |
Not sure if this helps, but I needed a mutable Vec of Mutable Strings, because I wanted to modify the collection’s elements, and modify the contents of the elements, so having a &mut Vec<&mut String> made the most sense to me. I stumbled across this while playing with collections of encrypted strings received out-of-order, where I wanted to sort them and decrypt... hence need the need for &mut Vec<&mut String>. |
You can use |
triage: no change the error message is different though!
|
All credits are due to this SO question. This code is tried:
I expect it to compile. Instead,
rustc
complains that the data needs to be borrowed as mutable, which is incorrect:This happened on
rustc-1.37.0
,beta-1.38.0
andnightly-1.39.0
.The text was updated successfully, but these errors were encountered: