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 ## Traits bounds for 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 the types they
54 accept. Consider this function, which does not compile:
57 fn print_area<T>(shape: T) {
58 println!("This shape has an area of {}", shape.area());
65 error: no method named `area` found for type `T` in the current scope
68 Because `T` can be any type, we can’t be sure that it implements the `area`
69 method. But we can add a ‘trait constraint’ to our generic `T`, ensuring
74 # fn area(&self) -> f64;
76 fn print_area<T: HasArea>(shape: T) {
77 println!("This shape has an area of {}", shape.area());
81 The syntax `<T: HasArea>` means “any type that implements the `HasArea` trait.”
82 Because traits define function type signatures, we can be sure that any type
83 which implements `HasArea` will have an `.area()` method.
85 Here’s an extended example of how this works:
89 fn area(&self) -> f64;
98 impl HasArea for Circle {
99 fn area(&self) -> f64 {
100 std::f64::consts::PI * (self.radius * self.radius)
110 impl HasArea for Square {
111 fn area(&self) -> f64 {
112 self.side * self.side
116 fn print_area<T: HasArea>(shape: T) {
117 println!("This shape has an area of {}", shape.area());
138 This program outputs:
141 This shape has an area of 3.141593
142 This shape has an area of 1
145 As you can see, `print_area` is now generic, but also ensures that we have
146 passed in the correct types. If we pass in an incorrect type:
152 We get a compile-time error:
155 error: the trait `HasArea` is not implemented for the type `_` [E0277]
158 ## Traits bounds for generic structs
160 Your generic structs can also benefit from trait constraints. All you need to
161 do is append the constraint when you declare type parameters. Here is a new
162 type `Rectangle<T>` and its operation `is_square()`:
165 struct Rectangle<T> {
172 impl<T: PartialEq> Rectangle<T> {
173 fn is_square(&self) -> bool {
174 self.width == self.height
179 let mut r = Rectangle {
186 assert!(r.is_square());
189 assert!(!r.is_square());
193 `is_square()` needs to check that the sides are equal, so the sides must be of
194 a type that implements the [`core::cmp::PartialEq`][PartialEq] trait:
197 impl<T: PartialEq> Rectangle<T> { ... }
200 Now, a rectangle can be defined in terms of any type that can be compared for
203 [PartialEq]: ../core/cmp/trait.PartialEq.html
205 Here we defined a new struct `Rectangle` that accepts numbers of any
206 precision—really, objects of pretty much any type—as long as they can be
207 compared for equality. Could we do the same for our `HasArea` structs, `Square`
208 and `Circle`? Yes, but they need multiplication, and to work with that we need
209 to know more about [operator traits][operators-and-overloading].
211 [operators-and-overloading]: operators-and-overloading.html
213 # Rules for implementing traits
215 So far, we’ve only added trait implementations to structs, but you can
216 implement a trait for any type. So technically, we _could_ implement `HasArea`
221 fn area(&self) -> f64;
224 impl HasArea for i32 {
225 fn area(&self) -> f64 {
226 println!("this is silly");
235 It is considered poor style to implement methods on such primitive types, even
236 though it is possible.
238 This may seem like the Wild West, but there are two restrictions around
239 implementing traits that prevent this from getting out of hand. The first is
240 that if the trait isn’t defined in your scope, it doesn’t apply. Here’s an
241 example: the standard library provides a [`Write`][write] trait which adds
242 extra functionality to `File`s, for doing file I/O. By default, a `File`
243 won’t have its methods:
245 [write]: ../std/io/trait.Write.html
248 let mut f = std::fs::File::open("foo.txt").ok().expect("Couldn’t open foo.txt");
249 let buf = b"whatever"; // byte string literal. buf: &[u8; 8]
250 let result = f.write(buf);
251 # result.unwrap(); // ignore the error
257 error: type `std::fs::File` does not implement any method in scope named `write`
258 let result = f.write(buf);
262 We need to `use` the `Write` trait first:
267 let mut f = std::fs::File::open("foo.txt").ok().expect("Couldn’t open foo.txt");
268 let buf = b"whatever";
269 let result = f.write(buf);
270 # result.unwrap(); // ignore the error
273 This will compile without error.
275 This means that even if someone does something bad like add methods to `i32`,
276 it won’t affect you, unless you `use` that trait.
278 There’s one more restriction on implementing traits: either the trait, or the
279 type you’re writing the `impl` for, must be defined by you. So, we could
280 implement the `HasArea` type for `i32`, because `HasArea` is in our code. But
281 if we tried to implement `ToString`, a trait provided by Rust, for `i32`, we could
282 not, because neither the trait nor the type are in our code.
284 One last thing about traits: generic functions with a trait bound use
285 ‘monomorphization’ (mono: one, morph: form), so they are statically dispatched.
286 What’s that mean? Check out the chapter on [trait objects][to] for more details.
288 [to]: trait-objects.html
290 # Multiple trait bounds
292 You’ve seen that you can bound a generic type parameter with a trait:
295 fn foo<T: Clone>(x: T) {
300 If you need more than one bound, you can use `+`:
305 fn foo<T: Clone + Debug>(x: T) {
311 `T` now needs to be both `Clone` as well as `Debug`.
315 Writing functions with only a few generic types and a small number of trait
316 bounds isn’t too bad, but as the number increases, the syntax gets increasingly
322 fn foo<T: Clone, K: Clone + Debug>(x: T, y: K) {
329 The name of the function is on the far left, and the parameter list is on the
330 far right. The bounds are getting in the way.
332 Rust has a solution, and it’s called a ‘`where` clause’:
337 fn foo<T: Clone, K: Clone + Debug>(x: T, y: K) {
343 fn bar<T, K>(x: T, y: K) where T: Clone, K: Clone + Debug {
350 foo("Hello", "world");
351 bar("Hello", "world");
355 `foo()` uses the syntax we showed earlier, and `bar()` uses a `where` clause.
356 All you need to do is leave off the bounds when defining your type parameters,
357 and then add `where` after the parameter list. For longer lists, whitespace can
363 fn bar<T, K>(x: T, y: K)
373 This flexibility can add clarity in complex situations.
375 `where` is also more powerful than the simpler syntax. For example:
378 trait ConvertTo<Output> {
379 fn convert(&self) -> Output;
382 impl ConvertTo<i64> for i32 {
383 fn convert(&self) -> i64 { *self as i64 }
386 // can be called with T == i32
387 fn normal<T: ConvertTo<i64>>(x: &T) -> i64 {
391 // can be called with T == i64
393 // this is using ConvertTo as if it were "ConvertTo<i64>"
394 where i32: ConvertTo<T> {
399 This shows off the additional feature of `where` clauses: they allow bounds
400 where the left-hand side is an arbitrary type (`i32` in this case), not just a
401 plain type parameter (like `T`).
405 If you already know how a typical implementor will define a method, you can
406 let your trait supply a default:
410 fn is_valid(&self) -> bool;
412 fn is_invalid(&self) -> bool { !self.is_valid() }
416 Implementors of the `Foo` trait need to implement `is_valid()`, but they don’t
417 need to implement `is_invalid()`. They’ll get this default behavior. They can
418 override the default if they so choose:
422 # fn is_valid(&self) -> bool;
424 # fn is_invalid(&self) -> bool { !self.is_valid() }
428 impl Foo for UseDefault {
429 fn is_valid(&self) -> bool {
430 println!("Called UseDefault.is_valid.");
435 struct OverrideDefault;
437 impl Foo for OverrideDefault {
438 fn is_valid(&self) -> bool {
439 println!("Called OverrideDefault.is_valid.");
443 fn is_invalid(&self) -> bool {
444 println!("Called OverrideDefault.is_invalid!");
445 true // this implementation is a self-contradiction!
449 let default = UseDefault;
450 assert!(!default.is_invalid()); // prints "Called UseDefault.is_valid."
452 let over = OverrideDefault;
453 assert!(over.is_invalid()); // prints "Called OverrideDefault.is_invalid!"
458 Sometimes, implementing a trait requires implementing another trait:
470 Implementors of `FooBar` must also implement `Foo`, like this:
476 # trait FooBar : Foo {
482 fn foo(&self) { println!("foo"); }
485 impl FooBar for Baz {
486 fn foobar(&self) { println!("foobar"); }
490 If we forget to implement `Foo`, Rust will tell us:
493 error: the trait `main::Foo` is not implemented for the type `main::Baz` [E0277]
498 Implementing traits like `Debug` and `Default` over and over again can become
499 quite tedious. For that reason, Rust provides an [attribute][attributes] that
500 allows you to let Rust automatically implement traits for you:
507 println!("{:?}", Foo);
511 [attributes]: attributes.html
513 However, deriving is limited to a certain set of traits:
515 - [`Clone`](../core/clone/trait.Clone.html)
516 - [`Copy`](../core/marker/trait.Copy.html)
517 - [`Debug`](../core/fmt/trait.Debug.html)
518 - [`Default`](../core/default/trait.Default.html)
519 - [`Eq`](../core/cmp/trait.Eq.html)
520 - [`Hash`](../core/hash/trait.Hash.html)
521 - [`Ord`](../core/cmp/trait.Ord.html)
522 - [`PartialEq`](../core/cmp/trait.PartialEq.html)
523 - [`PartialOrd`](../core/cmp/trait.PartialOrd.html)