Closed
Description
This code:
#![feature(unboxed_closures)]
#![feature(overloaded_calls)]
use std::cell::Cell;
fn main() {
let test = Cell::new(5i);
let do_something = ref |:| { println!("{}", test.get()) };
test.set(6i);
do_something();
println!("{}", test.get());
}
Erroneously prints
5
6
This piece of code, which I though should be exact equivalent to the former one, works correctly, however:
#![feature(unboxed_closures)]
#![feature(overloaded_calls)]
use std::cell::Cell;
fn main() {
let test = Cell::new(5i);
let test_ref = &test;
let do_something = |:| { println!("{}", test_ref.get()) };
test.set(6i);
do_something();
println!("{}", test.get());
}
It prints
6
6
Looks like it copies Cell
into the closure instead of taking it by reference.
Original question is on Stackoverflow