Structs
A struct is a named collection of typed fields. It is Rust's record type: the layout is fixed at compile time, and field access is value.field.
Detailed Explanation
struct Session { token: String, user_id: u64 } declares a type. A value is written with a struct literal: Session { token: "abc".into(), user_id: 17 }. Field names are part of the type, so renaming a public field is a breaking change and swapping two fields of the same type will not compile at the literal if you still use names — which is why named fields beat a 6-tuple.
Fields are private to the module by default. A pub struct with private fields can be named and passed around, but it cannot be built with a struct literal outside the module. That is how you attach a constructor and keep an invariant. A pub field is writable by anyone who can see the struct, so you cannot later reject empty tokens without a breaking change.
Update syntax copies the rest of another value: Session { user_id: 18, ..old }. The ..old moves any field that is not Copy and that you did not name, so old is usually dead after that line. #[derive(Clone)] and old.clone() are the way to keep both.
Structs may have no lifetime parameters or many. struct Line<'a> { text: &'a str } borrows; the struct cannot outlive text. Structs may be generic: struct Pair<T> { a: T, b: T }. They may carry #[derive(Debug, Clone, PartialEq)] so the compiler writes the obvious trait impls. Debug is the one you want immediately, because println!("{session:?}") is how you inspect values.
There is no inheritance. A struct does not extend another struct. You embed: struct Cached { session: Session, fetched_at: u64 }. Methods live in impl blocks and do not change the layout. Two structs with identical fields are still different types; there is no structural typing.
When you reach for a struct: a group of fields that travel together and have a name in the domain. When the group is two values you will unpack at the only call site, a tuple is enough. When the value is one of several shapes, an enum. When you need a single-field wrapper for type safety, a tuple struct.
Code Examples
struct Session {
token: String,
user_id: u64,
}
impl Session {
fn new(token: String, user_id: u64) -> Self {
Self { token, user_id }
}
}
fn main() {
let old = Session::new("abc".into(), 17);
let new = Session { user_id: 18, ..old };
println!("{} {}", new.user_id, new.token); // prints: 18 abc
}Common Gotchas
`..rest` moves the unused fields
After `Session { user_id: 1, ..old }`, `old` cannot be used unless every moved field is `Copy` or you cloned first.
Missing fields are a compile error
Struct literals are exhaustive. Use `..default` only when you have a value to copy from, or implement a constructor.
Public fields are API
A `pub token: String` lets callers write `session.token = String::new()`. Keep fields private if you have an invariant.
Identical layouts are still different types
You cannot pass `UserId(u64)` where `SessionId(u64)` is expected. That is the newtype benefit.