101591 Views
79842 Views
45596 Views
44332 Views
40991 Views
33419 Views
Raspberry Pi Time machine
Now Ad-Free
Guiding Light
Sync Files on your Pis, with Syncthing
NextCloud
Buddy Jr.
Introduction to FreeCAD for Beginners
Building a Robot Arm with Raspberry Pi and PCA9685
Building User Authentication for Static Sites with FastAPI
Mastering Pydantic for Robust Data Validation
Mastering Markdown for Documentation with Jekyll
Introduction to Rust
KevsRobots Learning Platform
56% Percent Complete
By Kevin McAleer, 2 Minutes
Concurrency in Rust is designed to be safe and efficient, preventing common pitfalls found in other languages. This lesson introduces Rust’s concurrency model, focusing on threads and the concept of fearless concurrency.
Threads in Rust allow you to perform different tasks concurrently. Creating new threads is done using the thread::spawn function, which takes a closure containing the code to be executed in the new thread.
thread::spawn
use std::thread; use std::time::Duration; thread::spawn(|| { for i in 1..10 { println!("hi number {} from the spawned thread!", i); thread::sleep(Duration::from_millis(1)); } });
Rust’s memory safety guarantees extend to concurrency, making it easier to write safe and concurrent code. This is known as fearless concurrency. Rust achieves this through:
This lesson covered the basics of implementing concurrency in Rust. By leveraging Rust’s strong type system and ownership rules, you can write concurrent code that is both safe and efficient, avoiding the pitfalls typically associated with threading and data races.
< Previous Next >