| 
							
								 | 
							
							<!DOCTYPE HTML>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<html lang="en" class="light sidebar-visible" dir="ltr">
 | 
						
						
						
						
							 | 
							
								 | 
							
							    <head>
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <!-- Book generated using mdBook -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <meta charset="UTF-8">
 | 
						
						
						
						
							 | 
							
								 | 
							
							        <title>闭包:可以捕获其环境的匿名函数 - 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/ch13-01-closures.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="闭包可以捕获环境的匿名函数"><a class="header" href="#闭包可以捕获环境的匿名函数">闭包:可以捕获环境的匿名函数</a></h2>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<!-- https://github.com/rust-lang/book/blob/main/src/ch13-01-closures.md -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							<!-- commit 56ec353290429e6547109e88afea4de027b0f1a9 -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>Rust 的 <strong>闭包</strong>(<em>closures</em>)是可以保存在变量中或作为参数传递给其他函数的匿名函数。你可以在一个地方创建闭包,然后在不同的上下文中执行闭包运算。不同于函数,闭包允许捕获其被定义时所在作用域中的值。我们将展示这些闭包特性如何支持代码复用和行为定制。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<h3 id="使用闭包捕获环境"><a class="header" href="#使用闭包捕获环境">使用闭包捕获环境</a></h3>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>我们首先了解如何通过闭包捕获定义它的环境中的值以便之后使用。考虑如下场景:我们的 T 恤公司偶尔会向邮件列表中的某位成员赠送一件限量版的独家 T 恤作为促销。邮件列表中的成员可以选择将他们的喜爱的颜色添加到个人信息中。如果被选中的成员设置了喜爱的颜色,他们将获得那个颜色的 T 恤。如果他没有设置喜爱的颜色,他们会获赠公司当前库存最多的颜色的款式。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>有很多种方式来实现这一点。例如,使用有 <code>Red</code> 和 <code>Blue</code> 两个变体的 <code>ShirtColor</code> 枚举(出于简单考虑限定为两种颜色)。我们使用 <code>Inventory</code> 结构体来代表公司的库存,它有一个类型为 <code>Vec<ShirtColor></code> 的 <code>shirts</code> 字段表示库存中的衬衫的颜色。<code>Inventory</code> 上定义的 <code>giveaway</code> 方法获取免费衬衫得主所喜爱的颜色(如有),并返回其获得的衬衫的颜色。初始代码如示例 13-1 所示:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="filename">文件名:src/main.rs</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><code class="language-rust noplayground">#[derive(Debug, PartialEq, Copy, Clone)]
 | 
						
						
						
						
							 | 
							
								 | 
							
							enum ShirtColor {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    Red,
 | 
						
						
						
						
							 | 
							
								 | 
							
							    Blue,
 | 
						
						
						
						
							 | 
							
								 | 
							
							}
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							struct Inventory {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    shirts: Vec<ShirtColor>,
 | 
						
						
						
						
							 | 
							
								 | 
							
							}
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							impl Inventory {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    fn giveaway(&self, user_preference: Option<ShirtColor>) -> ShirtColor {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        user_preference.unwrap_or_else(|| self.most_stocked())
 | 
						
						
						
						
							 | 
							
								 | 
							
							    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    fn most_stocked(&self) -> ShirtColor {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        let mut num_red = 0;
 | 
						
						
						
						
							 | 
							
								 | 
							
							        let mut num_blue = 0;
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							        for color in &self.shirts {
 | 
						
						
						
						
							 | 
							
								 | 
							
							            match color {
 | 
						
						
						
						
							 | 
							
								 | 
							
							                ShirtColor::Red => num_red += 1,
 | 
						
						
						
						
							 | 
							
								 | 
							
							                ShirtColor::Blue => num_blue += 1,
 | 
						
						
						
						
							 | 
							
								 | 
							
							            }
 | 
						
						
						
						
							 | 
							
								 | 
							
							        }
 | 
						
						
						
						
							 | 
							
								 | 
							
							        if num_red > num_blue {
 | 
						
						
						
						
							 | 
							
								 | 
							
							            ShirtColor::Red
 | 
						
						
						
						
							 | 
							
								 | 
							
							        } else {
 | 
						
						
						
						
							 | 
							
								 | 
							
							            ShirtColor::Blue
 | 
						
						
						
						
							 | 
							
								 | 
							
							        }
 | 
						
						
						
						
							 | 
							
								 | 
							
							    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							}
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let store = Inventory {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        shirts: vec![ShirtColor::Blue, ShirtColor::Red, ShirtColor::Blue],
 | 
						
						
						
						
							 | 
							
								 | 
							
							    };
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let user_pref1 = Some(ShirtColor::Red);
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let giveaway1 = store.giveaway(user_pref1);
 | 
						
						
						
						
							 | 
							
								 | 
							
							    println!(
 | 
						
						
						
						
							 | 
							
								 | 
							
							        "The user with preference {:?} gets {:?}",
 | 
						
						
						
						
							 | 
							
								 | 
							
							        user_pref1, giveaway1
 | 
						
						
						
						
							 | 
							
								 | 
							
							    );
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let user_pref2 = None;
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let giveaway2 = store.giveaway(user_pref2);
 | 
						
						
						
						
							 | 
							
								 | 
							
							    println!(
 | 
						
						
						
						
							 | 
							
								 | 
							
							        "The user with preference {:?} gets {:?}",
 | 
						
						
						
						
							 | 
							
								 | 
							
							        user_pref2, giveaway2
 | 
						
						
						
						
							 | 
							
								 | 
							
							    );
 | 
						
						
						
						
							 | 
							
								 | 
							
							}</code></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="caption">示例 13-1:衬衫公司赠送场景</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><code>main</code> 函数中定义的 <code>store</code> 还剩下两件蓝衬衫和一件红衬衫,可以在限量版促销活动中赠送。我们通过调用 <code>giveaway</code> 方法,为一个期望红衬衫的用户和一个没有特定偏好的用户进行赠送。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>再次强调,这段代码有多种实现方式。这里为了专注于闭包,我们继续使用已经学习过的概念,除了 <code>giveaway</code> 方法体中使用了闭包。在 <code>giveaway</code> 方法中,我们将用户偏好作为 <code>Option<ShirtColor></code> 类型的参数获取,并在 <code>user_preference</code> 上调用 <code>unwrap_or_else</code> 方法。<a href="https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or_else"><code>Option<T></code> 上的 <code>unwrap_or_else</code> 方法</a>由标准库定义。它接受一个无参闭包作为参数,该闭包返回一个 <code>T</code> 类型的值(与 <code>Option<T></code> 的 <code>Some</code> 变体中存储的值类型相同,这里是 <code>ShirtColor</code>)。如果 <code>Option<T></code> 是 <code>Some</code> 变体,则 <code>unwrap_or_else</code>  返回 <code>Some</code> 中的值。如果 <code>Option<T></code> 是 <code>None</code> 变体,则 <code>unwrap_or_else</code> 调用闭包并返回闭包的返回值。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>我们将闭包表达式 <code>|| self.most_stocked()</code> 作为 <code>unwrap_or_else</code> 的参数。这是一个本身不获取参数的闭包(如果闭包有参数,它们会出现在两道竖杠之间)。闭包体调用了 <code>self.most_stocked()</code>。我们在这里定义了闭包,而 <code>unwrap_or_else</code> 的实现会在之后需要其结果的时候执行闭包。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>运行代码会打印出:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><code class="language-console">$ cargo run
 | 
						
						
						
						
							 | 
							
								 | 
							
							   Compiling shirt-company v0.1.0 (file:///projects/shirt-company)
 | 
						
						
						
						
							 | 
							
								 | 
							
							    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.27s
 | 
						
						
						
						
							 | 
							
								 | 
							
							     Running `target/debug/shirt-company`
 | 
						
						
						
						
							 | 
							
								 | 
							
							The user with preference Some(Red) gets Red
 | 
						
						
						
						
							 | 
							
								 | 
							
							The user with preference None gets Blue
 | 
						
						
						
						
							 | 
							
								 | 
							
							</code></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>这里有一个有趣的地方是,我们传递了一个闭包,该闭包会在当前的 <code>Inventory</code> 实例上调用 <code>self.most_stocked()</code> 方法。标准库不需要了解我们定义的 <code>Inventory</code> 或 <code>ShirtColor</code> 类型,也不需要了解我们在这个场景中要使用的逻辑。闭包捕获了对 <code>self</code>(即 <code>Inventory</code> 实例)的不可变引用,并将其与我们指定的代码一起传递给 <code>unwrap_or_else</code> 方法。相比之下,函数无法以这种方式捕获其环境。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<h3 id="闭包类型推断和注解"><a class="header" href="#闭包类型推断和注解">闭包类型推断和注解</a></h3>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>函数与闭包还有更多区别。闭包通常不要求像 <code>fn</code> 函数那样对参数和返回值进行类型注解。函数需要类型注解是因为这些类型是暴露给用户的显式接口的一部分。严格定义这些接口对于确保所有人对函数使用和返回值的类型达成一致理解非常重要。与此相比,闭包并不用于这样暴露在外的接口:它们储存在变量中并被使用,不用命名它们或暴露给库的用户调用。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>闭包通常较短,并且只与特定的上下文相关,而不是适用于任意情境。在这些有限的上下文中,编译器可以推断参数和返回值的类型,类似于它推断大多数变量类型的方式(尽管在某些罕见的情况下,编译器也需要闭包的类型注解)。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>类似于变量,如果我们希望增加代码的明确性和清晰度,可以添加类型注解,但代价是会使代码变得比严格必要的更冗长。为示例 13-1 中定义的闭包标注类型看起来如示例 13-2 中的定义一样。这个例子中,我们定义了一个闭包并将它保存在变量中,而不是像示例 13-1 那样在传参的地方定义它。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="filename">文件名:src/main.rs</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">use std::thread;
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">use std::time::Duration;
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">fn generate_workout(intensity: u32, random_number: u32) {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span>    let expensive_closure = |num: u32| -> u32 {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        println!("calculating slowly...");
 | 
						
						
						
						
							 | 
							
								 | 
							
							        thread::sleep(Duration::from_secs(2));
 | 
						
						
						
						
							 | 
							
								 | 
							
							        num
 | 
						
						
						
						
							 | 
							
								 | 
							
							    };
 | 
						
						
						
						
							 | 
							
								 | 
							
							<span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    if intensity < 25 {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        println!("Today, do {} pushups!", expensive_closure(intensity));
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        println!("Next, do {} situps!", expensive_closure(intensity));
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    } else {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        if random_number == 3 {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">            println!("Take a break today! Remember to stay hydrated!");
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        } else {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">            println!(
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">                "Today, run for {} minutes!",
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">                expensive_closure(intensity)
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">            );
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">        }
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    let simulated_user_specified_value = 10;
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    let simulated_random_number = 7;
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">    generate_workout(simulated_user_specified_value, simulated_random_number);
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span><span class="boring">}</span></code></pre></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="caption">示例 13-2:为闭包的参数和返回值增加可选的类型注解</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>有了类型注解,闭包的语法看起来就更像函数的语法了。如下是一个对其参数加一的函数的定义与拥有相同行为闭包语法的纵向对比。这里增加了一些空格来对齐相应部分。这展示了除了使用管道符(|,pipes)以及一些可选语法外,闭包语法与函数语法有多么地相似:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><code class="language-rust ignore">fn  add_one_v1   (x: u32) -> u32 { x + 1 }
 | 
						
						
						
						
							 | 
							
								 | 
							
							let add_one_v2 = |x: u32| -> u32 { x + 1 };
 | 
						
						
						
						
							 | 
							
								 | 
							
							let add_one_v3 = |x|             { x + 1 };
 | 
						
						
						
						
							 | 
							
								 | 
							
							let add_one_v4 = |x|               x + 1  ;</code></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>第一行展示了一个函数定义,第二行展示了一个完整标注的闭包定义。第三行闭包定义中省略了类型注解,而第四行去掉了可选的大括号,因为闭包体只有一个表达式,所以大括号是可选的。这些都是有效的闭包定义,并在调用时产生相同的行为。调用闭包是 <code>add_one_v3</code> 和 <code>add_one_v4</code> 能够编译的必要条件,因为类型将从其用法中推断出来。这类似于 <code>let v = Vec::new();</code>,Rust 需要类型注解或是某种类型的值被插入到 <code>Vec</code> 中,才能推断其类型。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>对于闭包定义,编译器会为每个参数和返回值推断出一个具体类型。例如,示例 13-3 展示了一个简短的闭包定义,该闭包仅仅返回作为参数接收到的值。除了作为示例用途外,这个闭包并不是很实用。注意这个定义没有增加任何类型注解。因为没有类型注解,我们可以使用任意类型来调用这个闭包,我们在这里第一次调用时使用了 <code>String</code> 类型。但是如果我们接着尝试使用整数来调用 <code>example_closure</code>,就会得到一个错误。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="filename">文件名:src/main.rs</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><code class="language-rust ignore does_not_compile"><span class="boring">fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							</span>    let example_closure = |x| x;
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let s = example_closure(String::from("hello"));
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let n = example_closure(5);
 | 
						
						
						
						
							 | 
							
								 | 
							
							<span class="boring">}</span></code></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="caption">示例 13-3:尝试调用一个被推断为两个不同类型的闭包</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>编译器给出如下错误:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><code class="language-console">$ cargo run
 | 
						
						
						
						
							 | 
							
								 | 
							
							   Compiling closure-example v0.1.0 (file:///projects/closure-example)
 | 
						
						
						
						
							 | 
							
								 | 
							
							error[E0308]: mismatched types
 | 
						
						
						
						
							 | 
							
								 | 
							
							 --> src/main.rs:5:29
 | 
						
						
						
						
							 | 
							
								 | 
							
							  |
 | 
						
						
						
						
							 | 
							
								 | 
							
							5 |     let n = example_closure(5);
 | 
						
						
						
						
							 | 
							
								 | 
							
							  |             --------------- ^- help: try using a conversion method: `.to_string()`
 | 
						
						
						
						
							 | 
							
								 | 
							
							  |             |               |
 | 
						
						
						
						
							 | 
							
								 | 
							
							  |             |               expected `String`, found integer
 | 
						
						
						
						
							 | 
							
								 | 
							
							  |             arguments to this function are incorrect
 | 
						
						
						
						
							 | 
							
								 | 
							
							  |
 | 
						
						
						
						
							 | 
							
								 | 
							
							note: expected because the closure was earlier called with an argument of type `String`
 | 
						
						
						
						
							 | 
							
								 | 
							
							 --> src/main.rs:4:29
 | 
						
						
						
						
							 | 
							
								 | 
							
							  |
 | 
						
						
						
						
							 | 
							
								 | 
							
							4 |     let s = example_closure(String::from("hello"));
 | 
						
						
						
						
							 | 
							
								 | 
							
							  |             --------------- ^^^^^^^^^^^^^^^^^^^^^ expected because this argument is of type `String`
 | 
						
						
						
						
							 | 
							
								 | 
							
							  |             |
 | 
						
						
						
						
							 | 
							
								 | 
							
							  |             in this closure call
 | 
						
						
						
						
							 | 
							
								 | 
							
							note: closure parameter defined here
 | 
						
						
						
						
							 | 
							
								 | 
							
							 --> src/main.rs:2:28
 | 
						
						
						
						
							 | 
							
								 | 
							
							  |
 | 
						
						
						
						
							 | 
							
								 | 
							
							2 |     let example_closure = |x| x;
 | 
						
						
						
						
							 | 
							
								 | 
							
							  |                            ^
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							For more information about this error, try `rustc --explain E0308`.
 | 
						
						
						
						
							 | 
							
								 | 
							
							error: could not compile `closure-example` (bin "closure-example") due to 1 previous error
 | 
						
						
						
						
							 | 
							
								 | 
							
							</code></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>第一次使用 <code>String</code> 值调用 <code>example_closure</code> 时,编译器推断出 <code>x</code> 的类型以及闭包的返回类型为 <code>String</code>。接着这些类型被锁定进闭包 <code>example_closure</code> 中,如果尝试对同一闭包使用不同类型则就会得到类型错误。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<h3 id="捕获引用或移动所有权"><a class="header" href="#捕获引用或移动所有权">捕获引用或移动所有权</a></h3>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>闭包可以通过三种方式捕获其环境中的值,它们直接对应到函数获取参数的三种方式:不可变借用、可变借用和获取所有权。闭包将根据函数体中对捕获值的操作来决定使用哪种方式。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>在示例 13-4 中定义了一个捕获名为 <code>list</code> 的 vector 的不可变引用的闭包,因为只需不可变引用就能打印其值:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="filename">文件名:src/main.rs</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let list = vec![1, 2, 3];
 | 
						
						
						
						
							 | 
							
								 | 
							
							    println!("Before defining closure: {list:?}");
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let only_borrows = || println!("From closure: {list:?}");
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    println!("Before calling closure: {list:?}");
 | 
						
						
						
						
							 | 
							
								 | 
							
							    only_borrows();
 | 
						
						
						
						
							 | 
							
								 | 
							
							    println!("After calling closure: {list:?}");
 | 
						
						
						
						
							 | 
							
								 | 
							
							}</code></pre></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="caption">示例 13-4:定义并调用一个捕获不可变引用的闭包</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>这个示例也展示了变量可以绑定一个闭包定义,并且我们可以像使用函数名一样,使用变量名和括号来调用该闭包。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>因为同时可以有多个 <code>list</code> 的不可变引用,所以在闭包定义之前,闭包定义之后调用之前,闭包调用之后代码仍然可以访问 <code>list</code>。该代码可以编译、运行并输出:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><code class="language-console">$ cargo run
 | 
						
						
						
						
							 | 
							
								 | 
							
							   Compiling closure-example v0.1.0 (file:///projects/closure-example)
 | 
						
						
						
						
							 | 
							
								 | 
							
							    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.43s
 | 
						
						
						
						
							 | 
							
								 | 
							
							     Running `target/debug/closure-example`
 | 
						
						
						
						
							 | 
							
								 | 
							
							Before defining closure: [1, 2, 3]
 | 
						
						
						
						
							 | 
							
								 | 
							
							Before calling closure: [1, 2, 3]
 | 
						
						
						
						
							 | 
							
								 | 
							
							From closure: [1, 2, 3]
 | 
						
						
						
						
							 | 
							
								 | 
							
							After calling closure: [1, 2, 3]
 | 
						
						
						
						
							 | 
							
								 | 
							
							</code></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>接下来在示例 13-5 中,我们修改闭包体让它向 <code>list</code> vector 增加一个元素。闭包现在捕获一个可变引用:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="filename">文件名:src/main.rs</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let mut list = vec![1, 2, 3];
 | 
						
						
						
						
							 | 
							
								 | 
							
							    println!("Before defining closure: {list:?}");
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let mut borrows_mutably = || list.push(7);
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    borrows_mutably();
 | 
						
						
						
						
							 | 
							
								 | 
							
							    println!("After calling closure: {list:?}");
 | 
						
						
						
						
							 | 
							
								 | 
							
							}</code></pre></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="caption">示例 13-5:定义并调用一个捕获可变引用的闭包</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>代码可以编译、运行并打印:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><code class="language-console">$ cargo run
 | 
						
						
						
						
							 | 
							
								 | 
							
							   Compiling closure-example v0.1.0 (file:///projects/closure-example)
 | 
						
						
						
						
							 | 
							
								 | 
							
							    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.43s
 | 
						
						
						
						
							 | 
							
								 | 
							
							     Running `target/debug/closure-example`
 | 
						
						
						
						
							 | 
							
								 | 
							
							Before defining closure: [1, 2, 3]
 | 
						
						
						
						
							 | 
							
								 | 
							
							After calling closure: [1, 2, 3, 7]
 | 
						
						
						
						
							 | 
							
								 | 
							
							</code></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>注意在 <code>borrows_mutably</code> 闭包的定义和调用之间不再有 <code>println!</code>,这是因为当 <code>borrows_mutably</code> 被定义时,它捕获了对 <code>list</code> 的可变引用。闭包在被调用后就不再被使用,这时可变借用结束。因为当可变借用存在时不允许有其它的借用,所以在闭包定义和调用之间不能有不可变引用来进行打印。可以尝试在这里添加 <code>println!</code> 看看你会得到什么报错信息!</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>即使闭包体不严格需要所有权,如果希望强制闭包获取它在环境中所使用的值的所有权,可以在参数列表前使用 <code>move</code> 关键字。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>当将闭包传递到一个新的线程时,这个技巧特别有用,因为它将数据的所有权移动到新线程中。我们将在第十六章讨论并发时详细讨论线程以及为什么你可能需要使用它们。不过现在,我们先简要探索一下如何使用需要 <code>move</code> 关键字的闭包来生成一个新线程。示例 13-6 展示了如何修改示例 13-4,以便在一个新线程中而不是在主线程中打印 vector:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="filename">文件名:src/main.rs</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><pre class="playground"><code class="language-rust edition2021">use std::thread;
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let list = vec![1, 2, 3];
 | 
						
						
						
						
							 | 
							
								 | 
							
							    println!("Before defining closure: {list:?}");
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    thread::spawn(move || println!("From thread: {list:?}"))
 | 
						
						
						
						
							 | 
							
								 | 
							
							        .join()
 | 
						
						
						
						
							 | 
							
								 | 
							
							        .unwrap();
 | 
						
						
						
						
							 | 
							
								 | 
							
							}</code></pre></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="caption">示例 13-6:使用 <code>move</code> 来强制闭包为线程获取 <code>list</code> 的所有权</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>我们生成了一个新的线程,并给这个线程传递一个闭包作为参数来运行,闭包体打印出列表。在示例 13-4 中,闭包仅通过不可变引用捕获了 <code>list</code>,因为这是打印列表所需的最小访问权限。这个例子中,尽管闭包体依然只需要不可变引用,我们还是在闭包定义前写上 <code>move</code> 关键字,以确保 <code>list</code> 被移动到闭包中。新线程可能在主线程剩余部分执行完前执行完,也可能在主线程执行完之后执行完。如果主线程维护了 <code>list</code> 的所有权但却在新线程之前结束并且丢弃了 <code>list</code>,则在线程中的不可变引用将失效。因此,编译器要求 <code>list</code> 被移动到在新线程中运行的闭包中,这样引用就是有效的。试着移除 <code>move</code> 关键字,或者在闭包定义后在主线程中使用 <code>list</code>,看看你会得到什么编译器报错!</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<h3 id="将捕获的值移出闭包和-fn-trait"><a class="header" href="#将捕获的值移出闭包和-fn-trait">将捕获的值移出闭包和 <code>Fn</code> trait</a></h3>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>一旦闭包捕获了定义它的环境中的某个值的引用或所有权(也就影响了什么会被移<strong>进</strong>闭包,如有),闭包体中的代码则决定了在稍后执行闭包时,这些引用或值将如何处理(也就影响了什么会被移<strong>出</strong>闭包,如有)。闭包体可以执行以下任一操作:将一个捕获的值移出闭包,修改捕获的值,既不移动也不修改值,或者一开始就不从环境中捕获任何值。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>闭包捕获和处理环境中的值的方式会影响闭包实现哪些 trait,而 trait 是函数和结构体指定它们可以使用哪些类型闭包的方式。根据闭包体如何处理这些值,闭包会自动、渐进地实现一个、两个或全部三个 <code>Fn</code> trait。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<ol>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<li><code>FnOnce</code> 适用于只能被调用一次的闭包。所有闭包至少都实现了这个 trait,因为所有闭包都能被调用。一个会将捕获的值从闭包体中移出的闭包只会实现 <code>FnOnce</code> trait,而不会实现其他 <code>Fn</code> 相关的 trait,因为它只能被调用一次。</li>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<li><code>FnMut</code> 适用于不会将捕获的值移出闭包体,但可能会修改捕获值的闭包。这类闭包可以被调用多次。</li>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<li><code>Fn</code> 适用于既不将捕获的值移出闭包体,也不修改捕获值的闭包,同时也包括不从环境中捕获任何值的闭包。这类闭包可以被多次调用而不会改变其环境,这在会多次并发调用闭包的场景中十分重要。</li>
 | 
						
						
						
						
							 | 
							
								 | 
							
							</ol>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>让我们来看示例 13-1 中使用的在 <code>Option<T></code> 上的 <code>unwrap_or_else</code> 方法的定义:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><code class="language-rust ignore">impl<T> Option<T> {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    pub fn unwrap_or_else<F>(self, f: F) -> T
 | 
						
						
						
						
							 | 
							
								 | 
							
							    where
 | 
						
						
						
						
							 | 
							
								 | 
							
							        F: FnOnce() -> T
 | 
						
						
						
						
							 | 
							
								 | 
							
							    {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        match self {
 | 
						
						
						
						
							 | 
							
								 | 
							
							            Some(x) => x,
 | 
						
						
						
						
							 | 
							
								 | 
							
							            None => f(),
 | 
						
						
						
						
							 | 
							
								 | 
							
							        }
 | 
						
						
						
						
							 | 
							
								 | 
							
							    }
 | 
						
						
						
						
							 | 
							
								 | 
							
							}</code></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>回忆一下,<code>T</code> 是表示 <code>Option</code> 中 <code>Some</code> 变体中的值的类型的泛型。类型 <code>T</code> 也是 <code>unwrap_or_else</code> 函数的返回值类型:举例来说,在 <code>Option<String></code> 上调用 <code>unwrap_or_else</code> 会得到一个 <code>String</code>。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>接着注意到 <code>unwrap_or_else</code> 函数有额外的泛型参数 <code>F</code>。<code>F</code> 是参数 <code>f</code> 的类型,<code>f</code> 是调用 <code>unwrap_or_else</code> 时提供的闭包。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>泛型 <code>F</code> 的 trait bound 是 <code>FnOnce() -> T</code>,这意味着 <code>F</code> 必须能够被调用一次,没有参数并返回一个 <code>T</code>。在 trait bound 中使用 <code>FnOnce</code> 表示 <code>unwrap_or_else</code> 最多只会调用 <code>f</code> 一次。在 <code>unwrap_or_else</code> 的函数体中可以看到,如果 <code>Option</code> 是 <code>Some</code>,<code>f</code> 不会被调用。如果 <code>Option</code> 是 <code>None</code>,<code>f</code> 将会被调用一次。由于所有的闭包都实现了 <code>FnOnce</code>,<code>unwrap_or_else</code> 接受所有三种类型的闭包,灵活性达到极致。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<blockquote>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>注意:如果我们要做的事情不需要从环境中捕获值,则可以在需要某种实现了 <code>Fn</code> trait 的东西时使用函数而不是闭包。举个例子,可以在 <code>Option<Vec<T>></code> 的值上调用 <code>unwrap_or_else(Vec::new)</code>,以便在值为 <code>None</code> 时获取一个新的空的 vector。编译器会自动为函数定义实现适用的 <code>Fn</code> trait。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							</blockquote>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>现在让我们来看定义在 slice 上的标准库方法 <code>sort_by_key</code>,看看它与 <code>unwrap_or_else</code> 的区别,以及为什么 <code>sort_by_key</code> 使用 <code>FnMut</code> 而不是 <code>FnOnce</code> 作为 trait bound。这个闭包以一个 slice 中当前被考虑的元素的引用作为参数,并返回一个可以排序的 <code>K</code> 类型的值。当你想按照 slice 中每个元素的某个属性进行排序时,这个函数非常有用。在示例 13-7 中,我们有一个 <code>Rectangle</code> 实例的列表,并使用 <code>sort_by_key</code> 按 <code>Rectangle</code> 的 <code>width</code> 属性对它们从低到高排序:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="filename">文件名:src/main.rs</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><pre class="playground"><code class="language-rust edition2021">#[derive(Debug)]
 | 
						
						
						
						
							 | 
							
								 | 
							
							struct Rectangle {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    width: u32,
 | 
						
						
						
						
							 | 
							
								 | 
							
							    height: u32,
 | 
						
						
						
						
							 | 
							
								 | 
							
							}
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let mut list = [
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Rectangle { width: 10, height: 1 },
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Rectangle { width: 3, height: 5 },
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Rectangle { width: 7, height: 12 },
 | 
						
						
						
						
							 | 
							
								 | 
							
							    ];
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    list.sort_by_key(|r| r.width);
 | 
						
						
						
						
							 | 
							
								 | 
							
							    println!("{list:#?}");
 | 
						
						
						
						
							 | 
							
								 | 
							
							}</code></pre></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="caption">示例 13-7:使用  <code>sort_by_key</code> 对长方形按宽度排序</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>代码输出:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><code class="language-console">$ cargo run
 | 
						
						
						
						
							 | 
							
								 | 
							
							   Compiling rectangles v0.1.0 (file:///projects/rectangles)
 | 
						
						
						
						
							 | 
							
								 | 
							
							    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.41s
 | 
						
						
						
						
							 | 
							
								 | 
							
							     Running `target/debug/rectangles`
 | 
						
						
						
						
							 | 
							
								 | 
							
							[
 | 
						
						
						
						
							 | 
							
								 | 
							
							    Rectangle {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        width: 3,
 | 
						
						
						
						
							 | 
							
								 | 
							
							        height: 5,
 | 
						
						
						
						
							 | 
							
								 | 
							
							    },
 | 
						
						
						
						
							 | 
							
								 | 
							
							    Rectangle {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        width: 7,
 | 
						
						
						
						
							 | 
							
								 | 
							
							        height: 12,
 | 
						
						
						
						
							 | 
							
								 | 
							
							    },
 | 
						
						
						
						
							 | 
							
								 | 
							
							    Rectangle {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        width: 10,
 | 
						
						
						
						
							 | 
							
								 | 
							
							        height: 1,
 | 
						
						
						
						
							 | 
							
								 | 
							
							    },
 | 
						
						
						
						
							 | 
							
								 | 
							
							]
 | 
						
						
						
						
							 | 
							
								 | 
							
							</code></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><code>sort_by_key</code> 被定义为接收一个 <code>FnMut</code> 闭包的原因是它会多次调用这个闭包:对 slice 中的每个元素调用一次。闭包 <code>|r| r.width</code> 不捕获、修改或将任何东西移出它的环境,所以它满足 trait bound 的要求。</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>相比之下,示例 13-8 展示了一个只实现了 <code>FnOnce</code> trait 的闭包的例子,因为它从环境中移出了一个值。编译器不允许我们在 <code>sort_by_key</code> 中使用这个闭包:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="filename">文件名:src/main.rs</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><code class="language-rust ignore does_not_compile">#[derive(Debug)]
 | 
						
						
						
						
							 | 
							
								 | 
							
							struct Rectangle {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    width: u32,
 | 
						
						
						
						
							 | 
							
								 | 
							
							    height: u32,
 | 
						
						
						
						
							 | 
							
								 | 
							
							}
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let mut list = [
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Rectangle { width: 10, height: 1 },
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Rectangle { width: 3, height: 5 },
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Rectangle { width: 7, height: 12 },
 | 
						
						
						
						
							 | 
							
								 | 
							
							    ];
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let mut sort_operations = vec![];
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let value = String::from("closure called");
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    list.sort_by_key(|r| {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        sort_operations.push(value);
 | 
						
						
						
						
							 | 
							
								 | 
							
							        r.width
 | 
						
						
						
						
							 | 
							
								 | 
							
							    });
 | 
						
						
						
						
							 | 
							
								 | 
							
							    println!("{list:#?}");
 | 
						
						
						
						
							 | 
							
								 | 
							
							}</code></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="caption">示例 13-8:尝试在 <code>sort_by_key</code> 上使用一个 <code>FnOnce</code> 闭包</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>这是一个刻意构造的、复杂且无效的方式,试图统计在对 <code>list</code> 进行排序时 <code>sort_by_key</code> 调用闭包的次数。该代码试图通过将闭包环境中的 <code>value</code>(一个 <code>String</code>)插入 <code>sort_operations</code> vector 来实现计数。闭包捕获了 <code>value</code>,然后通过将 <code>value</code> 的所有权转移给 <code>sort_operations</code> vector 的方式将其移出闭包。这个闭包只能被调用一次;尝试第二次调用它将无法工作,因为这时 <code>value</code> 已经不在闭包的环境中,无法被再次插入 <code>sort_operations</code> 中!因而,这个闭包只实现了 <code>FnOnce</code>。当我们尝试编译此代码时,会出现错误提示:<code>value</code> 不能从闭包中移出,因为闭包必须实现 <code>FnMut</code>:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><code class="language-console">$ cargo run
 | 
						
						
						
						
							 | 
							
								 | 
							
							   Compiling rectangles v0.1.0 (file:///projects/rectangles)
 | 
						
						
						
						
							 | 
							
								 | 
							
							error[E0507]: cannot move out of `value`, a captured variable in an `FnMut` closure
 | 
						
						
						
						
							 | 
							
								 | 
							
							  --> src/main.rs:18:30
 | 
						
						
						
						
							 | 
							
								 | 
							
							   |
 | 
						
						
						
						
							 | 
							
								 | 
							
							15 |     let value = String::from("closure called");
 | 
						
						
						
						
							 | 
							
								 | 
							
							   |         ----- captured outer variable
 | 
						
						
						
						
							 | 
							
								 | 
							
							16 |
 | 
						
						
						
						
							 | 
							
								 | 
							
							17 |     list.sort_by_key(|r| {
 | 
						
						
						
						
							 | 
							
								 | 
							
							   |                      --- captured by this `FnMut` closure
 | 
						
						
						
						
							 | 
							
								 | 
							
							18 |         sort_operations.push(value);
 | 
						
						
						
						
							 | 
							
								 | 
							
							   |                              ^^^^^ move occurs because `value` has type `String`, which does not implement the `Copy` trait
 | 
						
						
						
						
							 | 
							
								 | 
							
							   |
 | 
						
						
						
						
							 | 
							
								 | 
							
							help: consider cloning the value if the performance cost is acceptable
 | 
						
						
						
						
							 | 
							
								 | 
							
							   |
 | 
						
						
						
						
							 | 
							
								 | 
							
							18 |         sort_operations.push(value.clone());
 | 
						
						
						
						
							 | 
							
								 | 
							
							   |                                   ++++++++
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							For more information about this error, try `rustc --explain E0507`.
 | 
						
						
						
						
							 | 
							
								 | 
							
							error: could not compile `rectangles` (bin "rectangles") due to 1 previous error
 | 
						
						
						
						
							 | 
							
								 | 
							
							</code></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>报错指向了闭包体中将 <code>value</code> 移出环境的那一行。要修复此问题,我们需要修改闭包体,使其不会将值移出环境。在环境中维护一个计数器,并在闭包体中递增其值,是计算闭包被调用次数的一个更直观的方法。示例 13-9 中的闭包可以在 <code>sort_by_key</code> 中使用,因为它只捕获了 <code>num_sort_operations</code> 计数器的可变引用,因此可以被多次调用:</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="filename">文件名:src/main.rs</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<pre><pre class="playground"><code class="language-rust edition2021">#[derive(Debug)]
 | 
						
						
						
						
							 | 
							
								 | 
							
							struct Rectangle {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    width: u32,
 | 
						
						
						
						
							 | 
							
								 | 
							
							    height: u32,
 | 
						
						
						
						
							 | 
							
								 | 
							
							}
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							fn main() {
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let mut list = [
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Rectangle { width: 10, height: 1 },
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Rectangle { width: 3, height: 5 },
 | 
						
						
						
						
							 | 
							
								 | 
							
							        Rectangle { width: 7, height: 12 },
 | 
						
						
						
						
							 | 
							
								 | 
							
							    ];
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    let mut num_sort_operations = 0;
 | 
						
						
						
						
							 | 
							
								 | 
							
							    list.sort_by_key(|r| {
 | 
						
						
						
						
							 | 
							
								 | 
							
							        num_sort_operations += 1;
 | 
						
						
						
						
							 | 
							
								 | 
							
							        r.width
 | 
						
						
						
						
							 | 
							
								 | 
							
							    });
 | 
						
						
						
						
							 | 
							
								 | 
							
							    println!("{list:#?}, sorted in {num_sort_operations} operations");
 | 
						
						
						
						
							 | 
							
								 | 
							
							}</code></pre></pre>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p><span class="caption">示例 13-9:允许在 <code>sort_by_key</code> 上使用一个  <code>FnMut</code>  闭包</span></p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							<p>当定义或使用涉及闭包的函数或类型时,<code>Fn</code> trait 十分重要。在下个小节中,我们将讨论迭代器。许多迭代器方法都接收闭包参数,因此在继续前,请记住这些闭包的细节!</p>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    </main>
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							                    <nav class="nav-wrapper" aria-label="Page navigation">
 | 
						
						
						
						
							 | 
							
								 | 
							
							                        <!-- Mobile navigation buttons -->
 | 
						
						
						
						
							 | 
							
								 | 
							
							                            <a rel="prev" href="ch13-00-functional-features.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="ch13-02-iterators.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="ch13-00-functional-features.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="ch13-02-iterators.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>
 |