Shared read-only variable with numeric value
- The integer is copied
examples/threads/shared-read-only-variable-number/src/main.rs
use std::time::Duration; fn main() { let answer = 42; println!("Before: {answer} {:p}", &answer); let mut handles = vec![]; for _ in 1..=3 { handles.push(std::thread::spawn(move || { println!("{:?} {} {:p}", std::thread::current().id(), answer, &answer); std::thread::sleep(Duration::from_millis(10)); })); } println!("Started: {answer} {:p}", &answer); for handle in handles { handle.join().unwrap(); } println!("After: {answer} {:p}", &answer); }
Before: 42 0x7ffd30c3fb2c Started: 42 0x7ffd30c3fb2c ThreadId(2) 42 0x79e7a53ffafc ThreadId(3) 42 0x79e7a4fffafc ThreadId(4) 42 0x79e7a4bffafc After: 42 0x7ffd30c3fb2c