1 % Using traits to share implementations
3 > **[FIXME]** Elaborate.
5 > **[FIXME]** We probably want to discourage this, at least when used in a way
6 > that is publicly exposed.
8 Traits that provide default implementations for function can provide code reuse
9 across types. For example, a `print` method can be defined across multiple
14 // Default method implementation
15 fn print(&self) { println!("{:?}", *self) }
18 impl Printable for i32 {}
20 impl Printable for String {
21 fn print(&self) { println!("{}", *self) }
24 impl Printable for bool {}
26 impl Printable for f32 {}
29 This allows the implementation of `print` to be shared across types, yet
30 overridden where needed, as seen in the `impl` for `String`.