Master Rust Programming with 50 free flashcards. Study using spaced repetition and focus mode for effective learning in Programming.
Rust is a systems programming language focused on safety, speed, and concurrency. It achieves memory safety without a garbage collector through its ownership system.
Ownership is Rust's core memory management concept. Each value has exactly one owner, and when the owner goes out of scope, the value is dropped (freed). This prevents memory leaks and double frees at compile time.
Each value in Rust has a single ownerThere can only be one owner at a timeWhen the owner goes out of scope, the value is dropped
Borrowing lets you reference a value without taking ownership. You create a reference with &. The original owner retains ownership while the borrower can read (or write, if mutable) the data.
&T is an immutable reference (shared borrow) — multiple allowed simultaneously.&mut T is a mutable reference (exclusive borrow) — only one allowed at a time, and no immutable references may coexist with it.
A lifetime is a compile-time annotation (e.g., 'a) that tells the compiler how long a reference is valid. Lifetimes prevent dangling references by ensuring borrowed data outlives the borrower.
Use lifetime parameters in angle brackets: fn longest<'a>(x: &'a str, y: &'a str) -> &'a str. This tells the compiler the returned reference lives at least as long as the shorter of x and y.
'static means the reference is valid for the entire duration of the program. String literals have type &'static str because they are embedded in the binary.
A struct is a custom data type that groups related fields. Example:struct User { name: String, age: u32 }
Rust has three kinds: named-field structs, tuple structs, and unit structs.
Use an impl block:impl User {
fn greet(&self) -> String {
format!("Hi, {}", self.name)
}
}
The first parameter is typically &self, &mut self, or self.
An enum defines a type that can be one of several variants. Variants can hold data:enum Shape {
Circle(f64),
Rectangle(f64, f64),
}
Enums are central to Rust's type system and pattern matching.
Pattern matching uses match to destructure and branch on values:match shape {
Shape::Circle(r) => ...,
Shape::Rectangle(w, h) => ...,
}
The compiler ensures all variants are handled (exhaustiveness).
Flashcards
Flip to reveal
Focus Mode
Spaced repetition
Multiple Choice
Test your knowledge
Type Answer
Active recall
Learn Mode
Multi-round mastery
Match Game
Memory challenge