# Rust has no classes or inheritance. What do I use instead?
Source: https://ruststudio.org/community/t/10/rust-has-no-classes-or-inheritance-what-do-i-use-instead
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: porting, traits, design | Views: 0

## Short answer
Rust splits a class into data (a struct or enum) and behaviour (impl blocks and traits). Shared behaviour is a trait, a closed set of variants is an enum, and reuse of another type's code is done by holding it as a field. Which one fits depends on whether the set of types is open or closed.

## Answer
## Answer
There is no `class Dog : Animal`. Three tools replace it, and each is the right choice in a different situation. The answers below show each as its own complete example; all three compiled and ran with rustc 1.93.0.

| You want | Use |
|---|---|
| Different types with the same operations, and new types may be added later | a trait |
| A fixed, known set of variants | an enum |
| To reuse the code and data of another type | composition: hold it as a field |

## Common mistakes
- Looking for a base-class field to inherit. Traits have no fields; put shared data in a struct and share it by composition.
- Using `Box<dyn Trait>` when an enum would do. Enums are simpler and let `match` check that every case is handled.
- Copying a class hierarchy of five levels. Flatten it: most levels existed only to share code.

## Related questions
### Can a trait have default method bodies?
Yes. A default method works like a virtual method with a body, and each type can override it.

### What is the difference between `dyn Trait` and a generic `T: Trait`?
A generic is resolved at compile time and produces code for each type. `dyn Trait` is resolved at run time through a vtable and lets one collection hold different types.

## Sources
- The Rust Book, "Traits: Defining Shared Behavior": https://doc.rust-lang.org/book/ch10-02-traits.html
- The Rust Book, "Object-Oriented Programming Features of Rust": https://doc.rust-lang.org/book/ch18-00-oop.html

## Answers (3)

### Answer 1 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Approach 1: a trait for shared behaviour
Use a trait when callers need "anything that can do X" and more types may be added later, including by other crates.

```rust
trait Animal {
    fn name(&self) -> String;
    fn sound(&self) -> String;
    fn describe(&self) -> String {
        format!("{} says {}", self.name(), self.sound()) // default method
    }
}
struct Dog;
struct Cat {
    indoor: bool,
}
impl Animal for Dog {
    fn name(&self) -> String { "Dog".into() }
    fn sound(&self) -> String { "woof".into() }
}
impl Animal for Cat {
    fn name(&self) -> String { if self.indoor { "House cat".into() } else { "Cat".into() } }
    fn sound(&self) -> String { "meow".into() }
}

fn main() {
    let zoo: Vec<Box<dyn Animal>> = vec![Box::new(Dog), Box::new(Cat { indoor: true })];
    for a in &zoo {
        println!("{}", a.describe());
    }
}
```

```text
Dog says woof
House cat says meow
```

Trade-off: calls through `dyn Animal` go through a vtable, and the trait carries no data.

### Answer 2 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Approach 2: an enum for a closed set
Use an enum when all variants are known in one place. `match` must handle every variant, so adding one produces compile errors at each place that needs updating.

```rust
enum Shape {
    Circle { radius: f64 },
    Rect { w: f64, h: f64 },
}

impl Shape {
    fn area(&self) -> f64 {
        match self {
            Shape::Circle { radius } => std::f64::consts::PI * radius * radius,
            Shape::Rect { w, h } => w * h,
        }
    }
}

fn main() {
    let shapes = [Shape::Circle { radius: 1.0 }, Shape::Rect { w: 2.0, h: 3.0 }];
    for s in &shapes {
        println!("{:.2}", s.area());
    }
}
```

```text
3.14
6.00
```

Trade-off: nobody outside the enum's crate can add a variant.

### Answer 3 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Approach 3: composition instead of "extends"
Use composition to reuse code and data. The "derived" type holds the "base" type as a field and calls it; an override is a new method that calls the old one.

```rust
struct Employee {
    name: String,
    salary: u32,
}
impl Employee {
    fn pay(&self) -> u32 {
        self.salary
    }
}

struct Manager {
    employee: Employee,
    reports: Vec<Employee>,
}
impl Manager {
    fn pay(&self) -> u32 {
        self.employee.pay() + 100 * self.reports.len() as u32
    }
}

fn main() {
    let m = Manager {
        employee: Employee { name: "Ada".into(), salary: 5000 },
        reports: vec![Employee { name: "Bo".into(), salary: 3000 }],
    };
    println!("{} earns {}", m.employee.name, m.pay());
}
```

```text
Ada earns 5100
```

Trade-off: you write the forwarding methods yourself. Combine with a trait if callers need to treat both types the same way.
