我是Rust和线程的新手,我正在尝试打印一个数字,同时在另一个线程中添加它.我怎么能做到这一点?
use std::thread;
use std::time::Duration;
fn main() {
let mut num = 5;
thread::spawn(move || {
loop {
num += 1;
thread::sleep(Duration::from_secs(10));
}
});
output(num);
}
fn output(num: i32) {
loop {
println!("{:?}", num);
thread::sleep(Duration::from_secs(5));
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用:它总是打印5,好像数字永远不会增加.