586 lines
43 KiB
HTML
586 lines
43 KiB
HTML
<!DOCTYPE HTML>
|
||
<html lang="en" class="light sidebar-visible" dir="ltr">
|
||
<head>
|
||
<!-- Book generated using mdBook -->
|
||
<meta charset="UTF-8">
|
||
<title>Advanced Types - The Rust Programming Language</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-0392ca55.css">
|
||
<link rel="stylesheet" href="css/chrome-fc474251.css">
|
||
<link rel="stylesheet" href="css/print-9e4910d8.css" media="print">
|
||
|
||
<!-- Fonts -->
|
||
<link rel="stylesheet" href="fonts/fonts-9644e21d.css">
|
||
|
||
<!-- Highlight.js Stylesheets -->
|
||
<link rel="stylesheet" id="mdbook-highlight-css" href="highlight-493f70e1.css">
|
||
<link rel="stylesheet" id="mdbook-tomorrow-night-css" href="tomorrow-night-4c0ae647.css">
|
||
<link rel="stylesheet" id="mdbook-ayu-highlight-css" href="ayu-highlight-3fdfc3ac.css">
|
||
|
||
<!-- Custom theme stylesheets -->
|
||
<link rel="stylesheet" href="ferris-d33b75bf.css">
|
||
<link rel="stylesheet" href="theme/2018-edition-4e126c62.css">
|
||
<link rel="stylesheet" href="theme/semantic-notes-9b5766c0.css">
|
||
<link rel="stylesheet" href="theme/listing-cab26221.css">
|
||
|
||
|
||
<!-- Provide site root and default themes to javascript -->
|
||
<script>
|
||
const path_to_root = "";
|
||
const default_light_theme = "light";
|
||
const default_dark_theme = "navy";
|
||
window.path_to_searchindex_js = "searchindex-6a1da8cc.js";
|
||
</script>
|
||
<!-- Start loading toc.js asap -->
|
||
<script src="toc-0e4ce700.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">The Rust Programming Language</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/rust-lang/book" title="Git repository" aria-label="Git repository">
|
||
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg></span>
|
||
</a>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
<div id="mdbook-search-wrapper" class="hidden">
|
||
<form id="mdbook-searchbar-outer" class="searchbar-outer">
|
||
<div class="search-wrapper">
|
||
<input type="search" id="mdbook-searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="mdbook-searchresults-outer" aria-describedby="searchresults-header">
|
||
<div class="spinner-wrapper">
|
||
<span class=fa-svg id="fa-spin"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M304 48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zm0 416c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM48 304c26.5 0 48-21.5 48-48s-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48zm464-48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM142.9 437c18.7-18.7 18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zm0-294.2c18.7-18.7 18.7-49.1 0-67.9S93.7 56.2 75 75s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zM369.1 437c18.7 18.7 49.1 18.7 67.9 0s18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9z"/></svg></span>
|
||
</div>
|
||
</div>
|
||
</form>
|
||
<div id="mdbook-searchresults-outer" class="searchresults-outer hidden">
|
||
<div id="mdbook-searchresults-header" class="searchresults-header"></div>
|
||
<ul id="mdbook-searchresults">
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
||
<script>
|
||
document.getElementById('mdbook-sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
||
document.getElementById('mdbook-sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
||
Array.from(document.querySelectorAll('#mdbook-sidebar a')).forEach(function(link) {
|
||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
||
});
|
||
</script>
|
||
|
||
<div id="mdbook-content" class="content">
|
||
<main>
|
||
<h2 id="advanced-types"><a class="header" href="#advanced-types">Advanced Types</a></h2>
|
||
<p>The Rust type system has some features that we’ve so far mentioned but haven’t
|
||
yet discussed. We’ll start by discussing newtypes in general as we examine why
|
||
they are useful as types. Then, we’ll move on to type aliases, a feature
|
||
similar to newtypes but with slightly different semantics. We’ll also discuss
|
||
the <code>!</code> type and dynamically sized types.</p>
|
||
<!-- Old headings. Do not remove or links may break. -->
|
||
<p><a id="using-the-newtype-pattern-for-type-safety-and-abstraction"></a></p>
|
||
<h3 id="type-safety-and-abstraction-with-the-newtype-pattern"><a class="header" href="#type-safety-and-abstraction-with-the-newtype-pattern">Type Safety and Abstraction with the Newtype Pattern</a></h3>
|
||
<p>This section assumes you’ve read the earlier section <a href="ch20-02-advanced-traits.html#implementing-external-traits-with-the-newtype-pattern">“Implementing External
|
||
Traits with the Newtype Pattern”</a><!-- ignore -->. The newtype pattern
|
||
is also useful for tasks beyond those we’ve discussed so far, including
|
||
statically enforcing that values are never confused and indicating the units of
|
||
a value. You saw an example of using newtypes to indicate units in Listing
|
||
20-16: Recall that the <code>Millimeters</code> and <code>Meters</code> structs wrapped <code>u32</code> values
|
||
in a newtype. If we wrote a function with a parameter of type <code>Millimeters</code>, we
|
||
wouldn’t be able to compile a program that accidentally tried to call that
|
||
function with a value of type <code>Meters</code> or a plain <code>u32</code>.</p>
|
||
<p>We can also use the newtype pattern to abstract away some implementation
|
||
details of a type: The new type can expose a public API that is different from
|
||
the API of the private inner type.</p>
|
||
<p>Newtypes can also hide internal implementation. For example, we could provide a
|
||
<code>People</code> type to wrap a <code>HashMap<i32, String></code> that stores a person’s ID
|
||
associated with their name. Code using <code>People</code> would only interact with the
|
||
public API we provide, such as a method to add a name string to the <code>People</code>
|
||
collection; that code wouldn’t need to know that we assign an <code>i32</code> ID to names
|
||
internally. The newtype pattern is a lightweight way to achieve encapsulation
|
||
to hide implementation details, which we discussed in the <a href="ch18-01-what-is-oo.html#encapsulation-that-hides-implementation-details">“Encapsulation that
|
||
Hides Implementation
|
||
Details”</a><!-- ignore -->
|
||
section in Chapter 18.</p>
|
||
<!-- Old headings. Do not remove or links may break. -->
|
||
<p><a id="creating-type-synonyms-with-type-aliases"></a></p>
|
||
<h3 id="type-synonyms-and-type-aliases"><a class="header" href="#type-synonyms-and-type-aliases">Type Synonyms and Type Aliases</a></h3>
|
||
<p>Rust provides the ability to declare a <em>type alias</em> to give an existing type
|
||
another name. For this we use the <code>type</code> keyword. For example, we can create
|
||
the alias <code>Kilometers</code> to <code>i32</code> like so:</p>
|
||
<pre class="playground"><code class="language-rust edition2024"><span class="boring">fn main() {
|
||
</span> type Kilometers = i32;
|
||
<span class="boring">
|
||
</span><span class="boring"> let x: i32 = 5;
|
||
</span><span class="boring"> let y: Kilometers = 5;
|
||
</span><span class="boring">
|
||
</span><span class="boring"> println!("x + y = {}", x + y);
|
||
</span><span class="boring">}</span></code></pre>
|
||
<p>Now the alias <code>Kilometers</code> is a <em>synonym</em> for <code>i32</code>; unlike the <code>Millimeters</code>
|
||
and <code>Meters</code> types we created in Listing 20-16, <code>Kilometers</code> is not a separate,
|
||
new type. Values that have the type <code>Kilometers</code> will be treated the same as
|
||
values of type <code>i32</code>:</p>
|
||
<pre class="playground"><code class="language-rust edition2024"><span class="boring">fn main() {
|
||
</span> type Kilometers = i32;
|
||
|
||
let x: i32 = 5;
|
||
let y: Kilometers = 5;
|
||
|
||
println!("x + y = {}", x + y);
|
||
<span class="boring">}</span></code></pre>
|
||
<p>Because <code>Kilometers</code> and <code>i32</code> are the same type, we can add values of both
|
||
types and can pass <code>Kilometers</code> values to functions that take <code>i32</code>
|
||
parameters. However, using this method, we don’t get the type-checking benefits
|
||
that we get from the newtype pattern discussed earlier. In other words, if we
|
||
mix up <code>Kilometers</code> and <code>i32</code> values somewhere, the compiler will not give us
|
||
an error.</p>
|
||
<p>The main use case for type synonyms is to reduce repetition. For example, we
|
||
might have a lengthy type like this:</p>
|
||
<pre><code class="language-rust ignore">Box<dyn Fn() + Send + 'static></code></pre>
|
||
<p>Writing this lengthy type in function signatures and as type annotations all
|
||
over the code can be tiresome and error-prone. Imagine having a project full of
|
||
code like that in Listing 20-25.</p>
|
||
<figure class="listing" id="listing-20-25">
|
||
<pre class="playground"><code class="language-rust edition2024"><span class="boring">fn main() {
|
||
</span> let f: Box<dyn Fn() + Send + 'static> = Box::new(|| println!("hi"));
|
||
|
||
fn takes_long_type(f: Box<dyn Fn() + Send + 'static>) {
|
||
// --snip--
|
||
}
|
||
|
||
fn returns_long_type() -> Box<dyn Fn() + Send + 'static> {
|
||
// --snip--
|
||
<span class="boring"> Box::new(|| ())
|
||
</span> }
|
||
<span class="boring">}</span></code></pre>
|
||
<figcaption><a href="#listing-20-25">Listing 20-25</a>: Using a long type in many places</figcaption>
|
||
</figure>
|
||
<p>A type alias makes this code more manageable by reducing the repetition. In
|
||
Listing 20-26, we’ve introduced an alias named <code>Thunk</code> for the verbose type and
|
||
can replace all uses of the type with the shorter alias <code>Thunk</code>.</p>
|
||
<figure class="listing" id="listing-20-26">
|
||
<pre class="playground"><code class="language-rust edition2024"><span class="boring">fn main() {
|
||
</span> type Thunk = Box<dyn Fn() + Send + 'static>;
|
||
|
||
let f: Thunk = Box::new(|| println!("hi"));
|
||
|
||
fn takes_long_type(f: Thunk) {
|
||
// --snip--
|
||
}
|
||
|
||
fn returns_long_type() -> Thunk {
|
||
// --snip--
|
||
<span class="boring"> Box::new(|| ())
|
||
</span> }
|
||
<span class="boring">}</span></code></pre>
|
||
<figcaption><a href="#listing-20-26">Listing 20-26</a>: Introducing a type alias, <code>Thunk</code>, to reduce repetition</figcaption>
|
||
</figure>
|
||
<p>This code is much easier to read and write! Choosing a meaningful name for a
|
||
type alias can help communicate your intent as well (<em>thunk</em> is a word for code
|
||
to be evaluated at a later time, so it’s an appropriate name for a closure that
|
||
gets stored).</p>
|
||
<p>Type aliases are also commonly used with the <code>Result<T, E></code> type for reducing
|
||
repetition. Consider the <code>std::io</code> module in the standard library. I/O
|
||
operations often return a <code>Result<T, E></code> to handle situations when operations
|
||
fail to work. This library has a <code>std::io::Error</code> struct that represents all
|
||
possible I/O errors. Many of the functions in <code>std::io</code> will be returning
|
||
<code>Result<T, E></code> where the <code>E</code> is <code>std::io::Error</code>, such as these functions in
|
||
the <code>Write</code> trait:</p>
|
||
<pre><code class="language-rust noplayground">use std::fmt;
|
||
use std::io::Error;
|
||
|
||
pub trait Write {
|
||
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>;
|
||
fn flush(&mut self) -> Result<(), Error>;
|
||
|
||
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>;
|
||
fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<(), Error>;
|
||
}</code></pre>
|
||
<p>The <code>Result<..., Error></code> is repeated a lot. As such, <code>std::io</code> has this type
|
||
alias declaration:</p>
|
||
<pre><code class="language-rust noplayground"><span class="boring">use std::fmt;
|
||
</span><span class="boring">
|
||
</span>type Result<T> = std::result::Result<T, std::io::Error>;
|
||
<span class="boring">
|
||
</span><span class="boring">pub trait Write {
|
||
</span><span class="boring"> fn write(&mut self, buf: &[u8]) -> Result<usize>;
|
||
</span><span class="boring"> fn flush(&mut self) -> Result<()>;
|
||
</span><span class="boring">
|
||
</span><span class="boring"> fn write_all(&mut self, buf: &[u8]) -> Result<()>;
|
||
</span><span class="boring"> fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<()>;
|
||
</span><span class="boring">}</span></code></pre>
|
||
<p>Because this declaration is in the <code>std::io</code> module, we can use the fully
|
||
qualified alias <code>std::io::Result<T></code>; that is, a <code>Result<T, E></code> with the <code>E</code>
|
||
filled in as <code>std::io::Error</code>. The <code>Write</code> trait function signatures end up
|
||
looking like this:</p>
|
||
<pre><code class="language-rust noplayground"><span class="boring">use std::fmt;
|
||
</span><span class="boring">
|
||
</span><span class="boring">type Result<T> = std::result::Result<T, std::io::Error>;
|
||
</span><span class="boring">
|
||
</span>pub trait Write {
|
||
fn write(&mut self, buf: &[u8]) -> Result<usize>;
|
||
fn flush(&mut self) -> Result<()>;
|
||
|
||
fn write_all(&mut self, buf: &[u8]) -> Result<()>;
|
||
fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<()>;
|
||
}</code></pre>
|
||
<p>The type alias helps in two ways: It makes code easier to write <em>and</em> it gives
|
||
us a consistent interface across all of <code>std::io</code>. Because it’s an alias, it’s
|
||
just another <code>Result<T, E></code>, which means we can use any methods that work on
|
||
<code>Result<T, E></code> with it, as well as special syntax like the <code>?</code> operator.</p>
|
||
<h3 id="the-never-type-that-never-returns"><a class="header" href="#the-never-type-that-never-returns">The Never Type That Never Returns</a></h3>
|
||
<p>Rust has a special type named <code>!</code> that’s known in type theory lingo as the
|
||
<em>empty type</em> because it has no values. We prefer to call it the <em>never type</em>
|
||
because it stands in the place of the return type when a function will never
|
||
return. Here is an example:</p>
|
||
<pre><code class="language-rust noplayground">fn bar() -> ! {
|
||
// --snip--
|
||
<span class="boring"> panic!();
|
||
</span>}</code></pre>
|
||
<p>This code is read as “the function <code>bar</code> returns never.” Functions that return
|
||
never are called <em>diverging functions</em>. We can’t create values of the type <code>!</code>,
|
||
so <code>bar</code> can never possibly return.</p>
|
||
<p>But what use is a type you can never create values for? Recall the code from
|
||
Listing 2-5, part of the number-guessing game; we’ve reproduced a bit of it
|
||
here in Listing 20-27.</p>
|
||
<figure class="listing" id="listing-20-27">
|
||
<pre><code class="language-rust ignore"><span class="boring">use std::cmp::Ordering;
|
||
</span><span class="boring">use std::io;
|
||
</span><span class="boring">
|
||
</span><span class="boring">use rand::Rng;
|
||
</span><span class="boring">
|
||
</span><span class="boring">fn main() {
|
||
</span><span class="boring"> println!("Guess the number!");
|
||
</span><span class="boring">
|
||
</span><span class="boring"> let secret_number = rand::thread_rng().gen_range(1..=100);
|
||
</span><span class="boring">
|
||
</span><span class="boring"> println!("The secret number is: {secret_number}");
|
||
</span><span class="boring">
|
||
</span><span class="boring"> loop {
|
||
</span><span class="boring"> println!("Please input your guess.");
|
||
</span><span class="boring">
|
||
</span><span class="boring"> let mut guess = String::new();
|
||
</span><span class="boring">
|
||
</span><span class="boring"> // --snip--
|
||
</span><span class="boring">
|
||
</span><span class="boring"> io::stdin()
|
||
</span><span class="boring"> .read_line(&mut guess)
|
||
</span><span class="boring"> .expect("Failed to read line");
|
||
</span><span class="boring">
|
||
</span> let guess: u32 = match guess.trim().parse() {
|
||
Ok(num) => num,
|
||
Err(_) => continue,
|
||
};
|
||
<span class="boring">
|
||
</span><span class="boring"> println!("You guessed: {guess}");
|
||
</span><span class="boring">
|
||
</span><span class="boring"> // --snip--
|
||
</span><span class="boring">
|
||
</span><span class="boring"> match guess.cmp(&secret_number) {
|
||
</span><span class="boring"> Ordering::Less => println!("Too small!"),
|
||
</span><span class="boring"> Ordering::Greater => println!("Too big!"),
|
||
</span><span class="boring"> Ordering::Equal => {
|
||
</span><span class="boring"> println!("You win!");
|
||
</span><span class="boring"> break;
|
||
</span><span class="boring"> }
|
||
</span><span class="boring"> }
|
||
</span><span class="boring"> }
|
||
</span><span class="boring">}</span></code></pre>
|
||
<figcaption><a href="#listing-20-27">Listing 20-27</a>: A <code>match</code> with an arm that ends in <code>continue</code></figcaption>
|
||
</figure>
|
||
<p>At the time, we skipped over some details in this code. In <a href="ch06-02-match.html#the-match-control-flow-construct">“The <code>match</code>
|
||
Control Flow Construct”</a><!-- ignore -->
|
||
section in Chapter 6, we discussed that <code>match</code> arms must all return the same
|
||
type. So, for example, the following code doesn’t work:</p>
|
||
<pre><code class="language-rust ignore does_not_compile"><span class="boring">fn main() {
|
||
</span><span class="boring"> let guess = "3";
|
||
</span> let guess = match guess.trim().parse() {
|
||
Ok(_) => 5,
|
||
Err(_) => "hello",
|
||
};
|
||
<span class="boring">}</span></code></pre>
|
||
<p>The type of <code>guess</code> in this code would have to be an integer <em>and</em> a string,
|
||
and Rust requires that <code>guess</code> have only one type. So, what does <code>continue</code>
|
||
return? How were we allowed to return a <code>u32</code> from one arm and have another arm
|
||
that ends with <code>continue</code> in Listing 20-27?</p>
|
||
<p>As you might have guessed, <code>continue</code> has a <code>!</code> value. That is, when Rust
|
||
computes the type of <code>guess</code>, it looks at both match arms, the former with a
|
||
value of <code>u32</code> and the latter with a <code>!</code> value. Because <code>!</code> can never have a
|
||
value, Rust decides that the type of <code>guess</code> is <code>u32</code>.</p>
|
||
<p>The formal way of describing this behavior is that expressions of type <code>!</code> can
|
||
be coerced into any other type. We’re allowed to end this <code>match</code> arm with
|
||
<code>continue</code> because <code>continue</code> doesn’t return a value; instead, it moves control
|
||
back to the top of the loop, so in the <code>Err</code> case, we never assign a value to
|
||
<code>guess</code>.</p>
|
||
<p>The never type is useful with the <code>panic!</code> macro as well. Recall the <code>unwrap</code>
|
||
function that we call on <code>Option<T></code> values to produce a value or panic with
|
||
this definition:</p>
|
||
<pre><code class="language-rust ignore"><span class="boring">enum Option<T> {
|
||
</span><span class="boring"> Some(T),
|
||
</span><span class="boring"> None,
|
||
</span><span class="boring">}
|
||
</span><span class="boring">
|
||
</span><span class="boring">use crate::Option::*;
|
||
</span><span class="boring">
|
||
</span>impl<T> Option<T> {
|
||
pub fn unwrap(self) -> T {
|
||
match self {
|
||
Some(val) => val,
|
||
None => panic!("called `Option::unwrap()` on a `None` value"),
|
||
}
|
||
}
|
||
}</code></pre>
|
||
<p>In this code, the same thing happens as in the <code>match</code> in Listing 20-27: Rust
|
||
sees that <code>val</code> has the type <code>T</code> and <code>panic!</code> has the type <code>!</code>, so the result
|
||
of the overall <code>match</code> expression is <code>T</code>. This code works because <code>panic!</code>
|
||
doesn’t produce a value; it ends the program. In the <code>None</code> case, we won’t be
|
||
returning a value from <code>unwrap</code>, so this code is valid.</p>
|
||
<p>One final expression that has the type <code>!</code> is a loop:</p>
|
||
<pre><code class="language-rust ignore"><span class="boring">fn main() {
|
||
</span> print!("forever ");
|
||
|
||
loop {
|
||
print!("and ever ");
|
||
}
|
||
<span class="boring">}</span></code></pre>
|
||
<p>Here, the loop never ends, so <code>!</code> is the value of the expression. However, this
|
||
wouldn’t be true if we included a <code>break</code>, because the loop would terminate
|
||
when it got to the <code>break</code>.</p>
|
||
<h3 id="dynamically-sized-types-and-the-sized-trait"><a class="header" href="#dynamically-sized-types-and-the-sized-trait">Dynamically Sized Types and the <code>Sized</code> Trait</a></h3>
|
||
<p>Rust needs to know certain details about its types, such as how much space to
|
||
allocate for a value of a particular type. This leaves one corner of its type
|
||
system a little confusing at first: the concept of <em>dynamically sized types</em>.
|
||
Sometimes referred to as <em>DSTs</em> or <em>unsized types</em>, these types let us write
|
||
code using values whose size we can know only at runtime.</p>
|
||
<p>Let’s dig into the details of a dynamically sized type called <code>str</code>, which
|
||
we’ve been using throughout the book. That’s right, not <code>&str</code>, but <code>str</code> on
|
||
its own, is a DST. In many cases, such as when storing text entered by a user,
|
||
we can’t know how long the string is until runtime. That means we can’t create
|
||
a variable of type <code>str</code>, nor can we take an argument of type <code>str</code>. Consider
|
||
the following code, which does not work:</p>
|
||
<pre><code class="language-rust ignore does_not_compile"><span class="boring">fn main() {
|
||
</span> let s1: str = "Hello there!";
|
||
let s2: str = "How's it going?";
|
||
<span class="boring">}</span></code></pre>
|
||
<p>Rust needs to know how much memory to allocate for any value of a particular
|
||
type, and all values of a type must use the same amount of memory. If Rust
|
||
allowed us to write this code, these two <code>str</code> values would need to take up the
|
||
same amount of space. But they have different lengths: <code>s1</code> needs 12 bytes of
|
||
storage and <code>s2</code> needs 15. This is why it’s not possible to create a variable
|
||
holding a dynamically sized type.</p>
|
||
<p>So, what do we do? In this case, you already know the answer: We make the type
|
||
of <code>s1</code> and <code>s2</code> string slice (<code>&str</code>) rather than <code>str</code>. Recall from the
|
||
<a href="ch04-03-slices.html#string-slices">“String Slices”</a><!-- ignore --> section in Chapter 4 that the
|
||
slice data structure only stores the starting position and the length of the
|
||
slice. So, although <code>&T</code> is a single value that stores the memory address of
|
||
where the <code>T</code> is located, a string slice is <em>two</em> values: the address of the
|
||
<code>str</code> and its length. As such, we can know the size of a string slice value at
|
||
compile time: It’s twice the length of a <code>usize</code>. That is, we always know the
|
||
size of a string slice, no matter how long the string it refers to is. In
|
||
general, this is the way in which dynamically sized types are used in Rust:
|
||
They have an extra bit of metadata that stores the size of the dynamic
|
||
information. The golden rule of dynamically sized types is that we must always
|
||
put values of dynamically sized types behind a pointer of some kind.</p>
|
||
<p>We can combine <code>str</code> with all kinds of pointers: for example, <code>Box<str></code> or
|
||
<code>Rc<str></code>. In fact, you’ve seen this before but with a different dynamically
|
||
sized type: traits. Every trait is a dynamically sized type we can refer to by
|
||
using the name of the trait. In the <a href="ch18-02-trait-objects.html#using-trait-objects-to-abstract-over-shared-behavior">“Using Trait Objects to Abstract over
|
||
Shared Behavior”</a><!--
|
||
ignore --> section in Chapter 18, we mentioned that to use traits as trait
|
||
objects, we must put them behind a pointer, such as <code>&dyn Trait</code> or <code>Box<dyn Trait></code> (<code>Rc<dyn Trait></code> would work too).</p>
|
||
<p>To work with DSTs, Rust provides the <code>Sized</code> trait to determine whether or not
|
||
a type’s size is known at compile time. This trait is automatically implemented
|
||
for everything whose size is known at compile time. In addition, Rust
|
||
implicitly adds a bound on <code>Sized</code> to every generic function. That is, a
|
||
generic function definition like this:</p>
|
||
<pre><code class="language-rust ignore">fn generic<T>(t: T) {
|
||
// --snip--
|
||
}</code></pre>
|
||
<p>is actually treated as though we had written this:</p>
|
||
<pre><code class="language-rust ignore">fn generic<T: Sized>(t: T) {
|
||
// --snip--
|
||
}</code></pre>
|
||
<p>By default, generic functions will work only on types that have a known size at
|
||
compile time. However, you can use the following special syntax to relax this
|
||
restriction:</p>
|
||
<pre><code class="language-rust ignore">fn generic<T: ?Sized>(t: &T) {
|
||
// --snip--
|
||
}</code></pre>
|
||
<p>A trait bound on <code>?Sized</code> means “<code>T</code> may or may not be <code>Sized</code>,” and this
|
||
notation overrides the default that generic types must have a known size at
|
||
compile time. The <code>?Trait</code> syntax with this meaning is only available for
|
||
<code>Sized</code>, not any other traits.</p>
|
||
<p>Also note that we switched the type of the <code>t</code> parameter from <code>T</code> to <code>&T</code>.
|
||
Because the type might not be <code>Sized</code>, we need to use it behind some kind of
|
||
pointer. In this case, we’ve chosen a reference.</p>
|
||
<p>Next, we’ll talk about functions and closures!</p>
|
||
|
||
</main>
|
||
|
||
<nav class="nav-wrapper" aria-label="Page navigation">
|
||
<!-- Mobile navigation buttons -->
|
||
<a rel="prev" href="ch20-02-advanced-traits.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="ch20-04-advanced-functions-and-closures.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z"/></svg></span>
|
||
</a>
|
||
|
||
<div style="clear: both"></div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
||
<a rel="prev" href="ch20-02-advanced-traits.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="ch20-04-advanced-functions-and-closures.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z"/></svg></span>
|
||
</a>
|
||
</nav>
|
||
|
||
</div>
|
||
|
||
<template id=fa-eye><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M288 32c-80.8 0-145.5 36.8-192.6 80.6C48.6 156 17.3 208 2.5 243.7c-3.3 7.9-3.3 16.7 0 24.6C17.3 304 48.6 356 95.4 399.4C142.5 443.2 207.2 480 288 480s145.5-36.8 192.6-80.6c46.8-43.5 78.1-95.4 93-131.1c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C433.5 68.8 368.8 32 288 32zM432 256c0 79.5-64.5 144-144 144s-144-64.5-144-144s64.5-144 144-144s144 64.5 144 144zM288 192c0 35.3-28.7 64-64 64c-11.5 0-22.3-3-31.6-8.4c-.2 2.8-.4 5.5-.4 8.4c0 53 43 96 96 96s96-43 96-96s-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6z"/></svg></span></template>
|
||
<template id=fa-eye-slash><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M38.8 5.1C28.4-3.1 13.3-1.2 5.1 9.2S-1.2 34.7 9.2 42.9l592 464c10.4 8.2 25.5 6.3 33.7-4.1s6.3-25.5-4.1-33.7L525.6 386.7c39.6-40.6 66.4-86.1 79.9-118.4c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C465.5 68.8 400.8 32 320 32c-68.2 0-125 26.3-169.3 60.8L38.8 5.1zM223.1 149.5C248.6 126.2 282.7 112 320 112c79.5 0 144 64.5 144 144c0 24.9-6.3 48.3-17.4 68.7L408 294.5c5.2-11.8 8-24.8 8-38.5c0-53-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6c0 10.2-2.4 19.8-6.6 28.3l-90.3-70.8zm223.1 298L373 389.9c-16.4 6.5-34.3 10.1-53 10.1c-79.5 0-144-64.5-144-144c0-6.9 .5-13.6 1.4-20.2L83.1 161.5C60.3 191.2 44 220.8 34.5 243.7c-3.3 7.9-3.3 16.7 0 24.6c14.9 35.7 46.2 87.7 93 131.1C174.5 443.2 239.2 480 320 480c47.8 0 89.9-12.9 126.2-32.5z"/></svg></span></template>
|
||
<template id=fa-copy><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M502.6 70.63l-61.25-61.25C435.4 3.371 427.2 0 418.7 0H255.1c-35.35 0-64 28.66-64 64l.0195 256C192 355.4 220.7 384 256 384h192c35.2 0 64-28.8 64-64V93.25C512 84.77 508.6 76.63 502.6 70.63zM464 320c0 8.836-7.164 16-16 16H255.1c-8.838 0-16-7.164-16-16L239.1 64.13c0-8.836 7.164-16 16-16h128L384 96c0 17.67 14.33 32 32 32h47.1V320zM272 448c0 8.836-7.164 16-16 16H63.1c-8.838 0-16-7.164-16-16L47.98 192.1c0-8.836 7.164-16 16-16H160V128H63.99c-35.35 0-64 28.65-64 64l.0098 256C.002 483.3 28.66 512 64 512h192c35.2 0 64-28.8 64-64v-32h-47.1L272 448z"/></svg></span></template>
|
||
<template id=fa-play><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80V432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"/></svg></span></template>
|
||
<template id=fa-clock-rotate-left><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M75 75L41 41C25.9 25.9 0 36.6 0 57.9V168c0 13.3 10.7 24 24 24H134.1c21.4 0 32.1-25.9 17-41l-30.8-30.8C155 85.5 203 64 256 64c106 0 192 86 192 192s-86 192-192 192c-40.8 0-78.6-12.7-109.7-34.4c-14.5-10.1-34.4-6.6-44.6 7.9s-6.6 34.4 7.9 44.6C151.2 495 201.7 512 256 512c141.4 0 256-114.6 256-256S397.4 0 256 0C185.3 0 121.3 28.7 75 75zm181 53c-13.3 0-24 10.7-24 24V256c0 6.4 2.5 12.5 7 17l72 72c9.4 9.4 24.6 9.4 33.9 0s9.4-24.6 0-33.9l-65-65V152c0-13.3-10.7-24-24-24z"/></svg></span></template>
|
||
|
||
|
||
|
||
<script>
|
||
window.playground_copyable = true;
|
||
</script>
|
||
|
||
|
||
<script src="elasticlunr-ef4e11c1.min.js"></script>
|
||
<script src="mark-09e88c2c.min.js"></script>
|
||
<script src="searcher-09f2665d.js"></script>
|
||
|
||
<script src="clipboard-1626706a.min.js"></script>
|
||
<script src="highlight-abc7f01d.js"></script>
|
||
<script src="book-c22b7243.js"></script>
|
||
|
||
<!-- Custom JS scripts -->
|
||
<script src="ferris-2317480c.js"></script>
|
||
|
||
|
||
|
||
</div>
|
||
</body>
|
||
</html>
|