<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="generator" content="rustdoc"> <title>Match</title> <link rel="stylesheet" type="text/css" href="../rust.css"> <meta name="robots" content="noindex,follow"> <link rel="shortcut icon" href="https://www.rust-lang.org/favicon.ico"> </head> <body class="rustdoc"> <!--[if lte IE 8]> <div class="warning"> This old browser is unsupported and will most likely display funky things. </div> <![endif]--> <div id="versioninfo"> <img src="https://www.rust-lang.org/logos/rust-logo-32x32-blk.png" width="32" height="32" alt="Rust logo"><br> <span class="white-sticker"><a href="https://www.rust-lang.org">Rust</a> 1.35.0</span><br> <a href="https://github.com/rust-lang/rust/commit/" class="hash white-sticker"></a> </div> <h1 class="title">Match</h1> <p><small>There is a new edition of the book and this is an old link.</small></p> <blockquote> <p><code>match</code> allows us to compare a value against a series of patterns and then execute code based on which pattern matches. Patterns can be made up of literal values, variable names, wildcards, and many other things.</p> </blockquote> <div class="example-wrap"><pre class="rust rust-example-rendered"> <span class="kw">enum</span> <span class="ident">Coin</span> { <span class="ident">Penny</span>, <span class="ident">Nickel</span>, <span class="ident">Dime</span>, <span class="ident">Quarter</span>, } <span class="kw">fn</span> <span class="ident">value_in_cents</span>(<span class="ident">coin</span>: <span class="ident">Coin</span>) <span class="op">-></span> <span class="ident">u32</span> { <span class="kw">match</span> <span class="ident">coin</span> { <span class="ident">Coin</span>::<span class="ident">Penny</span> <span class="op">=></span> <span class="number">1</span>, <span class="ident">Coin</span>::<span class="ident">Nickel</span> <span class="op">=></span> <span class="number">5</span>, <span class="ident">Coin</span>::<span class="ident">Dime</span> <span class="op">=></span> <span class="number">10</span>, <span class="ident">Coin</span>::<span class="ident">Quarter</span> <span class="op">=></span> <span class="number">25</span>, } }<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aenum%20Coin%20%7B%0A%20%20%20%20Penny%2C%0A%20%20%20%20Nickel%2C%0A%20%20%20%20Dime%2C%0A%20%20%20%20Quarter%2C%0A%7D%0A%0Afn%20value_in_cents(coin%3A%20Coin)%20-%3E%20u32%20%7B%0A%20%20%20%20match%20coin%20%7B%0A%20%20%20%20%20%20%20%20Coin%3A%3APenny%20%3D%3E%201%2C%0A%20%20%20%20%20%20%20%20Coin%3A%3ANickel%20%3D%3E%205%2C%0A%20%20%20%20%20%20%20%20Coin%3A%3ADime%20%3D%3E%2010%2C%0A%20%20%20%20%20%20%20%20Coin%3A%3AQuarter%20%3D%3E%2025%2C%0A%20%20%20%20%7D%0A%7D%0A%7D">Run</a></pre></div> <hr /> <p>Here are the relevant sections in the new and old books:</p> <ul> <li><strong><a href="ch06-02-match.html">in the current edition: Ch 6.02 — The <code>match</code> Control Flow Operator</a></strong></li> <li><a href="ch18-00-patterns.html">in the current edition: Ch 18.00 — Patterns</a></li> <li><small><a href="https://doc.rust-lang.org/1.30.0/book/first-edition/match.html">In the first edition: Ch 3.14 — Match</a></small></li> </ul> <footer><p> Copyright © 2011 The Rust Project Developers. Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a> or the <a href="https://opensource.org/licenses/MIT">MIT license</a>, at your option. </p><p> This file may not be copied, modified, or distributed except according to those terms. </p></footer> </body> </html>