Skip to content

Chapter 4 of 8

Traits, Generics, and Polymorphism

Traits are Rust's mechanism for describing shared behavior. A trait declares a set of methods that a type must implement, much like an interface in object-oriented languages, and a type implements a trait with `impl Trait for Type { ... }`. Once a trait is implemented, the type can be used anywhere the trait is required, which makes traits the foundation of Rust's polymorphism.

Generic functions and types use type parameters so that the same code can operate on many concrete types. To guarantee that a generic parameter supports the operations the function needs, you add trait bounds — either inline with `T: Trait` or with a `where` clause. At compile time, Rust performs monomorphization, generating a specialized copy of the code for each concrete type that is actually used, so generic code carries zero runtime cost compared to hand-written per-type versions.

Many useful behaviors are expressed as traits that the compiler can implement for you automatically when you opt in with `#[derive(...)]`. `Debug` enables `{:?}` formatting for inspection; `Clone` provides an explicit `.clone()`; `Copy` allows implicit bitwise duplication for stack-only types like integers and booleans; `PartialEq` and `Eq` enable equality; `PartialOrd` and `Ord` enable ordering; and `Hash` enables use in hash-based collections. All `Copy` types must also implement `Clone`, but not vice versa.

Two additional traits round out the common vocabulary. `From` describes how to construct a type from a value of type `T`, and implementing it automatically provides the reciprocal `Into`. `Display` provides user-facing formatting through `{}`, in contrast to the developer-facing output of `Debug`, and unlike `Debug` it cannot be derived. When you need heterogeneous collections or runtime polymorphism, `dyn Trait` enables dynamic dispatch through a vtable at a small runtime cost compared to the static dispatch used by generics.

All chapters
  1. 1Foundations of Rust Memory Safety
  2. 2Structures, Enums, and Pattern Matching
  3. 3Error Handling with Option and Result
  4. 4Traits, Generics, and Polymorphism
  5. 5Iterators, Closures, and Functional Patterns
  6. 6Modules, Crates, Cargo, and Testing
  7. 7Concurrency in Rust
  8. 8Smart Pointers and Unsafe Rust

Drill it

Reading is not remembering. These come from the Rust Programming deck:

Q

What is Rust?

Rust is a systems programming language focused on safety, speed, and concurrency. It achieves memory safety without a garbage collector through its ownership sy...

Q

What is ownership in Rust?

Ownership is Rust's core memory management concept. Each value has exactly one owner, and when the owner goes out of scope, the value is dropped (freed). This p...

Q

What are the three ownership rules in Rust?

Each value in Rust has a single ownerThere can only be one owner at a timeWhen the owner goes out of scope, the value is dropped

Q

What is borrowing in Rust?

Borrowing lets you reference a value without taking ownership. You create a reference with &. The original owner retains ownership while the borrower can re...