<!DOCTYPE HTML>
< html lang = "zh-CN" class = "light sidebar-visible" dir = "ltr" >
< head >
<!-- Book generated using mdBook -->
< meta charset = "UTF-8" >
< title > 模块 Module - 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-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 = "../../theme/style-234bffbf.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-4dbab867.js";
< / script >
<!-- Start loading toc.js asap -->
< script src = "../../toc-42887675.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语言圣经(Rust Course)< / 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/sunface/rust-course" 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 >
< a href = "https://github.com/sunface/rust-course/edit/main/src/basic/crate-module/module.md" title = "Suggest an edit" aria-label = "Suggest an edit" rel = "edit" >
< span class = fa-svg id = "git-edit-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 = "M421.7 220.3l-11.3 11.3-22.6 22.6-205 205c-6.6 6.6-14.8 11.5-23.8 14.1L30.8 511c-8.4 2.5-17.5 .2-23.7-6.1S-1.5 489.7 1 481.2L38.7 353.1c2.6-9 7.5-17.2 14.1-23.8l205-205 22.6-22.6 11.3-11.3 33.9 33.9 62.1 62.1 33.9 33.9zM96 353.9l-9.3 9.3c-.9 .9-1.6 2.1-2 3.4l-25.3 86 86-25.3c1.3-.4 2.5-1.1 3.4-2l9.3-9.3H112c-8.8 0-16-7.2-16-16V353.9zM453.3 19.3l39.4 39.4c25 25 25 65.5 0 90.5l-14.5 14.5-22.6 22.6-11.3 11.3-33.9-33.9-62.1-62.1L314.3 67.7l11.3-11.3 22.6-22.6 14.5-14.5c25-25 65.5-25 90.5 0z" / > < / 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 >
< h1 id = "模块-module" > < a class = "header" href = "#模块-module" > 模块 Module< / a > < / h1 >
< p > 在本章节,我们将深入讲讲 Rust 的代码构成单元:模块。使用模块可以将包中的代码按照功能性进行重组,最终实现更好的可读性及易用性。同时,我们还能非常灵活地去控制代码的可见性,进一步强化 Rust 的安全性。< / p >
< h2 id = "创建嵌套模块" > < a class = "header" href = "#创建嵌套模块" > 创建嵌套模块< / a > < / h2 >
< p > 小旅馆, sorry, 是小餐馆, 相信大家都挺熟悉的, 学校外的估计也没少去, 那么咱就用小餐馆为例, 来看看 Rust 的模块该如何使用。< / p >
< p > 使用 < code > cargo new --lib restaurant< / code > 创建一个小餐馆,注意,这里创建的是一个库类型的 < code > Package< / code > ,然后将以下代码放入 < code > src/lib.rs< / code > 中:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > // 餐厅前厅,用于吃饭
mod front_of_house {
mod hosting {
fn add_to_waitlist() {}
fn seat_at_table() {}
}
mod serving {
fn take_order() {}
fn serve_order() {}
fn take_payment() {}
}
}
< span class = "boring" > }< / span > < / code > < / pre >
< p > 以上的代码创建了三个模块,有几点需要注意的:< / p >
< ul >
< li > 使用 < code > mod< / code > 关键字来创建新模块,后面紧跟着模块名称< / li >
< li > 模块可以嵌套,这里嵌套的原因是招待客人和服务都发生在前厅,因此我们的代码模拟了真实场景< / li >
< li > 模块中可以定义各种 Rust 类型,例如函数、结构体、枚举、特征等< / li >
< li > 所有模块均定义在同一个文件中< / li >
< / ul >
< p > 类似上述代码中所做的,使用模块,我们就能将功能相关的代码组织到一起,然后通过一个模块名称来说明这些代码为何被组织在一起。这样其它程序员在使用你的模块时,就可以更快地理解和上手。< / p >
< h2 id = "模块树" > < a class = "header" href = "#模块树" > 模块树< / a > < / h2 >
< p > 在< a href = "https://course.rs/basic/crate-module/crate.html" > 上一节< / a > 中,我们提到过 < code > src/main.rs< / code > 和 < code > src/lib.rs< / code > 被称为包根(crate root),这个奇葩名称的来源(我不想承认是自己翻译水平太烂-,-)是由于这两个文件的内容形成了一个模块 < code > crate< / code > ,该模块位于包的树形结构(由模块组成的树形结构)的根部:< / p >
< pre > < code class = "language-console" > crate
└── front_of_house
├── hosting
│ ├── add_to_waitlist
│ └── seat_at_table
└── serving
├── take_order
├── serve_order
└── take_payment
< / code > < / pre >
< p > 这颗树展示了模块之间< strong > 彼此的嵌套< / strong > 关系,因此被称为< strong > 模块树< / strong > 。其中 < code > crate< / code > 包根是 < code > src/lib.rs< / code > 文件,包根文件中的三个模块分别形成了模块树的剩余部分。< / p >
< h4 id = "父子模块" > < a class = "header" href = "#父子模块" > 父子模块< / a > < / h4 >
< p > 如果模块 < code > A< / code > 包含模块 < code > B< / code > ,那么 < code > A< / code > 是 < code > B< / code > 的父模块,< code > B< / code > 是 < code > A< / code > 的子模块。在上例中,< code > front_of_house< / code > 是 < code > hosting< / code > 和 < code > serving< / code > 的父模块,反之,后两者是前者的子模块。< / p >
< p > 聪明的读者,应该能联想到,模块树跟计算机上文件系统目录树的相似之处。不仅仅是组织结构上的相似,就连使用方式都很相似:每个文件都有自己的路径,用户可以通过这些路径使用它们,在 Rust 中,我们也通过路径的方式来引用模块。< / p >
< h2 id = "用路径引用模块" > < a class = "header" href = "#用路径引用模块" > 用路径引用模块< / a > < / h2 >
< p > 想要调用一个函数,就需要知道它的路径,在 Rust 中,这种路径有两种形式:< / p >
< ul >
< li > < strong > 绝对路径< / strong > ,从包根开始,路径名以包名或者 < code > crate< / code > 作为开头< / li >
< li > < strong > 相对路径< / strong > ,从当前模块开始,以 < code > self< / code > , < code > super< / code > 或当前模块的标识符作为开头< / li >
< / ul >
< p > 让我们继续经营那个惨淡的小餐馆,这次为它实现一个小功能:
< span class = "filename" > 文件名: src/lib.rs< / span > < / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > mod front_of_house {
mod hosting {
fn add_to_waitlist() {}
}
}
pub fn eat_at_restaurant() {
// 绝对路径
crate::front_of_house::hosting::add_to_waitlist();
// 相对路径
front_of_house::hosting::add_to_waitlist();
}
< span class = "boring" > }< / span > < / code > < / pre >
< p > 上面的代码为了简化实现,省去了其余模块和函数,这样可以把关注点放在函数调用上。< code > eat_at_restaurant< / code > 是一个定义在包根中的函数,在该函数中使用了两种方式对 < code > add_to_waitlist< / code > 进行调用。< / p >
< h4 id = "绝对路径引用" > < a class = "header" href = "#绝对路径引用" > 绝对路径引用< / a > < / h4 >
< p > 因为 < code > eat_at_restaurant< / code > 和 < code > add_to_waitlist< / code > 都定义在一个包中,因此在绝对路径引用时,可以直接以 < code > crate< / code > 开头,然后逐层引用,每一层之间使用 < code > ::< / code > 分隔:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > crate::front_of_house::hosting::add_to_waitlist();
< span class = "boring" > }< / span > < / code > < / pre >
< p > 对比下之前的模块树:< / p >
< pre > < code class = "language-console" > crate
└── eat_at_restaurant
└── front_of_house
├── hosting
│ ├── add_to_waitlist
│ └── seat_at_table
└── serving
├── take_order
├── serve_order
└── take_payment
< / code > < / pre >
< p > 可以看出,绝对路径的调用,完全符合了模块树的层级递进,非常符合直觉,如果类比文件系统,就跟使用绝对路径调用可执行程序差不多:< code > /front_of_house/hosting/add_to_waitlist< / code > ,使用 < code > crate< / code > 作为开始就和使用 < code > /< / code > 作为开始一样。< / p >
< h4 id = "相对路径引用" > < a class = "header" href = "#相对路径引用" > 相对路径引用< / a > < / h4 >
< p > 再回到模块树中,因为 < code > eat_at_restaurant< / code > 和 < code > front_of_house< / code > 都处于包根 < code > crate< / code > 中,因此相对路径可以使用 < code > front_of_house< / code > 作为开头:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > front_of_house::hosting::add_to_waitlist();
< span class = "boring" > }< / span > < / code > < / pre >
< p > 如果类比文件系统,那么它类似于调用同一个目录下的程序,你可以这么做:< code > front_of_house/hosting/add_to_waitlist< / code > ,嗯也很符合直觉。< / p >
< h4 id = "绝对还是相对" > < a class = "header" href = "#绝对还是相对" > 绝对还是相对?< / a > < / h4 >
< p > 如果只是为了引用到指定模块中的对象,那么两种都可以,但是在实际使用时,需要遵循一个原则:< strong > 当代码被挪动位置时,尽量减少引用路径的修改< / strong > ,相信大家都遇到过,修改了某处代码,导致所有路径都要挨个替换,这显然不是好的路径选择。< / p >
< p > 回到之前的例子,假设要把 < code > front_of_house< / code > 模块和 < code > eat_at_restaurant< / code > 移动到一个模块中 < code > customer_experience< / code > ,那么绝对路径的引用方式就必须进行修改:< code > crate::customer_experience::front_of_house ...< / code > ,但如果使用的是相对路径,那该路径就无需修改,因为它们的相对位置其实没有变:< / p >
< pre > < code class = "language-console" > crate
└── customer_experience
└── eat_at_restaurant
└── front_of_house
├── hosting
│ ├── add_to_waitlist
│ └── seat_at_table
< / code > < / pre >
< p > 从新的模块树中可以很清晰的看出这一点。< / p >
< p > 再比如,其它的都不动,把 < code > eat_at_restaurant< / code > 移动到模块 < code > dining< / code > 中,如果使用相对路径,你需要修改该路径,但如果使用的是绝对路径,就无需修改:< / p >
< pre > < code class = "language-console" > crate
└── dining
└── eat_at_restaurant
└── front_of_house
├── hosting
│ ├── add_to_waitlist
< / code > < / pre >
< p > 不过,如果不确定哪个好,你可以考虑优先使用绝对路径,因为调用的地方和定义的地方往往是分离的,而定义的地方较少会变动。< / p >
< h2 id = "代码可见性" > < a class = "header" href = "#代码可见性" > 代码可见性< / a > < / h2 >
< p > 让我们运行下面(之前)的代码:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > mod front_of_house {
mod hosting {
fn add_to_waitlist() {}
}
}
pub fn eat_at_restaurant() {
// 绝对路径
crate::front_of_house::hosting::add_to_waitlist();
// 相对路径
front_of_house::hosting::add_to_waitlist();
}
< span class = "boring" > }< / span > < / code > < / pre >
< p > 运行 < code > cargo build< / code > 编译此库类型的 < code > Package< / code > ,意料之外的报错了,毕竟看上去确实很简单且没有任何问题:< / p >
< pre > < code class = "language-console" > error[E0603]: module `hosting` is private
--> src/lib.rs:9:28
|
9 | crate::front_of_house::hosting::add_to_waitlist();
| ^^^^^^^ private module
< / code > < / pre >
< p > 错误信息很清晰:< code > hosting< / code > 模块是私有的,无法在包根进行访问,那么为何 < code > front_of_house< / code > 模块就可以访问?因为它和 < code > eat_at_restaurant< / code > 同属于一个包根作用域内,同一个模块内的代码自然不存在私有化问题(所以我们之前章节的代码都没有报过这个错误!)。< / p >
< p > 模块不仅仅对于组织代码很有用,它还能定义代码的私有化边界:在这个边界内,什么内容能让外界看到,什么内容不能,都有很明确的定义。因此,如果希望让函数或者结构体等类型变成私有化的,可以使用模块。< / p >
< p > Rust 出于安全的考虑,默认情况下,所有的类型都是私有化的,包括函数、方法、结构体、枚举、常量,是的,就连模块本身也是私有化的。在中国,父亲往往不希望孩子拥有小秘密,但是在 Rust 中,< strong > 父模块完全无法访问子模块中的私有项,但是子模块却可以访问父模块、父父..模块的私有项< / strong > 。< / p >
< h4 id = "pub-关键字" > < a class = "header" href = "#pub-关键字" > pub 关键字< / a > < / h4 >
< p > 类似其它语言的 < code > public< / code > 或者 Go 语言中的首字母大写, Rust 提供了 < code > pub< / code > 关键字,通过它你可以控制模块和模块中指定项的可见性。< / p >
< p > 由于之前的解释,我们知道了只需要将 < code > hosting< / code > 模块标记为对外可见即可:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > mod front_of_house {
pub mod hosting {
fn add_to_waitlist() {}
}
}
/*--- snip ----*/
< span class = "boring" > }< / span > < / code > < / pre >
< p > 但是不幸的是,又报错了:< / p >
< pre > < code class = "language-console" > error[E0603]: function `add_to_waitlist` is private
--> src/lib.rs:12:30
|
12 | front_of_house::hosting::add_to_waitlist();
| ^^^^^^^^^^^^^^^ private function
< / code > < / pre >
< p > 哦?难道模块可见还不够,还需要将函数 < code > add_to_waitlist< / code > 标记为可见的吗? 是的,没错,模块可见性不代表模块内部项的可见性,模块的可见性仅仅是允许其它模块去引用它,但是想要引用它内部的项,还得继续将对应的项标记为 < code > pub< / code > 。< / p >
< p > 在实际项目中,一个模块需要对外暴露的数据和 API 往往就寥寥数个,如果将模块标记为可见代表着内部项也全部对外可见,那你是不是还得把那些不可见的,一个一个标记为 < code > private< / code > ?反而是更麻烦的多。< / p >
< p > 既然知道了如何解决,那么我们为函数也标记上 < code > pub< / code > : < / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
/*--- snip ----*/
< span class = "boring" > }< / span > < / code > < / pre >
< p > Bang, 顺利通过编译, 感觉自己又变强了。< / p >
< h2 id = "使用-super-引用模块" > < a class = "header" href = "#使用-super-引用模块" > 使用 < code > super< / code > 引用模块< / a > < / h2 >
< p > 在< a href = "#用路径引用模块" > 用路径引用模块< / a > 中,我们提到了相对路径有三种方式开始:< code > self< / code > 、< code > super< / code > 和 < code > crate< / code > 或者模块名,其中第三种在前面已经讲到过,现在来看看通过 < code > super< / code > 的方式引用模块项。< / p >
< p > < code > super< / code > 代表的是父模块为开始的引用方式,非常类似于文件系统中的 < code > ..< / code > 语法:< code > ../a/b< / code >
< span class = "filename" > 文件名: src/lib.rs< / span > < / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > fn serve_order() {}
// 厨房模块
mod back_of_house {
fn fix_incorrect_order() {
cook_order();
super::serve_order();
}
fn cook_order() {}
}
< span class = "boring" > }< / span > < / code > < / pre >
< p > 嗯,我们的小餐馆又完善了,终于有厨房了!看来第一个客人也快可以有了。。。在厨房模块中,使用 < code > super::serve_order< / code > 语法,调用了父模块(包根)中的 < code > serve_order< / code > 函数。< / p >
< p > 那么你可能会问,为何不使用 < code > crate::serve_order< / code > 的方式?额,其实也可以,不过如果你确定未来这种层级关系不会改变,那么 < code > super::serve_order< / code > 的方式会更稳定,未来就算它们都不在包根了,依然无需修改引用路径。所以路径的选用,往往还是取决于场景,以及未来代码的可能走向。< / p >
< h2 id = "使用-self-引用模块" > < a class = "header" href = "#使用-self-引用模块" > 使用 < code > self< / code > 引用模块< / a > < / h2 >
< p > < code > self< / code > 其实就是引用自身模块中的项,也就是说和我们之前章节的代码类似,都调用同一模块中的内容,区别在于之前章节中直接通过名称调用即可,而 < code > self< / code > ,你得多此一举:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > fn serve_order() {
self::back_of_house::cook_order()
}
mod back_of_house {
fn fix_incorrect_order() {
cook_order();
crate::serve_order();
}
pub fn cook_order() {}
}
< span class = "boring" > }< / span > < / code > < / pre >
< p > 是的,多此一举,因为完全可以直接调用 < code > back_of_house< / code > ,但是 < code > self< / code > 还有一个大用处,在下一节中我们会讲。< / p >
< h2 id = "结构体和枚举的可见性" > < a class = "header" href = "#结构体和枚举的可见性" > 结构体和枚举的可见性< / a > < / h2 >
< p > 为何要把结构体和枚举的可见性单独拎出来讲呢?因为这两个家伙的成员字段拥有完全不同的可见性:< / p >
< ul >
< li > 将结构体设置为 < code > pub< / code > ,但它的所有字段依然是私有的< / li >
< li > 将枚举设置为 < code > pub< / code > ,它的所有字段也将对外可见< / li >
< / ul >
< p > 原因在于,枚举和结构体的使用方式不一样。如果枚举的成员对外不可见,那该枚举将一点用都没有,因此枚举成员的可见性自动跟枚举可见性保持一致,这样可以简化用户的使用。< / p >
< p > 而结构体的应用场景比较复杂,其中的字段也往往部分在 A 处被使用,部分在 B 处被使用,因此无法确定成员的可见性,那索性就设置为全部不可见,将选择权交给程序员。< / p >
< h2 id = "模块与文件分离" > < a class = "header" href = "#模块与文件分离" > 模块与文件分离< / a > < / h2 >
< p > 在之前的例子中,我们所有的模块都定义在 < code > src/lib.rs< / code > 中,但是当模块变多或者变大时,需要将模块放入一个单独的文件中,让代码更好维护。< / p >
< p > 现在,把 < code > front_of_house< / code > 前厅分离出来,放入一个单独的文件中 < code > src/front_of_house.rs< / code > : < / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > pub mod hosting {
pub fn add_to_waitlist() {}
}
< span class = "boring" > }< / span > < / code > < / pre >
< p > 然后,将以下代码留在 < code > src/lib.rs< / code > 中:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > mod front_of_house;
pub use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}
< span class = "boring" > }< / span > < / code > < / pre >
< p > so easy! 其实跟之前在同一个文件中也没有太大的不同, 但是有几点值得注意: < / p >
< ul >
< li > < code > mod front_of_house;< / code > 告诉 Rust 从另一个和模块 < code > front_of_house< / code > 同名的文件中加载该模块的内容< / li >
< li > 使用绝对路径的方式来引用 < code > hosting< / code > 模块:< code > crate::front_of_house::hosting;< / code > < / li >
< / ul >
< p > 需要注意的是,和之前代码中 < code > mod front_of_house{..}< / code > 的完整模块不同,现在的代码中,模块的声明和实现是分离的,实现是在单独的 < code > front_of_house.rs< / code > 文件中,然后通过 < code > mod front_of_house;< / code > 这条声明语句从该文件中把模块内容加载进来。因此我们可以认为,模块 < code > front_of_house< / code > 的定义还是在 < code > src/lib.rs< / code > 中,只不过模块的具体内容被移动到了 < code > src/front_of_house.rs< / code > 文件中。< / p >
< p > 在这里出现了一个新的关键字 < code > use< / code > ,联想到其它章节我们见过的标准库引入 < code > use std::fmt;< / code > ,可以大致猜测,该关键字用来将外部模块中的项引入到当前作用域中来,这样无需冗长的父模块前缀即可调用:< code > hosting::add_to_waitlist();< / code > ,在下节中,我们将对 < code > use< / code > 进行详细的讲解。< / p >
< p > 当一个模块有许多子模块时,我们也可以通过文件夹的方式来组织这些子模块。< / p >
< p > 在上述例子中,我们可以创建一个目录 < code > front_of_house< / code > ,然后在文件夹里创建一个 < code > hosting.rs< / code > 文件,< code > hosting.rs< / code > 文件现在就剩下:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > pub fn add_to_waitlist() {}
< span class = "boring" > }< / span > < / code > < / pre >
< p > 现在,我们尝试编译程序,很遗憾,编译器报错:< / p >
< pre > < code > error[E0583]: file not found for module `front_of_house`
--> src/lib.rs:3:1
|
1 | mod front_of_house;
| ^^^^^^^^^^^^^^^^^^
|
= help: to create the module `front_of_house`, create file "src/front_of_house.rs" or "src/front_of_house/mod.rs"
< / code > < / pre >
< p > 是的,如果需要将文件夹作为一个模块,我们需要进行显式指定暴露哪些子模块。按照上述的报错信息,我们有两种方法:< / p >
< ul >
< li > 在 < code > front_of_house< / code > 目录里创建一个 < code > mod.rs< / code > ,如果你使用的 < code > rustc< / code > 版本 < code > 1.30< / code > 之前,这是唯一的方法。< / li >
< li > 在 < code > front_of_house< / code > < strong > 同级< / strong > 目录里创建一个与模块(目录)< strong > 同名< / strong > 的 rs 文件 < code > front_of_house.rs< / code > ,在新版本里,更建议使用这样的命名方式来避免项目中存在大量同名的 < code > mod.rs< / code > 文件( Python 点了个 < code > 踩< / code > )。< / li >
< / ul >
< p > 如果使用第二种方式,文件结构将如下所示:< / p >
< pre > < code class = "language-shell" > src
├── front_of_house
│ └── hosting.rs
├── front_of_house.rs
└── lib.rs
< / code > < / pre >
< p > 而无论是上述哪个方式创建的文件,其内容都是一样的,你需要在定义你(< code > mod.rs< / code > 或 < code > front_of_house.rs< / code > )的子模块(子模块名与文件名相同):< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > pub mod hosting;
// pub mod serving;
< span class = "boring" > }< / span > < / code > < / pre >
< h2 id = "课后练习" > < a class = "header" href = "#课后练习" > 课后练习< / a > < / h2 >
< blockquote >
< p > < a href = "https://practice-zh.course.rs/crate-module/module.html" > Rust By Practice< / a > ,支持代码在线编辑和运行,并提供详细的< a href = "https://github.com/sunface/rust-by-practice/blob/master/solutions/crate-module/module.md" > 习题解答< / a > 。< / p >
< / blockquote >
< / main >
< nav class = "nav-wrapper" aria-label = "Page navigation" >
<!-- Mobile navigation buttons -->
< a rel = "prev" href = "../../basic/crate-module/crate.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 = "../../basic/crate-module/use.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 = "../../basic/crate-module/crate.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 = "../../basic/crate-module/use.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 = "../../ace-2a3cd908.js" > < / script >
< script src = "../../mode-rust-2c9d5c9a.js" > < / script >
< script src = "../../editor-16ca416c.js" > < / script >
< script src = "../../theme-dawn-4493f9c8.js" > < / script >
< script src = "../../theme-tomorrow_night-9dbe62a9.js" > < / 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 = "../../assets/custom-690c9de5.js" > < / script >
< script src = "../../assets/bigPicture-be03f9c8.js" > < / script >
< / div >
< / body >
< / html >