1 # Disambiguating overlapping traits
3 A type can implement many different traits. What if two traits both require the same name? For example, many traits might have a method named `get()`. They might even have different return types!
5 Good news: because each trait implementation gets its own `impl` block, it's
6 clear which trait's `get` method you're implementing.
8 What about when it comes time to _call_ those methods? To disambiguate between
9 them, we have to use Fully Qualified Syntax.
12 trait UsernameWidget {
13 // Get the selected username out of this widget
14 fn get(&self) -> String;
18 // Get the selected age out of this widget
22 // A form with both a UsernameWidget and an AgeWidget
28 impl UsernameWidget for Form {
29 fn get(&self) -> String {
34 impl AgeWidget for Form {
42 username: "rustacean".to_owned(),
46 // If you uncomment this line, you'll get an error saying
47 // "multiple `get` found". Because, after all, there are multiple methods
49 // println!("{}", form.get());
51 let username = <Form as UsernameWidget>::get(&form);
52 assert_eq!("rustacean".to_owned(), username);
53 let age = <Form as AgeWidget>::get(&form);
60 [The Rust Programming Language chapter on Fully Qualified syntax][trpl_fqsyntax]
62 [trpl_fqsyntax]: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name