Structs as data
A Rust struct is a named bundle of fields, not a class. It holds data; behaviour is added later with impl. There is no hidden object header and no inheritance tree attached to the type.
Detailed Explanation
Object-oriented languages usually fuse three things into a class: the data layout, the methods, and a place in an inheritance hierarchy. Rust splits those apart. A struct is only the first piece — the layout. You declare the fields, their types, and (if you want) which of them other modules may name. That is the whole type.
This is why people coming from Java stare at a Rust file and ask where the class is. There is not one. struct Session { token: String, user_id: u64, expires_at: u64 } is a record. It occupies the space of its fields plus alignment padding. There is no vtable pointer unless you later put the value behind a trait object. There is no superclass. A Vec<Session> is a contiguous slab of session data, which is why Rust structs are cheap to store in arrays and cheap to move.
Rust has three struct shapes, and they all share this "data first" idea. A named-field struct is the usual record. A tuple struct is a named wrapper around positional fields, used for newtypes and small products. A unit struct has no fields at all and exists as a marker or a ZST (zero-sized type). None of them become a class when you add methods. Adding an impl block does not change the layout and does not introduce a hidden pointer.
Construction is explicit. There is no implicit default constructor. Session { token, user_id, expires_at } is a struct literal. If a field is private, callers outside the module cannot write that literal; they go through a constructor function you provide, typically Session::new. That is encapsulation by visibility, not by a language-level private keyword on a class.
Why this design exists: inheritance couples types in ways that are hard to undo, and a hidden object header makes every value more expensive than its fields. Rust wanted values you can put on the stack, move without a copy of a vptr, and compose by embedding one struct inside another. The cost is that "a Session is-a Persistable" is not a relationship you write in the type header. You express it with a trait bound, or you store a Box<dyn Persistable> when you truly need a mixed collection.
When you reach for a struct: any time a group of fields travel together. When you do not: a single-field wrapper that only exists to carry methods is often a tuple struct instead, and a closed set of variants is an enum. Do not invent a class hierarchy of structs; nest them.
Code Examples
struct Session {
token: String,
user_id: u64,
expires_at: u64,
}
struct RequestLog {
session: Session, // composition: a log *has* a session, it does not inherit one
path: String,
}
fn main() {
let session = Session {
token: "abc".into(),
user_id: 17,
expires_at: 1_700_000_000,
};
let log = RequestLog {
session,
path: "/health".into(),
};
println!("{} {}", log.session.user_id, log.path); // prints: 17 /health
}Common Gotchas
A struct is not a reference type
assignment moves the value unless the type implements `Copy`. `let b = a;` leaves `a` unusable for a non-`Copy` struct.
Struct literals require every field
omitting a field is a compile error. Use `..other` to fill the rest from an existing value, and remember that `..other` still moves the unused fields.
Adding methods does not change the layout
an `impl` block is not a vtable. Dynamic dispatch appears only when you use `dyn Trait`.
Public fields are part of your API
a `pub` field can be written by any crate, so you cannot later enforce an invariant on it without a breaking change.