![[Rust Guide] 6.3. The Match Control Flow Operator](/_next/image?url=https%3A%2F%2Fmedia2.dev.to%2Fdynamic%2Fimage%2Fwidth%3D1200%2Cheight%3D627%2Cfit%3Dcover%2Cgravity%3Dauto%2Cformat%3Dauto%2Fhttps%253A%252F%252Fdev-to-uploads.s3.amazonaws.com%252Fuploads%252Farticles%252Fa8vb03zm4y17l3l3yf4h.png&w=1200&q=75)
[Rust Guide] 6.3. The Match Control Flow Operator
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



