|
|
<!DOCTYPE HTML>
|
|
|
<html lang="zh-CN" class="light" dir="ltr">
|
|
|
<head>
|
|
|
<!-- Book generated using mdBook -->
|
|
|
<meta charset="UTF-8">
|
|
|
<title>全模式列表 - Rust语言圣经(Rust Course)</title>
|
|
|
|
|
|
|
|
|
<!-- Custom HTML head -->
|
|
|
|
|
|
<meta name="description" content="">
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
<meta name="theme-color" content="#ffffff">
|
|
|
|
|
|
<link rel="icon" href="../../favicon.svg">
|
|
|
<link rel="shortcut icon" href="../../favicon.png">
|
|
|
<link rel="stylesheet" href="../../css/variables.css">
|
|
|
<link rel="stylesheet" href="../../css/general.css">
|
|
|
<link rel="stylesheet" href="../../css/chrome.css">
|
|
|
<link rel="stylesheet" href="../../css/print.css" media="print">
|
|
|
|
|
|
<!-- Fonts -->
|
|
|
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
|
|
|
<link rel="stylesheet" href="../../fonts/fonts.css">
|
|
|
|
|
|
<!-- Highlight.js Stylesheets -->
|
|
|
<link rel="stylesheet" href="../../highlight.css">
|
|
|
<link rel="stylesheet" href="../../tomorrow-night.css">
|
|
|
<link rel="stylesheet" href="../../ayu-highlight.css">
|
|
|
|
|
|
<!-- Custom theme stylesheets -->
|
|
|
<link rel="stylesheet" href="../../theme/style.css">
|
|
|
|
|
|
</head>
|
|
|
<body class="sidebar-visible no-js">
|
|
|
<div id="body-container">
|
|
|
<!-- Provide site root to javascript -->
|
|
|
<script>
|
|
|
var path_to_root = "../../";
|
|
|
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
|
</script>
|
|
|
|
|
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
|
<script>
|
|
|
try {
|
|
|
var theme = localStorage.getItem('mdbook-theme');
|
|
|
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
|
|
|
|
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
|
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
|
}
|
|
|
|
|
|
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
|
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
|
}
|
|
|
} catch (e) { }
|
|
|
</script>
|
|
|
|
|
|
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
|
<script>
|
|
|
var theme;
|
|
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
|
var html = document.querySelector('html');
|
|
|
html.classList.remove('light')
|
|
|
html.classList.add(theme);
|
|
|
var body = document.querySelector('body');
|
|
|
body.classList.remove('no-js')
|
|
|
body.classList.add('js');
|
|
|
</script>
|
|
|
|
|
|
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
|
|
|
|
|
|
<!-- Hide / unhide sidebar before it is displayed -->
|
|
|
<script>
|
|
|
var body = document.querySelector('body');
|
|
|
var sidebar = null;
|
|
|
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
|
|
|
if (document.body.clientWidth >= 1080) {
|
|
|
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
|
sidebar = sidebar || 'visible';
|
|
|
} else {
|
|
|
sidebar = 'hidden';
|
|
|
}
|
|
|
sidebar_toggle.checked = sidebar === 'visible';
|
|
|
body.classList.remove('sidebar-visible');
|
|
|
body.classList.add("sidebar-" + sidebar);
|
|
|
</script>
|
|
|
|
|
|
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
|
<div class="sidebar-scrollbox">
|
|
|
<ol class="chapter"><li class="chapter-item affix "><a href="../../about-book.html">关于本书</a></li><li class="chapter-item affix "><a href="../../into-rust.html">进入 Rust 编程世界</a></li><li class="chapter-item affix "><a href="../../first-try/sth-you-should-not-do.html">避免从入门到放弃</a></li><li class="chapter-item affix "><a href="../../community.html">社区和锈书</a></li><li class="spacer"></li><li class="chapter-item affix "><a href="../../some-thoughts.html">Xobserve: 一切皆可观测</a></li><li class="chapter-item affix "><a href="../../beat-ai.html">BeatAI: 工程师 AI 入门圣经</a></li><li class="chapter-item affix "><li class="part-title">Rust 语言基础学习</li><li class="spacer"></li><li class="chapter-item "><a href="../../first-try/intro.html"><strong aria-hidden="true">1.</strong> 寻找牛刀,以便小试</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../first-try/installation.html"><strong aria-hidden="true">1.1.</strong> 安装 Rust 环境</a></li><li class="chapter-item "><a href="../../first-try/editor.html"><strong aria-hidden="true">1.2.</strong> 墙推 VSCode!</a></li><li class="chapter-item "><a href="../../first-try/cargo.html"><strong aria-hidden="true">1.3.</strong> 认识 Cargo</a></li><li class="chapter-item "><a href="../../first-try/hello-world.html"><strong aria-hidden="true">1.4.</strong> 不仅仅是 Hello world</a></li><li class="chapter-item "><a href="../../first-try/slowly-downloading.html"><strong aria-hidden="true">1.5.</strong> 下载依赖太慢了?</a></li></ol></li><li class="chapter-item expanded "><a href="../../basic/intro.html"><strong aria-hidden="true">2.</strong> Rust 基础入门</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/variable.html"><strong aria-hidden="true">2.1.</strong> 变量绑定与解构</a></li><li class="chapter-item "><a href="../../basic/base-type/index.html"><strong aria-hidden="true">2.2.</strong> 基本类型</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/base-type/numbers.html"><strong aria-hidden="true">2.2.1.</strong> 数值类型</a></li><li class="chapter-item "><a href="../../basic/base-type/char-bool.html"><strong aria-hidden="true">2.2.2.</strong> 字符、布尔、单元类型</a></li><li class="chapter-item "><a href="../../basic/base-type/statement-expression.html"><strong aria-hidden="true">2.2.3.</strong> 语句与表达式</a></li><li class="chapter-item "><a href="../../basic/base-type/function.html"><strong aria-hidden="true">2.2.4.</strong> 函数</a></li></ol></li><li class="chapter-item "><a href="../../basic/ownership/index.html"><strong aria-hidden="true">2.3.</strong> 所有权和借用</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/ownership/ownership.html"><strong aria-hidden="true">2.3.1.</strong> 所有权</a></li><li class="chapter-item "><a href="../../basic/ownership/borrowing.html"><strong aria-hidden="true">2.3.2.</strong> 引用与借用</a></li></ol></li><li class="chapter-item "><a href="../../basic/compound-type/intro.html"><strong aria-hidden="true">2.4.</strong> 复合类型</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/compound-type/string-slice.html"><strong aria-hidden="true">2.4.1.</strong> 字符串与切片</a></li><li class="chapter-item "><a href="../../basic/compound-type/tuple.html"><strong aria-hidden="true">2.4.2.</strong> 元组</a></li><li class="chapter-item "><a href="../../basic/compound-type/struct.html"><strong aria-hidden="true">2.4.3.</strong> 结构体</a></li><li class="chapter-item "><a href="../../basic/compound-type/enum.html"><strong aria-hidden="true">2.4.4.</strong> 枚举</a></li><li class="chapter-item "><a href="../../basic/compound-type/array.html"><strong aria-hidden="true">2.4.5.</strong> 数组</a></li></ol></li><li class="chapter-item "><a href="../../basic/flow-control.html"><strong aria-hidden="true">2.5.</strong> 流程控制</a></li><li class="chapter-item expanded "><a href="../../basic/match-pattern/intro.html"><strong aria-hidden="true">2.6.</strong> 模式匹配</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/match-pattern/match-if-let.html"><strong aria-hidden="true">2.6.1.</strong> match 和 if let</a></li><li class="chapter-item "><a href="../../basic/match-pattern/option.html"><strong aria-hidden="true">2.6.2.</strong> 解构 Option</a></li><li class="chapter-item "><a href="../../basic/match-pattern/pattern-match.html"><strong aria-hidden="true">2.6.3.</strong> 模式适用场景</a></li><li class="chapter-item expanded "><a href="../../basic/match-pattern/all-patterns.html" class="active"><strong aria-hidden="true">2.6.4.</strong> 全模式列表</a></li></ol></li><li class="chapter-item "><a href="../../basic/method.html"><strong aria-hidden="true">2.7.</strong> 方法 Method</a></li><li class="chapter-item "><a href="../../basic/trait/intro.html"><strong aria-hidden="true">2.8.</strong> 泛型和特征</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/trait/generic.html"><strong aria-hidden="true">2.8.1.</strong> 泛型 Generics</a></li><li class="chapter-item "><a href="../../basic/trait/trait.html"><strong aria-hidden="true">2.8.2.</strong> 特征 Trait</a></li><li class="chapter-item "><a href="../../basic/trait/trait-object.html"><strong aria-hidden="true">2.8.3.</strong> 特征对象</a></li><li class="chapter-item "><a href="../../basic/trait/advance-trait.html"><strong aria-hidden="true">2.8.4.</strong> 进一步深入特征</a></li></ol></li><li class="chapter-item "><a href="../../basic/collections/intro.html"><strong aria-hidden="true">2.9.</strong> 集合类型</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/collections/vector.html"><strong aria-hidden="true">2.9.1.</strong> 动态数组 Vector</a></li><li class="chapter-item "><a href="../../basic/collections/hashmap.html"><strong aria-hidden="true">2.9.2.</strong> KV 存储 HashMap</a></li></ol></li><li class="chapter-item "><a href="../../basic/lifetime.html"><strong aria-hidden="true">2.10.</strong> 认识生命周期</a></li><li class="chapter-item "><a href="../../basic/result-error/intro.html"><strong aria-hidden="true">2.11.</strong> 返回值和错误处理</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/result-error/panic.html"><strong aria-hidden="true">2.11.1.</strong> panic! 深入剖析</a></li><li class="chapter-item "><a href="../../basic/result-error/result.html"><strong aria-hidden="true">2.11.2.</strong> 返回值 Result 和?</a></li></ol></li><li class="chapter-item "><a href="../../basic/crate-module/intro.html"><strong aria-hidden="true">2.12.</strong> 包和模块</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/crate-module/crate.html"><strong aria-hidden="true">2.12.1.</strong> 包 Crate</a></li><li class="chapter-item "><a href="../../basic/crate-module/module.html"><strong aria-hidden="true">2.12.2.</strong> 模块 Module</a></li><li class="chapter-item "><a href="../../basic/crate-module/use.html"><strong aria-hidden="true">2.12.3.</strong> 使用 use 引入模块及受限可见性</a></li></ol></li><li class="chapter-item "><a href="../../basic/comment.html"><strong aria-hidden="true">2.13.</strong> 注释和文档</a></li><li class="chapter-item "><a href="../../basic/formatted-output.html"><strong aria-hidden="true">2.14.</strong> 格式化输出</a></li></ol></li><li class="chapter-item "><a href="../../basic-practice/intro.html"><strong aria-hidden="true">3.</strong> 入门实战:文件搜索工具</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic-practice/base-features.html"><strong aria-hidden="true">3.1.</strong> 基本功能</a></li><li class="chapter-item "><a href="../../basic-practice/refactoring.html"><strong aria-hidden="true">3.2.</strong> 增加模块化和错误处理</a></li><li class="chapter-item "><a href="../../basic-practice/tests.html"><strong aria-hidden="true">3.3.</strong> 测试驱动开发</a></li><li class="chapter-item "><a href="../../basic-practice/envs.html"><strong aria-hidden="true">3.4.</strong> 使用环境变量</a></li><li class="chapter-item "><a href="../../basic-practice/stderr.html"><strong aria-hidden="true">3.5.</strong> 重定向错误信息的输出</a></li><li class="chapter-item "><a href="../../basic-practice/iterators.html"><strong aria-hidden="true">3.6.</strong> 使用迭代器来改进程序(可选)</a></li></ol></li><li class="chapter-item "><li class="part-title">Rust 语言进阶学习</li><li class="spacer"></li><li class="chapter-item "><a href="../../advance/intro.html"><strong aria-hidden="true">4.</strong> Rust 高级进阶</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../advance/lifetime/intro.html"><strong aria-hidden="true">4.1.</strong> 生命周期</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../advance/lifetime/advance.html"><strong aria-hidden="true">4.1.1.</strong> 深入生命周期</a></li><li class="chapter-item "><a href="../../advance/lifetime/static.html"><strong aria-hidden="true">4.1.2.</strong> &'static 和 T: 'static</a></li></ol></li><li class="chapter-item "><a href="../../advance/functional-programing/intro.html"><strong aria-hidden="true">4.2.</strong> 函数式编程: 闭包、迭代器</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../advance/functional-programing/closure.html"><strong aria-hidden="true">4.2.1.</strong> 闭包 Closure</a></li><li class="chapter-item "><a href="../../advance/functional-programing/iterator.html"><strong aria-hidden="true">4.2.2.</strong> 迭代器 Iterator</a></li></ol></li><li class="chapter-item "><a href="../../advance/into-types/intro.html"><strong aria-hidden="true">4.3.</strong> 深入类型</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../advance/into-types/converse.html"><strong aria-hidden="true">4.3.1.</strong> 类型转换</a></li><li class="chapter-item "><a href="../../advance/into-types/custom-type.html"><strong aria-hidden="true">4.3.2.</strong> newtype 和 类型别名</a></li><li class="chapter-item "><a href="../../advance/into-types/sized.html"><strong aria-hidden="true">4.3.3.</strong> Sized 和不定长类型 DST</a></li><li class="chapter-item "><a href="../../advance/into-types/enum-int.html"><strong aria-hidden="true">4.3.4.</strong> 枚举和整数</a></li></ol></li><li class="chapter-item "><a href="../../advance/smart-pointer/intro.html"><strong aria-hidden="true">4.4.</strong> 智能指针</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../advance/smart-pointer/box.html"><strong aria-hidden="true">4.4.1.</strong> Box堆对象分配</a></li><li class="chapter-item "><a href="../../advance/smart-pointer/deref.html"><strong aria-hidden="true">4.4.2.</strong> Deref 解引用</a></li><li class="chapter-item "><a href="../../advance/smart-pointer/drop.html"><strong aria-hidden="true">4.4.3.</strong> Drop 释放资源</a></li><li class="chapter-item "><a href="../../advance/smart-pointer/rc-arc.html"><strong aria-hidden="true">4.4.4.</strong> Rc 与 Arc 实现 1vN 所有权机制</a></li><li class="chapter-item "><a href="../../advance/smart-pointer/cell-refcell.html"><strong aria-hidden="true">4.4.5.</strong> Cell 与 RefCell 内部可变性</a></li></ol></li><li class="chapter-item "><a href="../../advance/circle-self-ref/intro.html"><strong aria-hidden="true">4.5.</strong> 循环引用与自引用</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../advance/circle-self-ref/circle-reference.html"><strong aria-hidden="true">4.5.1.</strong> Weak 与循环引用</a></li><li class="chapter-item "><a href="../../advance/circle-self-ref/self-referential.html"><strong aria-hidden="true">4.5.2.</strong> 结构体中的自引用</a></li></ol></li><li class="chapter-item "><a href="../../advance/concurrency-with-threads/intro.html"><strong aria-hidden="true">4.6.</strong> 多线程并发编程</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../advance/concurrency-with-threads/concurrency-parallelism.html"><strong aria-hidden="true">4.6.1.</strong> 并发和并行</a></li><li class="chapter-item "><a href="../../advance/concurrency-with-threads/thread.html"><strong aria-hidden="true">4.6.2.</strong> 使用多线程</a></li><li class="chapter-item "><a href="../../advance/concurrency-with-threads/message-passing.html"><strong aria-hidden="true">4.6.3.</strong> 线程同步:消息传递</a></li><li class="chapter-item "><a href="../../advance/concurrency-with-threads/sync1.html"><strong aria-hidden="true">4.6.4.</strong> 线程同步:锁、Condvar 和信号量</a></li><li class="chapter-item "><a href="../../advance/concurrency-with-threads/sync2.html"><strong aria-hidden="true">4.6.5.</strong> 线程同步:Atomic 原子操作与内存顺序</a></li><li class="chapter-item "><a href="../../advance/concurrency-with-threads/send-sync.html"><strong aria-hidden="true">4.6.6.</strong> 基于 Send 和 Sync 的线程安全</a></li></ol></li><li class="chapter-item "><a href="../../advance/global-variable.html"><strong aria-hidden="true">4.7.</strong> 全局变量</a></li><li class="chapter-item "><a href="../../advance/errors.html"><strong aria-hidden="true">4.8.</strong> 错误处理</a></li><li class="chapter-item "><a href="../../advance/unsafe/intro.html"><strong aria-hidden="true">4.9.</strong> Unsafe Rust</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../advance/unsafe/superpowers.html"><strong aria-hidden="true">4.9.1.</strong> 五种兵器</a></li><li class="chapter-item "><a href="../../advance/unsafe/inline-asm.html"><strong aria-hidden="true">4.9.2.</strong> 内联汇编</a></li></ol></li><li class="chapter-item "><a href="../../advance/macro.html"><strong aria-hidden="true">4.10.</strong> Macro 宏编程</a></li><li class="chapter-item "><a href="../../advance/async/intro.html"><strong aria-hidden="true">4.11.</strong> async/await 异步编程</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../advance/async/getting-started.html"><strong aria-hidden="true">4.11.1.</strong> async 编程入门</a></li><li class="chapter-item "><a href="../../advance/async/future-excuting.html"><strong aria-hidden="true">4.11.2.</strong> 底层探秘: Future 执行与任务调度</a></li><li class="chapter-item "><a href="../../advance/async/pin-unpin.html"><strong aria-hidden="true">4.11.3.</strong> 定海神针 Pin 和 Unpin</a></li><li class="chapter-item "><a href="../../advance/async/async-await.html"><strong aria-hidden="true">4.11.4.</strong> async/await 和 Stream 流处理</a></li><li class="chapter-item "><a href="../../advance/async/multi-futures-simultaneous.html"><strong aria-hidden="true">4.11.5.</strong> 同时运行多个 Future</a></li><li class="chapter-item "><a href="../../advance/async/pain-points-and-workarounds.html"><strong aria-hidden="true">4.11.6.</strong> 一些疑难问题的解决办法</a></li><li class="chapter-item "><a href="../../advance/async/web-server.html"><strong aria-hidden="true">4.11.7.</strong> 实践应用:Async Web 服务器</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../advance-practice1/intro.html"><strong aria-hidden="true">5.</strong> 进阶实战1: 实现一个 web 服务器</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../advance-practice1/web-server.html"><strong aria-hidden="true">5.1.</strong> 单线程版本</a></li><li class="chapter-item "><a href="../../advance-practice1/multi-threads.html"><strong aria-hidden="true">5.2.</strong> 多线程版本</a></li><li class="chapter-item "><a href="../../advance-practice1/graceful-shutdown.html"><strong aria-hidden="true">5.3.</strong> 优雅关闭和资源清理</a></li></ol></li><li class="chapter-item "><a href="../../advance-practice/intro.html"><strong aria-hidden="true">6.</strong> 进阶实战2: 实现一个简单 Redis</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../advance-practice/overview.html"><strong aria-hidden="true">6.1.</strong> tokio 概览</a></li><li class="chapter-item "><a href="../../advance-practice/getting-startted.html"><strong aria-hidden="true">6.2.</strong> 使用初印象</a></li><li class="chapter-item "><a href="../../advance-practice/spawning.html"><strong aria-hidden="true">6.3.</strong> 创建异步任务</a></li><li class="chapter-item "><a href="../../advance-practice/shared-state.html"><strong aria-hidden="true">6.4.</strong> 共享状态</a></li><li class="chapter-item "><a href="../../advance-practice/channels.html"><strong aria-hidden="true">6.5.</strong> 消息传递</a></li><li class="chapter-item "><a href="../../advance-practice/io.html"><strong aria-hidden="true">6.6.</strong> I/O</a></li><li class="chapter-item "><a href="../../advance-practice/frame.html"><strong aria-hidden="true">6.7.</strong> 解析数据帧</a></li><li class="chapter-item "><a href="../../advance-practice/async.html"><strong aria-hidden="true">6.8.</strong> 深入 async</a></li><li class="chapter-item "><a href="../../advance-practice/select.html"><strong aria-hidden="true">6.9.</strong> select</a></li><li class="chapter-item "><a href="../../advance-practice/stream.html"><strong aria-hidden="true">6.10.</strong> 类似迭代器的 Stream</a></li><li class="chapter-item "><a href="../../advance-practice/graceful-shutdown.html"><strong aria-hidden="true">6.11.</strong> 优雅的关闭</a></li><li class="chapter-item "><a href="../../advance-practice/bridging-with-sync.html"><strong aria-hidden="true">6.12.</strong> 异步跟同步共存</a></li></ol></li><li class="chapter-item "><a href="../../difficulties/intro.html"><strong aria-hidden="true">7.</strong> Rust 难点攻关</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../difficulties/slice.html"><strong aria-hidden="true">7.1.</strong> 切片和切片引用</a></li><li class="chapter-item "><a href="../../difficulties/eq.html"><strong aria-hidden="true">7.2.</strong> Eq 和 PartialEq</a></li><li class="chapter-item "><a href="../../difficulties/string.html"><strong aria-hidden="true">7.3.</strong> String、&str 和 str TODO</a></li><li class="chapter-item "><a href="../../difficulties/lifetime.html"><strong aria-hidden="true">7.4.</strong> 作用域、生命周期和 NLL TODO</a></li><li class="chapter-item "><a href="../../difficulties/move-copy.html"><strong aria-hidden="true">7.5.</strong> move、Copy 和 Clone TODO</a></li><li class="chapter-item "><a href="../../advance/difficulties/pointer.html"><strong aria-hidden="true">7.6.</strong> 裸指针、引用和智能指针 TODO</a></li></ol></li><li class="chapter-item "><li class="part-title">常用工具链</li><li class="spacer"></li><li class="chapter-item "><a href="../../test/intro.html"><strong aria-hidden="true">8.</strong> 自动化测试</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../test/write-tests.html"><strong aria-hidden="true">8.1.</strong> 编写测试及控制执行</a></li><li class="chapter-item "><a href="../../test/unit-integration-test.html"><strong aria-hidden="true">8.2.</strong> 单元测试和集成测试</a></li><li class="chapter-item "><a href="../../test/assertion.html"><strong aria-hidden="true">8.3.</strong> 断言 assertion</a></li><li class="chapter-item "><a href="../../test/ci.html"><strong aria-hidden="true">8.4.</strong> 用 GitHub Actions 进行持续集成</a></li><li class="chapter-item "><a href="../../test/benchmark.html"><strong aria-hidden="true">8.5.</strong> 基准测试 benchmark</a></li></ol></li><li class="chapter-item "><a href="../../cargo/intro.html"><strong aria-hidden="true">9.</strong> Cargo 使用指南</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../cargo/getting-started.html"><strong aria-hidden="true">9.1.</strong> 上手使用</a></li><li class="chapter-item "><a href="../../cargo/guide/intro.html"><strong aria-hidden="true">9.2.</strong> 基础指南</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../cargo/guide/why-exist.html"><strong aria-hidden="true">9.2.1.</strong> 为何会有 Cargo</a></li><li class="chapter-item "><a href="../../cargo/guide/download-package.html"><strong aria-hidden="true">9.2.2.</strong> 下载并构建 Package</a></li><li class="chapter-item "><a href="../../cargo/guide/dependencies.html"><strong aria-hidden="true">9.2.3.</strong> 添加依赖</a></li><li class="chapter-item "><a href="../../cargo/guide/package-layout.html"><strong aria-hidden="true">9.2.4.</strong> Package 目录结构</a></li><li class="chapter-item "><a href="../../cargo/guide/cargo-toml-lock.html"><strong aria-hidden="true">9.2.5.</strong> Cargo.toml vs Cargo.lock</a></li><li class="chapter-item "><a href="../../cargo/guide/tests-ci.html"><strong aria-hidden="true">9.2.6.</strong> 测试和 CI</a></li><li class="chapter-item "><a href="../../cargo/guide/cargo-cache.html"><strong aria-hidden="true">9.2.7.</strong> Cargo 缓存</a></li><li class="chapter-item "><a href="../../cargo/guide/build-cache.html"><strong aria-hidden="true">9.2.8.</strong> Build 缓存</a></li></ol></li><li class="chapter-item "><a href="../../cargo/reference/intro.html"><strong aria-hidden="true">9.3.</strong> 进阶指南</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../cargo/reference/specify-deps.html"><strong aria-hidden="true">9.3.1.</strong> 指定依赖项</a></li><li class="chapter-item "><a href="../../cargo/reference/deps-overriding.html"><strong aria-hidden="true">9.3.2.</strong> 依赖覆盖</a></li><li class="chapter-item "><a href="../../cargo/reference/manifest.html"><strong aria-hidden="true">9.3.3.</strong> Cargo.toml 清单详解</a></li><li class="chapter-item "><a href="../../cargo/reference/cargo-target.html"><strong aria-hidden="true">9.3.4.</strong> Cargo Target</a></li><li class="chapter-item "><a href="../../cargo/reference/workspaces.html"><strong aria-hidden="true">9.3.5.</strong> 工作空间 Workspace</a></li><li class="chapter-item "><a href="../../cargo/reference/features/intro.html"><strong aria-hidden="true">9.3.6.</strong> 条件编译 Features</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../cargo/reference/features/examples.html"><strong aria-hidden="true">9.3.6.1.</strong> Features 示例</a></li></ol></li><li class="chapter-item "><a href="../../cargo/reference/profiles.html"><strong aria-hidden="true">9.3.7.</strong> 发布配置 Profile</a></li><li class="chapter-item "><a href="../../cargo/reference/configuration.html"><strong aria-hidden="true">9.3.8.</strong> 通过 config.toml 对 Cargo 进行配置</a></li><li class="chapter-item "><a href="../../cargo/reference/publishing-on-crates.io.html"><strong aria-hidden="true">9.3.9.</strong> 发布到 crates.io</a></li><li class="chapter-item "><a href="../../cargo/reference/build-script/intro.html"><strong aria-hidden="true">9.3.10.</strong> 构建脚本 build.rs</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../cargo/reference/build-script/examples.html"><strong aria-hidden="true">9.3.10.1.</strong> 构建脚本示例</a></li></ol></li></ol></li></ol></li><li class="chapter-item "><li class="part-title">开发实践</li><li class="spacer"></li><li class="chapter-item "><a href="../../usecases/intro.html"><strong aria-hidden="true">10.</strong> 企业落地实践</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../usecases/aws-rust.html"><strong aria-hidden="true">10.1.</strong> AWS 为何这么喜欢 Rust?</a></li></ol></li><li class="chapter-item "><a href="../../logs/intro.html"><strong aria-hidden="true">11.</strong> 日志和监控</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../logs/about-log.html"><strong aria-hidden="true">11.1.</strong> 日志详解</a></li><li class="chapter-item "><a href="../../logs/log.html"><strong aria-hidden="true">11.2.</strong> 日志门面 log</a></li><li class="chapter-item "><a href="../../logs/tracing.html"><strong aria-hidden="true">11.3.</strong> 使用 tracing 记录日志</a></li><li class="chapter-item "><a href="../../logs/tracing-logger.html"><strong aria-hidden="true">11.4.</strong> 自定义 tracing 的输出格式</a></li><li class="chapter-item "><a href="../../logs/observe/intro.html"><strong aria-hidden="true">11.5.</strong> 监控</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../logs/observe/about-observe.html"><strong aria-hidden="true">11.5.1.</strong> 可观测性</a></li><li class="chapter-item "><a href="../../logs/observe/trace.html"><strong aria-hidden="true">11.5.2.</strong> 分布式追踪</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../practice/intro.html"><strong aria-hidden="true">12.</strong> Rust 最佳实践</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../practice/third-party-libs.html"><strong aria-hidden="true">12.1.</strong> 日常开发三方库精选</a></li><li class="chapter-item "><a href="../../practice/naming.html"><strong aria-hidden="true">12.2.</strong> 命名规范</a></li><li class="chapter-item "><a href="../../practice/interview.html"><strong aria-hidden="true">12.3.</strong> 面试经验</a></li><li class="chapter-item "><a href="../../practice/best-pratice.html"><strong aria-hidden="true">12.4.</strong> 代码开发实践 todo</a></li></ol></li><li class="chapter-item "><a href="../../too-many-lists/intro.html"><strong aria-hidden="true">13.</strong> 手把手带你实现链表</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../too-many-lists/do-we-need-it.html"><strong aria-hidden="true">13.1.</strong> 我们到底需不需要链表</a></li><li class="chapter-item "><a href="../../too-many-lists/bad-stack/intro.html"><strong aria-hidden="true">13.2.</strong> 不太优秀的单向链表:栈</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../too-many-lists/bad-stack/layout.html"><strong aria-hidden="true">13.2.1.</strong> 数据布局</a></li><li class="chapter-item "><a href="../../too-many-lists/bad-stack/basic-operations.html"><strong aria-hidden="true">13.2.2.</strong> 基本操作</a></li><li class="chapter-item "><a href="../../too-many-lists/bad-stack/final-code.html"><strong aria-hidden="true">13.2.3.</strong> 最后实现</a></li></ol></li><li class="chapter-item "><a href="../../too-many-lists/ok-stack/intro.html"><strong aria-hidden="true">13.3.</strong> 还可以的单向链表</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../too-many-lists/ok-stack/type-optimizing.html"><strong aria-hidden="true">13.3.1.</strong> 优化类型定义</a></li><li class="chapter-item "><a href="../../too-many-lists/ok-stack/peek.html"><strong aria-hidden="true">13.3.2.</strong> 定义 Peek 函数</a></li><li class="chapter-item "><a href="../../too-many-lists/ok-stack/iter.html"><strong aria-hidden="true">13.3.3.</strong> IntoIter 和 Iter</a></li><li class="chapter-item "><a href="../../too-many-lists/ok-stack/itermut.html"><strong aria-hidden="true">13.3.4.</strong> IterMut 以及完整代码</a></li></ol></li><li class="chapter-item "><a href="../../too-many-lists/persistent-stack/intro.html"><strong aria-hidden="true">13.4.</strong> 持久化单向链表</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../too-many-lists/persistent-stack/layout.html"><strong aria-hidden="true">13.4.1.</strong> 数据布局和基本操作</a></li><li class="chapter-item "><a href="../../too-many-lists/persistent-stack/drop-arc.html"><strong aria-hidden="true">13.4.2.</strong> Drop、Arc 及完整代码</a></li></ol></li><li class="chapter-item "><a href="../../too-many-lists/deque/intro.html"><strong aria-hidden="true">13.5.</strong> 不咋样的双端队列</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../too-many-lists/deque/layout.html"><strong aria-hidden="true">13.5.1.</strong> 数据布局和基本操作</a></li><li class="chapter-item "><a href="../../too-many-lists/deque/peek.html"><strong aria-hidden="true">13.5.2.</strong> Peek</a></li><li class="chapter-item "><a href="../../too-many-lists/deque/symmetric.html"><strong aria-hidden="true">13.5.3.</strong> 基本操作的对称镜像</a></li><li class="chapter-item "><a href="../../too-many-lists/deque/iterator.html"><strong aria-hidden="true">13.5.4.</strong> 迭代器</a></li><li class="chapter-item "><a href="../../too-many-lists/deque/final-code.html"><strong aria-hidden="true">13.5.5.</strong> 最终代码</a></li></ol></li><li class="chapter-item "><a href="../../too-many-lists/unsafe-queue/intro.html"><strong aria-hidden="true">13.6.</strong> 不错的 unsafe 队列</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../too-many-lists/unsafe-queue/layout.html"><strong aria-hidden="true">13.6.1.</strong> 数据布局</a></li><li class="chapter-item "><a href="../../too-many-lists/unsafe-queue/basics.html"><strong aria-hidden="true">13.6.2.</strong> 基本操作</a></li><li class="chapter-item "><a href="../../too-many-lists/unsafe-queue/miri.html"><strong aria-hidden="true">13.6.3.</strong> Miri</a></li><li class="chapter-item "><a href="../../too-many-lists/unsafe-queue/stacked-borrow.html"><strong aria-hidden="true">13.6.4.</strong> 栈借用</a></li><li class="chapter-item "><a href="../../too-many-lists/unsafe-queue/testing-stacked-borrow.html"><strong aria-hidden="true">13.6.5.</strong> 测试栈借用</a></li><li class="chapter-item "><a href="../../too-many-lists/unsafe-queue/layout2.html"><strong aria-hidden="true">13.6.6.</strong> 数据布局 2</a></li><li class="chapter-item "><a href="../../too-many-lists/unsafe-queue/extra-junk.html"><strong aria-hidden="true">13.6.7.</strong> 额外的操作</a></li><li class="chapter-item "><a href="../../too-many-lists/unsafe-queue/final-code.html"><strong aria-hidden="true">13.6.8.</strong> 最终代码</a></li></ol></li><li class="chapter-item "><a href="../../too-many-lists/production-unsafe-deque/intro.html"><strong aria-hidden="true">13.7.</strong> 生产级的双向 unsafe 队列</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../too-many-lists/production-unsafe-deque/layout.html"><strong aria-hidden="true">13.7.1.</strong> 数据布局</a></li><li class="chapter-item "><a href="../../too-many-lists/production-unsafe-deque/variance-and-phantomData.html"><strong aria-hidden="true">13.7.2.</strong> 型变与子类型</a></li><li class="chapter-item "><a href="../../too-many-lists/production-unsafe-deque/basics.html"><strong aria-hidden="true">13.7.3.</strong> 基础结构</a></li><li class="chapter-item "><a href="../../too-many-lists/production-unsafe-deque/drop-and-panic-safety.html"><strong aria-hidden="true">13.7.4.</strong> 恐慌与安全</a></li><li class="chapter-item "><a href="../../too-many-lists/production-unsafe-deque/boring-combinatorics.html"><strong aria-hidden="true">13.7.5.</strong> 无聊的组合</a></li><li class="chapter-item "><a href="../../too-many-lists/production-unsafe-deque/filling-in-random-bits.html"><strong aria-hidden="true">13.7.6.</strong> 其它特征</a></li><li class="chapter-item "><a href="../../too-many-lists/production-unsafe-deque/testing.html"><strong aria-hidden="true">13.7.7.</strong> 测试</a></li><li class="chapter-item "><a href="../../too-many-lists/production-unsafe-deque/send-sync-and-compile-tests.html"><strong aria-hidden="true">13.7.8.</strong> Send,Sync和编译测试</a></li><li class="chapter-item "><a href="../../too-many-lists/production-unsafe-deque/implementing-cursors.html"><strong aria-hidden="true">13.7.9.</strong> 实现游标</a></li><li class="chapter-item "><a href="../../too-many-lists/production-unsafe-deque/testing-cursors.html"><strong aria-hidden="true">13.7.10.</strong> 测试游标</a></li><li class="chapter-item "><a href="../../too-many-lists/production-unsafe-deque/final-code.html"><strong aria-hidden="true">13.7.11.</strong> 最终代码</a></li></ol></li><li class="chapter-item "><a href="../../too-many-lists/advanced-lists/intro.html"><strong aria-hidden="true">13.8.</strong> 使用高级技巧实现链表</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../too-many-lists/advanced-lists/double-singly.html"><strong aria-hidden="true">13.8.1.</strong> 双单向链表</a></li><li class="chapter-item "><a href="../../too-many-lists/advanced-lists/stack-allocated.html"><strong aria-hidden="true">13.8.2.</strong> 栈上的链表</a></li></ol></li></ol></li><li class="chapter-item "><li class="part-title">攻克编译错误</li><li class="spacer"></li><li class="chapter-item "><a href="../../compiler/intro.html"><strong aria-hidden="true">14.</strong> 征服编译错误</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../compiler/fight-with-compiler/intro.html"><strong aria-hidden="true">14.1.</strong> 对抗编译检查</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../compiler/fight-with-compiler/lifetime/intro.html"><strong aria-hidden="true">14.1.1.</strong> 生命周期</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../compiler/fight-with-compiler/lifetime/too-long1.html"><strong aria-hidden="true">14.1.1.1.</strong> 生命周期过大-01</a></li><li class="chapter-item "><a href="../../compiler/fight-with-compiler/lifetime/too-long2.html"><strong aria-hidden="true">14.1.1.2.</strong> 生命周期过大-02</a></li><li class="chapter-item "><a href="../../compiler/fight-with-compiler/lifetime/loop.html"><strong aria-hidden="true">14.1.1.3.</strong> 循环中的生命周期</a></li><li class="chapter-item "><a href="../../compiler/fight-with-compiler/lifetime/closure-with-static.html"><strong aria-hidden="true">14.1.1.4.</strong> 闭包碰到特征对象-01</a></li></ol></li><li class="chapter-item "><a href="../../compiler/fight-with-compiler/borrowing/intro.html"><strong aria-hidden="true">14.1.2.</strong> 重复借用</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../compiler/fight-with-compiler/borrowing/ref-exist-in-out-fn.html"><strong aria-hidden="true">14.1.2.1.</strong> 同时在函数内外使用引用</a></li><li class="chapter-item "><a href="../../compiler/fight-with-compiler/borrowing/borrow-distinct-fields-of-struct.html"><strong aria-hidden="true">14.1.2.2.</strong> 智能指针引起的重复借用错误</a></li></ol></li><li class="chapter-item "><a href="../../compiler/fight-with-compiler/unconstrained.html"><strong aria-hidden="true">14.1.3.</strong> 类型未限制(todo)</a></li><li class="chapter-item "><a href="../../compiler/fight-with-compiler/phantom-data.html"><strong aria-hidden="true">14.1.4.</strong> 幽灵数据(todo)</a></li></ol></li><li class="chapter-item "><a href="../../compiler/pitfalls/index.html"><strong aria-hidden="true">14.2.</strong> Rust 常见陷阱</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../compiler/pitfalls/use-vec-in-for.html"><strong aria-hidden="true">14.2.1.</strong> for 循环中使用外部数组</a></li><li class="chapter-item "><a href="../../compiler/pitfalls/stack-overflow.html"><strong aria-hidden="true">14.2.2.</strong> 线程类型导致的栈溢出</a></li><li class="chapter-item "><a href="../../compiler/pitfalls/arithmetic-overflow.html"><strong aria-hidden="true">14.2.3.</strong> 算术溢出导致的 panic</a></li><li class="chapter-item "><a href="../../compiler/pitfalls/closure-with-lifetime.html"><strong aria-hidden="true">14.2.4.</strong> 闭包中奇怪的生命周期</a></li><li class="chapter-item "><a href="../../compiler/pitfalls/the-disabled-mutability.html"><strong aria-hidden="true">14.2.5.</strong> 可变变量不可变?</a></li><li class="chapter-item "><a href="../../compiler/pitfalls/multiple-mutable-references.html"><strong aria-hidden="true">14.2.6.</strong> 可变借用失败引发的深入思考</a></li><li class="chapter-item "><a href="../../compiler/pitfalls/lazy-iterators.html"><strong aria-hidden="true">14.2.7.</strong> 不太勤快的迭代器</a></li><li class="chapter-item "><a href="../../compiler/pitfalls/weird-ranges.html"><strong aria-hidden="true">14.2.8.</strong> 奇怪的序列 x..y</a></li><li class="chapter-item "><a href="../../compiler/pitfalls/iterator-everywhere.html"><strong aria-hidden="true">14.2.9.</strong> 无处不在的迭代器</a></li><li class="chapter-item "><a href="../../compiler/pitfalls/main-with-channel-blocked.html"><strong aria-hidden="true">14.2.10.</strong> 线程间传递消息导致主线程无法结束</a></li><li class="chapter-item "><a href="../../compiler/pitfalls/utf8-performance.html"><strong aria-hidden="true">14.2.11.</strong> 警惕 UTF-8 引发的性能隐患</a></li></ol></li></ol></li><li class="chapter-item "><li class="part-title">性能优化</li><li class="spacer"></li><li class="chapter-item "><a href="../../profiling/intro.html"><strong aria-hidden="true">15.</strong> Rust 性能优化 todo</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../profiling/memory/intro.html"><strong aria-hidden="true">15.1.</strong> 深入内存 todo</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../profiling/memory/pointer-ref.html"><strong aria-hidden="true">15.1.1.</strong> 指针和引用 todo</a></li><li class="chapter-item "><a href="../../profiling/memory/uninit.html"><strong aria-hidden="true">15.1.2.</strong> 未初始化内存 todo</a></li><li class="chapter-item "><a href="../../profiling/memory/allocation.html"><strong aria-hidden="true">15.1.3.</strong> 内存分配 todo</a></li><li class="chapter-item "><a href="../../profiling/memory/layout.html"><strong aria-hidden="true">15.1.4.</strong> 内存布局 todo</a></li><li class="chapter-item "><a href="../../profiling/memory/virtual.html"><strong aria-hidden="true">15.1.5.</strong> 虚拟内存 todo</a></li></ol></li><li class="chapter-item "><a href="../../profiling/performance/intro.html"><strong aria-hidden="true">15.2.</strong> 性能调优 doing</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../profiling/performance/string.html"><strong aria-hidden="true">15.2.1.</strong> 字符串操作性能</a></li><li class="chapter-item "><a href="../../profiling/performance/deep-into-move.html"><strong aria-hidden="true">15.2.2.</strong> 深入理解 move</a></li><li class="chapter-item "><a href="../../profiling/performance/early-optimise.html"><strong aria-hidden="true">15.2.3.</strong> 糟糕的提前优化 todo</a></li><li class="chapter-item "><a href="../../profiling/performance/clone-copy.html"><strong aria-hidden="true">15.2.4.</strong> Clone 和 Copy todo</a></li><li class="chapter-item "><a href="../../profiling/performance/runtime-check.html"><strong aria-hidden="true">15.2.5.</strong> 减少 Runtime check(todo)</a></li><li class="chapter-item "><a href="../../profiling/performance/cpu-cache.html"><strong aria-hidden="true">15.2.6.</strong> CPU 缓存性能优化 todo</a></li><li class="chapter-item "><a href="../../profiling/performance/calculate.html"><strong aria-hidden="true">15.2.7.</strong> 计算性能优化 todo</a></li><li class="chapter-item "><a href="../../profiling/performance/heap-stack.html"><strong aria-hidden="true">15.2.8.</strong> 堆和栈 todo</a></li><li class="chapter-item "><a href="../../profiling/performance/allocator.html"><strong aria-hidden="true">15.2.9.</strong> 内存 allocator todo</a></li><li class="chapter-item "><a href="../../profiling/performance/tools.html"><strong aria-hidden="true">15.2.10.</strong> 常用性能测试工具 todo</a></li><li class="chapter-item "><a href="../../profiling/performance/enum.html"><strong aria-hidden="true">15.2.11.</strong> Enum 内存优化 todo</a></li></ol></li><li class="chapter-item "><a href="../../profiling/compiler/intro.html"><strong aria-hidden="true">15.3.</strong> 编译优化 todo</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../profiling/compiler/llvm.html"><strong aria-hidden="true">15.3.1.</strong> LLVM todo</a></li><li class="chapter-item "><a href="../../profiling/compiler/attributes.html"><strong aria-hidden="true">15.3.2.</strong> 常见属性标记 todo</a></li><li class="chapter-item "><a href="../../profiling/compiler/speed-up.html"><strong aria-hidden="true">15.3.3.</strong> 提升编译速度 todo</a></li><li class="chapter-item "><a href="../../profiling/compiler/optimization/intro.html"><strong aria-hidden="true">15.3.4.</strong> 编译器优化 todo</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../profiling/compiler/optimization/option.html"><strong aria-hidden="true">15.3.4.1.</strong> Option 枚举 todo</a></li></ol></li></ol></li></ol></li><li class="chapter-item "><li class="part-title">附录</li><li class="spacer"></li><li class="chapter-item "><div><strong aria-hidden="true">16.</strong> Appendix</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../appendix/keywords.html"><strong aria-hidden="true">16.1.</strong> 关键字</a></li><li class="chapter-item "><a href="../../appendix/operators.html"><strong aria-hidden="true">16.2.</strong> 运算符与符号</a></li><li class="chapter-item "><a href="../../appendix/expressions.html"><strong aria-hidden="true">16.3.</strong> 表达式</a></li><li class="chapter-item "><a href="../../appendix/derive.html"><strong aria-hidden="true">16.4.</strong> 派生特征 trait</a></li><li class="chapter-item "><a href="../../appendix/prelude.html"><strong aria-hidden="true">16.5.</strong> prelude 模块 todo</a></li><li class="chapter-item "><a href="../../appendix/rust-version.html"><strong aria-hidden="true">16.6.</strong> Rust 版本说明</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/intro.html"><strong aria-hidden="true">16.7.</strong> Rust 历次版本更新解读</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../appendix/rust-versions/1.58.html"><strong aria-hidden="true">16.7.1.</strong> 1.58</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.59.html"><strong aria-hidden="true">16.7.2.</strong> 1.59</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.60.html"><strong aria-hidden="true">16.7.3.</strong> 1.60</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.61.html"><strong aria-hidden="true">16.7.4.</strong> 1.61</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.62.html"><strong aria-hidden="true">16.7.5.</strong> 1.62</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.63.html"><strong aria-hidden="true">16.7.6.</strong> 1.63</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.64.html"><strong aria-hidden="true">16.7.7.</strong> 1.64</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.65.html"><strong aria-hidden="true">16.7.8.</strong> 1.65</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.66.html"><strong aria-hidden="true">16.7.9.</strong> 1.66</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.67.html"><strong aria-hidden="true">16.7.10.</strong> 1.67</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.68.html"><strong aria-hidden="true">16.7.11.</strong> 1.68</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.69.html"><strong aria-hidden="true">16.7.12.</strong> 1.69</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.70.html"><strong aria-hidden="true">16.7.13.</strong> 1.70</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.71.html"><strong aria-hidden="true">16.7.14.</strong> 1.71</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.72.html"><strong aria-hidden="true">16.7.15.</strong> 1.72</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.73.html"><strong aria-hidden="true">16.7.16.</strong> 1.73</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.74.html"><strong aria-hidden="true">16.7.17.</strong> 1.74</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.75.html"><strong aria-hidden="true">16.7.18.</strong> 1.75</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.76.html"><strong aria-hidden="true">16.7.19.</strong> 1.76</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.77.html"><strong aria-hidden="true">16.7.20.</strong> 1.77</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.78.html"><strong aria-hidden="true">16.7.21.</strong> 1.78</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.79.html"><strong aria-hidden="true">16.7.22.</strong> 1.79</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.80.html"><strong aria-hidden="true">16.7.23.</strong> 1.80</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.81.html"><strong aria-hidden="true">16.7.24.</strong> 1.81</a></li><li class="chapter-item "><a href="../../appendix/rust-versions/1.82.html"><strong aria-hidden="true">16.7.25.</strong> 1.82</a></li></ol></li></ol></li></ol>
|
|
|
</div>
|
|
|
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
|
|
|
<div class="sidebar-resize-indicator"></div>
|
|
|
</div>
|
|
|
</nav>
|
|
|
|
|
|
<!-- Track and set sidebar scroll position -->
|
|
|
<script>
|
|
|
var sidebarScrollbox = document.querySelector('#sidebar .sidebar-scrollbox');
|
|
|
sidebarScrollbox.addEventListener('click', function(e) {
|
|
|
if (e.target.tagName === 'A') {
|
|
|
sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
|
|
|
}
|
|
|
}, { passive: true });
|
|
|
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
|
|
|
sessionStorage.removeItem('sidebar-scroll');
|
|
|
if (sidebarScrollTop) {
|
|
|
// preserve sidebar scroll position when navigating via links within sidebar
|
|
|
sidebarScrollbox.scrollTop = sidebarScrollTop;
|
|
|
} else {
|
|
|
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
|
|
|
var activeSection = document.querySelector('#sidebar .active');
|
|
|
if (activeSection) {
|
|
|
activeSection.scrollIntoView({ block: 'center' });
|
|
|
}
|
|
|
}
|
|
|
</script>
|
|
|
|
|
|
<div id="page-wrapper" class="page-wrapper">
|
|
|
|
|
|
<div class="page">
|
|
|
<div id="menu-bar-hover-placeholder"></div>
|
|
|
<div id="menu-bar" class="menu-bar sticky">
|
|
|
<div class="left-buttons">
|
|
|
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
|
<i class="fa fa-bars"></i>
|
|
|
</label>
|
|
|
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
|
<i class="fa fa-paint-brush"></i>
|
|
|
</button>
|
|
|
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
|
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
|
</ul>
|
|
|
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
|
<i class="fa fa-search"></i>
|
|
|
</button>
|
|
|
</div>
|
|
|
|
|
|
<h1 class="menu-title">Rust语言圣经(Rust Course)</h1>
|
|
|
|
|
|
<div class="right-buttons">
|
|
|
<a href="../../print.html" title="Print this book" aria-label="Print this book">
|
|
|
<i id="print-button" class="fa fa-print"></i>
|
|
|
</a>
|
|
|
<a href="https://github.com/sunface/rust-course" title="Git repository" aria-label="Git repository">
|
|
|
<i id="git-repository-button" class="fa fa-github"></i>
|
|
|
</a>
|
|
|
<a href="https://github.com/sunface/rust-course/edit/main/src/basic/match-pattern/all-patterns.md" title="Suggest an edit" aria-label="Suggest an edit">
|
|
|
<i id="git-edit-button" class="fa fa-edit"></i>
|
|
|
</a>
|
|
|
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<div id="search-wrapper" class="hidden">
|
|
|
<form id="searchbar-outer" class="searchbar-outer">
|
|
|
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
|
</form>
|
|
|
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
|
<div id="searchresults-header" class="searchresults-header"></div>
|
|
|
<ul id="searchresults">
|
|
|
</ul>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
|
<script>
|
|
|
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
|
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
|
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
|
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
|
});
|
|
|
</script>
|
|
|
|
|
|
<div id="content" class="content">
|
|
|
<!-- Page table of contents -->
|
|
|
<div class="sidetoc"><nav class="pagetoc"></nav></div>
|
|
|
<main>
|
|
|
<h1 id="全模式列表"><a class="header" href="#全模式列表">全模式列表</a></h1>
|
|
|
<p>在本书中我们已领略过许多不同类型模式的例子,本节的目标就是把这些模式语法都罗列出来,方便大家检索查阅(模式匹配在我们的开发中会经常用到)。</p>
|
|
|
<h3 id="匹配字面值"><a class="header" href="#匹配字面值">匹配字面值</a></h3>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>let x = 1;
|
|
|
|
|
|
match x {
|
|
|
1 => println!("one"),
|
|
|
2 => println!("two"),
|
|
|
3 => println!("three"),
|
|
|
_ => println!("anything"),
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>这段代码会打印 <code>one</code> 因为 <code>x</code> 的值是 1,如果希望代码获得特定的具体值,那么这种语法很有用。</p>
|
|
|
<h3 id="匹配命名变量"><a class="header" href="#匹配命名变量">匹配命名变量</a></h3>
|
|
|
<p>在 <a href="https://course.rs/basic/match-pattern/match-if-let.html#%E5%8F%98%E9%87%8F%E9%81%AE%E8%94%BD">match</a> 中,我们有讲过变量遮蔽的问题,这个在<strong>匹配命名变量</strong>时会遇到:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
|
|
|
let x = Some(5);
|
|
|
let y = 10;
|
|
|
|
|
|
match x {
|
|
|
Some(50) => println!("Got 50"),
|
|
|
Some(y) => println!("Matched, y = {:?}", y),
|
|
|
_ => println!("Default case, x = {:?}", x),
|
|
|
}
|
|
|
|
|
|
println!("at the end: x = {:?}, y = {:?}", x, y);
|
|
|
}</code></pre></pre>
|
|
|
<p>让我们看看当 <code>match</code> 语句运行的时候发生了什么。第一个匹配分支的模式并不匹配 <code>x</code> 中定义的值,所以代码继续执行。</p>
|
|
|
<p>第二个匹配分支中的模式引入了一个新变量 <code>y</code>,它会匹配任何 <code>Some</code> 中的值。因为这里的 <code>y</code> 在 <code>match</code> 表达式的作用域中,而不是之前 <code>main</code> 作用域中,所以这是一个新变量,不是开头声明为值 10 的那个 <code>y</code>。这个新的 <code>y</code> 绑定会匹配任何 <code>Some</code> 中的值,在这里是 <code>x</code> 中的值。因此这个 <code>y</code> 绑定了 <code>x</code> 中 <code>Some</code> 内部的值。这个值是 5,所以这个分支的表达式将会执行并打印出 <code>Matched,y = 5</code>。</p>
|
|
|
<p>如果 <code>x</code> 的值是 <code>None</code> 而不是 <code>Some(5)</code>,头两个分支的模式不会匹配,所以会匹配模式 <code>_</code>。这个分支的模式中没有引入变量 <code>x</code>,所以此时表达式中的 <code>x</code> 会是外部没有被遮蔽的 <code>x</code>,也就是 <code>None</code>。</p>
|
|
|
<p>一旦 <code>match</code> 表达式执行完毕,其作用域也就结束了,同理内部 <code>y</code> 的作用域也结束了。最后的 <code>println!</code> 会打印 <code>at the end: x = Some(5), y = 10</code>。</p>
|
|
|
<p>如果你不想引入变量遮蔽,可以使用另一个变量名而非 <code>y</code>,或者使用匹配守卫(match guard)的方式,稍后在<a href="#%E5%8C%B9%E9%85%8D%E5%AE%88%E5%8D%AB%E6%8F%90%E4%BE%9B%E7%9A%84%E9%A2%9D%E5%A4%96%E6%9D%A1%E4%BB%B6">匹配守卫提供的额外条件</a>中会讲解。</p>
|
|
|
<h3 id="单分支多模式"><a class="header" href="#单分支多模式">单分支多模式</a></h3>
|
|
|
<p>在 <code>match</code> 表达式中,可以使用 <code>|</code> 语法匹配多个模式,它代表 <strong>或</strong>的意思。例如,如下代码将 <code>x</code> 的值与匹配分支相比较,第一个分支有 <strong>或</strong> 选项,意味着如果 <code>x</code> 的值匹配此分支的任何一个模式,它就会运行:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>let x = 1;
|
|
|
|
|
|
match x {
|
|
|
1 | 2 => println!("one or two"),
|
|
|
3 => println!("three"),
|
|
|
_ => println!("anything"),
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>上面的代码会打印 <code>one or two</code>。</p>
|
|
|
<h3 id="通过序列--匹配值的范围"><a class="header" href="#通过序列--匹配值的范围">通过序列 <code>..=</code> 匹配值的范围</a></h3>
|
|
|
<p>在<a href="https://course.rs/basic/base-type/numbers.html#%E5%BA%8F%E5%88%97range">数值类型</a>中我们有讲到一个序列语法,该语法不仅可以用于循环中,还能用于匹配模式。</p>
|
|
|
<p><code>..=</code> 语法允许你匹配一个闭区间序列内的值。在如下代码中,当模式匹配任何在此序列内的值时,该分支会执行:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>let x = 5;
|
|
|
|
|
|
match x {
|
|
|
1..=5 => println!("one through five"),
|
|
|
_ => println!("something else"),
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>如果 <code>x</code> 是 1、2、3、4 或 5,第一个分支就会匹配。这相比使用 <code>|</code> 运算符表达相同的意思更为方便;相比 <code>1..=5</code>,使用 <code>|</code> 则不得不指定 <code>1 | 2 | 3 | 4 | 5</code> 这五个值,而使用 <code>..=</code> 指定序列就简短的多,比如希望匹配比如从 1 到 1000 的数字的时候!</p>
|
|
|
<p>序列只允许用于数字或字符类型,原因是:它们可以连续,同时编译器在编译期可以检查该序列是否为空,字符和数字值是 Rust 中仅有的可以用于判断是否为空的类型。</p>
|
|
|
<p>如下是一个使用字符类型序列的例子:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>let x = 'c';
|
|
|
|
|
|
match x {
|
|
|
'a'..='j' => println!("early ASCII letter"),
|
|
|
'k'..='z' => println!("late ASCII letter"),
|
|
|
_ => println!("something else"),
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>Rust 知道 <code>'c'</code> 位于第一个模式的序列内,所以会打印出 <code>early ASCII letter</code>。</p>
|
|
|
<h3 id="解构并分解值"><a class="header" href="#解构并分解值">解构并分解值</a></h3>
|
|
|
<p>也可以使用模式来解构结构体、枚举、元组、数组和引用。</p>
|
|
|
<h4 id="解构结构体"><a class="header" href="#解构结构体">解构结构体</a></h4>
|
|
|
<p>下面代码展示了如何用 <code>let</code> 解构一个带有两个字段 <code>x</code> 和 <code>y</code> 的结构体 <code>Point</code>:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">struct Point {
|
|
|
x: i32,
|
|
|
y: i32,
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
|
let p = Point { x: 0, y: 7 };
|
|
|
|
|
|
let Point { x: a, y: b } = p;
|
|
|
assert_eq!(0, a);
|
|
|
assert_eq!(7, b);
|
|
|
}</code></pre></pre>
|
|
|
<p>这段代码创建了变量 <code>a</code> 和 <code>b</code> 来匹配结构体 <code>p</code> 中的 <code>x</code> 和 <code>y</code> 字段,这个例子展示了<strong>模式中的变量名不必与结构体中的字段名一致</strong>。不过通常希望变量名与字段名一致以便于理解变量来自于哪些字段。</p>
|
|
|
<p>因为变量名匹配字段名是常见的,同时因为 <code>let Point { x: x, y: y } = p;</code> 中 <code>x</code> 和 <code>y</code> 重复了,所以对于匹配结构体字段的模式存在简写:只需列出结构体字段的名称,则模式创建的变量会有相同的名称。下例与上例有着相同行为的代码,不过 <code>let</code> 模式创建的变量为 <code>x</code> 和 <code>y</code> 而不是 <code>a</code> 和 <code>b</code>:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">struct Point {
|
|
|
x: i32,
|
|
|
y: i32,
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
|
let p = Point { x: 0, y: 7 };
|
|
|
|
|
|
let Point { x, y } = p;
|
|
|
assert_eq!(0, x);
|
|
|
assert_eq!(7, y);
|
|
|
}</code></pre></pre>
|
|
|
<p>这段代码创建了变量 <code>x</code> 和 <code>y</code>,与结构体 <code>p</code> 中的 <code>x</code> 和 <code>y</code> 字段相匹配。其结果是变量 <code>x</code> 和 <code>y</code> 包含结构体 <code>p</code> 中的值。</p>
|
|
|
<p>也可以使用字面值作为结构体模式的一部分进行解构,而不是为所有的字段创建变量。这允许我们测试一些字段为特定值的同时创建其他字段的变量。</p>
|
|
|
<p>下文展示了固定某个字段的匹配方式:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">struct Point {
|
|
|
</span><span class="boring"> x: i32,
|
|
|
</span><span class="boring"> y: i32,
|
|
|
</span><span class="boring">}
|
|
|
</span><span class="boring">
|
|
|
</span>fn main() {
|
|
|
let p = Point { x: 0, y: 7 };
|
|
|
|
|
|
match p {
|
|
|
Point { x, y: 0 } => println!("On the x axis at {}", x),
|
|
|
Point { x: 0, y } => println!("On the y axis at {}", y),
|
|
|
Point { x, y } => println!("On neither axis: ({}, {})", x, y),
|
|
|
}
|
|
|
}</code></pre></pre>
|
|
|
<p>首先是 <code>match</code> 第一个分支,指定匹配 <code>y</code> 为 <code>0</code> 的 <code>Point</code>;
|
|
|
然后第二个分支在第一个分支之后,匹配 <code>y</code> 不为 <code>0</code>,<code>x</code> 为 <code>0</code> 的 <code>Point</code>;
|
|
|
最后一个分支匹配 <code>x</code> 不为 <code>0</code>,<code>y</code> 也不为 <code>0</code> 的 <code>Point</code>。</p>
|
|
|
<p>在这个例子中,值 <code>p</code> 因为其 <code>x</code> 包含 0 而匹配第二个分支,因此会打印出 <code>On the y axis at 7</code>。</p>
|
|
|
<h4 id="解构枚举"><a class="header" href="#解构枚举">解构枚举</a></h4>
|
|
|
<p>下面代码以 <code>Message</code> 枚举为例,编写一个 <code>match</code> 使用模式解构每一个内部值:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">enum Message {
|
|
|
Quit,
|
|
|
Move { x: i32, y: i32 },
|
|
|
Write(String),
|
|
|
ChangeColor(i32, i32, i32),
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
|
let msg = Message::ChangeColor(0, 160, 255);
|
|
|
|
|
|
match msg {
|
|
|
Message::Quit => {
|
|
|
println!("The Quit variant has no data to destructure.")
|
|
|
}
|
|
|
Message::Move { x, y } => {
|
|
|
println!(
|
|
|
"Move in the x direction {} and in the y direction {}",
|
|
|
x,
|
|
|
y
|
|
|
);
|
|
|
}
|
|
|
Message::Write(text) => println!("Text message: {}", text),
|
|
|
Message::ChangeColor(r, g, b) => {
|
|
|
println!(
|
|
|
"Change the color to red {}, green {}, and blue {}",
|
|
|
r,
|
|
|
g,
|
|
|
b
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
}</code></pre></pre>
|
|
|
<p>这里老生常谈一句话,模式匹配一样要类型相同,因此匹配 <code>Message::Move{1,2}</code> 这样的枚举值,就必须要用 <code>Message::Move{x,y}</code> 这样的同类型模式才行。</p>
|
|
|
<p>这段代码会打印出 <code>Change the color to red 0, green 160, and blue 255</code>。尝试改变 <code>msg</code> 的值来观察其他分支代码的运行。</p>
|
|
|
<p>对于像 <code>Message::Quit</code> 这样没有任何数据的枚举成员,不能进一步解构其值。只能匹配其字面值 <code>Message::Quit</code>,因此模式中没有任何变量。</p>
|
|
|
<p>对于另外两个枚举成员,就用相同类型的模式去匹配出对应的值即可。</p>
|
|
|
<h4 id="解构嵌套的结构体和枚举"><a class="header" href="#解构嵌套的结构体和枚举">解构嵌套的结构体和枚举</a></h4>
|
|
|
<p>目前为止,所有的例子都只匹配了深度为一级的结构体或枚举。 <code>match</code> 也可以匹配嵌套的项!</p>
|
|
|
<p>例如使用下面的代码来同时支持 RGB 和 HSV 色彩模式:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">enum Color {
|
|
|
Rgb(i32, i32, i32),
|
|
|
Hsv(i32, i32, i32),
|
|
|
}
|
|
|
|
|
|
enum Message {
|
|
|
Quit,
|
|
|
Move { x: i32, y: i32 },
|
|
|
Write(String),
|
|
|
ChangeColor(Color),
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
|
let msg = Message::ChangeColor(Color::Hsv(0, 160, 255));
|
|
|
|
|
|
match msg {
|
|
|
Message::ChangeColor(Color::Rgb(r, g, b)) => {
|
|
|
println!(
|
|
|
"Change the color to red {}, green {}, and blue {}",
|
|
|
r,
|
|
|
g,
|
|
|
b
|
|
|
)
|
|
|
}
|
|
|
Message::ChangeColor(Color::Hsv(h, s, v)) => {
|
|
|
println!(
|
|
|
"Change the color to hue {}, saturation {}, and value {}",
|
|
|
h,
|
|
|
s,
|
|
|
v
|
|
|
)
|
|
|
}
|
|
|
_ => ()
|
|
|
}
|
|
|
}</code></pre></pre>
|
|
|
<p><code>match</code> 第一个分支的模式匹配一个 <code>Message::ChangeColor</code> 枚举成员,该枚举成员又包含了一个 <code>Color::Rgb</code> 的枚举成员,最终绑定了 3 个内部的 <code>i32</code> 值。第二个,就交给亲爱的读者来思考完成。</p>
|
|
|
<h4 id="解构结构体和元组"><a class="header" href="#解构结构体和元组">解构结构体和元组</a></h4>
|
|
|
<p>我们甚至可以用复杂的方式来混合、匹配和嵌套解构模式。如下是一个复杂结构体的例子,其中结构体和元组嵌套在元组中,并将所有的原始类型解构出来:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>struct Point {
|
|
|
x: i32,
|
|
|
y: i32,
|
|
|
}
|
|
|
|
|
|
let ((feet, inches), Point {x, y}) = ((3, 10), Point { x: 3, y: -10 });
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>这种将复杂类型分解匹配的方式,可以让我们单独得到感兴趣的某个值。</p>
|
|
|
<h4 id="解构数组"><a class="header" href="#解构数组">解构数组</a></h4>
|
|
|
<p>对于数组,我们可以用类似元组的方式解构,分为两种情况:</p>
|
|
|
<p>定长数组</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>let arr: [u16; 2] = [114, 514];
|
|
|
let [x, y] = arr;
|
|
|
|
|
|
assert_eq!(x, 114);
|
|
|
assert_eq!(y, 514);
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>不定长数组</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>let arr: &[u16] = &[114, 514];
|
|
|
|
|
|
if let [x, ..] = arr {
|
|
|
assert_eq!(x, &114);
|
|
|
}
|
|
|
|
|
|
if let &[.., y] = arr {
|
|
|
assert_eq!(y, 514);
|
|
|
}
|
|
|
|
|
|
let arr: &[u16] = &[];
|
|
|
|
|
|
assert!(matches!(arr, [..]));
|
|
|
assert!(!matches!(arr, [x, ..]));
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<h3 id="忽略模式中的值"><a class="header" href="#忽略模式中的值">忽略模式中的值</a></h3>
|
|
|
<p>有时忽略模式中的一些值是很有用的,比如在 <code>match</code> 中的最后一个分支使用 <code>_</code> 模式匹配所有剩余的值。 你也可以在另一个模式中使用 <code>_</code> 模式,使用一个以下划线开始的名称,或者使用 <code>..</code> 忽略所剩部分的值。</p>
|
|
|
<h4 id="使用-_-忽略整个值"><a class="header" href="#使用-_-忽略整个值">使用 <code>_</code> 忽略整个值</a></h4>
|
|
|
<p>虽然 <code>_</code> 模式作为 <code>match</code> 表达式最后的分支特别有用,但是它的作用还不限于此。例如可以将其用于函数参数中:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">fn foo(_: i32, y: i32) {
|
|
|
println!("This code only uses the y parameter: {}", y);
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
|
foo(3, 4);
|
|
|
}</code></pre></pre>
|
|
|
<p>这段代码会完全忽略作为第一个参数传递的值 <code>3</code>,并会打印出 <code>This code only uses the y parameter: 4</code>。</p>
|
|
|
<p>大部分情况当你不再需要特定函数参数时,最好修改签名不再包含无用的参数。在一些情况下忽略函数参数会变得特别有用,比如实现特征时,当你需要特定类型签名但是函数实现并不需要某个参数时。此时编译器就<strong>不会警告说存在未使用的函数参数</strong>,就跟使用命名参数一样。</p>
|
|
|
<h4 id="使用嵌套的-_-忽略部分值"><a class="header" href="#使用嵌套的-_-忽略部分值">使用嵌套的 <code>_</code> 忽略部分值</a></h4>
|
|
|
<p>可以在一个模式内部使用 <code>_</code> 忽略部分值:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>let mut setting_value = Some(5);
|
|
|
let new_setting_value = Some(10);
|
|
|
|
|
|
match (setting_value, new_setting_value) {
|
|
|
(Some(_), Some(_)) => {
|
|
|
println!("Can't overwrite an existing customized value");
|
|
|
}
|
|
|
_ => {
|
|
|
setting_value = new_setting_value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
println!("setting is {:?}", setting_value);
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>这段代码会打印出 <code>Can't overwrite an existing customized value</code> 接着是 <code>setting is Some(5)</code>。</p>
|
|
|
<p>第一个匹配分支,我们不关心里面的值,只关心元组中两个元素的类型,因此对于 <code>Some</code> 中的值,直接进行忽略。
|
|
|
剩下的形如 <code>(Some(_),None)</code>,<code>(None, Some(_))</code>, <code>(None,None)</code> 形式,都由第二个分支 <code>_</code> 进行分配。</p>
|
|
|
<p>还可以在一个模式中的多处使用下划线来忽略特定值,如下所示,这里忽略了一个五元元组中的第二和第四个值:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>let numbers = (2, 4, 8, 16, 32);
|
|
|
|
|
|
match numbers {
|
|
|
(first, _, third, _, fifth) => {
|
|
|
println!("Some numbers: {}, {}, {}", first, third, fifth)
|
|
|
},
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>老生常谈:模式匹配一定要类型相同,因此匹配 <code>numbers</code> 元组的模式,也必须有五个值(元组中元素的数量也属于元组类型的一部分)。</p>
|
|
|
<p>这会打印出 <code>Some numbers: 2, 8, 32</code>, 值 4 和 16 会被忽略。</p>
|
|
|
<h4 id="使用下划线开头忽略未使用的变量"><a class="header" href="#使用下划线开头忽略未使用的变量">使用下划线开头忽略未使用的变量</a></h4>
|
|
|
<p>如果你创建了一个变量却不在任何地方使用它,Rust 通常会给你一个警告,因为这可能会是个 BUG。但是有时创建一个不会被使用的变量是有用的,比如你正在设计原型或刚刚开始一个项目。这时你希望告诉 Rust 不要警告未使用的变量,为此可以用下划线作为变量名的开头:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
|
|
|
let _x = 5;
|
|
|
let y = 10;
|
|
|
}</code></pre></pre>
|
|
|
<p>这里得到了警告说未使用变量 <code>y</code>,至于 <code>x</code> 则没有警告。</p>
|
|
|
<p>注意, 只使用 <code>_</code> 和使用以下划线开头的名称有些微妙的不同:比如 <strong><code>_x</code> 仍会将值绑定到变量,而 <code>_</code> 则完全不会绑定</strong>。</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>let s = Some(String::from("Hello!"));
|
|
|
|
|
|
if let Some(_s) = s {
|
|
|
println!("found a string");
|
|
|
}
|
|
|
|
|
|
println!("{:?}", s);
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p><code>s</code> 是一个拥有所有权的动态字符串,在上面代码中,我们会得到一个错误,因为 <code>s</code> 的值会被转移给 <code>_s</code>,在 <code>println!</code> 中再次使用 <code>s</code> 会报错:</p>
|
|
|
<pre><code class="language-console">error[E0382]: borrow of partially moved value: `s`
|
|
|
--> src/main.rs:8:22
|
|
|
|
|
|
|
4 | if let Some(_s) = s {
|
|
|
| -- value partially moved here
|
|
|
...
|
|
|
8 | println!("{:?}", s);
|
|
|
| ^ value borrowed here after partial move
|
|
|
</code></pre>
|
|
|
<p>只使用下划线本身,则并不会绑定值,因为 <code>s</code> 没有被移动进 <code>_</code>:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>let s = Some(String::from("Hello!"));
|
|
|
|
|
|
if let Some(_) = s {
|
|
|
println!("found a string");
|
|
|
}
|
|
|
|
|
|
println!("{:?}", s);
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<h4 id="用--忽略剩余值"><a class="header" href="#用--忽略剩余值">用 <code>..</code> 忽略剩余值</a></h4>
|
|
|
<p>对于有多个部分的值,可以使用 <code>..</code> 语法来只使用部分值而忽略其它值,这样也不用再为每一个被忽略的值都单独列出下划线。<code>..</code> 模式会忽略模式中剩余的任何没有显式匹配的值部分。</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>struct Point {
|
|
|
x: i32,
|
|
|
y: i32,
|
|
|
z: i32,
|
|
|
}
|
|
|
|
|
|
let origin = Point { x: 0, y: 0, z: 0 };
|
|
|
|
|
|
match origin {
|
|
|
Point { x, .. } => println!("x is {}", x),
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>这里列出了 <code>x</code> 值,接着使用了 <code>..</code> 模式来忽略其它字段,这样的写法要比一一列出其它字段,然后用 <code>_</code> 忽略简洁的多。</p>
|
|
|
<p>还可以用 <code>..</code> 来忽略元组中间的某些值:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
|
|
|
let numbers = (2, 4, 8, 16, 32);
|
|
|
|
|
|
match numbers {
|
|
|
(first, .., last) => {
|
|
|
println!("Some numbers: {}, {}", first, last);
|
|
|
},
|
|
|
}
|
|
|
}</code></pre></pre>
|
|
|
<p>这里用 <code>first</code> 和 <code>last</code> 来匹配第一个和最后一个值。<code>..</code> 将匹配并忽略中间的所有值。</p>
|
|
|
<p>然而使用 <code>..</code> 必须是无歧义的。如果期望匹配和忽略的值是不明确的,Rust 会报错。下面代码展示了一个带有歧义的 <code>..</code> 例子,因此不能编译:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
|
|
|
let numbers = (2, 4, 8, 16, 32);
|
|
|
|
|
|
match numbers {
|
|
|
(.., second, ..) => {
|
|
|
println!("Some numbers: {}", second)
|
|
|
},
|
|
|
}
|
|
|
}</code></pre></pre>
|
|
|
<p>如果编译上面的例子,会得到下面的错误:</p>
|
|
|
<pre><code class="language-text">error: `..` can only be used once per tuple pattern // 每个元组模式只能使用一个 `..`
|
|
|
--> src/main.rs:5:22
|
|
|
|
|
|
|
5 | (.., second, ..) => {
|
|
|
| -- ^^ can only be used once per tuple pattern
|
|
|
| |
|
|
|
| previously used here // 上一次使用在这里
|
|
|
|
|
|
error: could not compile `world_hello` due to previous error ^^
|
|
|
</code></pre>
|
|
|
<p>Rust 无法判断,<code>second</code> 应该匹配 <code>numbers</code> 中的第几个元素,因此这里使用两个 <code>..</code> 模式,是有很大歧义的!</p>
|
|
|
<h3 id="匹配守卫提供的额外条件"><a class="header" href="#匹配守卫提供的额外条件">匹配守卫提供的额外条件</a></h3>
|
|
|
<p><strong>匹配守卫</strong>(<em>match guard</em>)是一个位于 <code>match</code> 分支模式之后的额外 <code>if</code> 条件,它能为分支模式提供更进一步的匹配条件。</p>
|
|
|
<p>这个条件可以使用模式中创建的变量:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>let num = Some(4);
|
|
|
|
|
|
match num {
|
|
|
Some(x) if x < 5 => println!("less than five: {}", x),
|
|
|
Some(x) => println!("{}", x),
|
|
|
None => (),
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>这个例子会打印出 <code>less than five: 4</code>。当 <code>num</code> 与模式中第一个分支匹配时,<code>Some(4)</code> 可以与 <code>Some(x)</code> 匹配,接着匹配守卫检查 <code>x</code> 值是否小于 5,因为 4 小于 5,所以第一个分支被选择。</p>
|
|
|
<p>相反如果 <code>num</code> 为 <code>Some(10)</code>,因为 10 不小于 5 ,所以第一个分支的匹配守卫为假。接着 Rust 会前往第二个分支,因为这里没有匹配守卫所以会匹配任何 <code>Some</code> 成员。</p>
|
|
|
<p>模式中无法提供类如 <code>if x < 5</code> 的表达能力,我们可以通过匹配守卫的方式来实现。</p>
|
|
|
<p>在之前,我们提到可以使用匹配守卫来解决模式中变量覆盖的问题,那里 <code>match</code> 表达式的模式中新建了一个变量而不是使用 <code>match</code> 之外的同名变量。内部变量覆盖了外部变量,意味着此时不能够使用外部变量的值,下面代码展示了如何使用匹配守卫修复这个问题。</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
|
|
|
let x = Some(5);
|
|
|
let y = 10;
|
|
|
|
|
|
match x {
|
|
|
Some(50) => println!("Got 50"),
|
|
|
Some(n) if n == y => println!("Matched, n = {}", n),
|
|
|
_ => println!("Default case, x = {:?}", x),
|
|
|
}
|
|
|
|
|
|
println!("at the end: x = {:?}, y = {}", x, y);
|
|
|
}</code></pre></pre>
|
|
|
<p>现在这会打印出 <code>Default case, x = Some(5)</code>。现在第二个匹配分支中的模式不会引入一个覆盖外部 <code>y</code> 的新变量 <code>y</code>,这意味着可以在匹配守卫中使用外部的 <code>y</code>。相比指定会覆盖外部 <code>y</code> 的模式 <code>Some(y)</code>,这里指定为 <code>Some(n)</code>。此新建的变量 <code>n</code> 并没有覆盖任何值,因为 <code>match</code> 外部没有变量 <code>n</code>。</p>
|
|
|
<p>匹配守卫 <code>if n == y</code> 并不是一个模式所以没有引入新变量。这个 <code>y</code> <strong>正是</strong> 外部的 <code>y</code> 而不是新的覆盖变量 <code>y</code>,这样就可以通过比较 <code>n</code> 和 <code>y</code> 来表达寻找一个与外部 <code>y</code> 相同的值的概念了。</p>
|
|
|
<p>也可以在匹配守卫中使用 <strong>或</strong> 运算符 <code>|</code> 来指定多个模式,<strong>同时匹配守卫的条件会作用于所有的模式</strong>。下面代码展示了匹配守卫与 <code>|</code> 的优先级。这个例子中看起来好像 <code>if y</code> 只作用于 <code>6</code>,但实际上匹配守卫 <code>if y</code> 作用于 <code>4</code>、<code>5</code> <strong>和</strong> <code>6</code> ,在满足 <code>x</code> 属于 <code>4 | 5 | 6</code> 后才会判断 <code>y</code> 是否为 <code>true</code>:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>let x = 4;
|
|
|
let y = false;
|
|
|
|
|
|
match x {
|
|
|
4 | 5 | 6 if y => println!("yes"),
|
|
|
_ => println!("no"),
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>这个匹配条件表明此分支只匹配 <code>x</code> 值为 <code>4</code>、<code>5</code> 或 <code>6</code> <strong>同时</strong> <code>y</code> 为 <code>true</code> 的情况。</p>
|
|
|
<p>虽然在第一个分支中,<code>x</code> 匹配了模式 <code>4</code> ,但是对于匹配守卫 <code>if y</code> 来说,因为 <code>y</code> 是 <code>false</code>,因此该守卫条件的值永远是 <code>false</code>,也意味着第一个分支永远无法被匹配。</p>
|
|
|
<p>下面的文字图解释了匹配守卫作用于多个模式时的优先级规则,第一张是正确的:</p>
|
|
|
<pre><code class="language-text">(4 | 5 | 6) if y => ...
|
|
|
</code></pre>
|
|
|
<p>而第二张图是错误的</p>
|
|
|
<pre><code class="language-text">4 | 5 | (6 if y) => ...
|
|
|
</code></pre>
|
|
|
<p>可以通过运行代码时的情况看出这一点:如果匹配守卫只作用于由 <code>|</code> 运算符指定的值列表的最后一个值,这个分支就会匹配且程序会打印出 <code>yes</code>。</p>
|
|
|
<h2 id="绑定"><a class="header" href="#绑定">@绑定</a></h2>
|
|
|
<p><code>@</code>(读作 at)运算符允许为一个字段绑定另外一个变量。下面例子中,我们希望测试 <code>Message::Hello</code> 的 <code>id</code> 字段是否位于 <code>3..=7</code> 范围内,同时也希望能将其值绑定到 <code>id_variable</code> 变量中以便此分支中相关的代码可以使用它。我们可以将 <code>id_variable</code> 命名为 <code>id</code>,与字段同名,不过出于示例的目的这里选择了不同的名称。</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>enum Message {
|
|
|
Hello { id: i32 },
|
|
|
}
|
|
|
|
|
|
let msg = Message::Hello { id: 5 };
|
|
|
|
|
|
match msg {
|
|
|
Message::Hello { id: id_variable @ 3..=7 } => {
|
|
|
println!("Found an id in range: {}", id_variable)
|
|
|
},
|
|
|
Message::Hello { id: 10..=12 } => {
|
|
|
println!("Found an id in another range")
|
|
|
},
|
|
|
Message::Hello { id } => {
|
|
|
println!("Found some other id: {}", id)
|
|
|
},
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>上例会打印出 <code>Found an id in range: 5</code>。通过在 <code>3..=7</code> 之前指定 <code>id_variable @</code>,我们捕获了任何匹配此范围的值并同时将该值绑定到变量 <code>id_variable</code> 上。</p>
|
|
|
<p>第二个分支只在模式中指定了一个范围,<code>id</code> 字段的值可以是 <code>10、11 或 12</code>,不过这个模式的代码并不知情也不能使用 <code>id</code> 字段中的值,因为没有将 <code>id</code> 值保存进一个变量。</p>
|
|
|
<p>最后一个分支指定了一个没有范围的变量,此时确实拥有可以用于分支代码的变量 <code>id</code>,因为这里使用了结构体字段简写语法。不过此分支中没有像头两个分支那样对 <code>id</code> 字段的值进行测试:任何值都会匹配此分支。</p>
|
|
|
<p>当你既想要限定分支范围,又想要使用分支的变量时,就可以用 <code>@</code> 来绑定到一个新的变量上,实现想要的功能。</p>
|
|
|
<h4 id="前绑定后解构rust-156-新增"><a class="header" href="#前绑定后解构rust-156-新增">@前绑定后解构(Rust 1.56 新增)</a></h4>
|
|
|
<p>使用 <code>@</code> 还可以在绑定新变量的同时,对目标进行解构:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">#[derive(Debug)]
|
|
|
struct Point {
|
|
|
x: i32,
|
|
|
y: i32,
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
|
// 绑定新变量 `p`,同时对 `Point` 进行解构
|
|
|
let p @ Point {x: px, y: py } = Point {x: 10, y: 23};
|
|
|
println!("x: {}, y: {}", px, py);
|
|
|
println!("{:?}", p);
|
|
|
|
|
|
|
|
|
let point = Point {x: 10, y: 5};
|
|
|
if let p @ Point {x: 10, y} = point {
|
|
|
println!("x is 10 and y is {} in {:?}", y, p);
|
|
|
} else {
|
|
|
println!("x was not 10 :(");
|
|
|
}
|
|
|
}</code></pre></pre>
|
|
|
<h4 id="新特性rust-153-新增"><a class="header" href="#新特性rust-153-新增">@新特性(Rust 1.53 新增)</a></h4>
|
|
|
<p>考虑下面一段代码:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
|
|
|
match 1 {
|
|
|
num @ 1 | 2 => {
|
|
|
println!("{}", num);
|
|
|
}
|
|
|
_ => {}
|
|
|
}
|
|
|
}</code></pre></pre>
|
|
|
<p>编译不通过,是因为 <code>num</code> 没有绑定到所有的模式上,只绑定了模式 <code>1</code>,你可能会试图通过这个方式来解决:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>num @ (1 | 2)
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>但是,如果你用的是 Rust 1.53 之前的版本,那这种写法会报错,因为编译器不支持。</p>
|
|
|
<p>至此,模式匹配的内容已经全部完结,复杂但是详尽,想要一次性全部记住属实不易,因此读者可以先留一个印象,等未来需要时,再来翻阅寻找具体的模式实现方式。</p>
|
|
|
<h2 id="课后练习"><a class="header" href="#课后练习">课后练习</a></h2>
|
|
|
<blockquote>
|
|
|
<p><a href="https://practice-zh.course.rs/pattern-match/patterns.html">Rust By Practice</a>,支持代码在线编辑和运行,并提供详细的<a href="https://github.com/sunface/rust-by-practice/blob/master/solutions/pattern-match/patterns.md">习题解答</a>。</p>
|
|
|
</blockquote>
|
|
|
|
|
|
<div id="giscus-container"></div>
|
|
|
</main>
|
|
|
|
|
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
|
<!-- Mobile navigation buttons -->
|
|
|
<a rel="prev" href="../../basic/match-pattern/pattern-match.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
|
<i class="fa fa-angle-left"></i>
|
|
|
</a>
|
|
|
|
|
|
<a rel="next prefetch" href="../../basic/method.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
|
<i class="fa fa-angle-right"></i>
|
|
|
</a>
|
|
|
|
|
|
<div style="clear: both"></div>
|
|
|
</nav>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
|
<a rel="prev" href="../../basic/match-pattern/pattern-match.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
|
<i class="fa fa-angle-left"></i>
|
|
|
</a>
|
|
|
|
|
|
<a rel="next prefetch" href="../../basic/method.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
|
<i class="fa fa-angle-right"></i>
|
|
|
</a>
|
|
|
</nav>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
window.playground_copyable = true;
|
|
|
</script>
|
|
|
|
|
|
<script src="../../ace.js"></script>
|
|
|
<script src="../../editor.js"></script>
|
|
|
<script src="../../mode-rust.js"></script>
|
|
|
<script src="../../theme-dawn.js"></script>
|
|
|
<script src="../../theme-tomorrow_night.js"></script>
|
|
|
|
|
|
<script src="../../elasticlunr.min.js"></script>
|
|
|
<script src="../../mark.min.js"></script>
|
|
|
<script src="../../searcher.js"></script>
|
|
|
|
|
|
<script src="../../clipboard.min.js"></script>
|
|
|
<script src="../../highlight.js"></script>
|
|
|
<script src="../../book.js"></script>
|
|
|
|
|
|
<script type="text/javascript" charset="utf-8">
|
|
|
var pagePath = "basic/match-pattern/all-patterns.md"
|
|
|
</script>
|
|
|
|
|
|
|
|
|
<!-- Custom JS scripts -->
|
|
|
<script src="../../assets/custom.js"></script>
|
|
|
<script src="../../assets/bigPicture.js"></script>
|
|
|
|
|
|
|
|
|
</div>
|
|
|
</body>
|
|
|
</html> |