Click a card to flip it · 19 terms
A struct is a custom type that groups named fields together. This deck covers how to define structs, attach methods with impl, and use patterns like field shorthand and struct update syntax.
A custom type that groups named fields of different types together.
Defining account data, payment plans, or any object with multiple properties.
struct Wallet { address: String, balance: u64,}
One named value inside a struct with an explicit type.
Every piece of data in a struct. Each field has a name and a type.
struct User { username: String, // field balance: u64, // field}
A specific struct with actual values filled in for each field.
Every time you create a real object from a struct definition.
let user = User { username: "Alice".to_string(), balance: 1_000_000,};
The block where you attach functions and methods to a struct.
Every time you want a struct to have its own behavior.
impl Wallet { fn balance(&self) -> u64 { self.balance }}
The current instance inside an impl block. &self reads, &mut self modifies.
Inside any method. Gives the method access to the struct's fields.
impl Wallet { fn show(&self) { println!("{}", self.balance); }}
A function inside an impl block that takes &self or &mut self. Called with dot notation.
Any behavior that belongs to the struct and uses its data.
wallet.deposit(amount);wallet.balance();
A read-only reference to the instance. Borrows without taking ownership.
When a method only needs to read the struct's fields.
fn balance(&self) -> u64 { self.balance // just reading}
A mutable reference to the instance. Lets the method change the struct's fields.
When a method needs to modify data. The variable must also be declared mut.
fn deposit(&mut self, amt: u64) { self.balance += amt;}
A function in impl that has no self parameter. Called with :: on the type name, not a dot.
Constructors and factory functions that create new instances.
let w = Wallet::new("abc", 0);// :: not .
When a variable name matches a field name, you can write it once instead of field: field.
Clean up struct initialization when your variable names already match the fields.
User { email, username }// instead of:// User { email: email, username: username }
..other copies all remaining fields from an existing struct instance.
Creating a variant of an existing instance, change a few fields, copy the rest.
let user2 = User { email: new_email, ..user1};
Inside an impl block, Self (capital S) is a shorthand for the type being implemented.
Cleaner constructors: write Self instead of repeating the type name.
fn new() -> Self { Self { balance: 0 }}
The convention of writing an associated function called new that creates a new instance.
Rust has no built-in new keyword. new() is just a naming convention.
impl Wallet { fn new(addr: String) -> Self { Self { address: addr, balance: 0 } }}
How you call an associated function. Written as TypeName::function_name.
Any time you call a function that belongs to the type itself, not an instance.
String::new()Vec::new()Wallet::new("abc", 0)
The block where you write all methods and associated functions for a type.
Every struct that needs behavior gets one (or more) impl blocks.
impl Rectangle { fn area(&self) -> u32 { self.width * self.height }}
A struct with unnamed positional fields, like a named tuple.
When you want a distinct type but don't need field names.
struct Color(u8, u8, u8);let red = Color(255, 0, 0);
A struct with no fields at all.
When you need a type marker or want to implement a trait without storing data.
struct AlwaysEqual;let x = AlwaysEqual;
A macro placed above a struct that automatically implements common traits.
Generating Debug printing, Clone, Copy, and PartialEq without writing the code yourself.
#[derive(Debug, Clone)]struct Wallet { balance: u64,}
Reading a field from a struct instance using dot notation.
Any time you need a value stored inside a struct.
let bal = wallet.balance;let addr = &wallet.address;