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.

If-let syntax

If-let syntax - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

If-let syntax

- [Instructor] Now that we've learned about the match pattern and its requirement to handle every possible case, you might be wondering, what if I only care about one of those many possible patterns? In the example shown here, we instantiate an option enum with the integer value 13 and then use a match expression to evaluate it. If it's 13, we'll print a message, but otherwise, if that option enum is anything else, including none, we do nothing. This code will work and I can prove it by running it and we get the output message of 13. However, Rust provides a simpler way to write expressions like this one, which only check for a single condition by using an if let statement. We'll replace the match expression with the keywords if let and then the pattern we're looking for, which is some(13). After that, we'll use the assignment operator, followed by the variable we're checking, and then the code to execute. In this…

Contents