|  |  | <!DOCTYPE HTML>
 | 
						
						
						
							|  |  | <html lang="en">
 | 
						
						
						
							|  |  |     <head>
 | 
						
						
						
							|  |  |         <meta charset="UTF-8">
 | 
						
						
						
							|  |  |         <title>Rust 程序设计语言 中文版</title>
 | 
						
						
						
							|  |  |         <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
 | 
						
						
						
							|  |  |         <meta name="description" content="Rust 程序设计语言 中文版">
 | 
						
						
						
							|  |  |         <meta name="viewport" content="width=device-width, initial-scale=1">
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |         <base href="">
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |         <link rel="stylesheet" href="book.css">
 | 
						
						
						
							|  |  |         <link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |         <link rel="shortcut icon" href="favicon.png">
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |         <!-- Font Awesome -->
 | 
						
						
						
							|  |  |         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |         <link rel="stylesheet" href="highlight.css">
 | 
						
						
						
							|  |  |         <link rel="stylesheet" href="tomorrow-night.css">
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |         <!-- MathJax -->
 | 
						
						
						
							|  |  |         <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |         <!-- Fetch JQuery from CDN but have a local fallback -->
 | 
						
						
						
							|  |  |         <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
 | 
						
						
						
							|  |  |         <script>
 | 
						
						
						
							|  |  |             if (typeof jQuery == 'undefined') {
 | 
						
						
						
							|  |  |                 document.write(unescape("%3Cscript src='jquery.js'%3E%3C/script%3E"));
 | 
						
						
						
							|  |  |             }
 | 
						
						
						
							|  |  |         </script>
 | 
						
						
						
							|  |  |     </head>
 | 
						
						
						
							|  |  |     <body class="light">
 | 
						
						
						
							|  |  |         <!-- Set the theme before any content is loaded, prevents flash -->
 | 
						
						
						
							|  |  |         <script type="text/javascript">
 | 
						
						
						
							|  |  |             var theme = localStorage.getItem('theme');
 | 
						
						
						
							|  |  |             if (theme == null) { theme = 'light'; }
 | 
						
						
						
							|  |  |             $('body').removeClass().addClass(theme);
 | 
						
						
						
							|  |  |         </script>
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |         <!-- Hide / unhide sidebar before it is displayed -->
 | 
						
						
						
							|  |  |         <script type="text/javascript">
 | 
						
						
						
							|  |  |             var sidebar = localStorage.getItem('sidebar');
 | 
						
						
						
							|  |  |             if (sidebar === "hidden") { $("html").addClass("sidebar-hidden") }
 | 
						
						
						
							|  |  |             else if (sidebar === "visible") { $("html").addClass("sidebar-visible") }
 | 
						
						
						
							|  |  |         </script>
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |         <div id="sidebar" class="sidebar">
 | 
						
						
						
							|  |  |             <ul class="chapter"><li><a href="ch01-00-introduction.html"><strong>1.</strong> 介绍</a></li><li><ul class="section"><li><a href="ch01-01-installation.html"><strong>1.1.</strong> 安装</a></li><li><a href="ch01-02-hello-world.html"><strong>1.2.</strong> Hello, World!</a></li></ul></li><li><a href="ch02-00-guessing-game-tutorial.html"><strong>2.</strong> 猜猜看教程</a></li><li><a href="ch03-00-common-programming-concepts.html"><strong>3.</strong> 通用编程概念</a></li><li><ul class="section"><li><a href="ch03-01-variables-and-mutability.html"><strong>3.1.</strong> 变量和可变性</a></li><li><a href="ch03-02-data-types.html"><strong>3.2.</strong> 数据类型</a></li><li><a href="ch03-03-how-functions-work.html"><strong>3.3.</strong> 函数如何工作</a></li><li><a href="ch03-04-comments.html"><strong>3.4.</strong> 注释</a></li><li><a href="ch03-05-control-flow.html"><strong>3.5.</strong> 控制流</a></li></ul></li><li><a href="ch04-00-understanding-ownership.html"><strong>4.</strong> 认识所有权</a></li><li><ul class="section"><li><a href="ch04-01-what-is-ownership.html"><strong>4.1.</strong> 什么是所有权</a></li><li><a href="ch04-02-references-and-borrowing.html"><strong>4.2.</strong> 引用 & 借用</a></li><li><a href="ch04-03-slices.html"><strong>4.3.</strong> Slices</a></li></ul></li><li><a href="ch05-00-structs.html"><strong>5.</strong> 结构体</a></li><li><ul class="section"><li><a href="ch05-01-method-syntax.html"><strong>5.1.</strong> 方法语法</a></li></ul></li><li><a href="ch06-00-enums.html"><strong>6.</strong> 枚举和模式匹配</a></li><li><ul class="section"><li><a href="ch06-01-defining-an-enum.html" class="active"><strong>6.1.</strong> 定义枚举</a></li><li><a href="ch06-02-match.html"><strong>6.2.</strong> <code>match</code>控制流运算符</a></li><li><a href="ch06-03-if-let.html"><strong>6.3.</strong> 使用<code>if let</code>的具体控制流</a></li></ul></li></ul>
 | 
						
						
						
							|  |  |         </div>
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |         <div id="page-wrapper" class="page-wrapper">
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |             <div class="page">
 | 
						
						
						
							|  |  |                 <div id="menu-bar" class="menu-bar">
 | 
						
						
						
							|  |  |                     <div class="left-buttons">
 | 
						
						
						
							|  |  |                         <i id="sidebar-toggle" class="fa fa-bars"></i>
 | 
						
						
						
							|  |  |                         <i id="theme-toggle" class="fa fa-paint-brush"></i>
 | 
						
						
						
							|  |  |                     </div>
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |                     <h1 class="menu-title">Rust 程序设计语言 中文版</h1>
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |                     <div class="right-buttons">
 | 
						
						
						
							|  |  |                         <i id="print-button" class="fa fa-print" title="Print this book"></i>
 | 
						
						
						
							|  |  |                     </div>
 | 
						
						
						
							|  |  |                 </div>
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |                 <div id="content" class="content">
 | 
						
						
						
							|  |  |                     <h1>定义枚举</h1>
 | 
						
						
						
							|  |  | <blockquote>
 | 
						
						
						
							|  |  | <p><a href="https://github.com/rust-lang/book/blob/master/src/ch06-01-defining-an-enum.md">ch06-01-defining-an-enum.md</a>
 | 
						
						
						
							|  |  | <br>
 | 
						
						
						
							|  |  | commit 396e2db4f7de2e5e7869b1f8bc905c45c631ad7d</p>
 | 
						
						
						
							|  |  | </blockquote>
 | 
						
						
						
							|  |  | <p>让我们通过一用代码来表现的场景,来看看为什么这里枚举是有用的而且比结构体更合适。比如我们要处理 IP 地。目前被广泛使用的两个主要 IP 标准:IPv4(version four)和 IPv6(version six)。这是我们的程序只可能会遇到两种 IP 地址:我们可以<strong>枚举</strong>出所有可能的值,这也正是它名字的由来。</p>
 | 
						
						
						
							|  |  | <p>任何一个 IP 地址要么是 IPv4 的要么是 IPv6 的而不能两者都是。IP 地址的这个特性使得枚举数据结构非常适合这个场景,因为枚举值尽可能是其一个成员。IPv4 和 IPv6 从根本上讲都是 IP 地址,所以当代码在处理申请任何类型的 IP 地址的场景时应该把他们当作相同的类型。</p>
 | 
						
						
						
							|  |  | <p>可以通过在代码中定义一个<code>IpAddrKind</code>枚举来表现这个概念并列出可能的 IP 地址类型,<code>V4</code>和<code>V6</code>。这被称为枚举的<strong>成员</strong>(<em>variants</em>):</p>
 | 
						
						
						
							|  |  | <pre><code class="language-rust">enum IpAddrKind {
 | 
						
						
						
							|  |  |     V4,
 | 
						
						
						
							|  |  |     V6,
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | </code></pre>
 | 
						
						
						
							|  |  | <p>现在<code>IpAddrKind</code>就是一个可以在代码中使用的自定义类型了。</p>
 | 
						
						
						
							|  |  | <h3>枚举值</h3>
 | 
						
						
						
							|  |  | <p>可以像这样创建<code>IpAddrKind</code>两个不同成员的实例:</p>
 | 
						
						
						
							|  |  | <pre><code class="language-rust"># enum IpAddrKind {
 | 
						
						
						
							|  |  | #     V4,
 | 
						
						
						
							|  |  | #     V6,
 | 
						
						
						
							|  |  | # }
 | 
						
						
						
							|  |  | #
 | 
						
						
						
							|  |  | let four = IpAddrKind::V4;
 | 
						
						
						
							|  |  | let six = IpAddrKind::V6;
 | 
						
						
						
							|  |  | </code></pre>
 | 
						
						
						
							|  |  | <p>注意枚举的成员位于其标识符的命名空间中,并使用两个冒号分开。这么设计的益处是现在<code>IpAddrKind::V4</code>和<code>IpAddrKind::V6</code>是相同类型的:<code>IpAddrKind</code>。例如,接着我们可以顶一个函数来获取<code>IpAddrKind</code>:</p>
 | 
						
						
						
							|  |  | <pre><code class="language-rust"># enum IpAddrKind {
 | 
						
						
						
							|  |  | #     V4,
 | 
						
						
						
							|  |  | #     V6,
 | 
						
						
						
							|  |  | # }
 | 
						
						
						
							|  |  | #
 | 
						
						
						
							|  |  | fn route(ip_type: IpAddrKind) { }
 | 
						
						
						
							|  |  | </code></pre>
 | 
						
						
						
							|  |  | <p>现在可以使用任意成员来调用这个函数:</p>
 | 
						
						
						
							|  |  | <pre><code class="language-rust"># enum IpAddrKind {
 | 
						
						
						
							|  |  | #     V4,
 | 
						
						
						
							|  |  | #     V6,
 | 
						
						
						
							|  |  | # }
 | 
						
						
						
							|  |  | #
 | 
						
						
						
							|  |  | # fn route(ip_type: IpAddrKind) { }
 | 
						
						
						
							|  |  | #
 | 
						
						
						
							|  |  | route(IpAddrKind::V4);
 | 
						
						
						
							|  |  | route(IpAddrKind::V6);
 | 
						
						
						
							|  |  | </code></pre>
 | 
						
						
						
							|  |  | <p>使用枚举甚至还有更多优势。进一步考虑一下我们的 IP 地址类型,目前没有一个储存实际 IP 地址<strong>数据</strong>的方法;只知道它是什么<strong>类型</strong>的。考虑到已经在第五章学习过结构体了,你可以想如列表 6-1 那样修改这个问题:</p>
 | 
						
						
						
							|  |  | <figure>
 | 
						
						
						
							|  |  | <pre><code class="language-rust">enum IpAddrKind {
 | 
						
						
						
							|  |  |     V4,
 | 
						
						
						
							|  |  |     V6,
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | struct IpAddr {
 | 
						
						
						
							|  |  |     kind: IpAddrKind,
 | 
						
						
						
							|  |  |     address: String,
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | let home = IpAddr {
 | 
						
						
						
							|  |  |     kind: IpAddrKind::V4,
 | 
						
						
						
							|  |  |     address: String::from("127.0.0.1"),
 | 
						
						
						
							|  |  | };
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | let loopback = IpAddr {
 | 
						
						
						
							|  |  |     kind: IpAddrKind::V6,
 | 
						
						
						
							|  |  |     address: String::from("::1"),
 | 
						
						
						
							|  |  | };
 | 
						
						
						
							|  |  | </code></pre>
 | 
						
						
						
							|  |  | <figcaption>
 | 
						
						
						
							|  |  | <p>Listing 6-1: Storing the data and <code>IpAddrKind</code> variant of an IP address using a
 | 
						
						
						
							|  |  | <code>struct</code></p>
 | 
						
						
						
							|  |  | </figcaption>
 | 
						
						
						
							|  |  | </figure>
 | 
						
						
						
							|  |  | <p>这里我们定义了一个有两个字段的结构体<code>IpAddr</code>:<code>kind</code>字段是<code>IpAddrKind</code>(之前定义的枚举)类型的而<code>address</code>字段是<code>String</code>类型的。这里有两个结构体的实例。第一个,<code>home</code>,它的<code>kind</code>的值是<code>IpAddrKind::V4</code>与之相关联的地址数据是<code>127.0.0.1</code>。第二个实例,<code>loopback</code>,<code>kind</code>的值是<code>IpAddrKind</code>的另一个成员,<code>V6</code>,关联的地址是<code>::1</code>。我们使用了要给结构体来将<code>kind</code>和<code>address</code>打包在一起,现在枚举成员就与值相关联了。</p>
 | 
						
						
						
							|  |  | <p>我们可以使用一种更简洁的方式来表达相同的概念,仅仅使用枚举并将数据直接放进每一个枚举成员而不是将枚举作为结构体的一部分。<code>IpAddr</code>枚举的新定义表明了<code>V4</code>和<code>V6</code>成员都关联了<code>String</code>值:</p>
 | 
						
						
						
							|  |  | <pre><code class="language-rust">enum IpAddr {
 | 
						
						
						
							|  |  |     V4(String),
 | 
						
						
						
							|  |  |     V6(String),
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | let home = IpAddr::V4(String::from("127.0.0.1"));
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | let loopback = IpAddr::V6(String::from("::1"));
 | 
						
						
						
							|  |  | </code></pre>
 | 
						
						
						
							|  |  | <p>我们直接将数据附加到枚举的每个成员上,这样就不需要一个额外的结构体了。</p>
 | 
						
						
						
							|  |  | <p>使用枚举而不是结构体还有另外一个优势:每个成员可以处理不同类型和数量的数据。</p>
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |                 </div>
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |                 <!-- Mobile navigation buttons -->
 | 
						
						
						
							|  |  |                 
 | 
						
						
						
							|  |  |                     <a href="ch06-00-enums.html" class="mobile-nav-chapters previous">
 | 
						
						
						
							|  |  |                         <i class="fa fa-angle-left"></i>
 | 
						
						
						
							|  |  |                     </a>
 | 
						
						
						
							|  |  |                 
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |                 
 | 
						
						
						
							|  |  |                     <a href="ch06-02-match.html" class="mobile-nav-chapters next">
 | 
						
						
						
							|  |  |                         <i class="fa fa-angle-right"></i>
 | 
						
						
						
							|  |  |                     </a>
 | 
						
						
						
							|  |  |                 
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |             </div>
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |             
 | 
						
						
						
							|  |  |                 <a href="ch06-00-enums.html" class="nav-chapters previous" title="You can navigate through the chapters using the arrow keys">
 | 
						
						
						
							|  |  |                     <i class="fa fa-angle-left"></i>
 | 
						
						
						
							|  |  |                 </a>
 | 
						
						
						
							|  |  |             
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |             
 | 
						
						
						
							|  |  |                 <a href="ch06-02-match.html" class="nav-chapters next" title="You can navigate through the chapters using the arrow keys">
 | 
						
						
						
							|  |  |                     <i class="fa fa-angle-right"></i>
 | 
						
						
						
							|  |  |                 </a>
 | 
						
						
						
							|  |  |             
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |         </div>
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |         <!-- Local fallback for Font Awesome -->
 | 
						
						
						
							|  |  |         <script>
 | 
						
						
						
							|  |  |             if ($(".fa").css("font-family") !== "FontAwesome") {
 | 
						
						
						
							|  |  |                 $('<link rel="stylesheet" type="text/css" href="_FontAwesome/css/font-awesome.css">').prependTo('head');
 | 
						
						
						
							|  |  |             }
 | 
						
						
						
							|  |  |         </script>
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |         <!-- Livereload script (if served using the cli tool) -->
 | 
						
						
						
							|  |  |         
 | 
						
						
						
							|  |  |         <script type="text/javascript">
 | 
						
						
						
							|  |  |             var socket = new WebSocket("ws://localhost:3001");
 | 
						
						
						
							|  |  |             socket.onmessage = function (event) {
 | 
						
						
						
							|  |  |                 if (event.data === "reload") {
 | 
						
						
						
							|  |  |                     socket.close();
 | 
						
						
						
							|  |  |                     location.reload(true); // force reload from server (not from cache)
 | 
						
						
						
							|  |  |                 }
 | 
						
						
						
							|  |  |             };
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |             window.onbeforeunload = function() {
 | 
						
						
						
							|  |  |                 socket.close();
 | 
						
						
						
							|  |  |             }
 | 
						
						
						
							|  |  |         </script>
 | 
						
						
						
							|  |  |     
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |         <script src="highlight.js"></script>
 | 
						
						
						
							|  |  |         <script src="book.js"></script>
 | 
						
						
						
							|  |  |     </body>
 | 
						
						
						
							|  |  | </html>
 |