113270 Views
93709 Views
85773 Views
53388 Views
50571 Views
48758 Views
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Making a Custom PCB for BurgerBot
Obsidian - the best tool for Makers
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
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 >