diff options
Diffstat (limited to 'src/doc/rust-by-example/src/trait/disambiguating.md')
-rw-r--r-- | src/doc/rust-by-example/src/trait/disambiguating.md | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/doc/rust-by-example/src/trait/disambiguating.md b/src/doc/rust-by-example/src/trait/disambiguating.md new file mode 100644 index 000000000..ae80d4acb --- /dev/null +++ b/src/doc/rust-by-example/src/trait/disambiguating.md @@ -0,0 +1,64 @@ +# Disambiguating overlapping traits + +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! + +Good news: because each trait implementation gets its own `impl` block, it's +clear which trait's `get` method you're implementing. + +What about when it comes time to _call_ those methods? To disambiguate between +them, we have to use Fully Qualified Syntax. + +```rust,editable +trait UsernameWidget { + // Get the selected username out of this widget + fn get(&self) -> String; +} + +trait AgeWidget { + // Get the selected age out of this widget + fn get(&self) -> u8; +} + +// A form with both a UsernameWidget and an AgeWidget +struct Form { + username: String, + age: u8, +} + +impl UsernameWidget for Form { + fn get(&self) -> String { + self.username.clone() + } +} + +impl AgeWidget for Form { + fn get(&self) -> u8 { + self.age + } +} + +fn main() { + let form = Form { + username: "rustacean".to_owned(), + age: 28, + }; + + // If you uncomment this line, you'll get an error saying + // "multiple `get` found". Because, after all, there are multiple methods + // named `get`. + // println!("{}", form.get()); + + let username = <Form as UsernameWidget>::get(&form); + assert_eq!("rustacean".to_owned(), username); + let age = <Form as AgeWidget>::get(&form); + assert_eq!(28, age); +} +``` + +### See also: + +[The Rust Programming Language chapter on Fully Qualified syntax][trpl_fqsyntax] + +[trpl_fqsyntax]: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name |