4 Building your own types

This chapter covers

  • Structs, which you can use to group values to build your own types
  • Enums, which are similar syntax to structs but used for choices, not groupings
  • Implementing types, which gives methods to your structs and enums
  • More on destructuring and taking types apart
  • References and the dot operator

It’s now time to look at the main ways to build your own types in Rust: structs and enums. You’ll also learn how to implement functions attached to and called on these types, called methods. These methods use the keyword self a lot, so get ready to see it!

4.1 A quick overview of structs and enums

Structs and enums have a similar syntax and are easiest to learn together, so that’s what we’ll do here. They also work ...

Get Learn Rust in a Month of Lunches now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.