From the course: Rust Essential Training
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Match with default placeholder - Rust Tutorial
From the course: Rust Essential Training
Match with default placeholder
- [Instructor] The match operator can be used to evaluate more than just enums. To demonstrate that, let's initialize a variable named my number and assign it the value one, which will be stored as a u8 by using the suffix notation to specify its data type. Next, we'll create a match expression to evaluate my number. As an unsigned integer, it's first possible value is zero. And for that case, let's return a string literal that says zero. Similar to an if else expression, we can use match expressions to return a value based on the selected case. So, let's capture that return value by assigning it to a variable named result. And by doing that, we're changing it from an expression to a statement. So we'll need to add a semi-colon after the match block. Let's also add a print statement after the match block to display that return value. Now, let's continue filling out the match arms to handle the remaining possibilities.…
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)
-
-
-