2018-08-30 07:18:55 -05:00
|
|
|
// run-pass
|
2015-01-24 16:25:07 -06:00
|
|
|
// Test that we mutate a counter on the stack only when we expect to.
|
|
|
|
|
|
|
|
fn call<F>(f: F) where F : FnOnce() {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2016-10-29 16:54:04 -05:00
|
|
|
let y = vec![format!("Hello"), format!("World")];
|
2015-01-24 16:25:07 -06:00
|
|
|
let mut counter = 22_u32;
|
|
|
|
|
|
|
|
call(|| {
|
|
|
|
// Move `y`, but do not move `counter`, even though it is read
|
|
|
|
// by value (note that it is also mutated).
|
2020-05-21 19:00:00 -05:00
|
|
|
for item in y { //~ WARN unused variable: `item`
|
2015-01-24 16:25:07 -06:00
|
|
|
let v = counter;
|
|
|
|
counter += v;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
assert_eq!(counter, 88);
|
|
|
|
|
|
|
|
call(move || {
|
|
|
|
// this mutates a moved copy, and hence doesn't affect original
|
2020-05-21 19:00:00 -05:00
|
|
|
counter += 1; //~ WARN value assigned to `counter` is never read
|
|
|
|
//~| WARN unused variable: `counter`
|
2015-01-24 16:25:07 -06:00
|
|
|
});
|
|
|
|
assert_eq!(counter, 88);
|
|
|
|
}
|