trait-concept
Rust Disjoint implementations for generic wrappers
Concrete generic arguments can distinguish implementations. For example, a wrapper around a count and one around a boolean are different types. Stable Rust does not generally permit overlapping implementations simply because one looks more specific. Expected output (not executed): count 7 active true Why: Field<u32> and Field<bool> cannot describe the same concrete type. Common mistake: Adding impl<T> Text for Field<T> and expecting the concrete impls to take priority.
Formal Syntax & Structure
rust syntaxrust
impl Trait for Wrapper<u32> { /* items */ }Working Example
Example Snippetrust
struct Field<T>(T);trait Text { fn text(&self) -> String; }impl Text for Field<u32> { fn text(&self) -> String { format!("count {}", self.0) } }impl Text for Field<bool> { fn text(&self) -> String { format!("active {}", self.0) } }fn main() { println!("{}", Field(7_u32).text()); println!("{}", Field(true).text()); }