Back to articles
[Rust Guide] 6.3. The Match Control Flow Operator

[Rust Guide] 6.3. The Match Control Flow Operator

via Dev.toSomeB1oody

6.3.1. What Is match ? match allows a value to be compared against a series of patterns and executes the code corresponding to the matching pattern. Patterns can be literals, variable names, wildcards, and more. Think of a match expression as a coin-sorting machine: coins slide down a track with holes of different sizes, and each coin falls through the first hole that fits it. In the same way, a value goes through each pattern in match , and when it “fits” the first pattern, it falls into the associated code block that will be used during execution. 6.3.2. Practical Use of match Let’s look at an example: write a function that takes an unknown U.S. coin and determines which coin it is in a counting-machine-like way, then returns its value in cents. enum Coin { Penny , // 1 cent Nickel , // 5 cents Dime , // 10 cents Quarter , // 25 cents } fn value_in_cents ( coin : Coin ) -> u8 { match coin { Coin :: Penny => 1 , Coin :: Nickel => 5 , Coin :: Dime => 10 , Coin :: Quarter => 25 , } } Th

Continue reading on Dev.to

Opens in a new tab

Read Full Article
3 views

Related Articles