Structs and traits
Rust has no classes and no inheritance. Data lives in a `struct`, behaviour lives in an `impl` block, and shared behaviour across types is described by a `trait`.
A trait is a contract: implement it and your type gains that capability everywhere the trait is accepted. This is how `println!("{}", value)` works — it requires `Display`, and any type that implements `Display` can be printed.
Because traits are resolved at compile time, this abstraction costs nothing at runtime. The generated machine code is as direct as if you had written the concrete call yourself.
Run it and change it
The editor below is live: edit anything and the real compiler output updates by itself.
Contrast AAA · 16.6:1
15px