diff options
| -rw-r--r-- | src/doc/trpl/ufcs.md | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/src/doc/trpl/ufcs.md b/src/doc/trpl/ufcs.md index 2353c63a606..7725970564b 100644 --- a/src/doc/trpl/ufcs.md +++ b/src/doc/trpl/ufcs.md @@ -109,19 +109,28 @@ Here’s an example of using the longer form. ```rust trait Foo { - fn clone(&self); + fn foo() -> i32; } -#[derive(Clone)] struct Bar; -impl Foo for Bar { - fn clone(&self) { - println!("Making a clone of Bar"); +impl Bar { + fn foo() -> i32 { + 20 + } +} - <Bar as Clone>::clone(self); +impl Foo for Bar { + fn foo() -> i32 { + 10 } } + +fn main() { + assert_eq!(10, <Bar as Foo>::foo()); + assert_eq!(20, Bar::foo()); +} ``` -This will call the `Clone` trait’s `clone()` method, rather than `Foo`’s. +Using the angle bracket syntax lets you call the trait method instead of the +inherent one. |
