trait-concept

Rust Disambiguating methods

When two visible traits offer methods with the same name for a receiver, specify the trait explicitly. This makes the selected behavior clear without renaming either trait. Expected output (not executed): pixels paper Why: The trait path selects the desired implementation. Common mistake: Calling Report.render() without resolving the two trait candidates.

Formal Syntax & Structure

rust syntaxrust
Trait::method(&value)

Working Example

Example Snippetrust
trait Screen { fn render(&self) -> &str; }trait Print { fn render(&self) -> &str; }struct Report;impl Screen for Report { fn render(&self) -> &str { "pixels" } }impl Print for Report { fn render(&self) -> &str { "paper" } }fn main() { println!("{} {}", Screen::render(&Report), Print::render(&Report)); }
Rust Reference