-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Iterative mutable slicing doesn't work #18902
Comments
This appears to just be the classic borrowck mutable loop wrangling issue. The following works: fn main() {
let i = vec!["hello", "there", "yo"];
let mut bytes = Vec::from_elem(1000, 0);
{
// We need to make the temp variable so that borrowck "gets" what we're doing.
let mut temp = bytes.as_mut_slice();
for s in i.iter() {
let bytes = temp;
std::slice::bytes::copy_memory(bytes, s.as_bytes());
bytes[s.len()] = 0;
temp = bytes.slice_from_mut(s.len() + 1);
}
}
} |
Closing as 'not a bug', then. |
How is it not a bug? |
Depends on how you interpret the lack of non-lexical borrow-checking, I guess. |
I don't see what it has to do with non-lexical borrow checking. Normally you can't work around that by introducing even more variables. |
Re-opening. At best, it's total butts that you need to do this. Unclear if non-lexical scopes could actually fix this. |
I don't think this one is related to the lexical scope of borrows. I believe it's due to how the borrow checker handles mutable borrows that are made through an already borrowed reference. This appears to place a mutable borrow restriction on the reference itself, not just the owned value. For example:
The above code result in "cannot assign to |
fwiw, this is a dup of #10520 I believe |
Where the outer bytes is a vector. This code is safe and the only alternative seems to be working with raw pointers.
The text was updated successfully, but these errors were encountered: