Structs and methods.
Give related fields a named shape and put behavior beside that shape.
The idea behind the syntax.
A struct names a collection of fields that belong to one concept. Instead of carrying separate variables for a task title, priority, and status, a Task can make the relationship explicit. The compiler then checks field names and types at each use. A tuple struct is useful when position is meaningful but a new type name matters; a unit-like struct can mark a type without storing fields.
Methods live in an impl block. A method taking &self reads an instance, &mut self changes it, and self consumes it. These receiver choices communicate ownership just as ordinary function parameters do. An associated function has no self receiver and is called on the type, often to construct a valid value. A well-designed constructor can enforce invariants before other code sees an instance.
What Rust is checking.
Fields may themselves own data, so moving a struct moves those owned fields. A struct update expression can reuse fields from another instance, but non-Copy fields moved that way are no longer usable through the old instance. Deriving traits such as Debug can make inspection easier; the derive does not invent domain rules for you.
A useful model keeps operations close to the data whose invariants they protect. If a status change must check a rule, a method can enforce it in one place. Rust does not require inheritance for this: structs model data, impl blocks model behavior, and traits introduced later model shared capabilities. Read the code below with attention to the receiver type and which fields remain owned afterward.
See the rule in context.
Each example isolates one part of the model. Follow the bindings, types, ownership transfers, and possible outcomes line by line.
Structs & field ownership
A struct groups named fields into a new type. Ownership rules apply to each field: a String field is owned, while a reference field borrows and needs a lifetime relationship. Start with owned fields when a value should stand independently. Struct update syntax can move fields from the original, so do not assume the original remains fully usable. Deriving Debug gives an inspection format; a public-facing report can use a deliberate formatter instead.
#[derive(Debug)]
struct Task {
title: String,
complete: bool,
}
fn main() {
let task = Task {
title: String::from("Ship the build"),
complete: false,
};
println!("{}: {}", task.title, task.complete);
}Ship the build: false
- Task is a new domain type, not an unlabelled tuple.
- Its title owns its String.
- Accessing fields for formatting borrows the data in this example.
The boundary: Making one field pub does not make every field public. Design the public API deliberately.
Methods & associated functions
An impl block attaches behavior to a type. A method has a self receiver: &self borrows for reading, &mut self borrows exclusively for mutation, and self consumes the value. An associated function without a self receiver is called using Type::function and often constructs a value. Keeping fields private and exposing purposeful methods can preserve invariants. A constructor is an ordinary function by convention, not a language keyword.
struct Counter {
value: u32,
}
impl Counter {
fn new() -> Self {
Self { value: 0 }
}
fn increment(&mut self) {
self.value += 1;
}
fn value(&self) -> u32 {
self.value
}
}
fn main() {
let mut c = Counter::new();
c.increment();
println!("{}", c.value());
}1
- new returns Self without needing an instance.
- increment needs an exclusive borrow.
- value only borrows for reading.
The boundary: A method taking self may intentionally prevent reuse of the original value. Check the receiver when diagnosing a move.
Keep this model.
A struct states what a concept contains; a method signature states how code may access or change it.