Created
October 11, 2019 02:25
-
-
Save cowboy-bebug/6b6d492a31e5e8a34bdef6ec0d78da18 to your computer and use it in GitHub Desktop.
specialization usage to impl a fn with default return values & "override" it for certain types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #![feature(specialization)] | |
| trait Foo { | |
| fn foo(&self); | |
| } | |
| impl<T> Foo for T { | |
| default fn foo(&self) { println!("default foo") } | |
| } | |
| struct SpecialType; | |
| impl Foo for SpecialType { | |
| fn foo(&self) { println!("foo for SpecialType") } | |
| } | |
| fn main() { | |
| 42_u32.foo(); | |
| SpecialType.foo(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment