Rust Foundations·Ownership as a design tool

Concurrency and a useful CLI

Rust’s type system carries into threads and command-line tools.

Intermediate 15 min read Lesson 10 of 300 Challenge included
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.

main.rsRust
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

Send a message between threads

Medium

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 in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 10 of 300 · 0% of course