Do you remember the impl
`impl` keyword, used to call a function with method
syntax?
struct Circle { x: f64, y: f64, radius: f64, } impl Circle { fn area(&self) -> f64 { std::f64::consts::PI * (self.radius * self.radius) } }
Traits are similar, except that we define a trait with just the method signature, then implement the trait for that struct. Like this:
fn main() { struct Circle { x: f64, y: f64, radius: f64, } trait HasArea { fn area(&self) -> f64; } impl HasArea for Circle { fn area(&self) -> f64 { std::f64::consts::PI * (self.radius * self.radius) } } }struct Circle { x: f64, y: f64, radius: f64, } trait HasArea { fn area(&self) -> f64; } impl HasArea for Circle { fn area(&self) -> f64 { std::f64::consts::PI * (self.radius * self.radius) } }
As you can see, the trait
`traitblock looks very similar to the
` block looks very similar to the impl
`implblock, but we don’t define a body, just a type signature. When we
` block,
but we don’t define a body, just a type signature. When we impl
`impla trait, we use
` a trait,
we use impl Trait for Item
`impl Trait for Item, rather than just
`, rather than just impl Item
`impl Item`.
We can use traits to constrain our generics. Consider this function, which does not compile, and gives us a similar error:
fn main() { fn print_area<T>(shape: T) { println!("This shape has an area of {}", shape.area()); } }fn print_area<T>(shape: T) { println!("This shape has an area of {}", shape.area()); }
Rust complains:
error: type `T` does not implement any method in scope named `area`
Because T
`Tcan be any type, we can’t be sure that it implements the
` can be any type, we can’t be sure that it implements the area
`areamethod. But we can add a ‘trait constraint’ to our generic
`
method. But we can add a ‘trait constraint’ to our generic T
`T`, ensuring
that it does:
fn print_area<T: HasArea>(shape: T) { println!("This shape has an area of {}", shape.area()); }
The syntax <T: HasArea>
`means
` means any type that implements the HasArea trait
`any type that implements the HasArea trait. Because traits define function type signatures, we can be sure that any type which implements
`.
Because traits define function type signatures, we can be sure that any type
which implements HasArea
`HasAreawill have an
` will have an .area()
`.area()` method.
Here’s an extended example of how this works:
trait HasArea { fn area(&self) -> f64; } struct Circle { x: f64, y: f64, radius: f64, } impl HasArea for Circle { fn area(&self) -> f64 { std::f64::consts::PI * (self.radius * self.radius) } } struct Square { x: f64, y: f64, side: f64, } impl HasArea for Square { fn area(&self) -> f64 { self.side * self.side } } fn print_area<T: HasArea>(shape: T) { println!("This shape has an area of {}", shape.area()); } fn main() { let c = Circle { x: 0.0f64, y: 0.0f64, radius: 1.0f64, }; let s = Square { x: 0.0f64, y: 0.0f64, side: 1.0f64, }; print_area(c); print_area(s); }trait HasArea { fn area(&self) -> f64; } struct Circle { x: f64, y: f64, radius: f64, } impl HasArea for Circle { fn area(&self) -> f64 { std::f64::consts::PI * (self.radius * self.radius) } } struct Square { x: f64, y: f64, side: f64, } impl HasArea for Square { fn area(&self) -> f64 { self.side * self.side } } fn print_area<T: HasArea>(shape: T) { println!("This shape has an area of {}", shape.area()); } fn main() { let c = Circle { x: 0.0f64, y: 0.0f64, radius: 1.0f64, }; let s = Square { x: 0.0f64, y: 0.0f64, side: 1.0f64, }; print_area(c); print_area(s); }
This program outputs:
This shape has an area of 3.141593
This shape has an area of 1
As you can see, print_area
`print_area` is now generic, but also ensures that we have
passed in the correct types. If we pass in an incorrect type:
print_area(5);
We get a compile-time error:
error: failed to find an implementation of trait main::HasArea for int
So far, we’ve only added trait implementations to structs, but you can
implement a trait for any type. So technically, we could implement HasArea
`HasAreafor
`
for i32
`i32`:
trait HasArea { fn area(&self) -> f64; } impl HasArea for i32 { fn area(&self) -> f64 { println!("this is silly"); *self as f64 } } 5.area();
It is considered poor style to implement methods on such primitive types, even though it is possible.
This may seem like the Wild West, but there are two other restrictions around
implementing traits that prevent this from getting out of hand. The first is
that if the trait isn’t defined in your scope, it doesn’t apply. Here’s an
example: the standard library provides a Write
`Write` trait which adds
extra functionality to File
`Files, for doing file I/O. By default, a
`s, for doing file I/O. By default, a File
`File`
won’t have its methods:
let mut f = std::fs::File::open("foo.txt").ok().expect("Couldn’t open foo.txt"); let result = f.write("whatever".as_bytes());
Here’s the error:
error: type `std::fs::File` does not implement any method in scope named `write`
let result = f.write(b"whatever");
^~~~~~~~~~~~~~~~~~
We need to use
`usethe
` the Write
`Write` trait first:
use std::io::Write; let mut f = std::fs::File::open("foo.txt").ok().expect("Couldn’t open foo.txt"); let result = f.write("whatever".as_bytes());
This will compile without error.
This means that even if someone does something bad like add methods to int
`int, it won’t affect you, unless you
`,
it won’t affect you, unless you use
`use` that trait.
There’s one more restriction on implementing traits. Either the trait or the
type you’re writing the impl
`implfor must be defined by you. So, we could implement the
` for must be defined by you. So, we could
implement the HasArea
`HasAreatype for
` type for i32
`i32, because
`, because HasArea
`HasAreais in our code. But if we tried to implement
` is in our code. But
if we tried to implement Float
`Float, a trait provided by Rust, for
`, a trait provided by Rust, for i32
`i32`, we could
not, because neither the trait nor the type are in our code.
One last thing about traits: generic functions with a trait bound use ‘monomorphization’ (mono: one, morph: form), so they are statically dispatched. What’s that mean? Check out the chapter on trait objects for more details.
You’ve seen that you can bound a generic type parameter with a trait:
fn main() { fn foo<T: Clone>(x: T) { x.clone(); } }fn foo<T: Clone>(x: T) { x.clone(); }
If you need more than one bound, you can use +
`+`:
use std::fmt::Debug; fn foo<T: Clone + Debug>(x: T) { x.clone(); println!("{:?}", x); }
T
`Tnow needs to be both
` now needs to be both Clone
`Cloneas well as
` as well as Debug
`Debug`.
Writing functions with only a few generic types and a small number of trait bounds isn’t too bad, but as the number increases, the syntax gets increasingly awkward:
fn main() { use std::fmt::Debug; fn foo<T: Clone, K: Clone + Debug>(x: T, y: K) { x.clone(); y.clone(); println!("{:?}", y); } }use std::fmt::Debug; fn foo<T: Clone, K: Clone + Debug>(x: T, y: K) { x.clone(); y.clone(); println!("{:?}", y); }
The name of the function is on the far left, and the parameter list is on the far right. The bounds are getting in the way.
Rust has a solution, and it’s called a ‘where
`where` clause’:
use std::fmt::Debug; fn foo<T: Clone, K: Clone + Debug>(x: T, y: K) { x.clone(); y.clone(); println!("{:?}", y); } fn bar<T, K>(x: T, y: K) where T: Clone, K: Clone + Debug { x.clone(); y.clone(); println!("{:?}", y); } fn main() { foo("Hello", "world"); bar("Hello", "workd"); }
foo()
`foo()uses the syntax we showed earlier, and
` uses the syntax we showed earlier, and bar()
`bar()uses a
` uses a where
`whereclause. All you need to do is leave off the bounds when defining your type parameters, and then add
` clause.
All you need to do is leave off the bounds when defining your type parameters,
and then add where
`where` after the parameter list. For longer lists, whitespace can
be added:
use std::fmt::Debug; fn bar<T, K>(x: T, y: K) where T: Clone, K: Clone + Debug { x.clone(); y.clone(); println!("{:?}", y); }
This flexibility can add clarity in complex situations.
where
`where` is also more powerful than the simpler syntax. For example:
trait ConvertTo<Output> { fn convert(&self) -> Output; } impl ConvertTo<i64> for i32 { fn convert(&self) -> i64 { *self as i64 } } // can be called with T == i32 fn normal<T: ConvertTo<i64>>(x: &T) -> i64 { x.convert() } // can be called with T == i64 fn inverse<T>() -> T // this is using ConvertTo as if it were "ConvertFrom<i32>" where i32: ConvertTo<T> { 1i32.convert() }
This shows off the additional feature of where
`whereclauses: they allow bounds where the left-hand side is an arbitrary type (
` clauses: they allow bounds
where the left-hand side is an arbitrary type (i32
`i32in this case), not just a plain type parameter (like
` in this case), not just a
plain type parameter (like T
`T`).
There’s one last feature of traits we should cover: default methods. It’s easiest just to show an example:
fn main() { trait Foo { fn bar(&self); fn baz(&self) { println!("We called baz."); } } }trait Foo { fn bar(&self); fn baz(&self) { println!("We called baz."); } }
Implementors of the Foo
`Footrait need to implement
` trait need to implement bar()
`bar(), but they don’t need to implement
`, but they don’t
need to implement baz()
`baz()`. They’ll get this default behavior. They can
override the default if they so choose:
struct UseDefault; impl Foo for UseDefault { fn bar(&self) { println!("We called bar."); } } struct OverrideDefault; impl Foo for OverrideDefault { fn bar(&self) { println!("We called bar."); } fn baz(&self) { println!("Override baz!"); } } let default = UseDefault; default.baz(); // prints "We called baz." let over = OverrideDefault; over.baz(); // prints "Override baz!"
Sometimes, implementing a trait requires implementing another trait:
fn main() { trait Foo { fn foo(&self); } trait FooBar : Foo { fn foobar(&self); } }trait Foo { fn foo(&self); } trait FooBar : Foo { fn foobar(&self); }
Implementors of FooBar
`FooBarmust also implement
` must also implement Foo
`Foo`, like this:
struct Baz; impl Foo for Baz { fn foo(&self) { println!("foo"); } } impl FooBar for Baz { fn foobar(&self) { println!("foobar"); } }
If we forget to implement Foo
`Foo`, Rust will tell us:
error: the trait `main::Foo` is not implemented for the type `main::Baz` [E0277]