rustHow to exit a thread in Rust?
Threads in Rust can be exited using the std::thread::Thread::join()
method. This method will block the current thread until the thread it is called on has finished executing.
use std::thread;
let handle = thread::spawn(|| {
println!("Hello from a thread!");
});
handle.join().unwrap();
The code above will create a new thread and execute the closure passed to thread::spawn()
. The handle.join().unwrap()
will block the current thread until the thread created by thread::spawn()
has finished executing.
thread::spawn()
: creates a new thread and executes the closure passed to ithandle.join()
: blocks the current thread until the thread created bythread::spawn()
has finished executingunwrap()
: returns the result of the thread, or panics if the thread panicked
Helpful links
More of Rust
- How to use regex with bytes in Rust?
- How to split a string with Rust regex?
- How to replace all using regex in Rust?
- How to use regex to match a double quote in Rust?
- How to replace all matches using Rust regex?
- How to replace strings using Rust regex?
- How to get a capture group using Rust regex?
- How to perform matrix operations in Rust?
- How to convert a Rust HashMap to a BTreeMap?
- How to use Unicode in a regex in Rust?
See more codes...