Closed
Description
Here's the surprising program:
fn main() {
let mut pixels = [0f32, ..8];
for &mut pixel in pixels.mut_iter() {
pixel = 2.0;
}
println!("{:?}", pixels);
}
Compiling produces the following output, which is a head-scratcher if you don't know what's going on:
um.rs:4:10: 4:19 warning: variable `pixel` is assigned to, but never used, #[warn(unused_variable)] on by default
um.rs:4 for &mut pixel in pixels.mut_iter() {
^~~~~~~~~
um.rs:5:9: 5:14 warning: value assigned to `pixel` is never read, #[warn(dead_assignment)] on by default
um.rs:5 pixel = 2.0;
^~~~~
Running it prints the following:
[0f32, 0f32, 0f32, 0f32, 0f32, 0f32, 0f32, 0f32]
So even though the error messages are trying to warn you that you're not actually mutating the array, it doesn't answer the question as to why you're not mutating the array.
Intuitively, I expected this program to be equivalent to this:
fn main() {
let mut pixels = [0f32, ..8];
for pixel in pixels.mut_iter() {
*pixel = 2.0;
}
println!("{:?}", pixels);
}
Which mutates the array as expected.
There's clearly some sort of implicit copying going on here that's very surprising, even if correct. @alexcrichton was also concerned that this might erroneously parsing &mut pixel
as &(mut pixel)
.
So the question is, what behavior do we expect here?
Metadata
Metadata
Assignees
Labels
No labels