<!DOCTYPE HTML> 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< html  lang = "en" > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    < head > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        < meta  charset = "UTF-8" > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								        < title > `match`控制流运算符 - 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" > < strong > 6.1.< / strong >  定义枚举< / a > < / li > < li > < a  href = "ch06-02-match.html"  class = "active" > < 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 > < li > < a  href = "ch07-00-modules.html" > < strong > 7.< / strong >  模块< / a > < / li > < li > < ul  class = "section" > < li > < a  href = "ch07-01-mod-and-the-filesystem.html" > < strong > 7.1.< / strong >  < code > mod< / code > 和文件系统< / a > < / li > < li > < a  href = "ch07-02-controlling-visibility-with-pub.html" > < strong > 7.2.< / strong >  使用< code > pub< / code > 控制可见性< / a > < / li > < li > < a  href = "ch07-03-importing-names-with-use.html" > < strong > 7.3.< / strong >  使用< code > use< / code > 导入命名< / a > < / li > < / ul > < / li > < li > < a  href = "ch08-00-common-collections.html" > < strong > 8.< / strong >  通用集合类型< / a > < / li > < li > < ul  class = "section" > < li > < a  href = "ch08-01-vectors.html" > < strong > 8.1.< / strong >  vector< / a > < / li > < li > < a  href = "ch08-02-strings.html" > < strong > 8.2.< / strong >  字符串< / a > < / li > < li > < a  href = "ch08-03-hash-maps.html" > < strong > 8.3.< / strong >  哈希 map< / a > < / li > < / ul > < / li > < li > < a  href = "ch09-00-error-handling.html" > < strong > 9.< / strong >  错误处理< / a > < / li > < li > < ul  class = "section" > < li > < a  href = "ch09-01-unrecoverable-errors-with-panic.html" > < strong > 9.1.< / strong >  < code > panic!< / code > 与不可恢复的错误< / a > < / li > < li > < a  href = "ch09-02-recoverable-errors-with-result.html" > < strong > 9.2.< / strong >  < code > Result< / code > 与可恢复的错误< / a > < / li > < li > < a  href = "ch09-03-to-panic-or-not-to-panic.html" > < strong > 9.3.< / strong >  < code > panic!< / code > 还是不< code > panic!< / code > < / a > < / li > < / ul > < / li > < li > < a  href = "ch10-00-generics.html" > < strong > 10.< / strong >  泛型、trait 和生命周期< / a > < / li > < li > < ul  class = "section" > < li > < a  href = "ch10-01-syntax.html" > < strong > 10.1.< / strong >  泛型数据类型< / a > < / li > < li > < a  href = "ch10-02-traits.html" > < strong > 10.2.< / strong >  trait:  定义共享的行为< / a > < / li > < li > < a  href = "ch10-03-lifetime-syntax.html" > < strong > 10.3.< / strong >  生命周期与引用有效性< / a > < / li > < / ul > < / li > < li > < a  href = "ch11-00-testing.html" > < strong > 11.< / strong >  测试< / a > < / li > < li > < ul  class = "section" > < li > < a  href = "ch11-01-writing-tests.html" > < strong > 11.1.< / strong >  编写测试< / a > < / li > < li > < a  href = "ch11-02-running-tests.html" > < strong > 11.2.< / strong >  运行测试< / a > < / li > < li > < a  href = "ch11-03-test-organization.html" > < strong > 11.3.< / strong >  测试的组织结构< / 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" > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								                    < a  class = "header"  href = "#match控制流运算符"  name = "match控制流运算符" > < h2 > < code > match< / code > 控制流运算符< / h2 > < / a > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								< blockquote > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > < a  href = "https://github.com/rust-lang/book/blob/master/src/ch06-02-match.md" > ch06-02-match.md< / a > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< br > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								commit 396e2db4f7de2e5e7869b1f8bc905c45c631ad7d< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / blockquote > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > Rust 有一个叫做< code > match< / code > 的极为强大的控制流运算符,它允许我们将一个值与一系列的模式相比较并根据匹配的模式执行代码。模式可由字面值、变量、通配符和许多其他内容构成;第十八章会讲到所有不同种类的模式以及他们的作用。< code > match< / code > 的力量来源于模式的表现力以及编译器检查,它确保了所有可能的情况都得到处理。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 把< code > match< / code > 表达式想象成某种硬币分啦机:硬币滑入有着不同大小孔洞的轨道,每一个硬币都会掉入符合它大小的孔洞。同样地,值也会检查< code > match< / code > 的每一个模式,并且在遇到第一个“符合”的模式时,值会进入相关联的代码块并在执行中被使用。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 因为刚刚提到了硬币,让我们用他们来作为一个使用< code > match< / code > 的例子!我们可以编写一个函数来获取一个未知的(美国)硬币,并以一种类似验钞机的方式,确定它是何种硬币并返回它的美分值,如列表 6-3 中所示:< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< figure > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< pre > < code  class = "language-rust" > enum Coin {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    Penny,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    Nickel,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    Dime,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    Quarter,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								fn value_in_cents(coin: Coin) ->  i32 {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    match coin {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        Coin::Penny =>  1,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        Coin::Nickel =>  5,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        Coin::Dime =>  10,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        Coin::Quarter =>  25,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / code > < / pre > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< figcaption > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > Listing 6-3: An enum and a < code > match< / code >  expression that has the variants of the enum
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								as its patterns.< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / figcaption > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / figure > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 拆开< code > value_in_cents< / code > 函数中的< code > match< / code > 来看。首先,我们列出< code > match< / code > 关键字后跟一个表达式,在这个例子中是< code > coin< / code > 的值。这看起来非常像< code > if< / code > 使用的表达式,不过这里有一个非常大的区别:对于< code > if< / code > ,表达式必须返回一个布尔值。而这里它可以是任何类型的。例子中的< code > coin< / code > 的类型是列表 6-3 中定义的< code > Coin< / code > 枚举。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 接下来是< code > match< / code > 的分支。一个分支有两个部分:一个模式和一些代码。第一个分支的模式是值< code > Coin::Penny< / code > 而之后的< code > => < / code > 运算符将模式和将要运行的代码分开。这里的代码就仅仅是值< code > 1< / code > 。每一个分支之间使用逗号分隔。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 每个分支相关联的代码是一个表达式,而表达式的结果值将作为整个< code > match< / code > 表达式的返回值。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
										 
									
								 
							
							
								< p > 如果分支代码较短的话可以不适用大括号,正如列表 6-3 中的每个分支都只是返回一个值。如果想要在分支中运行多行代码,可以使用大括号。例如,如下代码在每次使用< code > Coin::Penny< / code > 调用时都会打印出“Lucky penny!”,同时仍然返回代码块最后的值,< code > 1< / code > : < / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< pre > < code  class = "language-rust" > # enum Coin {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								#    Penny,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								#    Nickel,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								#    Dime,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								#    Quarter,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								# }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								#
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								fn value_in_cents(coin: Coin) ->  i32 {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    match coin {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        Coin::Penny =>  {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								            println!(" Lucky penny!" );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								            1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        Coin::Nickel =>  5,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        Coin::Dime =>  10,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        Coin::Quarter =>  25,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / code > < / pre > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								< a  class = "header"  href = "#绑定值的模式"  name = "绑定值的模式" > < h3 > 绑定值的模式< / h3 > < / a > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 匹配分支的另一个有用的功能是可以绑定匹配的模式的部分值。这也就是如何从枚举成员中提取值。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
										 
									
								 
							
							
								< p > 作为一个例子,  让我们修改枚举的一个成员来存放数据。1999 年到 2008 年间,美帝在 25 美分的硬币的一侧为 50 个州每一个都印刷了不同的设计。其他的硬币都没有这种区分州的设计,所以只有这些 25 美分硬币有特殊的价值。可以将这些信息加入我们的< code > enum< / code > ,通过改变< code > Quarter< / code > 成员来包含一个< code > State< / code > 值,列表 6-4 中完成了这些修改:< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< figure > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< pre > < code  class = "language-rust" > #[derive(Debug)] // So we can inspect the state in a minute
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								enum UsState {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    Alabama,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    Alaska,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    // ... etc
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								enum Coin {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    Penny,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    Nickel,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    Dime,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    Quarter(UsState),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / code > < / pre > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< figcaption > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > Listing 6-4: A < code > Coin< / code >  enum where the < code > Quarter< / code >  variant also holds a < code > UsState< / code > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								value< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / figcaption > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / figure > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 想象一下我们的一个朋友尝试收集所有 50 个州的 25 美分硬币。在根据硬币类型分类零钱的同时,也可以报告出每个 25 美分硬币所对应的州名称,这样如何我们的朋友没有的话,他可以把它加入收藏。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 在这些代码的匹配表达式中,我们在匹配< code > Coin::Quarter< / code > 成员的分支的模式中增加了一个叫做< code > state< / code > 的变量。当匹配到< code > Coin::Quarter< / code > 时,变量< code > state< / code > 将会绑定 25 美分硬币所对应州的值。接着在代码那个分支中使用< code > state< / code > ,如下:< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< pre > < code  class = "language-rust" > # #[derive(Debug)]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								# enum UsState {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								#    Alabama,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								#    Alaska,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								# }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								#
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								# enum Coin {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								#    Penny,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								#    Nickel,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								#    Dime,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								#    Quarter(UsState),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								# }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								#
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								fn value_in_cents(coin: Coin) ->  i32 {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    match coin {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        Coin::Penny =>  1,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        Coin::Nickel =>  5,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        Coin::Dime =>  10,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        Coin::Quarter(state) =>  {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								            println!(" State quarter from {:?}!" , state);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								            25
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / code > < / pre > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
										 
									
								 
							
							
								< p > 如果调用< code > value_in_cents(Coin::Quarter(UsState::Alaska))< / code > , < code > coin< / code > 将是< code > Coin::Quarter(UsState::Alaska)< / code > 。当将值与每个分支相比较时,没有分支会匹配知道遇到< code > Coin::Quarter(state)< / code > 。这时,< code > state< / code > 绑定的将会是值< code > UsState::Alaska< / code > 。接着就可以在< code > println!< / code > 表达式中使用这个绑定了,像这样就可以获取< code > Coin< / code > 枚举的< code > Quarter< / code > 成员中内部的州的值。< / p > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								< a  class = "header"  href = "#匹配optiont"  name = "匹配optiont" > < h3 > 匹配< code > Option< T> < / code > < / h3 > < / a > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 在之前的部分在使用< code > Option< T> < / code > 时我们想要从< code > Some< / code > 中取出其内部的< code > T< / code > 值;也可以像处理< code > Coin< / code > 枚举那样使用< code > match< / code > 处理< code > Option< T> < / code > !与其直接比较硬币,我们将比较< code > Option< T> < / code > 的成员,不过< code > match< / code > 表达式的工作方式保持不变。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 比如想要编写一个函数,它获取一个< code > Option< i32> < / code > 并且如果其中有一个值,将其加一。如果其中没有值,函数应该返回< code > None< / code > 值并不尝试执行任何操作。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 编写这个函数非常简单,得益于< code > match< / code > ,它将看起来像列表 6-5 中这样:< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< figure > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< pre > < code  class = "language-rust" > fn plus_one(x: Option< i32> ) ->  Option< i32>  {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    match x {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        None =>  None,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        Some(i) =>  Some(i + 1),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								let five = Some(5);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								let six = plus_one(five);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								let none = plus_one(None);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / code > < / pre > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< figcaption > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > Listing 6-5: A function that uses a < code > match< / code >  expression on an < code > Option< i32> < / code > < / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / figcaption > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / figure > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								< a  class = "header"  href = "#匹配somet"  name = "匹配somet" > < h4 > 匹配< code > Some(T)< / code > < / h4 > < / a > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 更仔细的检查< code > plus_one< / code > 的第一行操作。当调用< code > plus_one(five)< / code > 时,< code > plus_one< / code > 函数体中的< code > x< / code > 将会是值< code > Some(5)< / code > 。接着将其与每个分支比较。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< pre > < code  class = "language-rust,ignore" > None =>  None,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / code > < / pre > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 值< code > Some(5)< / code > 并不匹配模式< code > None< / code > ,所以继续进行下一个分支。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< pre > < code  class = "language-rust,ignore" > Some(i) =>  Some(i + 1),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / code > < / pre > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > < code > Some(5)< / code > 与< code > Some(i)< / code > 匹配吗?为什么不呢!他们是相同的成员。< code > i< / code > 绑定了< code > Some< / code > 中包含的值,所以< code > i< / code > 的值是< code > 5< / code > 。接着匹配分支的代码被执行,所以我们将< code > i< / code > 的值加一并返回一个含有值< code > 6< / code > 的新< code > Some< / code > 。< / p > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								< a  class = "header"  href = "#匹配none"  name = "匹配none" > < h4 > 匹配< code > None< / code > < / h4 > < / a > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 接着考虑下列表 6-5 中< code > plus_one< / code > 的第二个调用,这里< code > x< / code > 是< code > None< / code > 。我们进入< code > match< / code > 并与第一个分支相比较。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< pre > < code  class = "language-rust,ignore" > None =>  None,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / code > < / pre > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 匹配上了!这里没有值来加一,所以程序结束并返回< code > => < / code > 右侧的值< code > None< / code > ,因为第一个分支就匹配到了,其他的分支将不再比较。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 将< code > match< / code > 与枚举相结合在很多场景中都是有用的。你会在 Rust 代码中看到很多这样的模式:< code > match< / code > 一个枚举,绑定其中的值到一个变量,接着根据其值执行代码。这在一开有点复杂,不过一旦习惯了,你将希望所有语言都拥有它!这一直是用户的最爱。< / p > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								< a  class = "header"  href = "#匹配是穷尽的"  name = "匹配是穷尽的" > < h3 > 匹配是穷尽的< / h3 > < / a > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > < code > match< / code > 还有另一方面需要讨论。考虑一下< code > plus_one< / code > 函数的这个版本:< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< pre > < code  class = "language-rust,ignore" > fn plus_one(x: Option< i32> ) ->  Option< i32>  {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    match x {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        Some(i) =>  Some(i + 1),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / code > < / pre > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
										 
									
								 
							
							
								< p > 我们没有处理< code > None< / code > 的情况,所以这些代码会造成一个 bug。幸运的是,  这是一个 Rust 知道如何处理的 bug。如果尝试编译这段代码,  会得到这个错误: < / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< pre > < code > error[E0004]: non-exhaustive patterns: `None` not covered
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								 --> 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								  |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								6 |         match x {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								  |               ^ pattern `None` not covered
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / code > < / pre > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
										 
									
								 
							
							
								< p > Rust 知道我们没有覆盖所有可能的情况甚至知道那些模式被忘记了!  Rust 中的匹配是< strong > 穷尽的< / strong > (  *exhaustive) :  必须穷举到最后的可能性来使代码有效。特别的在这个< code > Option< T> < / code > 的例子中,  Rust 防止我们忘记明确的处理< code > None< / code > 的情况,这使我们免于假设拥有一个实际上为空的值,这造成了之前提到过的价值亿万的错误。< / p > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								< a  class = "header"  href = "#_通配符"  name = "_通配符" > < h3 > < code > _< / code > 通配符< / h3 > < / a > 
							 
						 
					
						
							
								
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > Rust 也提供了一个模式用于不想列举出所有可能值的场景。例如,< code > u8< / code > 可以拥有 0 到 255 的有效的值,如果我们只关心 1、3、5 和 7 这几个值,就并不想必须列出 0、2、4、6、8、9 一直到 255 的值。所幸我们不必这么做:可以使用特殊的模式< code > _< / code > 替代:< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< pre > < code  class = "language-rust" > let some_u8_value = 0u8;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								match some_u8_value {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    1 =>  println!(" one" ),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    3 =>  println!(" three" ),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    5 =>  println!(" five" ),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    7 =>  println!(" seven" ),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    _ =>  (),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / code > < / pre > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > < code > _< / code > 模式会匹配所有的值。通过将其放置于其他分支之后,< code > _< / code > 将会匹配所有之前没有指定的可能的值。< code > ()< / code > 就是 unit 值,所以< code > _< / code > 的情况什么也不会发生。因此,可以说我们想要对< code > _< / code > 通配符之前没有列出的所有可能的值不做任何处理。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< p > 然而,< code > match< / code > 在只关心< strong > 一个< / strong > 情况的场景中可能就有点啰嗦了。为此 Rust 提供了< code > if let< / code > 。< / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								                < / div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								                <!--  Mobile navigation buttons  --> 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								                
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								                    < a  href = "ch06-01-defining-an-enum.html"  class = "mobile-nav-chapters previous" > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								                        < i  class = "fa fa-angle-left" > < / i > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								                    < / a > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								                
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								                
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								                    < a  href = "ch06-03-if-let.html"  class = "mobile-nav-chapters next" > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								                        < i  class = "fa fa-angle-right" > < / i > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								                    < / a > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								                
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								            < / div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								            
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								                < a  href = "ch06-01-defining-an-enum.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-03-if-let.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  src = "highlight.js" > < / script > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								        < script  src = "book.js" > < / script > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								    < / body > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								< / html >