Flashcard Deck · 20 cards · Public

Rust - Ownership, Borrowing & Concurrency

Master Rust's core memory safety paradigm with flashcards covering ownership rules, borrow checking, lifetimes, smart pointers, and thread-safe concurrency.

Cards in this deck

(20 cards)

Preview terms and definitions before starting your study session.

#1
Term
Three Core Rules of Ownership
Definition
1. Each value in Rust has an owner (a variable).
2. There can only be one owner at a time.
3. When the owner goes out of scope, the value is dropped automatically.
#2
Term
Move Semantics vs. Copy Trait
Definition
When assigning or passing a variable by value:
  • Move: Transfers ownership to the new variable, invalidating the original (e.g., String, Vec).
  • Copy: Performs a bitwise duplicate if the type implements Copy (e.g., i32, bool), keeping the original valid.
#3
Term
The Borrowing Rule (Aliasing XOR Mutability)
Definition
At any given point in a program, you can have either:
  • Any number of immutable references (&T), OR
  • Exactly one mutable reference (&mut T).
References must always be valid for their entire lifetime.
#4
Term
Lifetimes ('a)
Definition
Annotations used by the borrow checker to ensure references remain valid for a specific scope. They do not change runtime execution, but explicitly declare relationships between reference lifetimes to prevent dangling pointers (e.g., fn longest<'a>(x: &'a str, y: &'a str) -> &'a str).
#5
Term
Lifetime Elision Rules
Definition
Three deterministic compiler rules for inferring reference lifetimes without explicit annotations:
1. Each elided reference parameter gets a unique lifetime parameter.
2. If there is exactly one input lifetime parameter, it is assigned to all output lifetimes.
3. If &self or &mut self is an input parameter, its lifetime is assigned to all output lifetimes.
#6
Term
Box<T> Smart Pointer
Definition
Allocates data directly on the heap while storing the pointer on the stack. Used for recursive types of unknown size at compile time, transferring ownership of large data without stack copies, or dynamic dispatch (Box<dyn Trait>).
#7
Term
Rc<T> Smart Pointer
Definition
Reference Counting smart pointer for single-threaded, shared ownership. Tracks reference counts and frees heap memory when the count reaches zero. Not thread-safe (does not implement Send or Sync).
#8
Term
Arc<T> Smart Pointer
Definition
Atomic Reference Counting smart pointer for thread-safe shared ownership across multiple threads. Uses atomic operations to update reference counts, enabling safe concurrently-shared data.
Showing 8 of 20 cards in this deck.
Study All Now