From the course: Rust Essential Training
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Define enums - Rust Tutorial
From the course: Rust Essential Training
Define enums
- [Instructor] In Rust an enum, which is short for enumeration is a data type with multiple possible variants. It enumerates a finite number of options or types of something. In this chapter, we'll look at how to define custom enum types, how enums are commonly used and a couple of standard enums you'll likely encounter when programming with Rust. I wish I could keep the space theme going here but to demonstrate enums, I think shapes make for an easy to understand example. So let's define an enum to represent one of three possible shapes. To do that, we'll use the keyword enum followed by the name for the new enum type, shape and then a pair of curly braces. Within that code block, we'll list out or enumerate the possible shapes we want to represent, starting with a circle, a rectangle and a triangle. This defines a new data type that can be one of three possible variants. Now let's instantiate a new…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
(Locked)
Define enums3m 43s
-
(Locked)
Match operator2m 43s
-
(Locked)
Match with default placeholder4m 34s
-
(Locked)
Enum methods3m 4s
-
(Locked)
Option<T> enum5m 15s
-
(Locked)
Matching Option<T>1m 31s
-
(Locked)
If-let syntax1m 57s
-
(Locked)
Challenge: Represent a location1m 4s
-
(Locked)
Solution: Represent a location1m 20s
-
(Locked)
-
-
-