Yes, there was: the function was calling the passed in closure from a different thread.
It is just one thread that’s actually appending to the Vec, but it’s still a multi-threaded example.
It is just one thread that’s actually appending to the Vec, but it’s still a multi-threaded example.
struct Database {
data: Vec<i32>
}
impl Database {
fn store(&mut self, data: i32) {
self.data.push(data);
}
}
fn main() {
let mut db = Database { data: vec![] };
do_work_and_then(|meaning_of_life| {
println!("oh man, I found it: {}", meaning_of_life);
db.store(meaning_of_life);
});
// I'd read from `db` here if I really were making a web server.
// But that's beside the point, so I'm not going to.
// (also `db` would have to be wrapped in an `Arc<Mutex<T>>`)
thread::sleep_ms(2000);
}
No threads are spawned. fn do_work_and_then(func: fn(i32)) {
thread::spawn(move || {
// Figuring out the meaning of life...
thread::sleep_ms(1000); // gee, this takes time to do...
// ah, that's it!
let result: i32 = 42;
// let's call the `func` and tell it the good news...
func(result)
});
}
`you missed a snippet, a thread is spawned.
edit: formatting