<!DOCTYPE HTML>
< html lang = "zh-CN" class = "light sidebar-visible" 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-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-78591068.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-4a1a74fa.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/ownership/ownership.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 = "所有权" > < a class = "header" href = "#所有权" > 所有权< / a > < / h1 >
< p > 所有的程序都必须和计算机内存打交道,如何从内存中申请空间来存放程序的运行内容,如何在不需要的时候释放这些空间,成了重中之重,也是所有编程语言设计的难点之一。在计算机语言不断演变过程中,出现了三种流派:< / p >
< ul >
< li > < strong > 垃圾回收机制(GC)< / strong > , 在程序运行时不断寻找不再使用的内存, 典型代表: Java、Go< / li >
< li > < strong > 手动管理内存的分配和释放< / strong > , 在程序中, 通过函数调用的方式来申请和释放内存, 典型代表: C++< / li >
< li > < strong > 通过所有权来管理内存< / strong > ,编译器在编译时会根据一系列规则进行检查< / li >
< / ul >
< p > 其中 Rust 选择了第三种,最妙的是,这种检查只发生在编译期,因此对于程序运行期,不会有任何性能上的损失。< / p >
< p > 由于所有权是一个新概念,因此读者需要花费一些时间来掌握它,一旦掌握,海阔天空任你飞跃,在本章,我们将通过 < code > 字符串< / code > 来引导讲解所有权的相关知识。< / p >
< h2 id = "一段不安全的代码" > < a class = "header" href = "#一段不安全的代码" > 一段不安全的代码< / a > < / h2 >
< p > 先来看看一段来自 C 语言的糟糕代码:< / p >
< pre > < code class = "language-c" > int* foo() {
int a; // 变量a的作用域开始
a = 100;
char *c = "xyz"; // 变量c的作用域开始
return & a;
} // 变量a和c的作用域结束
< / code > < / pre >
< p > 这段代码虽然可以编译通过,但是其实非常糟糕,变量 < code > a< / code > 和 < code > c< / code > 都是局部变量,函数结束后将局部变量 < code > a< / code > 的地址返回,但局部变量 < code > a< / code > 存在栈中,在离开作用域后,< code > a< / code > 所申请的栈上内存都会被系统回收,从而造成了 < code > 悬空指针(Dangling Pointer)< / code > 的问题。这是一个非常典型的内存安全问题,虽然编译可以通过,但是运行的时候会出现错误,很多编程语言都存在。< / p >
< p > 再来看变量 < code > c< / code > , < code > c< / code > 的值是常量字符串,存储于常量区,可能这个函数我们只调用了一次,也可能我们不再会使用这个字符串,但 < code > "xyz"< / code > 只有当整个程序结束后系统才能回收这片内存。< / p >
< p > 所以内存安全问题,一直都是程序员非常头疼的问题,好在,在 Rust 中这些问题即将成为历史,因为 Rust 在编译的时候就可以帮助我们发现内存不安全的问题,那 Rust 如何做到这一点呢?< / p >
< p > 在正式进入主题前,先来一个预热知识。< / p >
< h2 id = "栈stack与堆heap" > < a class = "header" href = "#栈stack与堆heap" > 栈(Stack)与堆(Heap)< / a > < / h2 >
< p > 栈和堆是编程语言最核心的数据结构,但是在很多语言中,你并不需要深入了解栈与堆。 但对于 Rust 这样的系统编程语言,值是位于栈上还是堆上非常重要,因为这会影响程序的行为和性能。< / p >
< p > 栈和堆的核心目标就是为程序在运行时提供可供使用的内存空间。< / p >
< h4 id = "栈" > < a class = "header" href = "#栈" > 栈< / a > < / h4 >
< p > 栈按照顺序存储值并以相反顺序取出值,这也被称作< strong > 后进先出< / strong > 。想象一下一叠盘子:当增加更多盘子时,把它们放在盘子堆的顶部,当需要盘子时,再从顶部拿走。不能从中间也不能从底部增加或拿走盘子!< / p >
< p > 增加数据叫做< strong > 进栈< / strong > ,移出数据则叫做< strong > 出栈< / strong > 。< / p >
< p > 因为上述的实现方式,栈中的所有数据都必须占用已知且固定大小的内存空间,假设数据大小是未知的,那么在取出数据时,你将无法取到你想要的数据。< / p >
< h4 id = "堆" > < a class = "header" href = "#堆" > 堆< / a > < / h4 >
< p > 与栈不同,对于大小未知或者可能变化的数据,我们需要将它存储在堆上。< / p >
< p > 当向堆上放入数据时,需要请求一定大小的内存空间。操作系统在堆的某处找到一块足够大的空位,把它标记为已使用,并返回一个表示该位置地址的< strong > 指针< / strong > ,该过程被称为< strong > 在堆上分配内存< / strong > ,有时简称为 “分配”(allocating)。< / p >
< p > 接着,该指针会被推入< strong > 栈< / strong > 中,因为指针的大小是已知且固定的,在后续使用过程中,你将通过栈中的< strong > 指针< / strong > ,来获取数据在堆上的实际内存位置,进而访问该数据。< / p >
< p > 由上可知,堆是一种缺乏组织的数据结构。想象一下去餐馆就座吃饭:进入餐馆,告知服务员有几个人,然后服务员找到一个够大的空桌子(堆上分配的内存空间)并领你们过去。如果有人来迟了,他们也可以通过桌号(栈上的指针)来找到你们坐在哪。< / p >
< h4 id = "性能区别" > < a class = "header" href = "#性能区别" > 性能区别< / a > < / h4 >
< p > 在栈上分配内存比在堆上分配内存要快,因为入栈时操作系统无需进行函数调用(或更慢的系统调用)来分配新的空间,只需要将新数据放入栈顶即可。相比之下,在堆上分配内存则需要更多的工作,这是因为操作系统必须首先找到一块足够存放数据的内存空间,接着做一些记录为下一次分配做准备,如果当前进程分配的内存页不足时,还需要进行系统调用来申请更多内存。
因此,处理器在栈上分配数据会比在堆上分配数据更加高效。< / p >
< h4 id = "所有权与堆栈" > < a class = "header" href = "#所有权与堆栈" > 所有权与堆栈< / a > < / h4 >
< p > 当你的代码调用一个函数时,传递给函数的参数(包括可能指向堆上数据的指针和函数的局部变量)依次被压入栈中,当函数调用结束时,这些值将被从栈中按照相反的顺序依次移除。< / p >
< p > 因为堆上的数据缺乏组织,因此跟踪这些数据何时分配和释放是非常重要的,否则堆上的数据将产生内存泄漏 —— 这些数据将永远无法被回收。这就是 Rust 所有权系统为我们提供的强大保障。< / p >
< p > 对于其他很多编程语言,你确实无需理解堆栈的原理,但是< strong > 在 Rust 中,明白堆栈的原理,对于我们理解所有权的工作原理会有很大的帮助< / strong > 。< / p >
< h2 id = "所有权原则" > < a class = "header" href = "#所有权原则" > 所有权原则< / a > < / h2 >
< p > 理解了堆栈,接下来看一下< em > 关于所有权的规则< / em > ,首先请谨记以下规则:< / p >
< blockquote >
< ol >
< li > Rust 中每一个值都被一个变量所拥有,该变量被称为值的所有者< / li >
< li > 一个值同时只能被一个变量所拥有,或者说一个值只能拥有一个所有者< / li >
< li > 当所有者(变量)离开作用域范围时,这个值将被丢弃(drop)< / li >
< / ol >
< / blockquote >
< h4 id = "变量作用域" > < a class = "header" href = "#变量作用域" > 变量作用域< / a > < / h4 >
< p > 作用域是一个变量在程序中有效的范围,假如有这样一个变量:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > let s = "hello";
< span class = "boring" > }< / span > < / code > < / pre >
< p > 变量 < code > s< / code > 绑定到了一个字符串字面值,该字符串字面值是硬编码到程序代码中的。< code > s< / code > 变量从声明的点开始直到当前作用域的结束都是有效的:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > { // s 在这里无效,它尚未声明
let s = "hello"; // 从此处起, s 是有效的
// 使用 s
} // 此作用域已结束, s不再有效
< span class = "boring" > }< / span > < / code > < / pre >
< p > 简而言之,< code > s< / code > 从创建开始就有效, 然后有效期持续到它离开作用域为止, 可以看出, 就作用域来说, Rust 语言跟其他编程语言没有区别。< / p >
< h4 id = "简单介绍-string-类型" > < a class = "header" href = "#简单介绍-string-类型" > 简单介绍 String 类型< / a > < / h4 >
< p > 之前提到过,本章会用 < code > String< / code > 作为例子,因此这里会进行一下简单的介绍,具体的 < code > String< / code > 学习请参见 < a href = "https://course.rs/basic/compound-type/string-slice.html" > String 类型< / a > 。< / p >
< p > 我们已经见过字符串字面值 < code > let s = "hello"< / code > , < code > s< / code > 是被硬编码进程序里的字符串值(类型为 < code > & str< / code > )。字符串字面值是很方便的,但是它并不适用于所有场景。原因有二:< / p >
< ul >
< li > < strong > 字符串字面值是不可变的< / strong > ,因为被硬编码到程序代码中< / li >
< li > 并非所有字符串的值都能在编写代码时得知< / li >
< / ul >
< p > 例如,字符串是需要程序运行时,通过用户动态输入然后存储在内存中的,这种情况,字符串字面值就完全无用武之地。 为此, Rust 为我们提供动态字符串类型: < code > String< / code > ,该类型被分配到堆上,因此可以动态伸缩,也就能存储在编译时大小未知的文本。< / p >
< p > 可以使用下面的方法基于字符串字面量来创建 < code > String< / code > 类型:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > let s = String::from("hello");
< span class = "boring" > }< / span > < / code > < / pre >
< p > < code > ::< / code > 是一种调用操作符,这里表示调用 < code > String< / code > 类型中的 < code > from< / code > 关联函数,由于 < code > String< / code > 类型存储在堆上,因此它是动态的,你可以这样修改:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > let mut s = String::from("hello");
s.push_str(", world!"); // push_str() 在字符串后追加字面值
println!("{}", s); // 将打印 `hello, world!`
< span class = "boring" > }< / span > < / code > < / pre >
< p > 言归正传,了解 < code > String< / code > 后,一起来看看关于所有权的交互。< / p >
< h2 id = "变量绑定背后的数据交互" > < a class = "header" href = "#变量绑定背后的数据交互" > 变量绑定背后的数据交互< / a > < / h2 >
< h4 id = "转移所有权" > < a class = "header" href = "#转移所有权" > 转移所有权< / a > < / h4 >
< p > 先来看一段代码:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > let x = 5;
let y = x;
< span class = "boring" > }< / span > < / code > < / pre >
< p > 这段代码并没有发生所有权的转移,原因很简单: 代码首先将 < code > 5< / code > 绑定到变量 < code > x< / code > ,接着< strong > 拷贝< / strong > < code > x< / code > 的值赋给 < code > y< / code > ,最终 < code > x< / code > 和 < code > y< / code > 都等于 < code > 5< / code > ,因为整数是 Rust 基本数据类型,是固定大小的简单值,因此这两个值都是通过< strong > 自动拷贝< / strong > 的方式来赋值的,都被存在栈中,完全无需在堆上分配内存。< / p >
< p > 整个过程中的赋值都是通过值拷贝的方式完成(发生在栈中),因此并不需要所有权转移。< / p >
< blockquote >
< p > 可能有同学会有疑问:这种拷贝不消耗性能吗?实际上,这种栈上的数据足够简单,而且拷贝非常非常快,只需要复制一个整数大小(< code > i32< / code > , 4 个字节)的内存即可,因此在这种情况下,拷贝的速度远比在堆上创建内存来得快的多。实际上,上一章我们讲到的 Rust 基本类型都是通过自动拷贝的方式来赋值的,就像上面代码一样。< / p >
< / blockquote >
< p > 然后再来看一段代码:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > let s1 = String::from("hello");
let s2 = s1;
< span class = "boring" > }< / span > < / code > < / pre >
< p > 此时,可能某个大聪明( 善意昵称 )已经想到了:嗯,上面一样,把 < code > s1< / code > 的内容拷贝一份赋值给 < code > s2< / code > , 实际上, 并不是这样。之前也提到了, 对于基本类型( 存储在栈上) , Rust 会自动拷贝,但是 < code > String< / code > 不是基本类型,而且是存储在堆上的,因此不能自动拷贝。< / p >
< p > 实际上, < code > String< / code > 类型是一个复杂类型,由存储在栈中的< strong > 堆指针< / strong > 、< strong > 字符串长度< / strong > 、< strong > 字符串容量< / strong > 共同组成,其中< strong > 堆指针< / strong > 是最重要的,它指向了真实存储字符串内容的堆内存,至于长度和容量,如果你有 Go 语言的经验,这里就很好理解:容量是堆内存分配空间的大小,长度是目前已经使用的大小。< / p >
< p > 总之 < code > String< / code > 类型指向了一个堆上的空间,这里存储着它的真实数据,下面对上面代码中的 < code > let s2 = s1< / code > 分成两种情况讨论:< / p >
< ol >
< li >
< p > 拷贝 < code > String< / code > 和存储在堆上的字节数组
如果该语句是拷贝所有数据(深拷贝),那么无论是 < code > String< / code > 本身还是底层的堆上数据,都会被全部拷贝,这对于性能而言会造成非常大的影响< / p >
< / li >
< li >
< p > 只拷贝 < code > String< / code > 本身
这样的拷贝非常快,因为在 64 位机器上就拷贝了 < code > 8字节的指针< / code > 、< code > 8字节的长度< / code > 、< code > 8字节的容量< / code > ,总计 24 字节,但是带来了新的问题,还记得我们之前提到的所有权规则吧?其中有一条就是:< strong > 一个值只允许有一个所有者< / strong > ,而现在这个值(堆上的真实字符串数据)有了两个所有者:< code > s1< / code > 和 < code > s2< / code > 。< / p >
< / li >
< / ol >
< p > 好吧,就假定一个值可以拥有两个所有者,会发生什么呢?< / p >
< p > 当变量离开作用域后, Rust 会自动调用 < code > drop< / code > 函数并清理变量的堆内存。不过由于两个 < code > String< / code > 变量指向了同一位置。这就有了一个问题:当 < code > s1< / code > 和 < code > s2< / code > 离开作用域,它们都会尝试释放相同的内存。这是一个叫做 < strong > 二次释放( double free) < / strong > 的错误,也是之前提到过的内存安全性 BUG 之一。两次释放(相同)内存会导致内存污染,它可能会导致潜在的安全漏洞。< / p >
< p > 因此, Rust 这样解决问题:< strong > 当 < code > s1< / code > 被赋予 < code > s2< / code > 后, Rust 认为 < code > s1< / code > 不再有效,因此也无需在 < code > s1< / code > 离开作用域后 < code > drop< / code > 任何东西,这就是把所有权从 < code > s1< / code > 转移给了 < code > s2< / code > , < code > s1< / code > 在被赋予 < code > s2< / code > 后就马上失效了< / strong > 。< / p >
< p > 再来看看,在所有权转移后再来使用旧的所有者,会发生什么:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > let s1 = String::from("hello");
let s2 = s1;
println!("{}, world!", s1);
< span class = "boring" > }< / span > < / code > < / pre >
< p > 由于 Rust 禁止你使用无效的引用,你会看到以下的错误:< / p >
< pre > < code class = "language-console" > error[E0382]: borrow of moved value: `s1`
--> src/main.rs:5:28
|
2 | let s1 = String::from("hello");
| -- move occurs because `s1` has type `String`, which does not implement the `Copy` trait
3 | let s2 = s1;
| -- value moved here
4 |
5 | println!("{}, world!", s1);
| ^^ value borrowed here after move
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider cloning the value if the performance cost is acceptable
|
3 | let s2 = s1.clone();
| ++++++++
For more information about this error, try `rustc --explain E0382`.
< / code > < / pre >
< p > 现在再回头看看之前的规则,相信大家已经有了更深刻的理解:< / p >
< blockquote >
< ol >
< li > Rust 中每一个值都被一个变量所拥有,该变量被称为值的所有者< / li >
< li > 一个值同时只能被一个变量所拥有,或者说一个值只能拥有一个所有者< / li >
< li > 当所有者(变量)离开作用域范围时,这个值将被丢弃(drop)< / li >
< / ol >
< / blockquote >
< p > 如果你在其他语言中听说过术语 < strong > 浅拷贝(shallow copy)< / strong > 和 < strong > 深拷贝(deep copy)< / strong > ,那么拷贝指针、长度和容量而不拷贝数据听起来就像浅拷贝,但是又因为 Rust 同时使第一个变量 < code > s1< / code > 无效了,因此这个操作被称为 < strong > 移动(move)< / strong > ,而不是浅拷贝。上面的例子可以解读为 < code > s1< / code > 被< strong > 移动< / strong > 到了 < code > s2< / code > 中。那么具体发生了什么,用一张图简单说明:< / p >
< img alt = "s1 moved to s2" src = "https://pic1.zhimg.com/80/v2-3ec77951de6a17584b5eb4a3838b4b61_1440w.jpg" class = "center" style = "width: 50%;" / >
< p > 这样就解决了我们之前的问题,< code > s1< / code > 不再指向任何数据,只有 < code > s2< / code > 是有效的,当 < code > s2< / code > 离开作用域,它就会释放内存。 相信此刻,你应该明白了,为什么 Rust 称呼 < code > let a = b< / code > 为< strong > 变量绑定< / strong > 了吧?< / p >
< p > 再来看一段代码:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
let x: & str = "hello, world";
let y = x;
println!("{},{}",x,y);
}< / code > < / pre >
< p > 这段代码,大家觉得会否报错?如果参考之前的 < code > String< / code > 所有权转移的例子,那这段代码也应该报错才是,但是实际上呢?< / p >
< p > 这段代码和之前的 < code > String< / code > 有一个本质上的区别:在 < code > String< / code > 的例子中 < code > s1< / code > 持有了通过< code > String::from("hello")< / code > 创建的值的所有权,而这个例子中,< code > x< / code > 只是引用了存储在二进制可执行文件( binary )中的字符串 < code > "hello, world"< / code > ,并没有持有所有权。< / p >
< p > 因此 < code > let y = x< / code > 中,仅仅是对该引用进行了拷贝,此时 < code > y< / code > 和 < code > x< / code > 都引用了同一个字符串。< strong > 如果还不理解也没关系,当学习了下一章节 “引用与借用” 后,大家自然而言就会理解。< / strong > < / p >
< h4 id = "克隆深拷贝" > < a class = "header" href = "#克隆深拷贝" > 克隆(深拷贝)< / a > < / h4 >
< p > 首先,< strong > Rust 永远也不会自动创建数据的 “深拷贝”< / strong > 。因此,任何< strong > 自动< / strong > 的复制都不是深拷贝,可以被认为对运行时性能影响较小。< / p >
< p > 如果我们< strong > 确实< / strong > 需要深度复制 < code > String< / code > 中堆上的数据,而不仅仅是栈上的数据,可以使用一个叫做 < code > clone< / code > 的方法。< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > let s1 = String::from("hello");
let s2 = s1.clone();
println!("s1 = {}, s2 = {}", s1, s2);
< span class = "boring" > }< / span > < / code > < / pre >
< p > 这段代码能够正常运行,说明 < code > s2< / code > 确实完整的复制了 < code > s1< / code > 的数据。< / p >
< p > 如果代码性能无关紧要,例如初始化程序时或者在某段时间只会执行寥寥数次时,你可以使用 < code > clone< / code > 来简化编程。但是对于执行较为频繁的代码(热点路径),使用 < code > clone< / code > 会极大的降低程序性能,需要小心使用!< / p >
< h4 id = "拷贝浅拷贝" > < a class = "header" href = "#拷贝浅拷贝" > 拷贝(浅拷贝)< / a > < / h4 >
< p > 浅拷贝只发生在栈上,因此性能很高,在日常编程中,浅拷贝无处不在。< / p >
< p > 再回到之前看过的例子:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > let x = 5;
let y = x;
println!("x = {}, y = {}", x, y);
< span class = "boring" > }< / span > < / code > < / pre >
< p > 但这段代码似乎与我们刚刚学到的内容相矛盾:没有调用 < code > clone< / code > ,不过依然实现了类似深拷贝的效果 —— 没有报所有权的错误。< / p >
< p > 原因是像整型这样的基本类型在编译时是已知大小的,会被存储在栈上,所以拷贝其实际的值是快速的。这意味着没有理由在创建变量 < code > y< / code > 后使 < code > x< / code > 无效(< code > x< / code > 、< code > y< / code > 都仍然有效)。换句话说,这里没有深浅拷贝的区别,因此这里调用 < code > clone< / code > 并不会与通常的浅拷贝有什么不同,我们可以不用管它(可以理解成在栈上做了深拷贝)。< / p >
< p > Rust 有一个叫做 < code > Copy< / code > 的特征,可以用在类似整型这样在栈中存储的类型。如果一个类型拥有 < code > Copy< / code > 特征,一个旧的变量在被赋值给其他变量后仍然可用,也就是赋值的过程即是拷贝的过程。< / p >
< p > 那么什么类型是可 < code > Copy< / code > 的呢?可以查看给定类型的文档来确认,这里可以给出一个通用的规则: < strong > 任何基本类型的组合可以 < code > Copy< / code > ,不需要分配内存或某种形式资源的类型是可以 < code > Copy< / code > 的< / strong > 。如下是一些 < code > Copy< / code > 的类型:< / p >
< ul >
< li > 所有整数类型,比如 < code > u32< / code > < / li >
< li > 布尔类型,< code > bool< / code > ,它的值是 < code > true< / code > 和 < code > false< / code > < / li >
< li > 所有浮点数类型,比如 < code > f64< / code > < / li >
< li > 字符类型,< code > char< / code > < / li >
< li > 元组,当且仅当其包含的类型也都是 < code > Copy< / code > 的时候。比如,< code > (i32, i32)< / code > 是 < code > Copy< / code > 的,但 < code > (i32, String)< / code > 就不是< / li >
< li > 不可变引用 < code > & T< / code > ,例如< a href = "#转移所有权" > 转移所有权< / a > 中的最后一个例子,< strong > 但是注意:可变引用 < code > & mut T< / code > 是不可以 Copy的< / strong > < / li >
< / ul >
< h2 id = "函数传值与返回" > < a class = "header" href = "#函数传值与返回" > 函数传值与返回< / a > < / h2 >
< p > 将值传递给函数,一样会发生 < code > 移动< / code > 或者 < code > 复制< / code > ,就跟 < code > let< / code > 语句一样,下面的代码展示了所有权、作用域的规则:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
let s = String::from("hello"); // s 进入作用域
takes_ownership(s); // s 的值移动到函数里 ...
// ... 所以到这里不再有效
let x = 5; // x 进入作用域
makes_copy(x); // x 应该移动函数里,
// 但 i32 是 Copy 的,所以在后面可继续使用 x
} // 这里, x 先移出了作用域,然后是 s。但因为 s 的值已被移走,
// 所以不会有特殊操作
fn takes_ownership(some_string: String) { // some_string 进入作用域
println!("{}", some_string);
} // 这里, some_string 移出作用域并调用 `drop` 方法。占用的内存被释放
fn makes_copy(some_integer: i32) { // some_integer 进入作用域
println!("{}", some_integer);
} // 这里, some_integer 移出作用域。不会有特殊操作< / code > < / pre >
< p > 你可以尝试在 < code > takes_ownership< / code > 之后,再使用 < code > s< / code > ,看看如何报错?例如添加一行 < code > println!("在move进函数后继续使用s: {}",s);< / code > 。< / p >
< p > 同样的,函数返回值也有所有权,例如:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
let s1 = gives_ownership(); // gives_ownership 将返回值
// 移给 s1
let s2 = String::from("hello"); // s2 进入作用域
let s3 = takes_and_gives_back(s2); // s2 被移动到
// takes_and_gives_back 中,
// 它也将返回值移给 s3
} // 这里, s3 移出作用域并被丢弃。s2 也移出作用域,但已被移走,
// 所以什么也不会发生。s1 移出作用域并被丢弃
fn gives_ownership() -> String { // gives_ownership 将返回值移动给
// 调用它的函数
let some_string = String::from("hello"); // some_string 进入作用域.
some_string // 返回 some_string 并移出给调用的函数
}
// takes_and_gives_back 将传入字符串并返回该值
fn takes_and_gives_back(a_string: String) -> String { // a_string 进入作用域
a_string // 返回 a_string 并移出给调用的函数
}< / code > < / pre >
< p > 所有权很强大,避免了内存的不安全性,但是也带来了一个新麻烦: < strong > 总是把一个值传来传去来使用它< / strong > 。 传入一个函数, 很可能还要从该函数传出去, 结果就是语言表达变得非常啰嗦, 幸运的是, Rust 提供了新功能解决这个问题。< / p >
< h2 id = "课后练习" > < a class = "header" href = "#课后练习" > 课后练习< / a > < / h2 >
< blockquote >
< p > < a href = "https://practice-zh.course.rs/ownership/ownership.html" > Rust By Practice< / a > ,支持代码在线编辑和运行,并提供详细的< a href = "https://github.com/sunface/rust-by-practice/blob/master/solutions/ownership/ownership.md" > 习题解答< / a > 。< / p >
< / blockquote >
< / main >
< nav class = "nav-wrapper" aria-label = "Page navigation" >
<!-- Mobile navigation buttons -->
< a rel = "prev" href = "../../basic/ownership/index.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/ownership/borrowing.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/ownership/index.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/ownership/borrowing.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-7d8ae1df.js" > < / script >
< script src = "../../assets/bigPicture-be03f9c8.js" > < / script >
< / div >
< / body >
< / html >