Composition over inheritance
Rust has no inheritance of data or behaviour. You reuse code by putting one struct inside another, and you share behaviour with traits. "Is-a" is not a relationship the language will draw for you.
Detailed Explanation
A Java class can extend another class, inherit its fields, override its methods, and be used wherever the parent is expected. Rust refuses that model. A struct cannot extend a struct. An impl block cannot inherit another impl block. There is no super. The closest syntax, struct Child { parent: Parent }, is containment: a Child has a Parent, and you name the field when you want the inner value.
That is composition, and it is the intended design. You are caching user sessions. A CachedSession needs the session data plus a fetched-at timestamp. In an inheritance language you might write class CachedSession extends Session. In Rust you write struct CachedSession { session: Session, fetched_at: u64 }. The inner session is a field. Methods on Session do not automatically appear on CachedSession. If you want cached.is_expired(now), you write that method and delegate: self.session.is_expired(now). The delegation is visible, which is the point — there is no hidden method resolution walking a parent chain.
Shared behaviour goes on a trait. If Session and CachedSession should both be persistable, they both impl Persistable. They do not need a common parent type. A function that accepts either one is generic: fn persist<T: Persistable>(item: &T). If you need a mixed list, you store Box<dyn Persistable>. That is subtype polymorphism without a class tree.
Rust will not flatten fields the way Go embedding does either. There is no promotion of inner methods onto the outer type. Some crates simulate it with macros; the language does not. The extra typing is the cost of not surprising readers about where a method came from.
Why Rust omitted inheritance: the fragile base class problem, the diamond problem, and the layout cost of a hidden parent pointer all show up in C++ and Java codebases. Composition plus traits gives reuse and polymorphism without those. The trade-off is that migrating a Java hierarchy into Rust is not a mechanical rewrite. You flatten the data into structs you actually store, and you recast the virtual methods as traits that only the types which need them implement.
When you reach for composition: every time you are about to invent a parent type "so the child can reuse the fields". Put the fields in a struct and store that struct. When you reach for a trait: more than one type must answer the same question. When you think you need inheritance: you almost certainly need one of those two.
Code Examples
struct Session {
user_id: u64,
expires_at: u64,
}
impl Session {
fn is_expired(&self, now: u64) -> bool {
now >= self.expires_at
}
}
struct CachedSession {
session: Session, // has-a, not is-a
fetched_at: u64,
}
impl CachedSession {
fn is_expired(&self, now: u64) -> bool {
self.session.is_expired(now) // explicit delegation
}
}
fn main() {
let cached = CachedSession {
session: Session { user_id: 17, expires_at: 100 },
fetched_at: 40,
};
println!("{}", cached.is_expired(50)); // prints: false
println!("{}", cached.fetched_at); // prints: 40
}Common Gotchas
There is no method promotion
`cached.is_expired` does not exist until you write it. Calling `cached.session.is_expired` is always legal if the field is visible.
Do not fake inheritance with macros as a first move
a macro that forwards every method hides the same coupling inheritance did. Forward the three methods you actually need.
A trait is not a parent class
implementing `Persistable` does not give you `Session`'s fields. Data lives in the struct; the trait only names operations.
`Deref` is not inheritance
implementing `Deref<Target = Session>` lets you call `Session` methods on `CachedSession`, and it surprises readers. Reserve `Deref` for smart pointers.