<!DOCTYPE HTML>
< html lang = "zh-CN" class = "light sidebar-visible" dir = "ltr" >
< head >
<!-- Book generated using mdBook -->
< meta charset = "UTF-8" >
< title > Deref 解引用 - 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-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/advance/smart-pointer/deref.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 = "deref-解引用" > < a class = "header" href = "#deref-解引用" > Deref 解引用< / a > < / h1 >
< p > 在开始之前,我们先来看一段代码:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > #[derive(Debug)]
struct Person {
name: String,
age: u8
}
impl Person {
fn new(name: String, age: u8) -> Self {
Person { name, age}
}
fn display(self: & mut Person, age: u8) {
let Person{name, age} = & self;
}
}
< span class = "boring" > }< / span > < / code > < / pre >
< p > 以上代码有一个很奇怪的地方:在 < code > display< / code > 方法中,< code > self< / code > 是 < code > & mut Person< / code > 的类型,接着我们对其取了一次引用 < code > & self< / code > ,此时 < code > & self< / code > 的类型是 < code > & & mut Person< / code > ,然后我们又将其和 < code > Person< / code > 类型进行匹配,取出其中的值。< / p >
< p > 那么问题来了, Rust 不是号称安全的语言吗?为何允许将 < code > & & mut Person< / code > 跟 < code > Person< / code > 进行匹配呢?答案就在本章节中,等大家学完后,再回头自己来解决这个问题 :) 下面正式开始咱们的新章节学习。< / p >
< p > 何为智能指针?能不让你写出 < code > ****s< / code > 形式的解引用,我认为就是智能: ),智能指针的名称来源,主要就在于它实现了 < code > Deref< / code > 和 < code > Drop< / code > 特征,这两个特征可以智能地帮助我们节省使用上的负担:< / p >
< ul >
< li > < code > Deref< / code > 可以让智能指针像引用那样工作,这样你就可以写出同时支持智能指针和引用的代码,例如 < code > *T< / code > < / li >
< li > < code > Drop< / code > 允许你指定智能指针超出作用域后自动执行的代码,例如做一些数据清除等收尾工作< / li >
< / ul >
< p > 先来看看 < code > Deref< / code > 特征是如何工作的。< / p >
< h2 id = "通过--获取引用背后的值" > < a class = "header" href = "#通过--获取引用背后的值" > 通过 < code > *< / code > 获取引用背后的值< / a > < / h2 >
< p > 在正式讲解 < code > Deref< / code > 之前,我们先来看下常规引用的解引用。< / p >
< p > 常规引用是一个指针类型,包含了目标数据存储的内存地址。对常规引用使用 < code > *< / code > 操作符,就可以通过解引用的方式获取到内存地址对应的数据值:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
let x = 5;
let y = & x;
assert_eq!(5, x);
assert_eq!(5, *y);
}< / code > < / pre >
< p > 这里 < code > y< / code > 就是一个常规引用,包含了值 < code > 5< / code > 所在的内存地址,然后通过解引用 < code > *y< / code > ,我们获取到了值 < code > 5< / code > 。如果你试图执行 < code > assert_eq!(5, y);< / code > ,代码就会无情报错,因为你无法将一个引用与一个数值做比较:< / p >
< pre > < code class = "language-console" > error[E0277]: can't compare `{integer}` with `& {integer}` //无法将{integer} 与& {integer}进行比较
--> src/main.rs:6:5
|
6 | assert_eq!(5, y);
| ^^^^^^^^^^^^^^^^^ no implementation for `{integer} == & {integer}`
|
= help: the trait `PartialEq< & {integer}> ` is not implemented for `{integer}`
// 你需要为{integer}实现用于比较的特征PartialEq< & {integer}>
< / code > < / pre >
< h2 id = "智能指针解引用" > < a class = "header" href = "#智能指针解引用" > 智能指针解引用< / a > < / h2 >
< p > 上面所说的解引用方式和其它大多数语言并无区别,但是 Rust 中将解引用提升到了一个新高度。考虑一下智能指针,它是一个结构体类型,如果你直接对它进行 < code > *myStruct< / code > ,显然编译器不知道该如何办,因此我们可以为智能指针结构体实现 < code > Deref< / code > 特征。< / p >
< p > 实现 < code > Deref< / code > 后的智能指针结构体,就可以像普通引用一样,通过 < code > *< / code > 进行解引用,例如 < code > Box< T> < / code > 智能指针:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
let x = Box::new(1);
let sum = *x + 1;
}< / code > < / pre >
< p > 智能指针 < code > x< / code > 被 < code > *< / code > 解引用为 < code > i32< / code > 类型的值 < code > 1< / code > ,然后再进行求和。< / p >
< h4 id = "定义自己的智能指针" > < a class = "header" href = "#定义自己的智能指针" > 定义自己的智能指针< / a > < / h4 >
< p > 现在,让我们一起来实现一个智能指针,功能上类似 < code > Box< T> < / code > 。由于 < code > Box< T> < / code > 本身很简单,并没有包含例如长度、最大长度等信息,因此用一个元组结构体即可。< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > struct MyBox< T> (T);
impl< T> MyBox< T> {
fn new(x: T) -> MyBox< T> {
MyBox(x)
}
}
< span class = "boring" > }< / span > < / code > < / pre >
< p > 跟 < code > Box< T> < / code > 一样,我们的智能指针也持有一个 < code > T< / code > 类型的值,然后使用关联函数 < code > MyBox::new< / code > 来创建智能指针。由于还未实现 < code > Deref< / code > 特征,此时使用 < code > *< / code > 肯定会报错:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
let y = MyBox::new(5);
assert_eq!(5, *y);
}< / code > < / pre >
< p > 运行后,报错如下:< / p >
< pre > < code class = "language-console" > error[E0614]: type `MyBox< {integer}> ` cannot be dereferenced
--> src/main.rs:12:19
|
12 | assert_eq!(5, *y);
| ^^
< / code > < / pre >
< h5 id = "为智能指针实现-deref-特征" > < a class = "header" href = "#为智能指针实现-deref-特征" > 为智能指针实现 Deref 特征< / a > < / h5 >
< p > 现在来为 < code > MyBox< / code > 实现 < code > Deref< / code > 特征,以支持 < code > *< / code > 解引用操作符:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > use std::ops::Deref;
impl< T> Deref for MyBox< T> {
type Target = T;
fn deref(& self) -> & Self::Target {
& self.0
}
}
< span class = "boring" > }< / span > < / code > < / pre >
< p > 很简单,当解引用 < code > MyBox< / code > 智能指针时,返回元组结构体中的元素 < code > & self.0< / code > ,有几点要注意的:< / p >
< ul >
< li > 在 < code > Deref< / code > 特征中声明了关联类型 < code > Target< / code > ,在之前章节中介绍过,关联类型主要是为了提升代码可读性< / li >
< li > < code > deref< / code > 返回的是一个常规引用,可以被 < code > *< / code > 进行解引用< / li >
< / ul >
< p > 之前报错的代码此时已能顺利编译通过。当然,标准库实现的智能指针要考虑很多边边角角情况,肯定比我们的实现要复杂。< / p >
< h2 id = "-背后的原理" > < a class = "header" href = "#-背后的原理" > < code > *< / code > 背后的原理< / a > < / h2 >
< p > 当我们对智能指针 < code > Box< / code > 进行解引用时,实际上 Rust 为我们调用了以下方法:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > *(y.deref())
< span class = "boring" > }< / span > < / code > < / pre >
< p > 首先调用 < code > deref< / code > 方法返回值的常规引用,然后通过 < code > *< / code > 对常规引用进行解引用,最终获取到目标值。< / p >
< p > 至于 Rust 为何要使用这个有点啰嗦的方式实现,原因在于所有权系统的存在。如果 < code > deref< / code > 方法直接返回一个值,而不是引用,那么该值的所有权将被转移给调用者,而我们不希望调用者仅仅只是 < code > *T< / code > 一下,就拿走了智能指针中包含的值。< / p >
< p > 需要注意的是,< code > *< / code > 不会无限递归替换,从 < code > *y< / code > 到 < code > *(y.deref())< / code > 只会发生一次,而不会继续进行替换然后产生形如 < code > *((y.deref()).deref())< / code > 的怪物。< / p >
< h2 id = "函数和方法中的隐式-deref-转换" > < a class = "header" href = "#函数和方法中的隐式-deref-转换" > 函数和方法中的隐式 Deref 转换< / a > < / h2 >
< p > 对于函数和方法的传参, Rust 提供了一个极其有用的隐式转换:< code > Deref < / code > 转换。若一个类型实现了 < code > Deref< / code > 特征,那它的引用在传给函数或方法时,会根据参数签名来决定是否进行隐式的 < code > Deref< / code > 转换,例如:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
let s = String::from("hello world");
display(& s)
}
fn display(s: & str) {
println!("{}",s);
}< / code > < / pre >
< p > 以上代码有几点值得注意:< / p >
< ul >
< li > < code > String< / code > 实现了 < code > Deref< / code > 特征,可以在需要时自动被转换为 < code > & str< / code > 类型< / li >
< li > < code > & s< / code > 是一个 < code > & String< / code > 类型,当它被传给 < code > display< / code > 函数时,自动通过 < code > Deref< / code > 转换成了 < code > & str< / code > < / li >
< li > 必须使用 < code > & s< / code > 的方式来触发 < code > Deref< / code > (仅引用类型的实参才会触发自动解引用)< / li >
< / ul >
< h4 id = "连续的隐式-deref-转换" > < a class = "header" href = "#连续的隐式-deref-转换" > 连续的隐式 Deref 转换< / a > < / h4 >
< p > 如果你以为 < code > Deref< / code > 仅仅这点作用,那就大错特错了。< code > Deref< / code > 可以支持连续的隐式转换,直到找到适合的形式为止:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
let s = MyBox::new(String::from("hello world"));
display(& s)
}
fn display(s: & str) {
println!("{}",s);
}< / code > < / pre >
< p > 这里我们使用了之前自定义的智能指针 < code > MyBox< / code > ,并将其通过连续的隐式转换变成 < code > & str< / code > 类型:首先 < code > MyBox< / code > 被 < code > Deref< / code > 成 < code > String< / code > 类型,结果并不能满足 < code > display< / code > 函数参数的要求,编译器发现 < code > String< / code > 还可以继续 < code > Deref< / code > 成 < code > & str< / code > ,最终成功的匹配了函数参数。< / p >
< p > 想象一下,假如 < code > Rust< / code > 没有提供这种隐式转换,我们该如何调用 < code > display< / code > 函数?< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
let m = MyBox::new(String::from("Rust"));
display(& (*m)[..]);
}< / code > < / pre >
< p > 结果不言而喻,肯定是 < code > & s< / code > 的方式优秀得多。总之,当参与其中的类型定义了 < code > Deref< / code > 特征时, Rust 会分析该类型并且连续使用 < code > Deref< / code > 直到最终获得一个引用来匹配函数或者方法的参数类型,这种行为完全不会造成任何的性能损耗,因为完全是在编译期完成。< / p >
< p > 但是 < code > Deref< / code > 并不是没有缺点,缺点就是:如果你不知道某个类型是否实现了 < code > Deref< / code > 特征,那么在看到某段代码时,并不能在第一时间反应过来该代码发生了隐式的 < code > Deref< / code > 转换。事实上,不仅仅是 < code > Deref< / code > ,在 Rust 中还有各种 < code > From/Into< / code > 等等会给阅读代码带来一定负担的特征。还是那句话, 一切选择都是权衡, 有得必有失, 得了代码的简洁性, 往往就失去了可读性, Go 语言就是一个刚好相反的例子。< / p >
< p > 再来看一下在方法、赋值中自动应用 < code > Deref< / code > 的例子:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
let s = MyBox::new(String::from("hello, world"));
let s1: & str = & s;
let s2: String = s.to_string();
}< / code > < / pre >
< p > 对于 < code > s1< / code > ,我们通过两次 < code > Deref< / code > 将 < code > & str< / code > 类型的值赋给了它(< strong > 赋值操作需要手动解引用< / strong > );而对于 < code > s2< / code > ,我们在其上直接调用方法 < code > to_string< / code > ,实际上 < code > MyBox< / code > 并没有实现该方法,能调用 < code > to_string< / code > ,完全是因为编译器对 < code > MyBox< / code > 应用了 < code > Deref< / code > ( < strong > 方法调用会自动解引用< / strong > )。< / p >
< h2 id = "deref-规则总结" > < a class = "header" href = "#deref-规则总结" > Deref 规则总结< / a > < / h2 >
< p > 在上面,我们零碎的介绍了不少关于 < code > Deref< / code > 特征的知识,下面来通过较为正式的方式来对其规则进行下总结。< / p >
< p > 一个类型为 < code > T< / code > 的对象 < code > foo< / code > ,如果 < code > T: Deref< Target=U> < / code > ,那么,相关 < code > foo< / code > 的引用 < code > & foo< / code > 在应用的时候会自动转换为 < code > & U< / code > 。< / p >
< p > 粗看这条规则,貌似有点类似于 < code > AsRef< / code > ,而跟 < code > 解引用< / code > 似乎风马牛不相及,实际里面有些玄妙之处。< / p >
< h4 id = "引用归一化" > < a class = "header" href = "#引用归一化" > 引用归一化< / a > < / h4 >
< p > Rust 编译器实际上只能对 < code > & v< / code > 形式的引用进行解引用操作,那么问题来了,如果是一个智能指针或者 < code > & & & & v< / code > 类型的呢? 该如何对这两个进行解引用?< / p >
< p > 答案是: Rust 会在解引用时自动把智能指针和 < code > & & & & v< / code > 做引用归一化操作,转换成 < code > & v< / code > 形式,最终再对 < code > & v< / code > 进行解引用:< / p >
< ul >
< li > 把智能指针( 比如在库中定义的, Box、Rc、Arc、Cow 等)从结构体脱壳为内部的引用类型,也就是转成结构体内部的 < code > & v< / code > < / li >
< li > 把多重< code > & < / code > ,例如 < code > & & & & & & & v< / code > ,归一成 < code > & v< / code > < / li >
< / ul >
< p > 关于第二种情况,这么干巴巴的说,也许大家会迷迷糊糊的,我们来看一段标准库源码:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > impl< T: ?Sized> Deref for & T {
type Target = T;
fn deref(& self) -> & T {
*self
}
}
< span class = "boring" > }< / span > < / code > < / pre >
< p > 在这段源码中,< code > & T< / code > 被自动解引用为 < code > T< / code > ,也就是 < code > & T: Deref< Target=T> < / code > 。 按照这个代码,< code > & & & & T< / code > 会被自动解引用为 < code > & & & T< / code > ,然后再自动解引用为 < code > & & T< / code > ,以此类推, 直到最终变成 < code > & T< / code > 。< / p >
< p > PS: 以下是 < code > LLVM< / code > 编译后的部分中间层代码:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > // Rust 代码
let mut _2: & i32;
let _3: & & & & i32;
bb0: {
_2 = (*(*(*_3)))
}
< span class = "boring" > }< / span > < / code > < / pre >
< h4 id = "几个例子" > < a class = "header" href = "#几个例子" > 几个例子< / a > < / h4 >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > fn foo(s: & str) {}
// 由于 String 实现了 Deref< Target=str>
let owned = "Hello".to_string();
// 因此下面的函数可以正常运行:
foo(& owned);
< span class = "boring" > }< / span > < / code > < / pre >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > use std::rc::Rc;
fn foo(s: & str) {}
// String 实现了 Deref< Target=str>
let owned = "Hello".to_string();
// 且 Rc 智能指针可以被自动脱壳为内部的 `owned` 引用: & String ,然后 & String 再自动解引用为 & str
let counted = Rc::new(owned);
// 因此下面的函数可以正常运行:
foo(& counted);
< span class = "boring" > }< / span > < / code > < / pre >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > struct Foo;
impl Foo {
fn foo(& self) { println!("Foo"); }
}
let f = & & Foo;
f.foo();
(& f).foo();
(& & f).foo();
(& & & & & & & & f).foo();
< span class = "boring" > }< / span > < / code > < / pre >
< h2 id = "三种-deref-转换" > < a class = "header" href = "#三种-deref-转换" > 三种 Deref 转换< / a > < / h2 >
< p > 在之前,我们讲的都是不可变的 < code > Deref< / code > 转换,实际上 Rust 还支持将一个可变的引用转换成另一个可变的引用以及将一个可变引用转换成不可变的引用,规则如下:< / p >
< ul >
< li > 当 < code > T: Deref< Target=U> < / code > ,可以将 < code > & T< / code > 转换成 < code > & U< / code > ,也就是我们之前看到的例子< / li >
< li > 当 < code > T: DerefMut< Target=U> < / code > ,可以将 < code > & mut T< / code > 转换成 < code > & mut U< / code > < / li >
< li > 当 < code > T: Deref< Target=U> < / code > ,可以将 < code > & mut T< / code > 转换成 < code > & U< / code > < / li >
< / ul >
< p > 来看一个关于 < code > DerefMut< / code > 的例子:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > struct MyBox< T> {
v: T,
}
impl< T> MyBox< T> {
fn new(x: T) -> MyBox< T> {
MyBox { v: x }
}
}
use std::ops::Deref;
impl< T> Deref for MyBox< T> {
type Target = T;
fn deref(& self) -> & Self::Target {
& self.v
}
}
use std::ops::DerefMut;
impl< T> DerefMut for MyBox< T> {
fn deref_mut(& mut self) -> & mut Self::Target {
& mut self.v
}
}
fn main() {
let mut s = MyBox::new(String::from("hello, "));
display(& mut s)
}
fn display(s: & mut String) {
s.push_str("world");
println!("{}", s);
}< / code > < / pre >
< p > 以上代码有几点值得注意:< / p >
< ul >
< li > 要实现 < code > DerefMut< / code > 必须要先实现 < code > Deref< / code > 特征:< code > pub trait DerefMut: Deref< / code > < / li >
< li > < code > T: DerefMut< Target=U> < / code > 解读:将 < code > & mut T< / code > 类型通过 < code > DerefMut< / code > 特征的方法转换为 < code > & mut U< / code > 类型,对应上例中,就是将 < code > & mut MyBox< String> < / code > 转换为 < code > & mut String< / code > < / li >
< / ul >
< p > 对于上述三条规则中的第三条, 它比另外两条稍微复杂了点: Rust 可以把可变引用隐式的转换成不可变引用,但反之则不行。< / p >
< p > 如果从 Rust 的所有权和借用规则的角度考虑,当你拥有一个可变的引用,那该引用肯定是对应数据的唯一借用,那么此时将可变引用变成不可变引用并不会破坏借用规则;但是如果你拥有一个不可变引用,那同时可能还存在其它几个不可变的引用,如果此时将其中一个不可变引用转换成可变引用,就变成了可变引用与不可变引用的共存,最终破坏了借用规则。< / p >
< h2 id = "总结" > < a class = "header" href = "#总结" > 总结< / a > < / h2 >
< p > < code > Deref< / code > 可以说是 Rust 中最常见的隐式类型转换,而且它可以连续的实现如 < code > Box< String> -> String -> & str< / code > 的隐式转换,只要链条上的类型实现了 < code > Deref< / code > 特征。< / p >
< p > 我们也可以为自己的类型实现 < code > Deref< / code > 特征,但是原则上来说,只应该为自定义的智能指针实现 < code > Deref< / code > 。例如,虽然你可以为自己的自定义数组类型实现 < code > Deref< / code > 以避免 < code > myArr.0[0]< / code > 的使用形式,但是 Rust 官方并不推荐这么做,特别是在你开发三方库时。< / p >
< / main >
< nav class = "nav-wrapper" aria-label = "Page navigation" >
<!-- Mobile navigation buttons -->
< a rel = "prev" href = "../../advance/smart-pointer/box.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 = "../../advance/smart-pointer/drop.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 = "../../advance/smart-pointer/box.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 = "../../advance/smart-pointer/drop.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 >