rustHow to get the result of a thread in Rust?
The result of a thread in Rust can be obtained by using the join method on the JoinHandle returned by the spawn method.
Example code
use std::thread;
let handle = thread::spawn(|| {
// thread code
});
let result = handle.join();
The join method will block the current thread until the thread associated with the JoinHandle terminates. The result of the thread is then returned by the join method.
Code explanation
thread::spawn: spawns a new thread and returns aJoinHandleJoinHandle::join: blocks the current thread until the thread associated with theJoinHandleterminates and returns the result of the thread
Helpful links
More of Rust
- How to use regex to match a double quote in Rust?
- How to replace a capture group using Rust regex?
- Regex example to match multiline string in Rust?
- How to clear a Rust HashMap?
- How to use non-capturing groups in Rust regex?
- Yield example in Rust
- How to perform matrix operations in Rust?
- How to replace strings using Rust regex?
- How to use regex lookbehind in Rust?
- How to convert the keys of a Rust HashMap to a vector?
See more codes...