Trait
A trait is a type-level construct that defines reusable behavior or members that types can implement or compose.
Idiomatic Rust
A Rust language feature that defines shared behavior through required or provided methods without requiring a common inheritance hierarchy.
- How it is used
- Provides shared behavior across different Rust types and supports design patterns without a common inheritance hierarchy.
- How deep it goes
- Works through definitions, implementations, bounds, and compiler errors; defers blanket implementations and advanced trait uses.
- Examples
- Tests shared geometry behavior for rectangles and squares, then diagnoses missing bounds in a generic description function.
- What you can do after
- Build it from scratch by defining a trait, implementing it for types, and bounding generic functions that call its methods.
Rust in Action: Systems programming concepts and techniques
The trait keyword is Rust's reserved keyword for declaring a trait.
- How it is used
- Defines shared behavior for types and constrains generic functions to operations they support.
- How deep it goes
- Demonstrates declaring and implementing a trait and applying an operator bound, while deferring a full explanation of trait method scope.
- Examples
- Uses a skeleton Read implementation for File, generic addition, and a TryInto numeric conversion.
- What you can do after
- Build a trait from scratch and implement it for a type, then apply trait bounds to a generic function.
Read how Rust in Action: Systems programming concepts and techniques explains trait →
Learn Rust in a Month of Lunches
A Rust trait is an abstract interface that defines capabilities or behavior a type can provide.
- How it is used
- Uses traits to give different types shared capabilities and constrain which types functions accept.
- How deep it goes
- Explains signatures, default methods, derived and manual implementations, while leaving parts of Display formatting syntax unexplained.
- Examples
- Uses animal methods, custom Cat formatting, and a compiler error from printing the unit type.
- What you can do after
- Builds traits from scratch and implements standard traits for custom types.
Rust Servers, Services, and Apps
A trait is a Rust abstraction that defines shared behavior or type constraints independently of a concrete type.
- How it is used
- Models shared behavior while building TCP, HTTP, static-content, and JSON server components.
- How deep it goes
- Demonstrates derived and hand-coded implementations on custom structs, without detailing trait internals.
- Examples
- Uses a TCP client-server exchange, an HTTP library, and a server for static pages and JSON data.
- What you can do after
- Apply traits to model protocol behavior and add derived or custom behavior to Rust data structures.
Also covered in
- Julia as a Second Language
- Embedded Software with RustIn early access (MEAP)
- Code Like a Pro in Rust
- Refactoring to Rust
- Akka in Action, Second Edition
What to know first
- RustRust is a statically typed systems programming language designed for performance, concurrency, and memory safety without requiring garbage collection.
- atomic typeAn atomic type is a type whose supported accesses and operations are indivisible with respect to concurrent execution and provide defined synchronization semantics.
- methodA method is a callable function or operation associated with a type, class, or object and invoked through that association.
- compile timeCompile time is the phase before program execution in which source code is analyzed, checked, or transformed by language tools.
Related concepts
- From traitincludes · From is Rust’s standard-library trait for infallibly converting a value of one type into another type.
- Fnincludes · Fn is a Rust trait for callable values that can be invoked through shared access without consuming their captured environment.
- FnMutincludes · FnMut is a Rust callable trait for values that can be invoked through mutable access and may mutate their captured environment.
- FnOnceincludes · FnOnce is a Rust callable trait for values that may consume their captured environment and therefore can be called at most once.
- monadincludes · A monad is a programming abstraction that provides operations for sequencing computations while encapsulating and preserving their computational context.
Questions about trait
What is trait?
A trait is a type-level construct that defines reusable behavior or members that types can implement or compose.
Which Manning books explain trait?
Idiomatic Rust, Rust in Action: Systems programming concepts and techniques, Learn Rust in a Month of Lunches and Rust Servers, Services, and Apps.
What should I know before learning trait?
Manning books that teach trait usually introduce Rust, atomic type, method and compile time first.
What kinds of trait are there?
Kinds of trait include From trait, extension trait, Fn, FnMut, FnOnce and monad.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.



