-
Book Overview & Buying
-
Table Of Contents
The Rust Programming Handbook
By :
Now that we understand the “why” of concurrency, let’s get to the “how” in Rust. The most fundamental way to achieve concurrency is by creating and managing threads.
What is a thread?
A thread is an independent path of execution within your program. Your operating system can schedule these threads to run, potentially in parallel on different CPU cores. Rust’s standard library provides std::thread.
The primary way to create a new thread in Rust is by calling the std::thread::spawn function.
This function takes a closure (an anonymous function) as an argument, and this closure contains the code that the new thread will execute.
When you call thread::spawn, it immediately returns a JoinHandle. The new thread starts executing its closure in the background, and your main thread (or the thread that called spawn) continues...