-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Immutable borrow as argument for mutable borrow does not compile #34035
Comments
I'm going to naively assume this is because |
I think this is being tracked at rust-lang/rfcs#811. |
Yeah, non-lexical borrows should fix this case. Until then you can use unborrow. |
I'm not entirely sure that non-lexical borrows can fix this without being a breaking change. The above code desugars to: fn main() {
let mut data = vec![1; 100];
{
let (left, right) = DerefMut::deref_mut(&mut data).split_at_mut(Deref::deref(&data).len() / 2);
}
println!("{:?}", data);
} |
@Stebalien I think non-lexical borrowck should allow that code. |
If we deref in order, we could write code that could print out the following:
Alternatively, we could unborrow and reborrow:
Or borrow late:
However, that's also a breaking change. Of course, one could argue that the behavior here is unspecified... |
Oh, indeed I missed the first deref_mut. Perhaps unborrow will remain
|
See this thread on the topic: https://internals.rust-lang.org/t/accepting-nested-method-calls-with-an-mut-self-receiver/4588 |
Current output:
|
What is the current state of this issue? I am relatively new to Rust and I don't think I fully understand the borrow checker yet, but this 'workaround' shouldn't be the way to go shouldn't it? |
@Xenomojin for a case like the one above, you must bind the value of |
Thank you very much for your response @estebank, I guess I just need to get used to it. I mean... its not that much of a cost compared to knowing for sure there won't be anything unforeseen at run time, unlike some C code where I was spending hours and hours not getting forward at all. But I'm seeing forward to having handled this little bumpiness in an improved manner some day in the future. |
I have the following piece of code which I expect to work but the borrow checker does not agree:
Please note that the extra scope is in principle not related to the issue I'm describing, I'm just doing it such that I can use the
println!
macro to ensure that the compiler does not optimize the code away because I'm not using it thereafter. (I'm new to Rust so I don't know the exact rules)The error I get is the following:
However what I would expect is the following:
data.len()
takes an immutable borrow.data.len()
returns the immutable borrow and divides the obtainedu32
by2
.data.split_at_mut()
takes a mutable borrow.It seems like the borrow checker currently evaluates the statement in the wrong order.
The following workaround is available, but it feels like this is a place where we should not have the need to (rightfully) fight the borrow checker.
The text was updated successfully, but these errors were encountered: