Rust flashcards
Fast, safe systems code with no garbage collector.
Beginner friendly, starts from zero
0
Rust at a glance
Rust is a programming language for fast, reliable programs that catches many mistakes before they ever run.
Like a strict but kind driving instructor who checks your mirrors and belts before you leave, so the drive itself is safe.
Words you will meet
- Compiler
- The translator that turns your code into a program and points out mistakes
- Variable
- A named box that holds a value
- Type
- The kind of thing a box holds, like a whole number or text
- Function
- A named set of steps that takes inputs and gives back a result
- Heap
- The big shelf of memory where growing things like text are kept
- Ownership
- Rust's rule that each value has exactly one owner who cleans it up
- Borrow
- Using a value without taking it from its owner, like borrowing a book
- Cargo
- Rust's helper that builds your project and adds packages
The steps
- 1. Start from zero (12 cards): What Rust is, why it exists and the words you need, in plain English.
- 2. Core basics (16 cards): Cargo, variables, numbers, expressions, strings, Vec and debugging.
- 3. Next steps (16 cards): Ownership, moves, borrowing, structs, enums, Option, Result, traits and iterators.
- 4. Go deeper (17 cards): Lifetimes, smart pointers, threads, Send and Sync, async, the 2024 edition and performance.
All 12 cards in Start from zero
- What is Rust, in one plain sentence?
- A programming language for writing fast, reliable programs that catches many mistakes before the program even runs.
- What problem was Rust made to solve?
- Fast languages like C and C++ let tiny memory mistakes crash programs or open security holes. Safer languages were often slower. Rust aims to give both speed and safety.
- Where do you already meet Rust in daily life?
- Inside the Firefox browser, parts of Windows and Android, the Linux system that runs most servers, and services like Discord and Cloudflare that carry a huge share of web traffic.
- What is a good everyday picture of how Rust feels to use?
- A strict but kind driving instructor who will not let you leave until the mirrors and belts are set. It is slower at first, but the drive itself is much safer.
- What are code, a program and a compiler?
- Code is instructions you write in a language like Rust. A program is code that runs. The compiler is a translator that turns your code into a program, and it also points out mistakes.
- What are a variable, a value and a type?
- A variable is a named box, and the value is what is inside it. The type says what kind of thing the box holds: a whole number (an integer), a decimal, text, or true or false.
- What is a function?
- A named set of steps that can take inputs and give back a result, like a recipe. In Rust it starts with
fn, and every program begins in the function calledmain. - What is memory, and why must a program give it back?
- Memory is the computer's short-term workspace. Small fixed things go on the stack, a fast pile; growing things go on the heap, a big shelf. Space not given back fills up and slows the computer.
- What do ownership, borrowing and scope mean?
- Each value has one owner. Borrowing lets other code read or use it without taking it, like lending a book. Scope is the block between
{and}where a thing lives; at the end, it is cleaned up. - What do you need before you start, and what are the terminal, Cargo and a crate?
- A computer and the free
rustupinstaller, or just a browser with the official Rust Playground. The terminal is a window for typed commands. Cargo is Rust's helper for building, and a crate is a package of Rust code. - Your first tiny win: what does each line of this program do?
fn main()is the main function where every program starts.let namemakes a variable holding text, andprintln!prints the line with the name filled in.- What will you be able to do after this topic?
- Set up a Rust project, use variables, types, loops and functions, understand ownership and borrowing, handle errors calmly, and build fast command-line tools.
Rust cheatsheet
Cargo
cargo new app / cargo new --lib coreNew binary / librarycargo checkFast type and borrow checkcargo run --release -- --flagOptimised run with argscargo test name -- --nocaptureFilter tests, show printscargo clippy --all-targets -- -D warningsStrict lintscargo fmt --checkFormatting gate in CIcargo add serde -F deriveAdd a dependency with a featurecargo tree -dFind duplicate crate versionscargo fix --editionMigrate to a new edition
Types and bindings
let x = 5; let mut y = 0;Immutable / mutableconst MAX: u32 = 10;Compile-time constanti32 u8 u64 usize f64 bool charCommon scalar typeslet (a, b) = pair;Destructure a tuple[0u8; 1024]Fixed array of 1024 zerosx.checked_add(1)Noneon overflowlet v: u32 = s.parse()?;Parse text to a numberstruct Id(u64);Newtype wrapper
Ownership and pointers
&T / &mut TShared / exclusive borrowx.clone()Explicit deep copyBox<T>Owned heap valueRc<T> / Arc<T>Shared owner: one thread / manyRefCell<T> / Mutex<T>Interior mutability: thread-local / sharedWeak<T>Non-owning link, breaks cyclesCow<'a, str>Borrow, or own only when neededstd::mem::take(&mut v)Move out, leave default
Errors
Option<T>: Some / NoneMaybe a valueResult<T, E>: Ok / ErrSuccess or failurevalue?Unwrap or return earlyopt.ok_or(Err::Missing)?Option toResultres.map_err(AppError::from)?Convert the error typeunwrap_or / unwrap_or_else / unwrap_or_defaultFallbackslet Some(x) = opt else { return };Bind or bailanyhow .context("msg")Add context in apps
Iterators
.iter() / .iter_mut() / .into_iter()Borrow / mutate / take items.filter(|x| ..).map(|x| ..)Lazy pipeline.collect::<Vec<_>>()Gather into a collection.sum::<u64>() / .count()Consumers.enumerate() / .zip(other)Index / pair up.find(|x| ..) / .any(..) / .all(..)Search and test.chunks(100) / .windows(2)Slices of a slice.fold(0, |acc, x| acc + x)Custom reduce