Getting Started with Rust
2026-08-27
Ask someone near you (different from Tue):
gitTue: the most-familiar bits (Ch 1-3)
let (and let mut)Mostly unsurprising for a C programmer.
Today: the more Rust-specific bits (Ch 4-6)
struct and methodsenum and matchingWeek 2: heap objects, memory layouts, smart pointers Week 3: error handling, traits and generics (And then we move on to systems programming)
What do you think this outputs?
vec! creates and initializes a Vecstruct (more soon)Vec type implements the method push
structDebug formatting (:?)
Vec implements Debug traitprintln! macro quietly borrows a reference to vVec allocated dynamic memoryDropped) at endLet’s dig into this a little deeper.
malloc/free)
Dropprintvec call!v1 cannot be usedVec is owned by v1printvecv2 via returnv2 goes out of scope, drop valuelet, function parameter, or returnCopy (just make a copy)
Clone (v.clone())clone calls are inefficient& (or a mutable ref with &mut)* operator dereferences. operator dereferences implicitly as needed (no ->)Rust will not compile this. Why?
Reader/writer safety enables “fearless concurrency”
(ref, len) pair (a “fat pointer”)&str) for UTF-8 stringsA 2D rotation of the point \((x^{\mathrm{in}}, y^{\mathrm{in}})\) by \(\theta\) looks like
\[\begin{aligned} x^{\mathrm{out}} &= \cos(\theta) x^{\mathrm{in}} - \sin(\theta) y^{\mathrm{in}} \\ y^{\mathrm{out}} &= \sin(\theta) x^{\mathrm{in}} + \cos(\theta) y^{\mathrm{in}} \end{aligned}\]
Suppose we represent 2D points as [f64; 2]. How would you complete the following function?
What does a call to rotate look like?
struct looks a lot like Cstruct fields indexed, not named// Named struct
struct Point2dNamed {
x: f64,
y: f64,
}
// Tuple struct
struct Point2d(f64, f64);
fn main() {
let pn = Point2dNamed {x: 1.0, y: 2.0, };
let pt = Point2d(3.0, 4.0);
let Point2dNamed{x: pnx, y: pny} = pn;
let Point2d(ptx,pty) = pt;
println!("Named: {}, {}", pnx, pny);
println!("Tuple: {}, {}", ptx, pty);
}derive macros auto-implement methods and traitsA 2D rotation of the point \((x^{\mathrm{in}}, y^{\mathrm{in}})\) by \(\theta\) looks like
\[\begin{aligned} x^{\mathrm{out}} &= \cos(\theta) x^{\mathrm{in}} - \sin(\theta) y^{\mathrm{in}} \\ y^{\mathrm{out}} &= \sin(\theta) x^{\mathrm{in}} + \cos(\theta) y^{\mathrm{in}} \end{aligned}\]
struct Point2d(f64, f64).p.rotate(theta) to produce a rotated version of the point penum introduces an enumerated type<T>)Option type used for optional results (of type T)NULL)Result type used for error handlingmatch is a little like C switch (or OCaml match)enum matches as wellpattern => result)_ => resultif-let)if-let is often more ergonomic than matchT must implement Display trait (more to come)let-else)let-else for defaults or fast returnenum TicketStatus {
Pending,
Assigned { assigned_to: String },
Done
}
fn count_assignments(tickets: &[TicketStatus], name: &str) -> u32 {
let mut count = 0;
// Loop over tickets and count how many are assigned to name
count
}
fn main() {
let t1 = TicketStatus::Assigned{ assigned_to: String::from("dbindel") };
let t2 = TicketStatus::Assigned{ assigned_to: String::from("raja") };
let t3 = TicketStatus::Assigned{ assigned_to: String::from("dbindel") };
let t4 = TicketStatus::Done;
let tickets = [t1, t2, t3, t4];
println!("dbindel assigned {} tickets", count_assignments(&tickets, "dbindel"));
println!("raja assigned {} tickets", count_assignments(&tickets, "raja"));
}A language that doesn’t affect the way you think about programming, is not worth knowing. – Alan Perlis
Learn Rust because:
(Asking AI to generate Rust will not change your thinking.)
For your sheet: what was most confusing today?