Traits define shared behavior—like interfaces in Java. Implement traits for your types to provide methods expected by generic code or standard library APIs.
Define and implement
trait Summary { fn summarize(&self) -> String; }
impl Summary for News { ... }
Important interview questions and answers
- Q: Trait vs interface?
A: Similar role—Rust traits support default methods, associated types, and static dispatch.
Self-check
- What keyword implements a trait?
- Can a type implement multiple traits?
Tip: Traits resemble Java interfaces but support default methods and work with generics for static dispatch.
Interview prep
- Trait vs interface?
Similar role defining behavior; Rust traits support default methods, associated types, and integrate with generics and trait objects.