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.
Matching Option<T> - Rust Tutorial
From the course: Rust Essential Training
Matching Option<T>
- [Instructor] The match expression is often used with the option enum to perform different actions depending on if it has some or none value. To demonstrate that, let's modify the example from the previous video to replace line four which increments the value of the number of variable. We'll replace the right side of that assignment operator with a match expression that matches to the number variable. Then we'll create a match arm for if number has some value. And in that case, we'll return the value of the number plus one. And then for the other case, if the option enum is none, we'll simply return zero. I'll use cargo to compile and run this program. And since the get method on line three, is indexing beyond the array bounds and therefore returning none, the match statement return zero. If we change the get index to a valid index within the array, like two, and run it again. Now, the get method will return some with…
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)
-
-
-