Files
100-exercises-to-learn-rust/book/src/04_traits/00_intro.md
LukeMathWalker 4401743807 Formatter
2024-05-24 18:16:20 +02:00

987 B
Raw Blame History

Traits

In the previous chapter we covered the basics of Rusts type and ownership system.
Its time to dig deeper: well explore traits, Rusts take on interfaces.

Once you learn about traits, youll start seeing their fingerprints all over the place.
In fact, youve already seen traits in action throughout the previous chapter, e.g. .into() invocations as well as operators like == and +.

On top of traits as a concept, well also cover some of the key traits that are defined in Rusts standard library:

  • Operator traits (e.g. Add, Sub, PartialEq, etc.)
  • From and Into, for infallible conversions
  • Clone and Copy, for copying values
  • Deref and deref coercion
  • Sized, to mark types with a known size
  • Drop, for custom cleanup logic

Since well be talking about conversions, well seize the opportunity to plug some of the “knowledge gaps” from the previous chapter—e.g. what is "A title", exactly? Time to learn more about slices too!