trait-concept

Rust Orphan rules

A crate cannot freely implement an external trait for an external type. A simple permitted case is implementing a foreign trait for a type defined in your crate. More general implementations involving trait arguments have ordered local-type and uncovered-parameter restrictions; this example is not the complete rule. Expected output (not executed): Ticket 8 Why: Ticket belongs to this crate, so the Display implementation is allowed. Common mistake: Treating a type alias for an external type as a new local type.

Formal Syntax & Structure

rust syntaxrust
impl ForeignTrait for LocalType { /* items */ }

Working Example

Example Snippetrust
use std::fmt;struct Ticket(u32);impl fmt::Display for Ticket {    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {        write!(f, "Ticket {}", self.0)    }}fn main() { println!("{}", Ticket(8)); }
Rust Reference