Rust Trait

Noun · Development

Definitions

  1. A named set of method signatures (and optionally default implementations) that types can implement to satisfy a shared interface. Traits enable bounded generics, dynamic dispatch via trait objects (dyn Trait), and are Rust's primary mechanism for polymorphism — similar to interfaces in Go or Java but with coherence rules preventing conflicting implementations.

    In plain English: A contract in Rust that defines what methods a type must have, allowing different types to be used interchangeably when they share the same behavior.

    Example: "Implement the Display trait for your error type so it can be printed with {} and used with the ? operator via From."

Related Terms