rust-syntax-traits
rust-syntax-traits
The mechanics of trait declaration and implementation in Rust: how to define a trait, how default methods work, how supertraits compose, how the orphan rule and coherence constrain impl Trait for Type, how to seal a trait against external implementors, and which built-in marker traits (Send, Sync, Sized, Copy, Unpin) have special compiler treatment.
Cross-references: [[rust-syntax-generics]] (trait bounds on type parameters) [[rust-syntax-trait-objects]] (dyn Trait and dyn-compatibility) [[rust-syntax-gats]] (associated types with generics) [[rust-errors-trait-bounds]] (E-codes for unsatisfied bounds) [[rust-core-memory-model]] (Send / Sync auto-trait rules)
When to use this skill
- User writes
trait Foo { ... }orimpl Trait for Type { ... }and needs the rules - User hits an orphan-rule error (E0117) or coherence error (E0119) and asks how to fix it
- User needs to add a method to a trait without breaking downstream implementors
- User asks about
Send/Sync/Sized/Copy/Unpinand how to opt in or out - User wants to write an extensible-looking trait that only the defining crate may implement (sealed trait)
- User confuses inherent methods (
impl Foo) with trait methods (impl Trait for Foo) - User asks about
#[derive(...)]and which derives are available instd
For dyn Trait and object safety see [[rust-syntax-trait-objects]]. For generic-parameter trait bounds (fn foo<T: Trait>()) see [[rust-syntax-generics]].