|
|
<!DOCTYPE HTML>
|
|
|
<html lang="en" class="light sidebar-visible" dir="ltr">
|
|
|
<head>
|
|
|
<!-- Book generated using mdBook -->
|
|
|
<meta charset="UTF-8">
|
|
|
<title>泛型数据类型 - Rust 程序设计语言 简体中文版</title>
|
|
|
|
|
|
|
|
|
<!-- Custom HTML head -->
|
|
|
|
|
|
<meta name="description" content="">
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
<meta name="theme-color" content="#ffffff">
|
|
|
|
|
|
<link rel="icon" href="favicon-de23e50b.svg">
|
|
|
<link rel="shortcut icon" href="favicon-8114d1fc.png">
|
|
|
<link rel="stylesheet" href="css/variables-8adf115d.css">
|
|
|
<link rel="stylesheet" href="css/general-2459343d.css">
|
|
|
<link rel="stylesheet" href="css/chrome-ae938929.css">
|
|
|
<link rel="stylesheet" href="css/print-9e4910d8.css" media="print">
|
|
|
|
|
|
<!-- Fonts -->
|
|
|
<link rel="stylesheet" href="fonts/fonts-9644e21d.css">
|
|
|
|
|
|
<!-- Highlight.js Stylesheets -->
|
|
|
<link rel="stylesheet" id="mdbook-highlight-css" href="highlight-493f70e1.css">
|
|
|
<link rel="stylesheet" id="mdbook-tomorrow-night-css" href="tomorrow-night-4c0ae647.css">
|
|
|
<link rel="stylesheet" id="mdbook-ayu-highlight-css" href="ayu-highlight-3fdfc3ac.css">
|
|
|
|
|
|
<!-- Custom theme stylesheets -->
|
|
|
<link rel="stylesheet" href="ferris-d33b75bf.css">
|
|
|
<link rel="stylesheet" href="theme/2018-edition-4e126c62.css">
|
|
|
<link rel="stylesheet" href="theme/semantic-notes-9b5766c0.css">
|
|
|
<link rel="stylesheet" href="theme/listing-cab26221.css">
|
|
|
|
|
|
|
|
|
<!-- Provide site root and default themes to javascript -->
|
|
|
<script>
|
|
|
const path_to_root = "";
|
|
|
const default_light_theme = "light";
|
|
|
const default_dark_theme = "navy";
|
|
|
window.path_to_searchindex_js = "searchindex-7d935aa5.js";
|
|
|
</script>
|
|
|
<!-- Start loading toc.js asap -->
|
|
|
<script src="toc-9d5d6d0e.js"></script>
|
|
|
</head>
|
|
|
<body>
|
|
|
<div id="mdbook-help-container">
|
|
|
<div id="mdbook-help-popup">
|
|
|
<h2 class="mdbook-help-title">Keyboard shortcuts</h2>
|
|
|
<div>
|
|
|
<p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p>
|
|
|
<p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
|
|
|
<p>Press <kbd>?</kbd> to show this help</p>
|
|
|
<p>Press <kbd>Esc</kbd> to hide this help</p>
|
|
|
</div>
|
|
|
</div>
|
|
|
</div>
|
|
|
<div id="mdbook-body-container">
|
|
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
|
<script>
|
|
|
try {
|
|
|
let theme = localStorage.getItem('mdbook-theme');
|
|
|
let sidebar = localStorage.getItem('mdbook-sidebar');
|
|
|
|
|
|
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
|
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
|
}
|
|
|
|
|
|
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
|
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
|
}
|
|
|
} catch (e) { }
|
|
|
</script>
|
|
|
|
|
|
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
|
<script>
|
|
|
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
|
|
|
let theme;
|
|
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
|
const html = document.documentElement;
|
|
|
html.classList.remove('light')
|
|
|
html.classList.add(theme);
|
|
|
html.classList.add("js");
|
|
|
</script>
|
|
|
|
|
|
<input type="checkbox" id="mdbook-sidebar-toggle-anchor" class="hidden">
|
|
|
|
|
|
<!-- Hide / unhide sidebar before it is displayed -->
|
|
|
<script>
|
|
|
let sidebar = null;
|
|
|
const sidebar_toggle = document.getElementById("mdbook-sidebar-toggle-anchor");
|
|
|
if (document.body.clientWidth >= 1080) {
|
|
|
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
|
sidebar = sidebar || 'visible';
|
|
|
} else {
|
|
|
sidebar = 'hidden';
|
|
|
sidebar_toggle.checked = false;
|
|
|
}
|
|
|
if (sidebar === 'visible') {
|
|
|
sidebar_toggle.checked = true;
|
|
|
} else {
|
|
|
html.classList.remove('sidebar-visible');
|
|
|
}
|
|
|
</script>
|
|
|
|
|
|
<nav id="mdbook-sidebar" class="sidebar" aria-label="Table of contents">
|
|
|
<!-- populated by js -->
|
|
|
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
|
|
|
<noscript>
|
|
|
<iframe class="sidebar-iframe-outer" src="toc.html"></iframe>
|
|
|
</noscript>
|
|
|
<div id="mdbook-sidebar-resize-handle" class="sidebar-resize-handle">
|
|
|
<div class="sidebar-resize-indicator"></div>
|
|
|
</div>
|
|
|
</nav>
|
|
|
|
|
|
<div id="mdbook-page-wrapper" class="page-wrapper">
|
|
|
|
|
|
<div class="page">
|
|
|
<div id="mdbook-menu-bar-hover-placeholder"></div>
|
|
|
<div id="mdbook-menu-bar" class="menu-bar sticky">
|
|
|
<div class="left-buttons">
|
|
|
<label id="mdbook-sidebar-toggle" class="icon-button" for="mdbook-sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="mdbook-sidebar">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M0 96C0 78.3 14.3 64 32 64H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32C14.3 128 0 113.7 0 96zM0 256c0-17.7 14.3-32 32-32H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32c-17.7 0-32-14.3-32-32zM448 416c0 17.7-14.3 32-32 32H32c-17.7 0-32-14.3-32-32s14.3-32 32-32H416c17.7 0 32 14.3 32 32z"/></svg></span>
|
|
|
</label>
|
|
|
<button id="mdbook-theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="mdbook-theme-list">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M371.3 367.1c27.3-3.9 51.9-19.4 67.2-42.9L600.2 74.1c12.6-19.5 9.4-45.3-7.6-61.2S549.7-4.4 531.1 9.6L294.4 187.2c-24 18-38.2 46.1-38.4 76.1L371.3 367.1zm-19.6 25.4l-116-104.4C175.9 290.3 128 339.6 128 400c0 3.9 .2 7.8 .6 11.6c1.8 17.5-10.2 36.4-27.8 36.4H96c-17.7 0-32 14.3-32 32s14.3 32 32 32H240c61.9 0 112-50.1 112-112c0-2.5-.1-5-.2-7.5z"/></svg></span>
|
|
|
</button>
|
|
|
<ul id="mdbook-theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-default_theme">Auto</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-light">Light</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-rust">Rust</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-coal">Coal</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-navy">Navy</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-ayu">Ayu</button></li>
|
|
|
</ul>
|
|
|
<button id="mdbook-search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="mdbook-searchbar">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M416 208c0 45.9-14.9 88.3-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM208 352c79.5 0 144-64.5 144-144s-64.5-144-144-144S64 128.5 64 208s64.5 144 144 144z"/></svg></span>
|
|
|
</button>
|
|
|
</div>
|
|
|
|
|
|
<h1 class="menu-title">Rust 程序设计语言 简体中文版</h1>
|
|
|
|
|
|
<div class="right-buttons">
|
|
|
<a href="print.html" title="Print this book" aria-label="Print this book">
|
|
|
<span class=fa-svg id="print-button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M128 0C92.7 0 64 28.7 64 64v96h64V64H354.7L384 93.3V160h64V93.3c0-17-6.7-33.3-18.7-45.3L400 18.7C388 6.7 371.7 0 354.7 0H128zM384 352v32 64H128V384 368 352H384zm64 32h32c17.7 0 32-14.3 32-32V256c0-35.3-28.7-64-64-64H64c-35.3 0-64 28.7-64 64v96c0 17.7 14.3 32 32 32H64v64c0 35.3 28.7 64 64 64H384c35.3 0 64-28.7 64-64V384zm-16-88c-13.3 0-24-10.7-24-24s10.7-24 24-24s24 10.7 24 24s-10.7 24-24 24z"/></svg></span>
|
|
|
</a>
|
|
|
<a href="https://github.com/KaiserY/trpl-zh-cn/tree/main" title="Git repository" aria-label="Git repository">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg></span>
|
|
|
</a>
|
|
|
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<div id="mdbook-search-wrapper" class="hidden">
|
|
|
<form id="mdbook-searchbar-outer" class="searchbar-outer">
|
|
|
<div class="search-wrapper">
|
|
|
<input type="search" id="mdbook-searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="mdbook-searchresults-outer" aria-describedby="searchresults-header">
|
|
|
<div class="spinner-wrapper">
|
|
|
<span class=fa-svg id="fa-spin"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M304 48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zm0 416c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM48 304c26.5 0 48-21.5 48-48s-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48zm464-48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM142.9 437c18.7-18.7 18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zm0-294.2c18.7-18.7 18.7-49.1 0-67.9S93.7 56.2 75 75s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zM369.1 437c18.7 18.7 49.1 18.7 67.9 0s18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9z"/></svg></span>
|
|
|
</div>
|
|
|
</div>
|
|
|
</form>
|
|
|
<div id="mdbook-searchresults-outer" class="searchresults-outer hidden">
|
|
|
<div id="mdbook-searchresults-header" class="searchresults-header"></div>
|
|
|
<ul id="mdbook-searchresults">
|
|
|
</ul>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
|
<script>
|
|
|
document.getElementById('mdbook-sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
|
document.getElementById('mdbook-sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
|
Array.from(document.querySelectorAll('#mdbook-sidebar a')).forEach(function(link) {
|
|
|
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
|
});
|
|
|
</script>
|
|
|
|
|
|
<div id="mdbook-content" class="content">
|
|
|
<main>
|
|
|
<h2 id="泛型数据类型"><a class="header" href="#泛型数据类型">泛型数据类型</a></h2>
|
|
|
<!-- https://github.com/rust-lang/book/blob/main/src/ch10-01-syntax.md -->
|
|
|
<!-- commit 6c22ad745951671f9ee2689936881e62ef2a8069 -->
|
|
|
<p>我们可以使用泛型为像函数签名或结构体这样的项创建定义,这样它们就可以用于多种不同的具体数据类型。让我们看看如何使用泛型定义函数、结构体、枚举和方法,然后我们将讨论泛型如何影响代码性能。</p>
|
|
|
<h3 id="在函数定义中使用泛型"><a class="header" href="#在函数定义中使用泛型">在函数定义中使用泛型</a></h3>
|
|
|
<p>当使用泛型定义函数时,本来在函数签名中指定参数和返回值的类型的地方,会改用泛型来表示。采用这种技术,使得代码适应性更强,从而为函数的调用者提供更多的功能,同时也避免了代码的重复。</p>
|
|
|
<p>回到 <code>largest</code> 函数,示例 10-4 中展示了两个函数,它们的功能都是寻找 slice 中最大值。接着我们使用泛型将其合并为一个函数。</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024">fn largest_i32(list: &[i32]) -> &i32 {
|
|
|
let mut largest = &list[0];
|
|
|
|
|
|
for item in list {
|
|
|
if item > largest {
|
|
|
largest = item;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
largest
|
|
|
}
|
|
|
|
|
|
fn largest_char(list: &[char]) -> &char {
|
|
|
let mut largest = &list[0];
|
|
|
|
|
|
for item in list {
|
|
|
if item > largest {
|
|
|
largest = item;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
largest
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
|
let number_list = vec![34, 50, 25, 100, 65];
|
|
|
|
|
|
let result = largest_i32(&number_list);
|
|
|
println!("The largest number is {result}");
|
|
|
<span class="boring"> assert_eq!(*result, 100);
|
|
|
</span>
|
|
|
let char_list = vec!['y', 'm', 'a', 'q'];
|
|
|
|
|
|
let result = largest_char(&char_list);
|
|
|
println!("The largest char is {result}");
|
|
|
<span class="boring"> assert_eq!(*result, 'y');
|
|
|
</span>}</code></pre>
|
|
|
<p><span class="caption">示例 10-4:两个函数,不同点只是名称和签名类型</span></p>
|
|
|
<p><code>largest_i32</code> 函数是从示例 10-3 中摘出来的,它用来寻找 slice 中最大的 <code>i32</code>。<code>largest_char</code> 函数寻找 slice 中最大的 <code>char</code>。因为两者函数体的代码是一样的,我们可以定义一个函数,再引进泛型参数来消除这种重复。</p>
|
|
|
<p>为了参数化这个新函数中的这些类型,我们需要为类型参数命名,道理和给函数的形参起名一样。任何标识符都可以作为类型参数的名字。这里选用 <code>T</code>,因为传统上来说,Rust 的类型参数名字都比较短,通常仅为一个字母,同时,Rust 类型名的命名规范是首字母大写驼峰式命名法(UpperCamelCase)。<code>T</code> 作为 “type” 的缩写是大部分 Rust 程序员的首选。</p>
|
|
|
<p>如果要在函数体中使用参数,就必须在函数签名中声明它的名字,好让编译器知道这个名字指代的是什么。同理,当在函数签名中使用一个类型参数时,必须在使用它之前就声明它。为了定义泛型版本的 <code>largest</code> 函数,类型参数声明位于函数名称与参数列表中间的尖括号 <code><></code> 中,像这样:</p>
|
|
|
<pre><code class="language-rust ignore">fn largest<T>(list: &[T]) -> &T {</code></pre>
|
|
|
<p>可以这样理解这个定义:函数 <code>largest</code> 有泛型类型 <code>T</code>。它有个参数 <code>list</code>,其类型是元素为 <code>T</code> 的 slice。<code>largest</code> 函数会返回一个与 <code>T</code> 相同类型的引用。</p>
|
|
|
<p>示例 10-5 中的 <code>largest</code> 函数在它的签名中使用了泛型,统一了两个实现。该示例也展示了如何调用 <code>largest</code> 函数,把 <code>i32</code> 值的 slice 或 <code>char</code> 值的 slice 传给它。请注意这些代码还不能编译。</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre><code class="language-rust ignore does_not_compile">fn largest<T>(list: &[T]) -> &T {
|
|
|
let mut largest = &list[0];
|
|
|
|
|
|
for item in list {
|
|
|
if item > largest {
|
|
|
largest = item;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
largest
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
|
let number_list = vec![34, 50, 25, 100, 65];
|
|
|
|
|
|
let result = largest(&number_list);
|
|
|
println!("The largest number is {result}");
|
|
|
|
|
|
let char_list = vec!['y', 'm', 'a', 'q'];
|
|
|
|
|
|
let result = largest(&char_list);
|
|
|
println!("The largest char is {result}");
|
|
|
}</code></pre>
|
|
|
<p><span class="caption">示例 10-5:一个使用泛型参数的 <code>largest</code> 函数定义,尚不能编译</span></p>
|
|
|
<p>如果现在就编译这段代码,会出现如下错误:</p>
|
|
|
<pre><code class="language-console">$ cargo run
|
|
|
Compiling chapter10 v0.1.0 (file:///projects/chapter10)
|
|
|
error[E0369]: binary operation `>` cannot be applied to type `&T`
|
|
|
--> src/main.rs:5:17
|
|
|
|
|
|
|
5 | if item > largest {
|
|
|
| ---- ^ ------- &T
|
|
|
| |
|
|
|
| &T
|
|
|
|
|
|
|
help: consider restricting type parameter `T` with trait `PartialOrd`
|
|
|
|
|
|
|
1 | fn largest<T: std::cmp::PartialOrd>(list: &[T]) -> &T {
|
|
|
| ++++++++++++++++++++++
|
|
|
|
|
|
For more information about this error, try `rustc --explain E0369`.
|
|
|
error: could not compile `chapter10` (bin "chapter10") due to 1 previous error
|
|
|
</code></pre>
|
|
|
<p>帮助说明中提到了 <code>std::cmp::PartialOrd</code>,这是一个 <em>trait</em>。下一部分会讲到 trait。不过简单来说,这个错误表明 <code>largest</code> 的函数体不能适用于 <code>T</code> 的所有可能的类型。因为在函数体需要比较 <code>T</code> 类型的值,不过它只能用于我们知道如何排序的类型。为了开启比较功能,标准库中定义的 <code>std::cmp::PartialOrd</code> trait 可以实现类型的比较功能(查看附录 C 获取该 trait 的更多信息)。依照帮助说明中的建议,我们限制 <code>T</code> 只对实现了 <code>PartialOrd</code> 的类型有效后代码就可以编译了,因为标准库为 <code>i32</code> 和 <code>char</code> 实现了 <code>PartialOrd</code>。</p>
|
|
|
<h3 id="结构体定义中的泛型"><a class="header" href="#结构体定义中的泛型">结构体定义中的泛型</a></h3>
|
|
|
<p>同样也可以用 <code><></code> 语法来定义结构体,它包含一个或多个泛型参数类型字段。示例 10-6 定义了一个可以存放任何类型的 <code>x</code> 和 <code>y</code> 坐标值的结构体 <code>Point</code>:</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024">struct Point<T> {
|
|
|
x: T,
|
|
|
y: T,
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
|
let integer = Point { x: 5, y: 10 };
|
|
|
let float = Point { x: 1.0, y: 4.0 };
|
|
|
}</code></pre>
|
|
|
<p><span class="caption">示例 10-6:<code>Point</code> 结构体存放了两个 <code>T</code> 类型的值 <code>x</code> 和 <code>y</code></span></p>
|
|
|
<p>在结构体定义中使用泛型的语法类似于函数定义中使用泛型。首先,必须在结构体名称后面的尖括号中声明泛型参数的名称。接着在结构体定义中可以指定具体数据类型的位置使用泛型类型。</p>
|
|
|
<p>注意 <code>Point<T></code> 的定义中只使用了一个泛型类型,这个定义表明结构体 <code>Point<T></code> 对于一些类型 <code>T</code> 是泛型的,而且字段 <code>x</code> 和 <code>y</code> <strong>都是</strong> 相同类型的,无论它具体是何类型。如果尝试创建一个有不同类型值的 <code>Point<T></code> 的实例,像示例 10-7 中的代码就不能编译:</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre><code class="language-rust ignore does_not_compile">struct Point<T> {
|
|
|
x: T,
|
|
|
y: T,
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
|
let wont_work = Point { x: 5, y: 4.0 };
|
|
|
}</code></pre>
|
|
|
<p><span class="caption">示例 10-7:字段 <code>x</code> 和 <code>y</code> 的类型必须相同,因为它们都有相同的泛型类型 <code>T</code></span></p>
|
|
|
<p>在这个例子中,当把整型值 <code>5</code> 赋值给 <code>x</code> 时,就告诉了编译器这个 <code>Point<T></code> 实例中的泛型 <code>T</code> 全是整型。接着指定 <code>y</code> 为浮点值 <code>4.0</code>,因为 <code>y</code> 被定义为与 <code>x</code> 相同类型,所以将会得到一个像这样的类型不匹配错误:</p>
|
|
|
<pre><code class="language-console">$ cargo run
|
|
|
Compiling chapter10 v0.1.0 (file:///projects/chapter10)
|
|
|
error[E0308]: mismatched types
|
|
|
--> src/main.rs:7:38
|
|
|
|
|
|
|
7 | let wont_work = Point { x: 5, y: 4.0 };
|
|
|
| ^^^ expected integer, found floating-point number
|
|
|
|
|
|
For more information about this error, try `rustc --explain E0308`.
|
|
|
error: could not compile `chapter10` (bin "chapter10") due to 1 previous error
|
|
|
</code></pre>
|
|
|
<p>如果想要定义一个 <code>x</code> 和 <code>y</code> 可以有不同类型且仍然是泛型的 <code>Point</code> 结构体,我们可以使用多个泛型类型参数。在示例 10-8 中,我们修改 <code>Point</code> 的定义为拥有两个泛型类型 <code>T</code> 和 <code>U</code>。其中字段 <code>x</code> 是 <code>T</code> 类型的,而字段 <code>y</code> 是 <code>U</code> 类型的:</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024">struct Point<T, U> {
|
|
|
x: T,
|
|
|
y: U,
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
|
let both_integer = Point { x: 5, y: 10 };
|
|
|
let both_float = Point { x: 1.0, y: 4.0 };
|
|
|
let integer_and_float = Point { x: 5, y: 4.0 };
|
|
|
}</code></pre>
|
|
|
<p><span class="caption">示例 10-8:使用两个泛型的 <code>Point</code>,这样 <code>x</code> 和 <code>y</code> 可能是不同类型</span></p>
|
|
|
<p>现在所有这些 <code>Point</code> 实例都合法了!你可以在定义中使用任意多的泛型类型参数,不过太多的话,代码将难以阅读和理解。当你发现代码中需要很多泛型时,这可能表明你的代码需要重构分解成更小的结构。</p>
|
|
|
<h3 id="枚举定义中的泛型"><a class="header" href="#枚举定义中的泛型">枚举定义中的泛型</a></h3>
|
|
|
<p>和结构体类似,枚举也可以在成员中存放泛型数据类型。第六章我们曾用过标准库提供的 <code>Option<T></code> 枚举,这里再回顾一下:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2024"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>enum Option<T> {
|
|
|
Some(T),
|
|
|
None,
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>现在这个定义应该更容易理解了。如你所见 <code>Option<T></code> 是一个拥有泛型 <code>T</code> 的枚举,它有两个成员:<code>Some</code>,它存放了一个类型 <code>T</code> 的值,和不存在任何值的<code>None</code>。通过 <code>Option<T></code> 枚举可以表达有一个可能的值的抽象概念,同时因为 <code>Option<T></code> 是泛型的,无论这个可能的值是什么类型都可以使用这个抽象。</p>
|
|
|
<p>枚举也可以拥有多个泛型类型。第九章使用过的 <code>Result</code> 枚举定义就是一个这样的例子:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2024"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>enum Result<T, E> {
|
|
|
Ok(T),
|
|
|
Err(E),
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p><code>Result</code> 枚举有两个泛型类型,<code>T</code> 和 <code>E</code>。<code>Result</code> 有两个成员:<code>Ok</code>,它存放一个类型 <code>T</code> 的值,而 <code>Err</code> 则存放一个类型 <code>E</code> 的值。这个定义使得 <code>Result</code> 枚举能很方便的表达任何可能成功(返回 <code>T</code> 类型的值)也可能失败(返回 <code>E</code> 类型的值)的操作。实际上,这就是我们在示例 9-3 用来打开文件的方式:当成功打开文件的时候,<code>T</code> 对应的是 <code>std::fs::File</code> 类型;而当打开文件出现问题时,<code>E</code> 的值则是 <code>std::io::Error</code> 类型。</p>
|
|
|
<p>当你意识到代码中定义了多个结构体或枚举,它们不一样的地方只是其中的值的类型的时候,不妨通过泛型类型来避免重复。</p>
|
|
|
<h3 id="方法定义中的泛型"><a class="header" href="#方法定义中的泛型">方法定义中的泛型</a></h3>
|
|
|
<p>在为结构体和枚举实现方法时(像第五章那样),一样也可以用泛型。示例 10-9 中展示了示例 10-6 中定义的结构体 <code>Point<T></code>,和在其上实现的名为 <code>x</code> 的方法。</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024">struct Point<T> {
|
|
|
x: T,
|
|
|
y: T,
|
|
|
}
|
|
|
|
|
|
impl<T> Point<T> {
|
|
|
fn x(&self) -> &T {
|
|
|
&self.x
|
|
|
}
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
|
let p = Point { x: 5, y: 10 };
|
|
|
|
|
|
println!("p.x = {}", p.x());
|
|
|
}</code></pre>
|
|
|
<p><span class="caption">示例 10-9:在 <code>Point<T></code> 结构体上实现方法 <code>x</code>,它返回 <code>T</code> 类型的字段 <code>x</code> 的引用</span></p>
|
|
|
<p>这里在 <code>Point<T></code> 上定义了一个叫做 <code>x</code> 的方法用于返回字段 <code>x</code> 中数据的引用。</p>
|
|
|
<p>注意必须在 <code>impl</code> 后面声明 <code>T</code>,这样就可以在 <code>Point<T></code> 上实现的方法中使用 <code>T</code> 了。通过在 <code>impl</code> 之后声明泛型 <code>T</code>,Rust 就知道 <code>Point</code> 的尖括号中的类型是泛型而不是具体类型。我们可以为泛型参数选择一个与结构体定义中声明的泛型参数所不同的名称,不过依照惯例使用了相同的名称。如果你在<code>impl</code>中编写一个声明泛型类型的方法,那么该方法将在任何类型的实例上定义,无论最终用什么具体类型来替换泛型类型。(译者注:以示例 10-9 为例,<code>impl</code> 中声明了泛型类型参数 <code>T</code>,<code>x</code> 是编写在 <code>impl</code> 中的方法,<code>x</code> 方法将会定义在 <code>Point<T></code> 的任何实例上,无论最终替换泛型类型参数 <code>T</code> 的是何具体类型)。</p>
|
|
|
<p>定义方法时也可以为泛型指定限制(constraint)。例如,可以选择为 <code>Point<f32></code> 实例实现方法,而不是为泛型 <code>Point</code> 实例。示例 10-10 展示了一个没有在 <code>impl</code> 之后(的尖括号)声明泛型的例子,这里使用了一个具体类型,<code>f32</code>:</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024"><span class="boring">struct Point<T> {
|
|
|
</span><span class="boring"> x: T,
|
|
|
</span><span class="boring"> y: T,
|
|
|
</span><span class="boring">}
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring">impl<T> Point<T> {
|
|
|
</span><span class="boring"> fn x(&self) -> &T {
|
|
|
</span><span class="boring"> &self.x
|
|
|
</span><span class="boring"> }
|
|
|
</span><span class="boring">}
|
|
|
</span><span class="boring">
|
|
|
</span>impl Point<f32> {
|
|
|
fn distance_from_origin(&self) -> f32 {
|
|
|
(self.x.powi(2) + self.y.powi(2)).sqrt()
|
|
|
}
|
|
|
}
|
|
|
<span class="boring">
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span><span class="boring"> let p = Point { x: 5, y: 10 };
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring"> println!("p.x = {}", p.x());
|
|
|
</span><span class="boring">}</span></code></pre>
|
|
|
<p><span class="caption">示例 10-10:构建一个只用于拥有泛型参数 <code>T</code> 的结构体的具体类型的 <code>impl</code> 块</span></p>
|
|
|
<p>这段代码意味着 <code>Point<f32></code> 类型会有一个方法 <code>distance_from_origin</code>,而其他 <code>T</code> 不是 <code>f32</code> 类型的 <code>Point<T></code> 实例则没有定义此方法。这个方法计算点实例与坐标 (0.0, 0.0) 之间的距离,并使用了只能用于浮点型的数学运算符。</p>
|
|
|
<p>结构体定义中的泛型类型参数并不总是与结构体方法签名中使用的泛型是同一类型。示例 10-11 中为 <code>Point</code> 结构体使用了泛型类型 <code>X1</code> 和 <code>Y1</code>,为 <code>mixup</code> 方法签名使用了 <code>X2</code> 和 <code>Y2</code> 来使得示例更加清楚。这个方法用 <code>self</code> 的 <code>Point</code> 类型的 <code>x</code> 值(类型 <code>X1</code>)和参数的 <code>Point</code> 类型的 <code>y</code> 值(类型 <code>Y2</code>)来创建一个新 <code>Point</code> 类型的实例:</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024">struct Point<X1, Y1> {
|
|
|
x: X1,
|
|
|
y: Y1,
|
|
|
}
|
|
|
|
|
|
impl<X1, Y1> Point<X1, Y1> {
|
|
|
fn mixup<X2, Y2>(self, other: Point<X2, Y2>) -> Point<X1, Y2> {
|
|
|
Point {
|
|
|
x: self.x,
|
|
|
y: other.y,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
|
let p1 = Point { x: 5, y: 10.4 };
|
|
|
let p2 = Point { x: "Hello", y: 'c' };
|
|
|
|
|
|
let p3 = p1.mixup(p2);
|
|
|
|
|
|
println!("p3.x = {}, p3.y = {}", p3.x, p3.y);
|
|
|
}</code></pre>
|
|
|
<p><span class="caption">示例 10-11:方法使用了与结构体定义中不同类型的泛型</span></p>
|
|
|
<p>在 <code>main</code> 函数中,定义了一个有 <code>i32</code> 类型的 <code>x</code>(其值为 <code>5</code>)和 <code>f64</code> 的 <code>y</code>(其值为 <code>10.4</code>)的 <code>Point</code>。<code>p2</code> 则是一个有着字符串 slice 类型的 <code>x</code>(其值为 <code>"Hello"</code>)和 <code>char</code> 类型的 <code>y</code>(其值为<code>c</code>)的 <code>Point</code>。在 <code>p1</code> 上以 <code>p2</code> 作为参数调用 <code>mixup</code> 会返回一个 <code>p3</code>,它会有一个 <code>i32</code> 类型的 <code>x</code>,因为 <code>x</code> 来自 <code>p1</code>,并拥有一个 <code>char</code> 类型的 <code>y</code>,因为 <code>y</code> 来自 <code>p2</code>。<code>println!</code> 会打印出 <code>p3.x = 5, p3.y = c</code>。</p>
|
|
|
<p>这个例子的目的是展示一些泛型通过 <code>impl</code> 声明而另一些通过方法定义声明的情况。这里泛型参数 <code>X1</code> 和 <code>Y1</code> 声明于 <code>impl</code> 之后,因为它们与结构体定义相对应。而泛型参数 <code>X2</code> 和 <code>Y2</code> 声明于 <code>fn mixup</code> 之后,因为它们只是相对于方法本身的。</p>
|
|
|
<h3 id="泛型代码的性能"><a class="header" href="#泛型代码的性能">泛型代码的性能</a></h3>
|
|
|
<p>你可能会好奇使用泛型类型参数是否会有运行时消耗。好消息是泛型并不会使程序比具体类型运行得慢。</p>
|
|
|
<p>Rust 通过在编译时进行泛型代码的<strong>单态化</strong>(<em>monomorphization</em>)来保证效率。单态化是一个通过填充编译时使用的具体类型,将通用代码转换为特定代码的过程。</p>
|
|
|
<p>在这个过程中,编译器所做的工作正好与示例 10-5 中我们创建泛型函数的步骤相反。编译器寻找所有泛型代码被调用的位置并使用泛型代码针对具体类型生成代码。</p>
|
|
|
<p>让我们看看这在标准库的 <code>Option<T></code> 枚举上是如何工作的:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2024"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>let integer = Some(5);
|
|
|
let float = Some(5.0);
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>当 Rust 编译这些代码的时候,它会进行单态化。编译器会读取传递给 <code>Option<T></code> 的值并发现有两种 <code>Option<T></code>:一个对应 <code>i32</code> 另一个对应 <code>f64</code>。为此,它会将泛型定义 <code>Option<T></code> 展开为两个针对 <code>i32</code> 和 <code>f64</code> 的定义,接着将泛型定义替换为这两个具体的定义。</p>
|
|
|
<p>编译器生成的单态化版本的代码看起来像这样(编译器会使用不同于如下假想的名字):</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024">enum Option_i32 {
|
|
|
Some(i32),
|
|
|
None,
|
|
|
}
|
|
|
|
|
|
enum Option_f64 {
|
|
|
Some(f64),
|
|
|
None,
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
|
let integer = Option_i32::Some(5);
|
|
|
let float = Option_f64::Some(5.0);
|
|
|
}</code></pre>
|
|
|
<p>泛型 <code>Option<T></code> 被编译器替换为了具体的定义。因为 Rust 会将每种情况下的泛型代码编译为具体类型,使用泛型没有运行时开销。当代码运行时,它的执行效率就跟好像手写每个具体定义的重复代码一样。这个单态化过程正是 Rust 泛型在运行时极其高效的原因。</p>
|
|
|
|
|
|
</main>
|
|
|
|
|
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
|
<!-- Mobile navigation buttons -->
|
|
|
<a rel="prev" href="ch10-00-generics.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="ch10-02-traits.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="ch10-00-generics.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="ch10-02-traits.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z"/></svg></span>
|
|
|
</a>
|
|
|
</nav>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<template id=fa-eye><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M288 32c-80.8 0-145.5 36.8-192.6 80.6C48.6 156 17.3 208 2.5 243.7c-3.3 7.9-3.3 16.7 0 24.6C17.3 304 48.6 356 95.4 399.4C142.5 443.2 207.2 480 288 480s145.5-36.8 192.6-80.6c46.8-43.5 78.1-95.4 93-131.1c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C433.5 68.8 368.8 32 288 32zM432 256c0 79.5-64.5 144-144 144s-144-64.5-144-144s64.5-144 144-144s144 64.5 144 144zM288 192c0 35.3-28.7 64-64 64c-11.5 0-22.3-3-31.6-8.4c-.2 2.8-.4 5.5-.4 8.4c0 53 43 96 96 96s96-43 96-96s-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6z"/></svg></span></template>
|
|
|
<template id=fa-eye-slash><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M38.8 5.1C28.4-3.1 13.3-1.2 5.1 9.2S-1.2 34.7 9.2 42.9l592 464c10.4 8.2 25.5 6.3 33.7-4.1s6.3-25.5-4.1-33.7L525.6 386.7c39.6-40.6 66.4-86.1 79.9-118.4c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C465.5 68.8 400.8 32 320 32c-68.2 0-125 26.3-169.3 60.8L38.8 5.1zM223.1 149.5C248.6 126.2 282.7 112 320 112c79.5 0 144 64.5 144 144c0 24.9-6.3 48.3-17.4 68.7L408 294.5c5.2-11.8 8-24.8 8-38.5c0-53-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6c0 10.2-2.4 19.8-6.6 28.3l-90.3-70.8zm223.1 298L373 389.9c-16.4 6.5-34.3 10.1-53 10.1c-79.5 0-144-64.5-144-144c0-6.9 .5-13.6 1.4-20.2L83.1 161.5C60.3 191.2 44 220.8 34.5 243.7c-3.3 7.9-3.3 16.7 0 24.6c14.9 35.7 46.2 87.7 93 131.1C174.5 443.2 239.2 480 320 480c47.8 0 89.9-12.9 126.2-32.5z"/></svg></span></template>
|
|
|
<template id=fa-copy><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M502.6 70.63l-61.25-61.25C435.4 3.371 427.2 0 418.7 0H255.1c-35.35 0-64 28.66-64 64l.0195 256C192 355.4 220.7 384 256 384h192c35.2 0 64-28.8 64-64V93.25C512 84.77 508.6 76.63 502.6 70.63zM464 320c0 8.836-7.164 16-16 16H255.1c-8.838 0-16-7.164-16-16L239.1 64.13c0-8.836 7.164-16 16-16h128L384 96c0 17.67 14.33 32 32 32h47.1V320zM272 448c0 8.836-7.164 16-16 16H63.1c-8.838 0-16-7.164-16-16L47.98 192.1c0-8.836 7.164-16 16-16H160V128H63.99c-35.35 0-64 28.65-64 64l.0098 256C.002 483.3 28.66 512 64 512h192c35.2 0 64-28.8 64-64v-32h-47.1L272 448z"/></svg></span></template>
|
|
|
<template id=fa-play><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80V432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"/></svg></span></template>
|
|
|
<template id=fa-clock-rotate-left><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M75 75L41 41C25.9 25.9 0 36.6 0 57.9V168c0 13.3 10.7 24 24 24H134.1c21.4 0 32.1-25.9 17-41l-30.8-30.8C155 85.5 203 64 256 64c106 0 192 86 192 192s-86 192-192 192c-40.8 0-78.6-12.7-109.7-34.4c-14.5-10.1-34.4-6.6-44.6 7.9s-6.6 34.4 7.9 44.6C151.2 495 201.7 512 256 512c141.4 0 256-114.6 256-256S397.4 0 256 0C185.3 0 121.3 28.7 75 75zm181 53c-13.3 0-24 10.7-24 24V256c0 6.4 2.5 12.5 7 17l72 72c9.4 9.4 24.6 9.4 33.9 0s9.4-24.6 0-33.9l-65-65V152c0-13.3-10.7-24-24-24z"/></svg></span></template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
window.playground_copyable = true;
|
|
|
</script>
|
|
|
|
|
|
|
|
|
<script src="elasticlunr-ef4e11c1.min.js"></script>
|
|
|
<script src="mark-09e88c2c.min.js"></script>
|
|
|
<script src="searcher-c2a407aa.js"></script>
|
|
|
|
|
|
<script src="clipboard-1626706a.min.js"></script>
|
|
|
<script src="highlight-abc7f01d.js"></script>
|
|
|
<script src="book-a0b12cfe.js"></script>
|
|
|
|
|
|
<!-- Custom JS scripts -->
|
|
|
<script src="ferris-2317480c.js"></script>
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
</body>
|
|
|
</html>
|