O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Takeaways and tasks

OK, time to wrap traits up for now and head for the summary and exercises. Here's what you need to remember from traits:

  • Traits are like interfaces in OO languages: they allow giving types additional functionalities in a controlled way
  • We can make types fulfill the traits by supplying impl blocks
  • We can define impl blocks for types that have generics too, although we need to be careful about the type bounds
  • Type bounds allow us to narrow down our generic types by declaring what traits need to be implemented for a type

And here's some extra work you can and should do to drive traits in:

  1. Make your own type, without generics. Perhaps just strip off the generic type from our Money<T>. Implement some or all of the operators ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required