From the course: First Look: Rust
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Pattern matching
- [Instructor] Let's look at how pattern matching and the match expression makes it easy to run different code for different values Rust has an extremely powerful control operator called match that allows us to compare a value against a series of patterns and then execute code based on which pattern it matches. Patterns can be made of both literal values, variable names, wildcards and many other things. The power of match comes from the expressiveness of the patterns and the compiler checks that all possible cases handle. Let's create a new project and call it patterns. Let's navigate into the project, let's open our main dot RS file, take off the printLN statement here. Let's define a new variable called name, that is of string type. And then name it ram. Now ram, here, has only three characters. Therefore, if you want to get a character at index four, it's not going to work. So right here, there's a problem. Given any index, we either get a value, or don't get any value at all…
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.