This cheat sheet gives grades 11-12 students a quick reference for reading and writing basic Rust syntax. Rust is used for fast, safe systems programming, so students need to understand how its rules differ from languages like Python, JavaScript, or Java. The reference focuses on syntax patterns that appear often in beginner Rust programs, including variables, functions, conditionals, loops, collections, and types.
Key Facts
- Variables are immutable by default, so use let x = 5; for a fixed value and let mut x = 5; when the value must change.
- A function is declared with fn name(parameter: Type) -> ReturnType { expression }, such as fn square(n: i32) -> i32 { n * n }.
- Rust uses type annotations such as i32, f64, bool, char, String, and Vec<T>, but it can often infer types from values.
- An if expression must use a bool condition, and it can return a value with syntax like let result = if score >= 60 { "pass" } else { "fail" };.
- A for loop over a range uses start..end to exclude the end value and start..=end to include it, such as for i in 1..=5 { println!("{}", i); }.
- Ownership means each value has one owner at a time, and assignment to another variable can move the value unless the type implements Copy.
- Borrowing uses references, with &value for an immutable borrow and &mut value for a mutable borrow.
- Pattern matching uses match value { pattern => result, _ => default }, and every possible case must be handled.
Vocabulary
- Ownership
- Ownership is Rust's rule that each value has one variable responsible for managing its memory.
- Borrowing
- Borrowing means using a reference to a value without taking ownership of that value.
- Mutable
- A mutable variable or reference can be changed after it is created.
- Struct
- A struct is a custom data type that groups named fields into one value.
- Enum
- An enum is a custom type whose value must be one of several named variants.
- Result
- Result<T, E> is a type used for operations that can either succeed with Ok(T) or fail with Err(E).
Common Mistakes to Avoid
- Forgetting mut in let x = 3; x = 4; is wrong because Rust variables cannot be reassigned unless they are declared with let mut.
- Using a moved value after let b = a; is wrong for many heap-owned types because ownership may transfer from a to b.
- Passing &mut value while another reference is still active is wrong because Rust prevents simultaneous mutable and immutable access to avoid data races.
- Putting a semicolon after the final expression in a return-value function can be wrong because n * n returns a value, but n * n; returns ().
- Leaving out the default _ arm in match can be wrong when not all possible cases are covered, because Rust requires exhaustive pattern matching.
Practice Questions
- 1 What is printed by this code: let mut x = 4; x += 3; println!("{}", x);
- 2 How many numbers are printed by this loop: for i in 2..7 { println!("{}", i); }
- 3 Write a Rust function header and body for a function named double that takes an i32 and returns that number multiplied by 2.
- 4 Explain why Rust allows many immutable references to a value but only one mutable reference at a time.