You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trpl-zh-cn/ch20-05-macros.html

464 lines
49 KiB

<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>宏 - Rust 程序设计语言 简体中文版</title>
<!-- Custom HTML head -->
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="favicon-de23e50b.svg">
<link rel="shortcut icon" href="favicon-8114d1fc.png">
<link rel="stylesheet" href="css/variables-8adf115d.css">
<link rel="stylesheet" href="css/general-2459343d.css">
<link rel="stylesheet" href="css/chrome-ae938929.css">
<link rel="stylesheet" href="css/print-9e4910d8.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="fonts/fonts-9644e21d.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" id="mdbook-highlight-css" href="highlight-493f70e1.css">
<link rel="stylesheet" id="mdbook-tomorrow-night-css" href="tomorrow-night-4c0ae647.css">
<link rel="stylesheet" id="mdbook-ayu-highlight-css" href="ayu-highlight-3fdfc3ac.css">
<!-- Custom theme stylesheets -->
<link rel="stylesheet" href="ferris-d33b75bf.css">
<link rel="stylesheet" href="theme/2018-edition-4e126c62.css">
<link rel="stylesheet" href="theme/semantic-notes-9b5766c0.css">
<link rel="stylesheet" href="theme/listing-cab26221.css">
<!-- Provide site root and default themes to javascript -->
<script>
const path_to_root = "";
const default_light_theme = "light";
const default_dark_theme = "navy";
window.path_to_searchindex_js = "searchindex-7d935aa5.js";
</script>
<!-- Start loading toc.js asap -->
<script src="toc-9d5d6d0e.js"></script>
</head>
<body>
<div id="mdbook-help-container">
<div id="mdbook-help-popup">
<h2 class="mdbook-help-title">Keyboard shortcuts</h2>
<div>
<p>Press <kbd></kbd> or <kbd></kbd> to navigate between chapters</p>
<p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
<p>Press <kbd>?</kbd> to show this help</p>
<p>Press <kbd>Esc</kbd> to hide this help</p>
</div>
</div>
</div>
<div id="mdbook-body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
let theme = localStorage.getItem('mdbook-theme');
let sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
let theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="mdbook-sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
let sidebar = null;
const sidebar_toggle = document.getElementById("mdbook-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 = false;
}
if (sidebar === 'visible') {
sidebar_toggle.checked = true;
} else {
html.classList.remove('sidebar-visible');
}
</script>
<nav id="mdbook-sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="toc.html"></iframe>
</noscript>
<div id="mdbook-sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="mdbook-page-wrapper" class="page-wrapper">
<div class="page">
<div id="mdbook-menu-bar-hover-placeholder"></div>
<div id="mdbook-menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="mdbook-sidebar-toggle" class="icon-button" for="mdbook-sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="mdbook-sidebar">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M0 96C0 78.3 14.3 64 32 64H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32C14.3 128 0 113.7 0 96zM0 256c0-17.7 14.3-32 32-32H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32c-17.7 0-32-14.3-32-32zM448 416c0 17.7-14.3 32-32 32H32c-17.7 0-32-14.3-32-32s14.3-32 32-32H416c17.7 0 32 14.3 32 32z"/></svg></span>
</label>
<button id="mdbook-theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="mdbook-theme-list">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M371.3 367.1c27.3-3.9 51.9-19.4 67.2-42.9L600.2 74.1c12.6-19.5 9.4-45.3-7.6-61.2S549.7-4.4 531.1 9.6L294.4 187.2c-24 18-38.2 46.1-38.4 76.1L371.3 367.1zm-19.6 25.4l-116-104.4C175.9 290.3 128 339.6 128 400c0 3.9 .2 7.8 .6 11.6c1.8 17.5-10.2 36.4-27.8 36.4H96c-17.7 0-32 14.3-32 32s14.3 32 32 32H240c61.9 0 112-50.1 112-112c0-2.5-.1-5-.2-7.5z"/></svg></span>
</button>
<ul id="mdbook-theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-default_theme">Auto</button></li>
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-ayu">Ayu</button></li>
</ul>
<button id="mdbook-search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="mdbook-searchbar">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M416 208c0 45.9-14.9 88.3-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM208 352c79.5 0 144-64.5 144-144s-64.5-144-144-144S64 128.5 64 208s64.5 144 144 144z"/></svg></span>
</button>
</div>
<h1 class="menu-title">Rust 程序设计语言 简体中文版</h1>
<div class="right-buttons">
<a href="print.html" title="Print this book" aria-label="Print this book">
<span class=fa-svg id="print-button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M128 0C92.7 0 64 28.7 64 64v96h64V64H354.7L384 93.3V160h64V93.3c0-17-6.7-33.3-18.7-45.3L400 18.7C388 6.7 371.7 0 354.7 0H128zM384 352v32 64H128V384 368 352H384zm64 32h32c17.7 0 32-14.3 32-32V256c0-35.3-28.7-64-64-64H64c-35.3 0-64 28.7-64 64v96c0 17.7 14.3 32 32 32H64v64c0 35.3 28.7 64 64 64H384c35.3 0 64-28.7 64-64V384zm-16-88c-13.3 0-24-10.7-24-24s10.7-24 24-24s24 10.7 24 24s-10.7 24-24 24z"/></svg></span>
</a>
<a href="https://github.com/KaiserY/trpl-zh-cn/tree/main" title="Git repository" aria-label="Git repository">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg></span>
</a>
</div>
</div>
<div id="mdbook-search-wrapper" class="hidden">
<form id="mdbook-searchbar-outer" class="searchbar-outer">
<div class="search-wrapper">
<input type="search" id="mdbook-searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="mdbook-searchresults-outer" aria-describedby="searchresults-header">
<div class="spinner-wrapper">
<span class=fa-svg id="fa-spin"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M304 48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zm0 416c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM48 304c26.5 0 48-21.5 48-48s-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48zm464-48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM142.9 437c18.7-18.7 18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zm0-294.2c18.7-18.7 18.7-49.1 0-67.9S93.7 56.2 75 75s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zM369.1 437c18.7 18.7 49.1 18.7 67.9 0s18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9z"/></svg></span>
</div>
</div>
</form>
<div id="mdbook-searchresults-outer" class="searchresults-outer hidden">
<div id="mdbook-searchresults-header" class="searchresults-header"></div>
<ul id="mdbook-searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('mdbook-sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('mdbook-sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#mdbook-sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="mdbook-content" class="content">
<main>
<h2 id="宏"><a class="header" href="#宏"></a></h2>
<!-- https://github.com/rust-lang/book/blob/main/src/ch20-05-macros.md -->
<!-- commit 1d1424ba1c30b8efab636c911be0a215df305eea -->
<p>我们已经在本书中使用过像 <code>println!</code> 这样的宏了,不过尚未深入探讨什么是宏以及它是如何工作的。<strong></strong><em>Macro</em>)指的是 Rust 中一系列的功能:使用 <code>macro_rules!</code><strong>声明宏</strong><em>declarative macro</em>),和三种 <strong>过程宏</strong><em>procedural macro</em></p>
<ul>
<li>自定义 <code>#[derive]</code> 宏,用于在结构体和枚举上通过添加 <code>derive</code> 属性生成代码</li>
<li>类属性宏,定义可用于任意项的自定义属性</li>
<li>类函数宏,看起来像函数,但操作的是作为其参数传递的 token</li>
</ul>
<p>我们会依次讨论每一种宏,不过首要的是,让我们看看为什么已经有了函数还需要宏呢?</p>
<h3 id="宏和函数的区别"><a class="header" href="#宏和函数的区别">宏和函数的区别</a></h3>
<p>从根本上来说,宏是一种为写其他代码而写代码的方式,即所谓的 <strong>元编程</strong><em>metaprogramming</em>)。在附录 C 中会探讨 <code>derive</code> 属性,其生成各种 trait 的实现。我们也在本书中一直使用 <code>println!</code> 宏和 <code>vec!</code> 宏。所有的这些宏以 <strong>展开</strong> 的方式来生成比你所手写出的更多的代码。</p>
<p>元编程对于减少大量编写和维护的代码是非常有用的,它也扮演了函数所扮演的角色。但宏有一些函数所没有的附加能力。</p>
<p>一个函数签名必须声明函数参数的数量和类型。相比之下,宏能够接收可变数量的参数:用一个参数调用 <code>println!("hello")</code> 或用两个参数调用 <code>println!("hello {}", name)</code> 。而且,宏可以在编译器解析代码前展开,例如,宏可以在一个给定类型上实现 trait。而函数则不行因为函数是在运行时被调用而 trait 需要在编译时实现。</p>
<p>实现宏的缺点是与函数的定义相比宏的定义更复杂,因为你正在编写生成 Rust 代码的 Rust 代码。由于这样的间接性,宏定义通常要比函数定义更难阅读、理解和维护。</p>
<p>宏和函数的最后一个重要的区别是:在一个文件里调用宏 <strong>之前</strong> 必须定义它,或将其引入作用域,而函数则可以在任何地方定义和调用。</p>
<h3 id="使用-macro_rules-的声明宏用于通用元编程"><a class="header" href="#使用-macro_rules-的声明宏用于通用元编程">使用 <code>macro_rules!</code> 的声明宏用于通用元编程</a></h3>
<p>Rust 最常用的宏形式是 <strong>声明宏</strong><em>declarative macros</em>)。它们有时也被称为 “macros by example”、“<code>macro_rules!</code> 宏” 或者就是 “macros”。其核心概念是声明宏允许我们编写一些类似 Rust <code>match</code> 表达式的代码。正如在第六章讨论的那样,<code>match</code> 表达式是一种控制结构,其接收一个表达式,与表达式的结果进行模式匹配,然后根据模式匹配执行相关代码。宏也将一个值和包含相关代码的模式进行比较:此种情况下,该值是传递给宏的 Rust 源代码字面值;模式用于和前面提到的源代码字面值进行比较,一旦匹配成功,每个模式的相关代码会替换传递给宏的代码。所有这一切都发生于编译时。</p>
<p>可以使用 <code>macro_rules!</code> 来定义宏。让我们通过查看 <code>vec!</code> 宏定义来探索如何使用 <code>macro_rules!</code> 结构。第八章讲述了如何使用 <code>vec!</code> 宏来生成一个给定值的新 vector。例如下面的宏用三个整数创建一个 vector</p>
<pre class="playground"><code class="language-rust edition2024"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>let v: Vec&lt;u32&gt; = vec![1, 2, 3];
<span class="boring">}</span></code></pre>
<p>也可以使用 <code>vec!</code> 宏来构造两个整数的 vector 或五个字符串 slice 的 vector。但却无法使用函数做相同的事情因为我们无法预先知道参数值的数量和类型。</p>
<p>在示例 20-35 中展示了 <code>vec!</code> 宏的一个稍微简化的定义。</p>
<p><span class="filename">文件名src/lib.rs</span></p>
<pre><code class="language-rust noplayground">#[macro_export]
macro_rules! vec {
( $( $x:expr ),* ) =&gt; {
{
let mut temp_vec = Vec::new();
$(
temp_vec.push($x);
)*
temp_vec
}
};
}</code></pre>
<p><span class="caption">示例 20-35: 一个 <code>vec!</code> 宏定义的简化版本</span></p>
<blockquote>
<p>注意:标准库中实际定义的 <code>vec!</code> 包括预分配正确数量内存的代码。这部分为代码优化,为了让示例简化,此处并没有包含在内。</p>
</blockquote>
<p><code>#[macro_export]</code> 注解表明只要导入了定义这个宏的 crate该宏就应该是可用的。如果没有该注解这个宏不能被引入作用域。</p>
<p>接着使用 <code>macro_rules!</code> 和宏名称开始宏定义,且所定义的宏并 <strong>不带</strong> 感叹号。名字后跟大括号表示宏定义体,在该例中宏名称是 <code>vec</code></p>
<p><code>vec!</code> 宏的结构和 <code>match</code> 表达式的结构类似。此处有一个分支模式 <code>( $( $x:expr ),* )</code> ,后跟 <code>=&gt;</code> 以及和模式相关的代码块。如果模式匹配,该相关代码块将被展开。鉴于这个宏只有一个模式,那就只有一个有效匹配方式,其他任何模式方向(译者注:不匹配这个模式)都会导致错误。更复杂的宏会有不止一个分支。</p>
<p>宏定义中有效模式语法和在第十九章提及的模式语法是不同的,因为宏模式所匹配的是 Rust 代码结构而不是值。回过头来检查下示例 20-29 中模式片段什么意思。有关完整的宏模式语法,请查阅 <a href="https://doc.rust-lang.org/reference/macros-by-example.html">Rust 参考</a></p>
<p>首先,一对括号包含了整个模式。我们使用美元符号(<code>$</code>)在宏系统中声明一个变量来包含匹配该模式的 Rust 代码。美元符号明确表明这是一个宏变量而不是普通 Rust 变量。之后是一对括号,其捕获了符合括号内模式的值用以在替代代码中使用。<code>$()</code> 内则是 <code>$x:expr</code> ,其匹配 Rust 的任意表达式,并将该表达式命名为 <code>$x</code></p>
<p><code>$()</code> 之后的逗号表示在每个与 <code>$()</code> 内代码匹配的实例之间必须出现一个字面量逗号分隔符。紧随逗号之后的 <code>*</code> 说明该模式匹配零个或更多个 <code>*</code> 之前的任何模式。</p>
<p>当以 <code>vec![1, 2, 3];</code> 调用宏时,<code>$x</code> 模式与三个表达式 <code>1</code><code>2</code><code>3</code> 对应进行了三次匹配。</p>
<p>现在让我们来看看与此分支模式相关联的代码块中的模式:在 <code>$()*</code> 部分,<code>temp_vec.push($x)</code> 会针对模式中每次匹配到 <code>$()</code> 的部分,生成零次或多次,取决于模式匹配到多少次。<code>$x</code> 由每个与之相匹配的表达式所替换。当以 <code>vec![1, 2, 3];</code> 调用该宏时,替换该宏调用所生成的代码会是下面这样:</p>
<pre><code class="language-rust ignore">{
let mut temp_vec = Vec::new();
temp_vec.push(1);
temp_vec.push(2);
temp_vec.push(3);
temp_vec
}</code></pre>
<p>我们已经定义了一个宏,其可以接收任意数量和类型的参数,同时可以生成能够创建包含指定元素的 vector 的代码。</p>
<p>要了解更多关于如何编写宏的信息,请查阅在线文档或其他资源,如由 Daniel Keep 发起、Lukas Wirth 继续维护的 <a href="https://veykril.github.io/tlborm/">“The Little Book of Rust Macros”</a></p>
<h3 id="用于从属性生成代码的过程宏"><a class="header" href="#用于从属性生成代码的过程宏">用于从属性生成代码的过程宏</a></h3>
<p>第二种形式的宏被称为 <strong>过程宏</strong><em>procedural macros</em>),因为它们更像函数(一种类型的过程)。过程宏接收 Rust 代码作为输入在这些代码上进行操作然后产生另一些代码作为输出而非像声明式宏那样匹配对应模式然后以另一部分代码替换当前代码。有三种类型的过程宏自定义派生derive类属性和类函数它们的工作原理都类似。</p>
<p>创建过程宏时,其定义必须驻留在它们自己的具有特殊 crate 类型的 crate 中。这么做出于一些复杂的技术原因,将来我们希望能够消除这些限制。在示例 20-36 中展示了如何定义过程宏,其中 <code>some_attribute</code> 是一个使用特定宏变体的占位符。</p>
<p><span class="filename">文件名src/lib.rs</span></p>
<pre><code class="language-rust ignore">use proc_macro;
#[some_attribute]
pub fn some_name(input: TokenStream) -&gt; TokenStream {
}</code></pre>
<p><span class="caption">示例 20-36: 一个定义过程宏的例子</span></p>
<p>定义过程宏的函数接收一个 <code>TokenStream</code> 作为输入并生成 <code>TokenStream</code> 作为输出。<code>TokenStream</code> 是定义于 <code>proc_macro</code> crate 里代表一系列 token 的类型Rust 默认携带了<code>proc_macro</code> crate。这就是宏的核心宏所处理的源代码组成了输入 <code>TokenStream</code>,宏生成的代码是输出 <code>TokenStream</code>。函数上还有一个属性;这个属性指明了我们创建的过程宏的类型。在同一 crate 中可以有多种的过程宏。</p>
<p>让我们看看不同种类的程序宏。我们将从一个自定义的派生宏开始,然后解释使其他形式不同的小差异。</p>
<h3 id="如何编写自定义-derive-宏"><a class="header" href="#如何编写自定义-derive-宏">如何编写自定义 <code>derive</code></a></h3>
<p>让我们创建一个 <code>hello_macro</code> crate其包含名为 <code>HelloMacro</code> 的 trait 和关联函数 <code>hello_macro</code>。不同于让用户为其每一个类型实现 <code>HelloMacro</code> trait我们将会提供一个过程式宏以便用户可以使用 <code>#[derive(HelloMacro)]</code> 注解它们的类型来得到 <code>hello_macro</code> 函数的默认实现。该默认实现会打印 <code>Hello, Macro! My name is TypeName!</code>,其中 <code>TypeName</code> 为定义了 trait 的类型名。换言之,我们会创建一个 crate使程序员能够写类似示例 20-37 中的代码。</p>
<p><span class="filename">文件名src/main.rs</span></p>
<pre><code class="language-rust ignore does_not_compile">use hello_macro::HelloMacro;
use hello_macro_derive::HelloMacro;
#[derive(HelloMacro)]
struct Pancakes;
fn main() {
Pancakes::hello_macro();
}</code></pre>
<p><span class="caption">示例 20-37: 我们 crate 的用户所写的能够使用过程式宏的代码</span></p>
<p>运行该代码将会打印 <code>Hello, Macro! My name is Pancakes!</code> 第一步是像下面这样新建一个库 crate</p>
<pre><code class="language-console">$ cargo new hello_macro --lib
</code></pre>
<p>接下来,会定义 <code>HelloMacro</code> trait 以及其关联函数:</p>
<p><span class="filename">文件名src/lib.rs</span></p>
<pre><code class="language-rust noplayground">pub trait HelloMacro {
fn hello_macro();
}</code></pre>
<p><span class="caption">示例 20-38: 一个我们会用于 <code>derive</code> 宏的简单 trait</span></p>
<p>现在有了一个 trait 及其相关函数。此时crate 用户可以像示例 20-39 那样实现该 trait 来达到期望的功能,像这样:</p>
<p><span class="filename">文件名src/main.rs</span></p>
<pre><code class="language-rust ignore">use hello_macro::HelloMacro;
struct Pancakes;
impl HelloMacro for Pancakes {
fn hello_macro() {
println!("Hello, Macro! My name is Pancakes!");
}
}
fn main() {
Pancakes::hello_macro();
}</code></pre>
<p><span class="caption">示例 20-39: 如果用户手动编写了一个 <code>HelloMacro</code> trait 实现看起来如何</span></p>
<p>然而,他们需要为每一个想要与 <code>hello_macro</code> 一同使用的类型编写实现的代码块。我们希望免去他们的这份工作。</p>
<p>另外,我们也无法为 <code>hello_macro</code> 函数提供一个能够打印实现了该 trait 的类型的名字的默认实现Rust 没有反射能力,因此其无法在运行时获取类型名。我们需要一个在编译时生成代码的宏。</p>
<p>下一步是定义过程宏。在编写本部分时,过程式宏必须在其自己的 crate 内。该限制最终可能被取消。crate 及其宏 crate 的结构惯例如下:对于一个名为 <code>foo</code> 的 crate其自定义 derive 过程宏 crate 通常命名为 <code>foo_derive</code>。让我们在 <code>hello_macro</code> 项目中,新建一个名为 <code>hello_macro_derive</code> 的 crate。</p>
<pre><code class="language-console">$ cargo new hello_macro_derive --lib
</code></pre>
<p>由于两个 crate 紧密相关,因此在 <code>hello_macro</code> 包的目录下创建过程式宏的 crate。如果改变在 <code>hello_macro</code> 中定义的 trait同时也必须改变在 <code>hello_macro_derive</code> 中过程宏的实现。这两个包需要分别发布,编程人员如果使用这些包,则需要同时添加这两个依赖并将其引入作用域。我们也可以只用 <code>hello_macro</code> 包而将 <code>hello_macro_derive</code> 作为一个依赖,并重导出过程式宏的代码。但现在我们组织项目的方式使编程人员在无需 <code>derive</code> 功能时也能够单独使用 <code>hello_macro</code></p>
<p>我们需要声明 <code>hello_macro_derive</code> crate 为过程宏 (proc-macro) crate。我们还需要 <code>syn</code><code>quote</code> crate 中的功能,正如你即将看到的,需要将它们加到依赖中。将下面的代码加入到 <code>hello_macro_derive</code><em>Cargo.toml</em> 文件中。</p>
<p><span class="filename">文件名hello_macro_derive/Cargo.toml</span></p>
<pre><code class="language-toml">[lib]
proc-macro = true
[dependencies]
syn = "2.0"
quote = "1.0"
</code></pre>
<p>为定义一个过程式宏,请将示例 20-40 中的代码放在 <code>hello_macro_derive</code> crate 的 <em>src/lib.rs</em> 文件里面。注意这段代码在我们添加 <code>impl_hello_macro</code> 函数的定义之前是无法编译的。</p>
<p><span class="filename">文件名hello_macro_derive/src/lib.rs</span></p>
<pre><code class="language-rust ignore does_not_compile">use proc_macro::TokenStream;
use quote::quote;
#[proc_macro_derive(HelloMacro)]
pub fn hello_macro_derive(input: TokenStream) -&gt; TokenStream {
// 将 Rust 代码构建成我们可以操作的语法树。
let ast = syn::parse(input).unwrap();
// 生成 trait 的实现。
impl_hello_macro(&amp;ast)
}</code></pre>
<p><span class="caption">示例 20-40: 大多数过程式宏处理 Rust 代码时所需的代码</span></p>
<p>注意我们将代码分成了 <code>hello_macro_derive</code><code>impl_hello_macro</code> 两个函数,前者负责解析 <code>TokenStream</code>,后者负责转换语法树:这使得编写过程宏更加方便。几乎你看到或者创建的每一个过程宏的外部函数(这里是 <code>hello_macro_derive</code>)中的代码都跟这里是一样的。你放入内部函数(这里是 <code>impl_hello_macro</code>)中的代码根据你的过程宏的设计目的会有所不同。</p>
<p>现在,我们已经引入了三个新的 crate<code>proc_macro</code><a href="https://crates.io/crates/syn"><code>syn</code></a><a href="https://crates.io/crates/quote"><code>quote</code></a> 。Rust 自带 <code>proc_macro</code> crate因此无需将其加到 <em>Cargo.toml</em> 文件的依赖中。<code>proc_macro</code> crate 是编译器提供用来读取和操作我们 Rust 代码的 API。</p>
<p><code>syn</code> crate 将字符串中的 Rust 代码解析成为一个可以操作的数据结构。<code>quote</code> crate 则将 <code>syn</code> 解析的数据结构转换回 Rust 代码。这些 crate 让解析任何我们所要处理的 Rust 代码变得更加简单:为 Rust 编写完整的解析器并不是一件简单的工作。</p>
<p>当用户在一个类型上指定 <code>#[derive(HelloMacro)]</code> 时,<code>hello_macro_derive</code> 函数将会被调用。我们已使用 <code>proc_macro_derive</code> 注解该函数并指定名称 <code>HelloMacro</code>,该名称与我们的 trait 名称相匹配;这是大多数过程宏遵循的惯例。</p>
<p>该函数首先将来自 <code>TokenStream</code><code>input</code> 转换为一个我们可以解释和操作的数据结构。这正是 <code>syn</code> 派上用场的地方。<code>syn</code> 中的 <code>parse</code> 函数获取一个 <code>TokenStream</code> 并返回一个表示解析出的 Rust 代码的 <code>DeriveInput</code> 结构体。示例 20-41 展示了从字符串 <code>struct Pancakes;</code> 中解析出来的 <code>DeriveInput</code> 结构体的相关部分:</p>
<pre><code class="language-rust ignore">DeriveInput {
// --snip--
ident: Ident {
ident: "Pancakes",
span: #0 bytes(95..103)
},
data: Struct(
DataStruct {
struct_token: Struct,
fields: Unit,
semi_token: Some(
Semi
)
}
)
}</code></pre>
<p><span class="caption">示例 20-41: 解析示例 20-37 中带有宏属性的代码时得到的 <code>DeriveInput</code> 实例</span></p>
<p>该结构体的字段展示了我们解析的 Rust 代码是一个类单元结构体,其 <code>ident</code>identifier表示名字<code>Pancakes</code>。该结构体里面有更多字段描述了所有类型的 Rust 代码,查阅 <a href="https://docs.rs/syn/1.0/syn/struct.DeriveInput.html"><code>syn</code><code>DeriveInput</code> 的文档</a> 以获取更多信息。</p>
<p>很快我们将定义 <code>impl_hello_macro</code> 函数,其用于构建所要包含在内的 Rust 新代码。但在此之前,注意其输出也是 <code>TokenStream</code>。所返回的 <code>TokenStream</code> 会被加到我们的 crate 用户所写的代码中,因此,当用户编译他们的 crate 时,他们会通过修改后的 <code>TokenStream</code> 获取到我们所提供的额外功能。</p>
<p>你可能也注意到了,当调用 <code>syn::parse</code> 函数失败时,我们用 <code>unwrap</code> 来使 <code>hello_macro_derive</code> 函数 panic。在错误时 panic 对过程宏来说是必须的,因为 <code>proc_macro_derive</code> 函数必须返回 <code>TokenStream</code> 而不是 <code>Result</code>,以此来符合过程宏的 API。这里选择用 <code>unwrap</code> 来简化了这个例子;在生产代码中,则应该通过 <code>panic!</code><code>expect</code> 来提供关于发生何种错误的更加明确的错误信息。</p>
<p>现在我们有了将注解的 Rust 代码从 <code>TokenStream</code> 转换为 <code>DeriveInput</code> 实例的代码,让我们来创建在注解类型上实现 <code>HelloMacro</code> trait 的代码,如示例 20-42 所示。</p>
<p><span class="filename">文件名hello_macro_derive/src/lib.rs</span></p>
<pre><code class="language-rust ignore"><span class="boring">use proc_macro::TokenStream;
</span><span class="boring">use quote::quote;
</span><span class="boring">
</span><span class="boring">#[proc_macro_derive(HelloMacro)]
</span><span class="boring">pub fn hello_macro_derive(input: TokenStream) -&gt; TokenStream {
</span><span class="boring"> // Construct a representation of Rust code as a syntax tree
</span><span class="boring"> // that we can manipulate
</span><span class="boring"> let ast = syn::parse(input).unwrap();
</span><span class="boring">
</span><span class="boring"> // Build the trait implementation
</span><span class="boring"> impl_hello_macro(&amp;ast)
</span><span class="boring">}
</span><span class="boring">
</span>fn impl_hello_macro(ast: &amp;syn::DeriveInput) -&gt; TokenStream {
let name = &amp;ast.ident;
let generated = quote! {
impl HelloMacro for #name {
fn hello_macro() {
println!("Hello, Macro! My name is {}!", stringify!(#name));
}
}
};
generated.into()
}</code></pre>
<p><span class="caption">示例 20-42: 使用解析过的 Rust 代码实现 <code>HelloMacro</code> trait</span></p>
<p>我们得到一个包含以 <code>ast.ident</code> 作为注解类型名字(标识符)的 <code>Ident</code> 结构体实例。示例 20-33 中的结构体表明当 <code>impl_hello_macro</code> 函数运行于示例 20-31 中的代码上时 <code>ident</code> 字段的值是 <code>"Pancakes"</code>。因此,示例 20-34 中 <code>name</code> 变量会包含一个 <code>Ident</code> 结构体的实例,当打印时,会是字符串 <code>"Pancakes"</code>,也就是示例 20-37 中结构体的名称。</p>
<p><code>quote!</code> 宏能让我们编写希望返回的 Rust 代码。<code>quote!</code> 宏执行的直接结果并不是编译器所期望的所以需要转换为 <code>TokenStream</code>。为此需要调用 <code>into</code> 方法它会消费这个中间表示intermediate representationIR并返回所需的 <code>TokenStream</code> 类型值。</p>
<p>这个宏也提供了一些非常酷的模板机制;我们可以写 <code>#name</code> ,然后 <code>quote!</code> 会以名为 <code>name</code> 的变量值来替换它。你甚至可以做一些类似常用宏那样的重复代码的工作。查阅 <a href="https://docs.rs/quote"><code>quote</code> crate 的文档</a> 来获取完整的介绍。</p>
<p>我们期望我们的过程式宏能够为通过 <code>#name</code> 获取到的用户注解类型生成 <code>HelloMacro</code> trait 的实现。该 trait 的实现有一个函数 <code>hello_macro</code> ,其函数体包括了我们期望提供的功能:打印 <code>Hello, Macro! My name is</code> 和注解的类型名。</p>
<p>此处所使用的 <code>stringify!</code> 为 Rust 内置宏。其接收一个 Rust 表达式,如 <code>1 + 2</code> ,然后在编译时将表达式转换为一个字符串常量,如 <code>"1 + 2"</code> 。这与计算表达式并接着将结果转换为 <code>String</code><code>format!</code><code>println!</code> 不同。有一种可能的情况是,所输入的 <code>#name</code> 可能是一个需要打印的表达式,因此我们用 <code>stringify!</code><code>stringify!</code> 也能通过在编译时将 <code>#name</code> 转换为字符串字面值来节省一次内存分配。</p>
<p>此时,<code>cargo build</code> 应该都能成功编译 <code>hello_macro</code><code>hello_macro_derive</code> 。我们将这些 crate 连接到示例 20-31 的代码中来看看过程宏的行为!在 <em>projects</em> 目录下用 <code>cargo new pancakes</code> 命令新建一个二进制项目。需要将 <code>hello_macro</code><code>hello_macro_derive</code> 作为依赖加到 <code>pancakes</code> 包的 <em>Cargo.toml</em> 文件中去。如果你正将 <code>hello_macro</code><code>hello_macro_derive</code> 的版本发布到 <a href="https://crates.io/">crates.io</a> 上,它们将是常规依赖;否则,则可以像下面这样将其指定为 <code>path</code> 依赖:</p>
<pre><code class="language-toml">hello_macro = { path = "../hello_macro" }
hello_macro_derive = { path = "../hello_macro/hello_macro_derive" }
</code></pre>
<p>把示例 20-37 中的代码放在 <em>src/main.rs</em> ,然后执行 <code>cargo run</code>:其应该打印 <code>Hello, Macro! My name is Pancakes!</code>。其包含了该过程宏中 <code>HelloMacro</code> trait 的实现,而无需 <code>pancakes</code> crate 实现它;<code>#[derive(HelloMacro)]</code> 增加了该 trait 实现。</p>
<p>接下来,让我们探索一下其他类型的过程宏与自定义 <code>derive</code> 宏有何区别。</p>
<h3 id="类属性宏"><a class="header" href="#类属性宏">类属性宏</a></h3>
<p>类属性Attribute-Like宏与自定义 <code>derive</code> 宏相似,不同之处在于它们不是为 <code>derive</code> 属性生成代码,而是允许你创建新的属性。它们也更为灵活;<code>derive</code> 只能用于结构体和枚举;属性还可以用于其它的项,比如函数。作为一个使用类属性宏的例子,可以创建一个名为 <code>route</code> 的属性用于注解 web 应用程序框架web application framework的函数</p>
<pre><code class="language-rust ignore">#[route(GET, "/")]
fn index() {</code></pre>
<p><code>#[route]</code> 属性将由框架本身定义为一个过程宏。其宏定义的函数签名看起来像这样:</p>
<pre><code class="language-rust ignore">#[proc_macro_attribute]
pub fn route(attr: TokenStream, item: TokenStream) -&gt; TokenStream {</code></pre>
<p>这里有两个 <code>TokenStream</code> 类型的参数;第一个用于属性内容本身,也就是 <code>GET, "/"</code> 部分。第二个是属性所标记的项:在本例中,是 <code>fn index() {}</code> 和剩下的函数体。</p>
<p>除此之外,类属性宏与自定义派生宏工作方式一致:创建 <code>proc-macro</code> crate 类型的 crate 并实现生成所希望的代码的函数!</p>
<h3 id="类函数宏"><a class="header" href="#类函数宏">类函数宏</a></h3>
<p>类函数Function-like宏的定义看起来像函数调用的宏。类似于 <code>macro_rules!</code>,它们比函数更灵活;例如,可以接受未知数量的参数。然而 <code>macro_rules!</code> 宏只能使用之前 <a href="#使用-macro_rules-的声明宏用于通用元编程">“使用 <code>macro_rules!</code> 的声明宏用于通用元编程”</a> 介绍的类匹配的语法定义。类函数宏获取 <code>TokenStream</code> 参数,其定义使用 Rust 代码操纵 <code>TokenStream</code>,就像另两种过程宏一样。一个类函数宏例子是可以像这样被调用的 <code>sql!</code> 宏:</p>
<pre><code class="language-rust ignore">let sql = sql!(SELECT * FROM posts WHERE id=1);</code></pre>
<p>这个宏会解析其中的 SQL 语句并检查其是否是句法正确的,这是比 <code>macro_rules!</code> 可以做到的更为复杂的处理。<code>sql!</code> 宏会被类似于像这样定义:</p>
<pre><code class="language-rust ignore">#[proc_macro]
pub fn sql(input: TokenStream) -&gt; TokenStream {</code></pre>
<p>这类似于自定义 <code>derive</code> 宏的签名:获取括号中的 token并返回希望生成的代码。</p>
<h2 id="总结"><a class="header" href="#总结">总结</a></h2>
<p>呼!现在你的工具箱中有了一些 Rust 特性,虽然你可能不会经常使用它们,但在非常特定的情况下你会知道它们可用。我们介绍了几个复杂的主题,以便当你在错误信息建议或他人代码中遇到它们时,能够识别这些概念和语法。本章可作为查找解决方案的参考。</p>
<p>接下来,我们将再开始一个项目,将本书所学的所有内容付诸实践!</p>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="ch20-04-advanced-functions-and-closures.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 246.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z"/></svg></span>
</a>
<a rel="next prefetch" href="ch21-00-final-project-a-web-server.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z"/></svg></span>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="ch20-04-advanced-functions-and-closures.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 246.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z"/></svg></span>
</a>
<a rel="next prefetch" href="ch21-00-final-project-a-web-server.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z"/></svg></span>
</a>
</nav>
</div>
<template id=fa-eye><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M288 32c-80.8 0-145.5 36.8-192.6 80.6C48.6 156 17.3 208 2.5 243.7c-3.3 7.9-3.3 16.7 0 24.6C17.3 304 48.6 356 95.4 399.4C142.5 443.2 207.2 480 288 480s145.5-36.8 192.6-80.6c46.8-43.5 78.1-95.4 93-131.1c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C433.5 68.8 368.8 32 288 32zM432 256c0 79.5-64.5 144-144 144s-144-64.5-144-144s64.5-144 144-144s144 64.5 144 144zM288 192c0 35.3-28.7 64-64 64c-11.5 0-22.3-3-31.6-8.4c-.2 2.8-.4 5.5-.4 8.4c0 53 43 96 96 96s96-43 96-96s-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6z"/></svg></span></template>
<template id=fa-eye-slash><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M38.8 5.1C28.4-3.1 13.3-1.2 5.1 9.2S-1.2 34.7 9.2 42.9l592 464c10.4 8.2 25.5 6.3 33.7-4.1s6.3-25.5-4.1-33.7L525.6 386.7c39.6-40.6 66.4-86.1 79.9-118.4c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C465.5 68.8 400.8 32 320 32c-68.2 0-125 26.3-169.3 60.8L38.8 5.1zM223.1 149.5C248.6 126.2 282.7 112 320 112c79.5 0 144 64.5 144 144c0 24.9-6.3 48.3-17.4 68.7L408 294.5c5.2-11.8 8-24.8 8-38.5c0-53-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6c0 10.2-2.4 19.8-6.6 28.3l-90.3-70.8zm223.1 298L373 389.9c-16.4 6.5-34.3 10.1-53 10.1c-79.5 0-144-64.5-144-144c0-6.9 .5-13.6 1.4-20.2L83.1 161.5C60.3 191.2 44 220.8 34.5 243.7c-3.3 7.9-3.3 16.7 0 24.6c14.9 35.7 46.2 87.7 93 131.1C174.5 443.2 239.2 480 320 480c47.8 0 89.9-12.9 126.2-32.5z"/></svg></span></template>
<template id=fa-copy><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M502.6 70.63l-61.25-61.25C435.4 3.371 427.2 0 418.7 0H255.1c-35.35 0-64 28.66-64 64l.0195 256C192 355.4 220.7 384 256 384h192c35.2 0 64-28.8 64-64V93.25C512 84.77 508.6 76.63 502.6 70.63zM464 320c0 8.836-7.164 16-16 16H255.1c-8.838 0-16-7.164-16-16L239.1 64.13c0-8.836 7.164-16 16-16h128L384 96c0 17.67 14.33 32 32 32h47.1V320zM272 448c0 8.836-7.164 16-16 16H63.1c-8.838 0-16-7.164-16-16L47.98 192.1c0-8.836 7.164-16 16-16H160V128H63.99c-35.35 0-64 28.65-64 64l.0098 256C.002 483.3 28.66 512 64 512h192c35.2 0 64-28.8 64-64v-32h-47.1L272 448z"/></svg></span></template>
<template id=fa-play><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80V432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"/></svg></span></template>
<template id=fa-clock-rotate-left><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M75 75L41 41C25.9 25.9 0 36.6 0 57.9V168c0 13.3 10.7 24 24 24H134.1c21.4 0 32.1-25.9 17-41l-30.8-30.8C155 85.5 203 64 256 64c106 0 192 86 192 192s-86 192-192 192c-40.8 0-78.6-12.7-109.7-34.4c-14.5-10.1-34.4-6.6-44.6 7.9s-6.6 34.4 7.9 44.6C151.2 495 201.7 512 256 512c141.4 0 256-114.6 256-256S397.4 0 256 0C185.3 0 121.3 28.7 75 75zm181 53c-13.3 0-24 10.7-24 24V256c0 6.4 2.5 12.5 7 17l72 72c9.4 9.4 24.6 9.4 33.9 0s9.4-24.6 0-33.9l-65-65V152c0-13.3-10.7-24-24-24z"/></svg></span></template>
<script>
window.playground_copyable = true;
</script>
<script src="elasticlunr-ef4e11c1.min.js"></script>
<script src="mark-09e88c2c.min.js"></script>
<script src="searcher-c2a407aa.js"></script>
<script src="clipboard-1626706a.min.js"></script>
<script src="highlight-abc7f01d.js"></script>
<script src="book-a0b12cfe.js"></script>
<!-- Custom JS scripts -->
<script src="ferris-2317480c.js"></script>
</div>
</body>
</html>