| 
							
								 | 
							
							<!DOCTYPE HTML>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<html lang="en" class="light sidebar-visible" dir="ltr">
 | 
						
						
						
						
							 | 
							
								 | 
							
							    <head>
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <!-- Book generated using mdBook -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <meta charset="UTF-8">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <title>if let 和 let else 简洁控制流 - Rust 程序设计语言 简体中文版</title>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <!-- Custom HTML head -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <meta name="description" content="">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <meta name="viewport" content="width=device-width, initial-scale=1">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <meta name="theme-color" content="#ffffff">
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="icon" href="favicon.svg">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="shortcut icon" href="favicon.png">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="stylesheet" href="css/variables.css">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="stylesheet" href="css/general.css">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="stylesheet" href="css/chrome.css">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="stylesheet" href="css/print.css" media="print">
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <!-- Fonts -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="stylesheet" href="fonts/fonts.css">
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <!-- Highlight.js Stylesheets -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="stylesheet" id="highlight-css" href="highlight.css">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="stylesheet" id="tomorrow-night-css" href="tomorrow-night.css">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="stylesheet" id="ayu-highlight-css" href="ayu-highlight.css">
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <!-- Custom theme stylesheets -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="stylesheet" href="ferris.css">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="stylesheet" href="theme/2018-edition.css">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="stylesheet" href="theme/semantic-notes.css">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <link rel="stylesheet" href="theme/listing.css">
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <!-- Provide site root and default themes to javascript -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							            const path_to_root = "";
 | 
						
						
						
						
							 | 
							
								 | 
							
							            const default_light_theme = "light";
 | 
						
						
						
						
							 | 
							
								 | 
							
							            const default_dark_theme = "navy";
 | 
						
						
						
						
							 | 
							
								 | 
							
							        </script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <!-- Start loading toc.js asap -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <script src="toc.js"></script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							    </head>
 | 
						
						
						
						
							 | 
							
								 | 
							
							    <body>
 | 
						
						
						
						
							 | 
							
								 | 
							
							    <div id="body-container">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <!-- Work around some values being stored in localStorage wrapped in quotes -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							            try {
 | 
						
						
						
						
							 | 
							
								 | 
							
							                let theme = localStorage.getItem('mdbook-theme');
 | 
						
						
						
						
							 | 
							
								 | 
							
							                let sidebar = localStorage.getItem('mdbook-sidebar');
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                if (theme.startsWith('"') && theme.endsWith('"')) {
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
 | 
						
						
						
						
							 | 
							
								 | 
							
							                }
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
 | 
						
						
						
						
							 | 
							
								 | 
							
							                }
 | 
						
						
						
						
							 | 
							
								 | 
							
							            } catch (e) { }
 | 
						
						
						
						
							 | 
							
								 | 
							
							        </script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <!-- Set the theme before any content is loaded, prevents flash -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							            const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
 | 
						
						
						
						
							 | 
							
								 | 
							
							            let theme;
 | 
						
						
						
						
							 | 
							
								 | 
							
							            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
 | 
						
						
						
						
							 | 
							
								 | 
							
							            if (theme === null || theme === undefined) { theme = default_theme; }
 | 
						
						
						
						
							 | 
							
								 | 
							
							            const html = document.documentElement;
 | 
						
						
						
						
							 | 
							
								 | 
							
							            html.classList.remove('light')
 | 
						
						
						
						
							 | 
							
								 | 
							
							            html.classList.add(theme);
 | 
						
						
						
						
							 | 
							
								 | 
							
							            html.classList.add("js");
 | 
						
						
						
						
							 | 
							
								 | 
							
							        </script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <!-- Hide / unhide sidebar before it is displayed -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							            let sidebar = null;
 | 
						
						
						
						
							 | 
							
								 | 
							
							            const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
 | 
						
						
						
						
							 | 
							
								 | 
							
							            if (document.body.clientWidth >= 1080) {
 | 
						
						
						
						
							 | 
							
								 | 
							
							                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
 | 
						
						
						
						
							 | 
							
								 | 
							
							                sidebar = sidebar || 'visible';
 | 
						
						
						
						
							 | 
							
								 | 
							
							            } else {
 | 
						
						
						
						
							 | 
							
								 | 
							
							                sidebar = 'hidden';
 | 
						
						
						
						
							 | 
							
								 | 
							
							            }
 | 
						
						
						
						
							 | 
							
								 | 
							
							            sidebar_toggle.checked = sidebar === 'visible';
 | 
						
						
						
						
							 | 
							
								 | 
							
							            html.classList.remove('sidebar-visible');
 | 
						
						
						
						
							 | 
							
								 | 
							
							            html.classList.add("sidebar-" + sidebar);
 | 
						
						
						
						
							 | 
							
								 | 
							
							        </script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
 | 
						
						
						
						
							 | 
							
								 | 
							
							            <!-- populated by js -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							            <mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
 | 
						
						
						
						
							 | 
							
								 | 
							
							            <noscript>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                <iframe class="sidebar-iframe-outer" src="toc.html"></iframe>
 | 
						
						
						
						
							 | 
							
								 | 
							
							            </noscript>
 | 
						
						
						
						
							 | 
							
								 | 
							
							            <div id="sidebar-resize-handle" class="sidebar-resize-handle">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                <div class="sidebar-resize-indicator"></div>
 | 
						
						
						
						
							 | 
							
								 | 
							
							            </div>
 | 
						
						
						
						
							 | 
							
								 | 
							
							        </nav>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <div id="page-wrapper" class="page-wrapper">
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							            <div class="page">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                <div id="menu-bar-hover-placeholder"></div>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                <div id="menu-bar" class="menu-bar sticky">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    <div class="left-buttons">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <i class="fa fa-bars"></i>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        </label>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <i class="fa fa-paint-brush"></i>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        </button>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        </ul>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <i class="fa fa-search"></i>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        </button>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    </div>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    <h1 class="menu-title">Rust 程序设计语言 简体中文版</h1>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    <div class="right-buttons">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <a href="print.html" title="Print this book" aria-label="Print this book">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <i id="print-button" class="fa fa-print"></i>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        </a>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <a href="https://github.com/KaiserY/trpl-zh-cn/tree/main" title="Git repository" aria-label="Git repository">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <i id="git-repository-button" class="fa fa-github"></i>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        </a>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <a href="https://github.com/KaiserY/trpl-zh-cn/edit/main/src/ch06-03-if-let.md" title="Suggest an edit" aria-label="Suggest an edit">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <i id="git-edit-button" class="fa fa-edit"></i>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        </a>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    </div>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                </div>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                <div id="search-wrapper" class="hidden">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    <form id="searchbar-outer" class="searchbar-outer">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    </form>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    <div id="searchresults-outer" class="searchresults-outer hidden">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <div id="searchresults-header" class="searchresults-header"></div>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <ul id="searchresults">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        </ul>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    </div>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                </div>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							                <script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    });
 | 
						
						
						
						
							 | 
							
								 | 
							
							                </script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                <div id="content" class="content">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    <main>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <h2 id="if-let-和-let-else-简洁控制流"><a class="header" href="#if-let-和-let-else-简洁控制流"><code>if let</code> 和 <code>let else</code> 简洁控制流</a></h2>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<!-- https://github.com/rust-lang/book/blob/main/src/ch06-03-if-let.md -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							<!-- commit 746bf4c43ef0a2810b1f2cb234bfadd5ca5382f6 -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><code>if let</code> 语法让我们以一种不那么冗长的方式结合 <code>if</code> 和 <code>let</code>,来处理只匹配一个模式的值而忽略其他模式的情况。考虑示例 6-6 中的程序,它匹配一个 <code>config_max</code> 变量中的 <code>Option<u8></code> 值并只希望当值为 <code>Some</code> 变体时执行代码:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span>    let config_max = Some(3u8);
 | 
						
						
						
						
							 | 
							
								 | 
							
							    match config_max {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Some(max) => println!("The maximum is configured to be {max}"),
 | 
						
						
						
						
							 | 
							
								 | 
							
							        _ => (),
 | 
						
						
						
						
							 | 
							
								 | 
							
							    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							<span class="boring">}</span></code></pre></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="caption">示例 6-6:<code>match</code> 只关心当值为 <code>Some</code> 时执行代码</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>如果值是 <code>Some</code>,我们希望打印出 <code>Some</code> 变体中的值,这个值被绑定到模式中的 <code>max</code> 变量里。对于 <code>None</code> 值我们不希望做任何操作。为了满足 <code>match</code> 表达式(穷尽性)的要求,必须在处理完这唯一的变体后加上 <code>_ => ()</code>,这样也要增加很多繁琐的样板代码。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>不过我们可以使用 <code>if let</code> 这种简洁的方式编写。如下代码与示例 6-6 中的 <code>match</code> 行为一致:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span>    let config_max = Some(3u8);
 | 
						
						
						
						
							 | 
							
								 | 
							
							    if let Some(max) = config_max {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        println!("The maximum is configured to be {max}");
 | 
						
						
						
						
							 | 
							
								 | 
							
							    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							<span class="boring">}</span></code></pre></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><code>if let</code> 语法获取通过等号分隔的一个模式和一个表达式。它的工作方式与 <code>match</code> 相同,这里的表达式对应 <code>match</code> 而模式则对应第一个分支。在这个例子中,模式是 <code>Some(max)</code>,<code>max</code> 绑定为 <code>Some</code> 中的值。接着可以在 <code>if let</code> 代码块中使用 <code>max</code> 了,就跟在对应的 <code>match</code> 分支中一样。只有当值匹配该模式时,<code>if let</code> 块中的代码才会执行。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>使用 <code>if let</code> 意味着编写更少代码,更少的缩进和更少的样板代码。然而,这样会失去 <code>match</code> 强制要求的穷尽性检查来确保你没有忘记处理某些情况。<code>match</code> 和 <code>if let</code> 之间的选择依赖特定的环境以及增加简洁度和失去穷尽性检查的权衡取舍。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>换句话说,可以认为 <code>if let</code> 是 <code>match</code> 的一个语法糖,它当值匹配某一模式时执行代码而忽略所有其他值。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>可以在 <code>if let</code> 中包含一个 <code>else</code>。<code>else</code> 块中的代码与 <code>match</code> 表达式中的 <code>_</code> 分支块中的代码相同,这样的 <code>match</code> 表达式就等同于 <code>if let</code> 和 <code>else</code>。回忆一下示例 6-4 中 <code>Coin</code> 枚举的定义,其 <code>Quarter</code> 变体也包含一个 <code>UsState</code> 值。如果想要计数所有不是 25 美分的硬币的同时也报告 25 美分硬币所属的州,可以使用这样一个 <code>match</code> 表达式:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#[derive(Debug)]
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">enum UsState {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Alabama,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Alaska,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    // --snip--
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">enum Coin {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Penny,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Nickel,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Dime,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Quarter(UsState),
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    let coin = Coin::Penny;
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span>    let mut count = 0;
 | 
						
						
						
						
							 | 
							
								 | 
							
							    match coin {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Coin::Quarter(state) => println!("State quarter from {state:?}!"),
 | 
						
						
						
						
							 | 
							
								 | 
							
							        _ => count += 1,
 | 
						
						
						
						
							 | 
							
								 | 
							
							    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							<span class="boring">}</span></code></pre></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>或者可以使用这样的 <code>if let</code> 和 <code>else</code> 表达式:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#[derive(Debug)]
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">enum UsState {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Alabama,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Alaska,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    // --snip--
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">enum Coin {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Penny,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Nickel,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Dime,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Quarter(UsState),
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    let coin = Coin::Penny;
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span>    let mut count = 0;
 | 
						
						
						
						
							 | 
							
								 | 
							
							    if let Coin::Quarter(state) = coin {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        println!("State quarter from {state:?}!");
 | 
						
						
						
						
							 | 
							
								 | 
							
							    } else {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        count += 1;
 | 
						
						
						
						
							 | 
							
								 | 
							
							    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							<span class="boring">}</span></code></pre></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<h2 id="使用-letelse-来保持在-愉快路径happy-path"><a class="header" href="#使用-letelse-来保持在-愉快路径happy-path">使用 <code>let...else</code> 来保持在 “愉快路径”(“Happy Path”)</a></h2>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>在实际编程中,一个常见的场景是:如果某个值存在,就对它做一些操作;如果不存在,就返回一个默认值。还是继续用处理 <code>UsState</code> 的硬币作为例子。假设我们要写点有趣的逻辑,它依赖于硬币所代表的州成立了多久。我们就可以在 <code>UsState</code> 上定义一个方法,用来检查州的“年龄”:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#[derive(Debug)] // so we can inspect the state in a minute
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">enum UsState {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Alabama,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Alaska,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    // --snip--
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span>impl UsState {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    fn existed_in(&self, year: u16) -> bool {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        match self {
 | 
						
						
						
						
							 | 
							
								 | 
							
							            UsState::Alabama => year >= 1819,
 | 
						
						
						
						
							 | 
							
								 | 
							
							            UsState::Alaska => year >= 1959,
 | 
						
						
						
						
							 | 
							
								 | 
							
							            // -- snip --
 | 
						
						
						
						
							 | 
							
								 | 
							
							        }
 | 
						
						
						
						
							 | 
							
								 | 
							
							    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							}
 | 
						
						
						
						
							 | 
							
								 | 
							
							<span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">enum Coin {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Penny,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Nickel,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Dime,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Quarter(UsState),
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">fn describe_state_quarter(coin: Coin) -> Option<String> {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    if let Coin::Quarter(state) = coin {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        if state.existed_in(1900) {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">            Some(format!("{state:?} is pretty old, for America!"))
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        } else {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">            Some(format!("{state:?} is relatively new."))
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        }
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    } else {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        None
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    if let Some(desc) = describe_state_quarter(Coin::Quarter(UsState::Alaska)) {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        println!("{desc}");
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}</span></code></pre></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>接着我们可能使用 <code>if let</code> 来匹配硬币的类型,在条件代码中引入一个 <code>state</code>,如示例 6-7 所示。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<figure class="listing">
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#[derive(Debug)] // so we can inspect the state in a minute
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">enum UsState {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Alabama,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Alaska,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    // --snip--
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">impl UsState {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    fn existed_in(&self, year: u16) -> bool {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        match self {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">            UsState::Alabama => year >= 1819,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">            UsState::Alaska => year >= 1959,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">            // -- snip --
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        }
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">enum Coin {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Penny,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Nickel,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Dime,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Quarter(UsState),
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span>fn describe_state_quarter(coin: Coin) -> Option<String> {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    if let Coin::Quarter(state) = coin {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        if state.existed_in(1900) {
 | 
						
						
						
						
							 | 
							
								 | 
							
							            Some(format!("{state:?} is pretty old, for America!"))
 | 
						
						
						
						
							 | 
							
								 | 
							
							        } else {
 | 
						
						
						
						
							 | 
							
								 | 
							
							            Some(format!("{state:?} is relatively new."))
 | 
						
						
						
						
							 | 
							
								 | 
							
							        }
 | 
						
						
						
						
							 | 
							
								 | 
							
							    } else {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        None
 | 
						
						
						
						
							 | 
							
								 | 
							
							    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							}
 | 
						
						
						
						
							 | 
							
								 | 
							
							<span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    if let Some(desc) = describe_state_quarter(Coin::Quarter(UsState::Alaska)) {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        println!("{desc}");
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}</span></code></pre></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<figcaption>示例 6-7:使用嵌套在 `if let` 中的条件来检查一个州在 1900 年是否存在</figcaption>
 | 
						
						
						
						
							 | 
							
								 | 
							
							</figure>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>这样固然可以完成任务,不过这将工作推进了 <code>if let</code> 语句中,如果需要完成的工作更为复杂,则可能难以追踪顶层分支是如何关联的。我们也可以利用这个表达式要么从 <code>if let</code> 中生成一个 <code>state</code> 要么提前返回的优势,如示例 6-8 所示。(使用 <code>match</code> 也可以实现类似效果。)</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<figure class="listing">
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#[derive(Debug)] // so we can inspect the state in a minute
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">enum UsState {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Alabama,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Alaska,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    // --snip--
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">impl UsState {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    fn existed_in(&self, year: u16) -> bool {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        match self {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">            UsState::Alabama => year >= 1819,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">            UsState::Alaska => year >= 1959,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">            // -- snip --
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        }
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">enum Coin {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Penny,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Nickel,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Dime,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Quarter(UsState),
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span>fn describe_state_quarter(coin: Coin) -> Option<String> {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let state = if let Coin::Quarter(state) = coin {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        state
 | 
						
						
						
						
							 | 
							
								 | 
							
							    } else {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        return None;
 | 
						
						
						
						
							 | 
							
								 | 
							
							    };
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    if state.existed_in(1900) {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Some(format!("{state:?} is pretty old, for America!"))
 | 
						
						
						
						
							 | 
							
								 | 
							
							    } else {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Some(format!("{state:?} is relatively new."))
 | 
						
						
						
						
							 | 
							
								 | 
							
							    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							}
 | 
						
						
						
						
							 | 
							
								 | 
							
							<span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    if let Some(desc) = describe_state_quarter(Coin::Quarter(UsState::Alaska)) {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        println!("{desc}");
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}</span></code></pre></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<figcaption>示例 6-8:使用 `if let` 来产生一个值或提前返回</figcaption>
 | 
						
						
						
						
							 | 
							
								 | 
							
							</figure>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>不过这样写在某种程度上会让人觉得有些繁琐!<code>if let</code> 的一个分支产生一个值,而另一个分支则直接从函数中返回。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>为了使这个通用模式更容易表达,Rust 提供了 <code>let...else</code>。<code>let...else</code> 语法左侧是一个模式,右侧是一个表达式,非常类似于 <code>if let</code>,不过它没有 <code>if</code> 分支,只有 <code>else</code> 分支。如果模式匹配,它会将匹配到的值绑定到外层作用域。如果模式<strong>不</strong>匹配,程序流会指向 <code>else</code> 分支,它必须从函数返回。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>在示例 6-9 中,可以看到当在示例 6-8 中的 <code>if let</code> 替换为 <code>let...else</code> 时看起来如何。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<figure class="listing">
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#[derive(Debug)] // so we can inspect the state in a minute
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">enum UsState {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Alabama,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Alaska,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    // --snip--
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">impl UsState {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    fn existed_in(&self, year: u16) -> bool {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        match self {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">            UsState::Alabama => year >= 1819,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">            UsState::Alaska => year >= 1959,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">            // -- snip --
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        }
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">enum Coin {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Penny,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Nickel,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Dime,
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    Quarter(UsState),
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span>fn describe_state_quarter(coin: Coin) -> Option<String> {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let Coin::Quarter(state) = coin else {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        return None;
 | 
						
						
						
						
							 | 
							
								 | 
							
							    };
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    if state.existed_in(1900) {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Some(format!("{state:?} is pretty old, for America!"))
 | 
						
						
						
						
							 | 
							
								 | 
							
							    } else {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Some(format!("{state:?} is relatively new."))
 | 
						
						
						
						
							 | 
							
								 | 
							
							    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							}
 | 
						
						
						
						
							 | 
							
								 | 
							
							<span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    if let Some(desc) = describe_state_quarter(Coin::Quarter(UsState::Alaska)) {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        println!("{desc}");
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}</span></code></pre></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<figcaption>示例 6-9:使用 `let...else` 来明确函数的流向</figcaption>
 | 
						
						
						
						
							 | 
							
								 | 
							
							</figure>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>注意它以这种方式在函数主体中保持了 “愉快路径”(“Happy Path”),而不用像 <code>if let</code> 那样在两个分支中拥有明显不同的控制流</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>如果你的程序遇到一个使用 <code>match</code> 表达起来过于冗长的逻辑,记住 <code>if let</code> 和 <code>let...else</code> 也在你的 Rust 工具箱中。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<h2 id="总结"><a class="header" href="#总结">总结</a></h2>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>现在我们涉及到了如何使用枚举来创建有一系列可列举值的自定义类型。我们也展示了标准库的 <code>Option<T></code> 类型是如何帮助你利用类型系统来避免出错的。当枚举值包含数据时,你可以根据需要处理多少情况来选择使用 <code>match</code> 或 <code>if let</code> 来获取并使用这些值。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>你的 Rust 程序现在能够使用结构体和枚举在自己的作用域内表现其内容了。在你的 API 中使用自定义类型保证了类型安全:编译器会确保你的函数只会得到它期望的类型的值。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>为了向你的用户提供一个组织良好的 API,它使用起来很直观并且只向用户暴露他们确实需要的部分,那么现在就让我们转向 Rust 的模块系统吧。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    </main>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    <nav class="nav-wrapper" aria-label="Page navigation">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <!-- Mobile navigation buttons -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <a rel="prev" href="ch06-02-match.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                                <i class="fa fa-angle-left"></i>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            </a>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <a rel="next prefetch" href="ch07-00-managing-growing-projects-with-packages-crates-and-modules.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                                <i class="fa fa-angle-right"></i>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            </a>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <div style="clear: both"></div>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    </nav>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                </div>
 | 
						
						
						
						
							 | 
							
								 | 
							
							            </div>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							            <nav class="nav-wide-wrapper" aria-label="Page navigation">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    <a rel="prev" href="ch06-02-match.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <i class="fa fa-angle-left"></i>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    </a>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    <a rel="next prefetch" href="ch07-00-managing-growing-projects-with-packages-crates-and-modules.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <i class="fa fa-angle-right"></i>
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    </a>
 | 
						
						
						
						
							 | 
							
								 | 
							
							            </nav>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        </div>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							            window.playground_copyable = true;
 | 
						
						
						
						
							 | 
							
								 | 
							
							        </script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <script src="elasticlunr.min.js"></script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <script src="mark.min.js"></script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <script src="searcher.js"></script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <script src="clipboard.min.js"></script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <script src="highlight.js"></script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <script src="book.js"></script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <!-- Custom JS scripts -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <script src="ferris.js"></script>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    </div>
 | 
						
						
						
						
							 | 
							
								 | 
							
							    </body>
 | 
						
						
						
						
							 | 
							
								 | 
							
							</html>
 |