Description
This may be a duplicate of issue #57431; I don't understand Rust well enough yet to be sure.
When using chunks_exact
instead of chunks_exact_mut
and then mutating through the returned slices, rustc complains and suggests iterating over &mut mut chunks
in the loop, which appears to be a syntax error.
I tried this code:
fn main() {
let mut v = vec![1, 2, 3, 4, 5, 6, 7];
let mut chunks = v.chunks_exact(2);
for chunk in &mut chunks {
chunk[0] += 1;
}
println!("v = {:?}; remainder = {:?}", v, chunks.remainder());
}
I expected to see this happen:
The actual bug is chunks_exact
should be chunks_exact_mut
(and then switch remainder
to into_remainder
to match). That's probably a tall order for the compiler to suggest, but the error message should definitely not suggest a syntax error.
Instead, this happened:
warning: variable does not need to be mutable
--> src/main.rs:2:9
|
2 | let mut v = vec![1, 2, 3, 4, 5, 6, 7];
| ----^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` on by default
error[E0594]: cannot assign to `chunk[_]` which is behind a `&` reference
--> src/main.rs:5:9
|
4 | for chunk in &mut chunks {
| ----------- help: consider changing this to be a mutable reference: `&mut mut chunks`
5 | chunk[0] += 1;
| ^^^^^^^^^^^^^ `chunk` is a `&` reference, so the data it refers to cannot be written
error: aborting due to previous error
Applying the suggestion results in a syntax error:
error: expected expression, found keyword `mut`
--> src/main.rs:4:23
|
4 | for chunk in &mut mut chunks {
| ^^^ expected expression
error: aborting due to previous error
Meta
rustc --version --verbose
:
rustc 1.40.0 (73528e3 2019-12-16)
binary: rustc
commit-hash: 73528e3
commit-date: 2019-12-16
host: x86_64-unknown-linux-gnu
release: 1.40.0
LLVM version: 9.0