Concurrency and a useful CLI
Rust’s type system carries into threads and command-line tools.
What you will be able to do
- Move owned data into a thread explicitly.
- Use channels to transfer values between threads.
- Return std::process::ExitCode from main for scripts and CI.
In plain English
Threads, channels and async runtimes each solve different problems. Use std::thread for CPU work and message passing for ownership transfer. A CLI should parse arguments, return meaningful exit codes and print errors to stderr.
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
tx.send("finished").unwrap();
});
println!("{}", rx.recv().unwrap());
}Your turn
MediumSend a message between threads
Spawn a thread that sends "done" through an mpsc channel and print the received value.
RUST · starter
let (tx, rx) = mpsc::channel();Check your understanding before moving on.
Knowledge checkQuestion 1
What does mpsc stand for?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.