If you’re coming from an object-oriented language like Java, C++, or C#, one of the first surprises you’ll encounter in Rust is:
There is no class inheritance.
Noclass Dog extends Animal
.
No overriding methods.
No superclass fields.
So⊠how the heck do you write reusable, extensible code in Rust?
Turns out, Rust gives you powerful tools that make class inheritance unnecessary â and even undesirable.
Letâs dive into the âwhyâ and âhow.â
đ§Ź Why No Class Inheritance?
Rustâs core values are safety, clarity, and performance. Class inheritance often leads to:
- Tight coupling between classes
- Fragile hierarchies that break easily with changes
- The “diamond problem” in multiple inheritance
- Hidden dependencies via superclass fields and methods
Rust instead encourages composition over inheritance, meaning:
“Instead of saying ‘is-a’, you say ‘has-a’ or ‘can-do’.”
â How to Overcome the Lack of Inheritance
1. Use Structs and Traits
Rust separates data (struct
) from behavior (trait
), and you can implement multiple traits per struct:
struct Dog {
name: String,
}
trait Animal {
fn speak(&self);
}
impl Animal for Dog {
fn speak(&self) {
println!("{} says: Woof!", self.name);
}
}
Want a Cat
too? Just implement the same trait:
struct Cat {
name: String,
}
impl Animal for Cat {
fn speak(&self) {
println!("{} says: Meow!", self.name);
}
}
Now you can treat them both as Animal
s without inheritance.
2. Trait Objects and Polymorphism
If you want polymorphism like youâd get from inheritance:
fn make_it_talk(animal: &dyn Animal) {
animal.speak();
}
This is like calling virtual methods in OOP â but explicit and safer.
3. Composition (Structs Inside Structs)
You can embed structs inside other structs for code reuse:
struct Engine {
power: u32,
}
struct Car {
engine: Engine,
}
impl Car {
fn start(&self) {
println!("Engine starts with {} horsepower!", self.engine.power);
}
}
You donât inherit from Engine
; you own one.
4. Default Trait Implementations
Traits can define default behavior, which can be overridden:
trait Animal {
fn speak(&self) {
println!("(silent animal)");
}
}
impl Animal for Dog {
fn speak(&self) {
println!("Woof!");
}
}
impl Animal for Cat {} // uses default
5. Enums for Shared Behavior
Rust enums can be used to group types and delegate behavior:
enum Pet {
Dog(Dog),
Cat(Cat),
}
impl Pet {
fn speak(&self) {
match self {
Pet::Dog(d) => d.speak(),
Pet::Cat(c) => c.speak(),
}
}
}
This gives you a clean switch-style dispatch.
đ§ Final Thoughts
You donât need class inheritance in Rust â and thatâs liberating.
By using traits for behavior, composition for structure, and enums for polymorphism, Rust gives you flexibility without sacrificing clarity or performance.
Instead of asking:
âHow do I simulate inheritance in Rust?â
Ask:
âHow can I model my problem in a more composable and safe way?â
Rustâs answer is often simpler â and smarter.
Comments
One response to “đ« Rust Doesnât Do Class Inheritance â And Thatâs a Good Thing”
đ Fontes Oficiais e TĂ©cnicas
Rust Book â “Traits”
đ https://doc.rust-lang.org/book/ch10-02-traits.html
Explica como traits funcionam, incluindo exemplos e comparaçÔes com interfaces.
Rust Reference â “Traits”
đ§ https://doc.rust-lang.org/reference/items/traits.html
Fonte técnica mais detalhada sobre a definição e uso de traits na linguagem.
Rust by Example â “Traits”
đ https://doc.rust-lang.org/rust-by-example/trait.html
Traz exemplos prĂĄticos e concisos de como implementar e usar traits.
Blog do Steve Klabnik â “Rust doesnât have inheritance. Get over it.”
âïž https://words.steveklabnik.com/rust-doesnt-have-inheritance-get-over-it
Um Ăłtimo artigo do core team do Rust explicando a filosofia por trĂĄs da ausĂȘncia de herança.
“Composition over Inheritance” â Martin Fowler
đ§± https://martinfowler.com/bliki/CompositionOverInheritance.html
Explica por que composição Ă© geralmente mais saudĂĄvel que herança em design OO â princĂpio seguido pelo Rust.
đ„ Extras (para estudo complementar)
“Object-Oriented Programming in Rust” â Jon Gjengset (YouTube)
đș https://www.youtube.com/watch?v=8O0Nt9qY_vo
Uma explicação moderna e pråtica sobre como usar traits e composição para alcançar OOP em Rust.
Crate dyn-clone â para quem precisa de objetos clonĂĄveis via dyn Trait
đ§° https://docs.rs/dyn-clone/