4 ms·
One option the author didn't mention is that threads can join with a value, so instead of needing scope it could have looked like (on my phone so syntax is prob
by vgel 4y ago
One option the author didn't mention is that threads can join with a value, so instead of needing scope it could have looked like (on my phone so syntax is probably not quite right):
let user = todo!();
let handle: JoinHandle<User> = spawn(move || {
// do stuff with user
// hand back user
user
});
// wait for thread to stop and retake ownership of user
let user = handle.join();
I used this recently to remove locking in a worker thread and it was a big performance boost, and made the code nicer -- win/win.