3 A trait is a language feature that tells the Rust compiler about
4 functionality a type must provide.
6 Do you remember the `impl` keyword, used to call a function with [method
17 fn area(&self) -> f64 {
18 std::f64::consts::PI * (self.radius * self.radius)
23 [methodsyntax]: method-syntax.html
25 Traits are similar, except that we define a trait with just the method
26 signature, then implement the trait for that struct. Like this:
36 fn area(&self) -> f64;
39 impl HasArea for Circle {
40 fn area(&self) -> f64 {
41 std::f64::consts::PI * (self.radius * self.radius)
46 As you can see, the `trait` block looks very similar to the `impl` block,
47 but we don’t define a body, just a type signature. When we `impl` a trait,
48 we use `impl Trait for Item`, rather than just `impl Item`.
50 ## Trait bounds on generic functions
52 Traits are useful because they allow a type to make certain promises about its
53 behavior. Generic functions can exploit this to constrain, or [bound][bounds], the types they
54 accept. Consider this function, which does not compile:
56 [bounds]: glossary.html#bounds
59 fn print_area<T>(shape: T) {
60 println!("This shape has an area of {}", shape.area());
67 error: no method named `area` found for type `T` in the current scope
70 Because `T` can be any type, we can’t be sure that it implements the `area`
71 method. But we can add a trait bound to our generic `T`, ensuring
76 # fn area(&self) -> f64;
78 fn print_area<T: HasArea>(shape: T) {
79 println!("This shape has an area of {}", shape.area());
83 The syntax `<T: HasArea>` means “any type that implements the `HasArea` trait.”
84 Because traits define function type signatures, we can be sure that any type
85 which implements `HasArea` will have an `.area()` method.
87 Here’s an extended example of how this works:
91 fn area(&self) -> f64;
100 impl HasArea for Circle {
101 fn area(&self) -> f64 {
102 std::f64::consts::PI * (self.radius * self.radius)
112 impl HasArea for Square {
113 fn area(&self) -> f64 {
114 self.side * self.side
118 fn print_area<T: HasArea>(shape: T) {
119 println!("This shape has an area of {}", shape.area());
140 This program outputs:
143 This shape has an area of 3.141593
144 This shape has an area of 1
147 As you can see, `print_area` is now generic, but also ensures that we have
148 passed in the correct types. If we pass in an incorrect type:
154 We get a compile-time error:
157 error: the trait `HasArea` is not implemented for the type `_` [E0277]
160 ## Trait bounds on generic structs
162 Your generic structs can also benefit from trait bounds. All you need to
163 do is append the bound when you declare type parameters. Here is a new
164 type `Rectangle<T>` and its operation `is_square()`:
167 struct Rectangle<T> {
174 impl<T: PartialEq> Rectangle<T> {
175 fn is_square(&self) -> bool {
176 self.width == self.height
181 let mut r = Rectangle {
188 assert!(r.is_square());
191 assert!(!r.is_square());
195 `is_square()` needs to check that the sides are equal, so the sides must be of
196 a type that implements the [`core::cmp::PartialEq`][PartialEq] trait:
199 impl<T: PartialEq> Rectangle<T> { ... }
202 Now, a rectangle can be defined in terms of any type that can be compared for
205 [PartialEq]: ../core/cmp/trait.PartialEq.html
207 Here we defined a new struct `Rectangle` that accepts numbers of any
208 precision—really, objects of pretty much any type—as long as they can be
209 compared for equality. Could we do the same for our `HasArea` structs, `Square`
210 and `Circle`? Yes, but they need multiplication, and to work with that we need
211 to know more about [operator traits][operators-and-overloading].
213 [operators-and-overloading]: operators-and-overloading.html
215 # Rules for implementing traits
217 So far, we’ve only added trait implementations to structs, but you can
218 implement a trait for any type. So technically, we _could_ implement `HasArea`
223 fn area(&self) -> f64;
226 impl HasArea for i32 {
227 fn area(&self) -> f64 {
228 println!("this is silly");
237 It is considered poor style to implement methods on such primitive types, even
238 though it is possible.
240 This may seem like the Wild West, but there are two restrictions around
241 implementing traits that prevent this from getting out of hand. The first is
242 that if the trait isn’t defined in your scope, it doesn’t apply. Here’s an
243 example: the standard library provides a [`Write`][write] trait which adds
244 extra functionality to `File`s, for doing file I/O. By default, a `File`
245 won’t have its methods:
247 [write]: ../std/io/trait.Write.html
250 let mut f = std::fs::File::open("foo.txt").expect("Couldn’t open foo.txt");
251 let buf = b"whatever"; // byte string literal. buf: &[u8; 8]
252 let result = f.write(buf);
253 # result.unwrap(); // ignore the error
259 error: type `std::fs::File` does not implement any method in scope named `write`
260 let result = f.write(buf);
264 We need to `use` the `Write` trait first:
269 let mut f = std::fs::File::open("foo.txt").expect("Couldn’t open foo.txt");
270 let buf = b"whatever";
271 let result = f.write(buf);
272 # result.unwrap(); // ignore the error
275 This will compile without error.
277 This means that even if someone does something bad like add methods to `i32`,
278 it won’t affect you, unless you `use` that trait.
280 There’s one more restriction on implementing traits: either the trait, or the
281 type you’re writing the `impl` for, must be defined by you. So, we could
282 implement the `HasArea` type for `i32`, because `HasArea` is in our code. But
283 if we tried to implement `ToString`, a trait provided by Rust, for `i32`, we could
284 not, because neither the trait nor the type are in our code.
286 One last thing about traits: generic functions with a trait bound use
287 ‘monomorphization’ (mono: one, morph: form), so they are statically dispatched.
288 What’s that mean? Check out the chapter on [trait objects][to] for more details.
290 [to]: trait-objects.html
292 # Multiple trait bounds
294 You’ve seen that you can bound a generic type parameter with a trait:
297 fn foo<T: Clone>(x: T) {
302 If you need more than one bound, you can use `+`:
307 fn foo<T: Clone + Debug>(x: T) {
313 `T` now needs to be both `Clone` as well as `Debug`.
317 Writing functions with only a few generic types and a small number of trait
318 bounds isn’t too bad, but as the number increases, the syntax gets increasingly
324 fn foo<T: Clone, K: Clone + Debug>(x: T, y: K) {
331 The name of the function is on the far left, and the parameter list is on the
332 far right. The bounds are getting in the way.
334 Rust has a solution, and it’s called a ‘`where` clause’:
339 fn foo<T: Clone, K: Clone + Debug>(x: T, y: K) {
345 fn bar<T, K>(x: T, y: K) where T: Clone, K: Clone + Debug {
352 foo("Hello", "world");
353 bar("Hello", "world");
357 `foo()` uses the syntax we showed earlier, and `bar()` uses a `where` clause.
358 All you need to do is leave off the bounds when defining your type parameters,
359 and then add `where` after the parameter list. For longer lists, whitespace can
365 fn bar<T, K>(x: T, y: K)
375 This flexibility can add clarity in complex situations.
377 `where` is also more powerful than the simpler syntax. For example:
380 trait ConvertTo<Output> {
381 fn convert(&self) -> Output;
384 impl ConvertTo<i64> for i32 {
385 fn convert(&self) -> i64 { *self as i64 }
388 // can be called with T == i32
389 fn normal<T: ConvertTo<i64>>(x: &T) -> i64 {
393 // can be called with T == i64
395 // this is using ConvertTo as if it were "ConvertTo<i64>"
396 where i32: ConvertTo<T> {
401 This shows off the additional feature of `where` clauses: they allow bounds
402 where the left-hand side is an arbitrary type (`i32` in this case), not just a
403 plain type parameter (like `T`). In this example, `i32` must implement
404 `ConvertTo<T>`. Rather than defining what `i32` is (since that's obvious), the
405 `where` clause here is a constraint on `T`.
409 If you already know how a typical implementor will define a method, you can
410 let your trait supply a default:
414 fn is_valid(&self) -> bool;
416 fn is_invalid(&self) -> bool { !self.is_valid() }
420 Implementors of the `Foo` trait need to implement `is_valid()`, but they don’t
421 need to implement `is_invalid()`. They’ll get this default behavior. They can
422 override the default if they so choose:
426 # fn is_valid(&self) -> bool;
428 # fn is_invalid(&self) -> bool { !self.is_valid() }
432 impl Foo for UseDefault {
433 fn is_valid(&self) -> bool {
434 println!("Called UseDefault.is_valid.");
439 struct OverrideDefault;
441 impl Foo for OverrideDefault {
442 fn is_valid(&self) -> bool {
443 println!("Called OverrideDefault.is_valid.");
447 fn is_invalid(&self) -> bool {
448 println!("Called OverrideDefault.is_invalid!");
449 true // this implementation is a self-contradiction!
453 let default = UseDefault;
454 assert!(!default.is_invalid()); // prints "Called UseDefault.is_valid."
456 let over = OverrideDefault;
457 assert!(over.is_invalid()); // prints "Called OverrideDefault.is_invalid!"
462 Sometimes, implementing a trait requires implementing another trait:
474 Implementors of `FooBar` must also implement `Foo`, like this:
480 # trait FooBar : Foo {
486 fn foo(&self) { println!("foo"); }
489 impl FooBar for Baz {
490 fn foobar(&self) { println!("foobar"); }
494 If we forget to implement `Foo`, Rust will tell us:
497 error: the trait `main::Foo` is not implemented for the type `main::Baz` [E0277]
502 Implementing traits like `Debug` and `Default` over and over again can become
503 quite tedious. For that reason, Rust provides an [attribute][attributes] that
504 allows you to let Rust automatically implement traits for you:
511 println!("{:?}", Foo);
515 [attributes]: attributes.html
517 However, deriving is limited to a certain set of traits:
519 - [`Clone`](../core/clone/trait.Clone.html)
520 - [`Copy`](../core/marker/trait.Copy.html)
521 - [`Debug`](../core/fmt/trait.Debug.html)
522 - [`Default`](../core/default/trait.Default.html)
523 - [`Eq`](../core/cmp/trait.Eq.html)
524 - [`Hash`](../core/hash/trait.Hash.html)
525 - [`Ord`](../core/cmp/trait.Ord.html)
526 - [`PartialEq`](../core/cmp/trait.PartialEq.html)
527 - [`PartialOrd`](../core/cmp/trait.PartialOrd.html)