Code
fn main() {
let mut values = vec![1, 2, 3];
// The diagnostic identifies the conflicting borrows, but does not explain
// that the loop keeps `values` borrowed for the iteration or give any
// beginner-facing guidance about restructuring the operation.
for value in &values {
if *value == 2 {
values.push(4);
//~^ ERROR cannot borrow `values` as mutable because it is also borrowed as immutable
}
}
}
Current output
error[E0502]: cannot borrow `values` as mutable because it is also borrowed as immutable
--> $DIR/mutate-vec-while-iterating.rs:12:13
|
LL | for value in &values {
| -------
| |
| immutable borrow occurs here
| immutable borrow later used here
LL | if *value == 2 {
LL | values.push(4);
| ^^^^^^^^^^^^^^ mutable borrow occurs here
Desired output
It should explain iterator invalidation and render in a more pedagogical way.
Rationale and extra context
No response
Other cases
Rust Version
Anything else?
No response
Code
Current output
Desired output
It should explain iterator invalidation and render in a more pedagogical way.Rationale and extra context
No response
Other cases
Rust Version
Anything else?
No response