105800 Views
82496 Views
47243 Views
47066 Views
45458 Views
38527 Views
Pi-Apps
Intermediate MicroPython
Arduino Alvik
Raspberry Pi Time machine
Now Ad-Free
Guiding Light
Intermediate level MicroPython
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
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 >