rustHow to drop a thread in Rust?
Threads can be dropped in Rust using the drop
method. This method takes ownership of the thread and terminates it.
Example
let handle = thread::spawn(|| {
println!("Hello from a thread!");
});
drop(handle);
The drop
method takes ownership of the thread, which terminates it. In the example above, the thread is terminated after the drop
method is called.
Code explanation
let handle = thread::spawn(|| {
: creates a thread and stores it in thehandle
variable.println!("Hello from a thread!");
: prints a message from the thread.drop(handle);
: takes ownership of the thread and terminates it.
Helpful links
More of Rust
- How do I identify unused variables in Rust?
- How to parse JSON string in Rust?
- How to use a tuple as a key in a Rust HashMap?
- How to replace a capture group using Rust regex?
- How to get a capture group using Rust regex?
- How to convert the keys of a Rust HashMap to a vector?
- How to get the length of a Rust HashMap?
- How to replace strings using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to use a HashBrown with a Rust HashMap?
See more codes...