From the course: Rust Essential Training

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Match with default placeholder

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.…

Contents