Traits as interfaces
A trait is Rust's interface: a named set of method signatures that a type can implement. Polymorphism is "any type that implements this trait", not "any subclass of this class".
Detailed Explanation
In Java you write interface Persistable { void save(); } and then class Session implements Persistable. In Rust you write trait Persistable { fn save(&self); } and then impl Persistable for Session { fn save(&self) { ... } }. The idea is the same — a contract — but the mechanics differ in ways that change how you design programs.
Implementation is explicit and separate from the type. The struct does not list the traits it implements. You can add an impl Trait for Type in the crate that owns the trait or the crate that owns the type. You cannot implement a foreign trait for a foreign type (the orphan rule). That rule is what keeps two crates from shipping conflicting impls of the same pair.
A trait may include default method bodies. Implementors get those for free and may override them. A trait may also include associated types (type Err;) and associated constants. Those are part of the contract: each implementor picks a concrete Err, and callers name it as <T as Persistable>::Err.
The usual way you use a trait is not a trait object. It is a generic bound: fn persist<T: Persistable>(item: &T). The compiler monomorphises that function for each T you call it with, so the call is a direct function call. This is static dispatch. You reach for dyn Persistable only when you need a single collection that holds mixed implementors, or when you want to avoid monomorphising a large function.
Rust traits do more than Java interfaces in one important direction: they can be implemented for types you did not write, if you wrote the trait. impl Serialize for Session can live in your crate even though Serialize came from serde. That is how the ecosystem shares behaviour without a shared base class. They do less in another direction: there is no interface inheritance used as a modelling tool the way extends is used in Java. Traits can inherit other traits (trait Debug: Display), which means "to implement Debug you must also implement Display". That is a bound, not a class hierarchy.
Why this exists: Rust wanted polymorphism without forcing every value to carry a vtable pointer, and without forcing types into a single inheritance tree. The cost is verbosity — impl Trait for Type blocks add lines — and the orphan rule, which sometimes means you wrap a foreign type in a newtype just so you can implement a foreign trait for it.
When you reach for a trait: more than one type should answer the same question, or you are writing a generic function that should work for a family of types. When you do not: a single type's behaviour belongs in an inherent impl.
Code Examples
trait Persistable {
fn save(&self);
}
struct Session {
user_id: u64,
}
impl Persistable for Session {
fn save(&self) {
println!("saving {}", self.user_id);
}
}
fn persist<T: Persistable>(item: &T) {
item.save(); // static dispatch: compiled as a direct call for each T
}
fn main() {
persist(&Session { user_id: 17 }); // prints: saving 17
}Common Gotchas
Orphan rule
you cannot `impl ForeignTrait for ForeignType` in your crate. Wrap the type in a newtype you own, then implement the trait for the wrapper.
Trait bounds are not inheritance
`T: Persistable` does not let you treat `T` as a subtype you can store in a `Vec<Persistable>`. For a mixed collection you need `Vec<Box<dyn Persistable>>`.
Default methods can hide missing work
if the trait ships a default `save` that no-ops, an implementor can "implement" the trait without doing anything. Prefer required methods for the behaviour you actually need.
Coherence is global
a type has at most one `impl` of a given trait. You cannot have two competing `impl Persistable for Session` and pick at the call site.