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 aJoinHandle
JoinHandle::join
: blocks the current thread until the thread associated with theJoinHandle
terminates and returns the result of the thread
Helpful links
More of Rust
- How to calculate the inverse of a matrix in Rust?
- How to convert a Rust HashMap to a BTreeMap?
- How to use regex to match a double quote in Rust?
- How to use regex to match a group in Rust?
- How to convert a u8 slice to a hex string in Rust?
- How to match the end of a line in a Rust regex?
- How to convert a Rust slice to a fixed array?
- How to get a capture group using Rust regex?
- How to use regex with bytes in Rust?
- How to replace all using regex in Rust?
See more codes...