trait-concept
Rust Trait coherence
Coherence prevents competing trait implementations from making the meaning of a call ambiguous. For a particular trait and concrete type combination, applicable implementations must not overlap. Separate non-overlapping implementations can coexist. Expected output (not executed): 2 5 Why: Each receiver has a distinct implementing type. Common mistake: Assuming a second implementation overrides the first.
Formal Syntax & Structure
rust syntaxrust
impl Trait for Type { /* items */ }Working Example
Example Snippetrust
trait Fee { fn fee(&self) -> u32; }struct Local;struct Remote;impl Fee for Local { fn fee(&self) -> u32 { 2 } }impl Fee for Remote { fn fee(&self) -> u32 { 5 } }fn main() { println!("{} {}", Local.fee(), Remote.fee()); }