|
|
|
|
<!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" class="active"><strong>6.</strong> 枚举和模式匹配</a></li><li><ul class="section"><li><a href="ch06-01-defining-an-enum.html"><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-00-enums.md">ch06-00-enums.md</a>
|
|
|
|
|
<br>
|
|
|
|
|
commit 396e2db4f7de2e5e7869b1f8bc905c45c631ad7d</p>
|
|
|
|
|
</blockquote>
|
|
|
|
|
<p>本章介绍<strong>枚举</strong>,也被称作 <em>enums</em>。枚举允许你通过列举可能的值来定义一个类型。首先,我们会定义并使用一个枚举来展示它是如何连同数据一起编码信息的。接下来,我们会探索一个特别有用的枚举,叫做<code>Option</code>,它代表一个值要么是一些值要么什么都不是。然后会讲到<code>match</code>表达式中的模式匹配如何使对枚举不同的值运行不同的代码变得容易。最后会涉及到<code>if let</code>,另一个简洁方便处理代码中枚举的结构。</p>
|
|
|
|
|
<p>枚举是一个很多语言都有的功能,不过不同语言中的功能各不相同。Rust 的枚举与像F#、OCaml 和 Haskell这样的函数式编程语言中的<strong>代数数据类型</strong>(<em>algebraic data types</em>)最为相似。</p>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- Mobile navigation buttons -->
|
|
|
|
|
|
|
|
|
|
<a href="ch05-01-method-syntax.html" class="mobile-nav-chapters previous">
|
|
|
|
|
<i class="fa fa-angle-left"></i>
|
|
|
|
|
</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<a href="ch06-01-defining-an-enum.html" class="mobile-nav-chapters next">
|
|
|
|
|
<i class="fa fa-angle-right"></i>
|
|
|
|
|
</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<a href="ch05-01-method-syntax.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-01-defining-an-enum.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>
|