From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Matching Option<T>

Matching Option<T> - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

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…

Contents