Defining an Enum - The Rust Programming Language
Enums are a way of defining custom data types in a different way than you do with structs. Let’s look at a situation we might want to express in code and see why enums are useful and more appropriate than structs in this case. Say we need to work with IP addresses. Currently, two major standards are used for IP addresses: version four and version six. Because these are the only possibilities for an IP address that our program will come across, we can enumerate all possible variants, which is where enumeration gets its name.
Defining an Enum - The Rust Programming Language Keyboard shortcuts Press ← or → to navigate between chapters Press S or / to search in the book Press ? to show this help Press Esc to hide this help Auto Light Rust Coal Navy Ayu The Rust Programming Language Defining an Enum Where structs give you a way of grouping together related fields and data, like a Rectangle with its width and height , enums give you a way of saying a value is one of a possible set of values. For example, we may want to say that Rectangle is one of a set of possible shapes that also includes Circle and Triangle . To do
Explore this link on the map →saved by
related reading
- Generic Data Types - The Rust Programming Languagedoc.rust-lang.org
- Defining and Instantiating Structs - The Rust Programming Languagedoc.rust-lang.org
- Defining Shared Behavior with Traits - The Rust Programming Languagedoc.rust-lang.org
- An Example Program Using Structs - The Rust Programming Languagedoc.rust-lang.org
- What is Ownership? - The Rust Programming Languagedoc.rust-lang.org
- Storing Lists of Values with Vectors - The Rust Programming Languagedoc.rust-lang.org
- Methods - The Rust Programming Languagedoc.rust-lang.org
- The match Control Flow Construct - The Rust Programming Languagedoc.rust-lang.org
- References and Borrowing - The Rust Programming Languagedoc.rust-lang.org
- A half-hour to learn Rustfasterthanli.me
- The Slice Type - The Rust Programming Languagedoc.rust-lang.org
- Programming a Guessing Game - The Rust Programming Languagedoc.rust-lang.org