<!DOCTYPE HTML>
< html lang = "zh-CN" class = "light sidebar-visible" dir = "ltr" >
< head >
<!-- Book generated using mdBook -->
< meta charset = "UTF-8" >
< title > 格式化输出 - Rust语言圣经(Rust Course)< / title >
<!-- Custom HTML head -->
< meta name = "description" content = "" >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< meta name = "theme-color" content = "#ffffff" >
< link rel = "icon" href = "../favicon-de23e50b.svg" >
< link rel = "shortcut icon" href = "../favicon-8114d1fc.png" >
< link rel = "stylesheet" href = "../css/variables-8adf115d.css" >
< link rel = "stylesheet" href = "../css/general-2459343d.css" >
< link rel = "stylesheet" href = "../css/chrome-ae938929.css" >
< link rel = "stylesheet" href = "../css/print-9e4910d8.css" media = "print" >
<!-- Fonts -->
< link rel = "stylesheet" href = "../fonts/fonts-9644e21d.css" >
<!-- Highlight.js Stylesheets -->
< link rel = "stylesheet" id = "mdbook-highlight-css" href = "../highlight-493f70e1.css" >
< link rel = "stylesheet" id = "mdbook-tomorrow-night-css" href = "../tomorrow-night-4c0ae647.css" >
< link rel = "stylesheet" id = "mdbook-ayu-highlight-css" href = "../ayu-highlight-3fdfc3ac.css" >
<!-- Custom theme stylesheets -->
< link rel = "stylesheet" href = "../theme/style-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-848c21ce.js";
< / script >
<!-- Start loading toc.js asap -->
< script src = "../toc-42887675.js" > < / script >
< / head >
< body >
< div id = "mdbook-help-container" >
< div id = "mdbook-help-popup" >
< h2 class = "mdbook-help-title" > Keyboard shortcuts< / h2 >
< div >
< p > Press < kbd > ←< / kbd > or < kbd > →< / kbd > to navigate between chapters< / p >
< p > Press < kbd > S< / kbd > or < kbd > /< / kbd > to search in the book< / p >
< p > Press < kbd > ?< / kbd > to show this help< / p >
< p > Press < kbd > Esc< / kbd > to hide this help< / p >
< / div >
< / div >
< / div >
< div id = "mdbook-body-container" >
<!-- Work around some values being stored in localStorage wrapped in quotes -->
< script >
try {
let theme = localStorage.getItem('mdbook-theme');
let sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') & & theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') & & sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
< / script >
<!-- Set the theme before any content is loaded, prevents flash -->
< script >
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
let theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
< / script >
< input type = "checkbox" id = "mdbook-sidebar-toggle-anchor" class = "hidden" >
<!-- Hide / unhide sidebar before it is displayed -->
< script >
let sidebar = null;
const sidebar_toggle = document.getElementById("mdbook-sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
sidebar_toggle.checked = false;
}
if (sidebar === 'visible') {
sidebar_toggle.checked = true;
} else {
html.classList.remove('sidebar-visible');
}
< / script >
< nav id = "mdbook-sidebar" class = "sidebar" aria-label = "Table of contents" >
<!-- populated by js -->
< mdbook-sidebar-scrollbox class = "sidebar-scrollbox" > < / mdbook-sidebar-scrollbox >
< noscript >
< iframe class = "sidebar-iframe-outer" src = "../toc.html" > < / iframe >
< / noscript >
< div id = "mdbook-sidebar-resize-handle" class = "sidebar-resize-handle" >
< div class = "sidebar-resize-indicator" > < / div >
< / div >
< / nav >
< div id = "mdbook-page-wrapper" class = "page-wrapper" >
< div class = "page" >
< div id = "mdbook-menu-bar-hover-placeholder" > < / div >
< div id = "mdbook-menu-bar" class = "menu-bar sticky" >
< div class = "left-buttons" >
< label id = "mdbook-sidebar-toggle" class = "icon-button" for = "mdbook-sidebar-toggle-anchor" title = "Toggle Table of Contents" aria-label = "Toggle Table of Contents" aria-controls = "mdbook-sidebar" >
< span class = fa-svg > < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 448 512" > <!-- ! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --> < path d = "M0 96C0 78.3 14.3 64 32 64H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32C14.3 128 0 113.7 0 96zM0 256c0-17.7 14.3-32 32-32H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32c-17.7 0-32-14.3-32-32zM448 416c0 17.7-14.3 32-32 32H32c-17.7 0-32-14.3-32-32s14.3-32 32-32H416c17.7 0 32 14.3 32 32z" / > < / svg > < / span >
< / label >
< button id = "mdbook-theme-toggle" class = "icon-button" type = "button" title = "Change theme" aria-label = "Change theme" aria-haspopup = "true" aria-expanded = "false" aria-controls = "mdbook-theme-list" >
< span class = fa-svg > < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 576 512" > <!-- ! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --> < path d = "M371.3 367.1c27.3-3.9 51.9-19.4 67.2-42.9L600.2 74.1c12.6-19.5 9.4-45.3-7.6-61.2S549.7-4.4 531.1 9.6L294.4 187.2c-24 18-38.2 46.1-38.4 76.1L371.3 367.1zm-19.6 25.4l-116-104.4C175.9 290.3 128 339.6 128 400c0 3.9 .2 7.8 .6 11.6c1.8 17.5-10.2 36.4-27.8 36.4H96c-17.7 0-32 14.3-32 32s14.3 32 32 32H240c61.9 0 112-50.1 112-112c0-2.5-.1-5-.2-7.5z" / > < / svg > < / span >
< / button >
< ul id = "mdbook-theme-list" class = "theme-popup" aria-label = "Themes" role = "menu" >
< li role = "none" > < button role = "menuitem" class = "theme" id = "mdbook-theme-default_theme" > Auto< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "mdbook-theme-light" > Light< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "mdbook-theme-rust" > Rust< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "mdbook-theme-coal" > Coal< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "mdbook-theme-navy" > Navy< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "mdbook-theme-ayu" > Ayu< / button > < / li >
< / ul >
< button id = "mdbook-search-toggle" class = "icon-button" type = "button" title = "Search (`/`)" aria-label = "Toggle Searchbar" aria-expanded = "false" aria-keyshortcuts = "/ s" aria-controls = "mdbook-searchbar" >
< span class = fa-svg > < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 512 512" > <!-- ! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --> < path d = "M416 208c0 45.9-14.9 88.3-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM208 352c79.5 0 144-64.5 144-144s-64.5-144-144-144S64 128.5 64 208s64.5 144 144 144z" / > < / svg > < / span >
< / button >
< / div >
< h1 class = "menu-title" > Rust语言圣经(Rust Course)< / h1 >
< div class = "right-buttons" >
< a href = "../print.html" title = "Print this book" aria-label = "Print this book" >
< span class = fa-svg id = "print-button" > < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 512 512" > <!-- ! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --> < path d = "M128 0C92.7 0 64 28.7 64 64v96h64V64H354.7L384 93.3V160h64V93.3c0-17-6.7-33.3-18.7-45.3L400 18.7C388 6.7 371.7 0 354.7 0H128zM384 352v32 64H128V384 368 352H384zm64 32h32c17.7 0 32-14.3 32-32V256c0-35.3-28.7-64-64-64H64c-35.3 0-64 28.7-64 64v96c0 17.7 14.3 32 32 32H64v64c0 35.3 28.7 64 64 64H384c35.3 0 64-28.7 64-64V384zm-16-88c-13.3 0-24-10.7-24-24s10.7-24 24-24s24 10.7 24 24s-10.7 24-24 24z" / > < / svg > < / span >
< / a >
< a href = "https://github.com/sunface/rust-course" title = "Git repository" aria-label = "Git repository" >
< span class = fa-svg > < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 496 512" > <!-- ! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --> < path d = "M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z" / > < / svg > < / span >
< / a >
< a href = "https://github.com/sunface/rust-course/edit/main/src/basic/formatted-output.md" title = "Suggest an edit" aria-label = "Suggest an edit" rel = "edit" >
< span class = fa-svg id = "git-edit-button" > < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 512 512" > <!-- ! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --> < path d = "M421.7 220.3l-11.3 11.3-22.6 22.6-205 205c-6.6 6.6-14.8 11.5-23.8 14.1L30.8 511c-8.4 2.5-17.5 .2-23.7-6.1S-1.5 489.7 1 481.2L38.7 353.1c2.6-9 7.5-17.2 14.1-23.8l205-205 22.6-22.6 11.3-11.3 33.9 33.9 62.1 62.1 33.9 33.9zM96 353.9l-9.3 9.3c-.9 .9-1.6 2.1-2 3.4l-25.3 86 86-25.3c1.3-.4 2.5-1.1 3.4-2l9.3-9.3H112c-8.8 0-16-7.2-16-16V353.9zM453.3 19.3l39.4 39.4c25 25 25 65.5 0 90.5l-14.5 14.5-22.6 22.6-11.3 11.3-33.9-33.9-62.1-62.1L314.3 67.7l11.3-11.3 22.6-22.6 14.5-14.5c25-25 65.5-25 90.5 0z" / > < / svg > < / span >
< / a >
< / div >
< / div >
< div id = "mdbook-search-wrapper" class = "hidden" >
< form id = "mdbook-searchbar-outer" class = "searchbar-outer" >
< div class = "search-wrapper" >
< input type = "search" id = "mdbook-searchbar" name = "searchbar" placeholder = "Search this book ..." aria-controls = "mdbook-searchresults-outer" aria-describedby = "searchresults-header" >
< div class = "spinner-wrapper" >
< span class = fa-svg id = "fa-spin" > < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 512 512" > <!-- ! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --> < path d = "M304 48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zm0 416c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM48 304c26.5 0 48-21.5 48-48s-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48zm464-48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM142.9 437c18.7-18.7 18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zm0-294.2c18.7-18.7 18.7-49.1 0-67.9S93.7 56.2 75 75s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zM369.1 437c18.7 18.7 49.1 18.7 67.9 0s18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9z" / > < / svg > < / span >
< / div >
< / div >
< / form >
< div id = "mdbook-searchresults-outer" class = "searchresults-outer hidden" >
< div id = "mdbook-searchresults-header" class = "searchresults-header" > < / div >
< ul id = "mdbook-searchresults" >
< / ul >
< / div >
< / div >
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
< script >
document.getElementById('mdbook-sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('mdbook-sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#mdbook-sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
< / script >
< div id = "mdbook-content" class = "content" >
< main >
< h1 id = "格式化输出" > < a class = "header" href = "#格式化输出" > 格式化输出< / a > < / h1 >
< p > 提到格式化输出,可能很多人立刻就想到 < code > "{}"< / code > ,但是 Rust 能做到的远比这个多的多,本章节我们将深入讲解格式化输出的各个方面。< / p >
< h2 id = "满分初印象" > < a class = "header" href = "#满分初印象" > 满分初印象< / a > < / h2 >
< p > 先来一段代码,看看格式化输出的初印象:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > println!("Hello"); // => "Hello"
println!("Hello, {}!", "world"); // => "Hello, world!"
println!("The number is {}", 1); // => "The number is 1"
println!("{:?}", (3, 4)); // => "(3, 4)"
println!("{value}", value=4); // => "4"
println!("{} {}", 1, 2); // => "1 2"
println!("{:04}", 42); // => "0042" with leading zeros
< span class = "boring" > }< / span > < / code > < / pre >
< p > 可以看到 < code > println!< / code > 宏接受的是可变参数,第一个参数是一个字符串常量,它表示最终输出字符串的格式,包含其中形如 < code > {}< / code > 的符号是< strong > 占位符< / strong > ,会被 < code > println!< / code > 后面的参数依次替换。< / p >
< h2 id = "printprintlnformat" > < a class = "header" href = "#printprintlnformat" > < code > print!< / code > , < code > println!< / code > , < code > format!< / code > < / a > < / h2 >
< p > 它们是 Rust 中用来格式化输出的三大金刚,用途如下:< / p >
< ul >
< li > < code > print!< / code > 将格式化文本输出到标准输出,不带换行符< / li >
< li > < code > println!< / code > 同上,但是在行的末尾添加换行符< / li >
< li > < code > format!< / code > 将格式化文本输出到 < code > String< / code > 字符串< / li >
< / ul >
< p > 在实际项目中,最常用的是 < code > println!< / code > 及 < code > format!< / code > ,前者常用来调试输出,后者常用来生成格式化的字符串:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
let s = "hello";
println!("{}, world", s);
let s1 = format!("{}, world", s);
print!("{}", s1);
print!("{}\n", "!");
}< / code > < / pre >
< p > 其中,< code > s1< / code > 是通过 < code > format!< / code > 生成的 < code > String< / code > 字符串,最终输出如下:< / p >
< pre > < code class = "language-console" > hello, world
hello, world!
< / code > < / pre >
< h4 id = "eprinteprintln" > < a class = "header" href = "#eprinteprintln" > < code > eprint!< / code > , < code > eprintln!< / code > < / a > < / h4 >
< p > 除了三大金刚外,还有两大护法,使用方式跟 < code > print!< / code > , < code > println!< / code > 很像,但是它们输出到标准错误输出:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > eprintln!("Error: Could not complete task")
< span class = "boring" > }< / span > < / code > < / pre >
< p > 它们仅应该被用于输出错误信息和进度信息,其它场景都应该使用 < code > print!< / code > 系列。< / p >
< h2 id = "-与-" > < a class = "header" href = "#-与-" > < code > {}< / code > 与 < code > {:?}< / code > < / a > < / h2 >
< p > 与其它语言常用的 < code > %d< / code > , < code > %s< / code > 不同, Rust 特立独行地选择了 < code > {}< / code > 作为格式化占位符( 说到这个, 有点想吐槽下, Rust 中自创的概念其实还挺多的,真不知道该夸奖还是该吐槽-,-),事实证明,这种选择非常正确,它帮助用户减少了很多使用成本,你无需再为特定的类型选择特定的占位符,统一用 < code > {}< / code > 来替代即可,剩下的类型推导等细节只要交给 Rust 去做。< / p >
< p > 与 < code > {}< / code > 类似,< code > {:?}< / code > 也是占位符:< / p >
< ul >
< li > < code > {}< / code > 适用于实现了 < code > std::fmt::Display< / code > 特征的类型,用来以更优雅、更友好的方式格式化文本,例如展示给用户< / li >
< li > < code > {:?}< / code > 适用于实现了 < code > std::fmt::Debug< / code > 特征的类型,用于调试场景< / li >
< / ul >
< p > 其实两者的选择很简单,当你在写代码需要调试时,使用 < code > {:?}< / code > ,剩下的场景,选择 < code > {}< / code > 。< / p >
< h4 id = "debug-特征" > < a class = "header" href = "#debug-特征" > < code > Debug< / code > 特征< / a > < / h4 >
< p > 事实上,为了方便我们调试,大多数 Rust 类型都实现了 < code > Debug< / code > 特征或者支持派生该特征:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > #[derive(Debug)]
struct Person {
name: String,
age: u8
}
fn main() {
let i = 3.1415926;
let s = String::from("hello");
let v = vec![1, 2, 3];
let p = Person{name: "sunface".to_string(), age: 18};
println!("{:?}, {:?}, {:?}, {:?}", i, s, v, p);
}< / code > < / pre >
< p > 对于数值、字符串、数组,可以直接使用 < code > {:?}< / code > 进行输出,但是对于结构体,需要< a href = "https://course.rs/appendix/derive.html" > 派生< code > Debug< / code > < / a > 特征后,才能进行输出,总之很简单。< / p >
< h4 id = "display-特征" > < a class = "header" href = "#display-特征" > < code > Display< / code > 特征< / a > < / h4 >
< p > 与大部分类型实现了 < code > Debug< / code > 不同,实现了 < code > Display< / code > 特征的 Rust 类型并没有那么多,往往需要我们自定义想要的格式化方式:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > let i = 3.1415926;
let s = String::from("hello");
let v = vec![1, 2, 3];
let p = Person {
name: "sunface".to_string(),
age: 18,
};
println!("{}, {}, {}, {}", i, s, v, p);
< span class = "boring" > }< / span > < / code > < / pre >
< p > 运行后可以看到 < code > v< / code > 和 < code > p< / code > 都无法通过编译,因为没有实现 < code > Display< / code > 特征,但是你又不能像派生 < code > Debug< / code > 一般派生 < code > Display< / code > ,只能另寻他法:< / p >
< ul >
< li > 使用 < code > {:?}< / code > 或 < code > {:#?}< / code > < / li >
< li > 为自定义类型实现 < code > Display< / code > 特征< / li >
< li > 使用 < code > newtype< / code > 为外部类型实现 < code > Display< / code > 特征< / li >
< / ul >
< p > 下面来一一看看这三种方式。< / p >
< h4 id = "" > < a class = "header" href = "#" > < code > {:#?}< / code > < / a > < / h4 >
< p > < code > {:#?}< / code > 与 < code > {:?}< / code > 几乎一样,唯一的区别在于它能更优美地输出内容:< / p >
< pre > < code class = "language-console" > // {:?}
[1, 2, 3], Person { name: "sunface", age: 18 }
// {:#?}
[
1,
2,
3,
], Person {
name: "sunface",
}
< / code > < / pre >
< p > 因此对于 < code > Display< / code > 不支持的类型,可以考虑使用 < code > {:#?}< / code > 进行格式化,虽然理论上它更适合进行调试输出。< / p >
< h4 id = "为自定义类型实现-display-特征" > < a class = "header" href = "#为自定义类型实现-display-特征" > 为自定义类型实现 < code > Display< / code > 特征< / a > < / h4 >
< p > 如果你的类型是定义在当前作用域中的,那么可以为其实现 < code > Display< / code > 特征,即可用于格式化输出:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > struct Person {
name: String,
age: u8,
}
use std::fmt;
impl fmt::Display for Person {
fn fmt(& self, f: & mut fmt::Formatter) -> fmt::Result {
write!(
f,
"大佬在上,请受我一拜,小弟姓名{},年芳{},家里无田又无车,生活苦哈哈",
self.name, self.age
)
}
}
fn main() {
let p = Person {
name: "sunface".to_string(),
age: 18,
};
println!("{}", p);
}< / code > < / pre >
< p > 如上所示,只要实现 < code > Display< / code > 特征中的 < code > fmt< / code > 方法,即可为自定义结构体 < code > Person< / code > 添加自定义输出:< / p >
< pre > < code class = "language-console" > 大佬在上, 请受我一拜, 小弟姓名sunface, 年芳18, 家里无田又无车, 生活苦哈哈
< / code > < / pre >
< h4 id = "为外部类型实现-display-特征" > < a class = "header" href = "#为外部类型实现-display-特征" > 为外部类型实现 < code > Display< / code > 特征< / a > < / h4 >
< p > 在 Rust 中,无法直接为外部类型实现外部特征,但是可以使用< a href = "https://course.rs/advance/into-types/custom-type.html#newtype" > < code > newtype< / code > < / a > 解决此问题:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > struct Array(Vec< i32> );
use std::fmt;
impl fmt::Display for Array {
fn fmt(& self, f: & mut fmt::Formatter) -> fmt::Result {
write!(f, "数组是:{:?}", self.0)
}
}
fn main() {
let arr = Array(vec![1, 2, 3]);
println!("{}", arr);
}< / code > < / pre >
< p > < code > Array< / code > 就是我们的 < code > newtype< / code > ,它将想要格式化输出的 < code > Vec< / code > 包裹在内,最后只要为 < code > Array< / code > 实现 < code > Display< / code > 特征,即可进行格式化输出:< / p >
< pre > < code class = "language-console" > 数组是:[1, 2, 3]
< / code > < / pre >
< p > 至此,关于 < code > {}< / code > 与 < code > {:?}< / code > 的内容已介绍完毕,下面让我们正式开始格式化输出的旅程。< / p >
< h2 id = "位置参数" > < a class = "header" href = "#位置参数" > 位置参数< / a > < / h2 >
< p > 除了按照依次顺序使用值去替换占位符之外,还能让指定位置的参数去替换某个占位符,例如 < code > {1}< / code > ,表示用第二个参数替换该占位符(索引从 0 开始):< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
println!("{}{}", 1, 2); // => "12"
println!("{1}{0}", 1, 2); // => "21"
// => Alice, this is Bob. Bob, this is Alice
println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
println!("{1}{}{0}{}", 1, 2); // => 2112
}< / code > < / pre >
< h2 id = "具名参数" > < a class = "header" href = "#具名参数" > 具名参数< / a > < / h2 >
< p > 除了像上面那样指定位置外,我们还可以为参数指定名称:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
println!("{argument}", argument = "test"); // => "test"
println!("{name} {}", 1, name = 2); // => "2 1"
println!("{a} {c} {b}", a = "a", b = 'b', c = 3); // => "a 3 b"
}< / code > < / pre >
< p > 需要注意的是:< strong > 带名称的参数必须放在不带名称参数的后面< / strong > ,例如下面代码将报错:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > println!("{abc} {1}", abc = "def", 2);
< 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 > error: positional arguments cannot follow named arguments
--> src/main.rs:4:36
|
4 | println!("{abc} {1}", abc = "def", 2);
| ----- ^ positional arguments must be before named arguments
| |
| named argument
< span class = "boring" > }< / span > < / code > < / pre >
< h2 id = "格式化参数" > < a class = "header" href = "#格式化参数" > 格式化参数< / a > < / h2 >
< p > 格式化输出,意味着对输出格式会有更多的要求,例如只输出浮点数的小数点后两位:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
let v = 3.1415926;
// Display => 3.14
println!("{:.2}", v);
// Debug => 3.14
println!("{:.2?}", v);
}< / code > < / pre >
< p > 上面代码只输出小数点后两位。同时我们还展示了 < code > {}< / code > 和 < code > {:?}< / code > 的用法,后面如无特殊区别,就只针对 < code > {}< / code > 提供格式化参数说明。< / p >
< p > 接下来,让我们一起来看看 Rust 中有哪些格式化参数。< / p >
< h3 id = "宽度" > < a class = "header" href = "#宽度" > 宽度< / a > < / h3 >
< p > 宽度用来指示输出目标的长度,如果长度不够,则进行填充和对齐:< / p >
< h4 id = "字符串填充" > < a class = "header" href = "#字符串填充" > 字符串填充< / a > < / h4 >
< p > 字符串格式化默认使用空格进行填充,并且进行左对齐。< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
//-----------------------------------
// 以下全部输出 "Hello x !"
// 为"x"后面填充空格, 补齐宽度5
println!("Hello {:5}!", "x");
// 使用参数5来指定宽度
println!("Hello {:1$}!", "x", 5);
// 使用x作为占位符输出内容, 同时使用5作为宽度
println!("Hello {1:0$}!", 5, "x");
// 使用有名称的参数作为宽度
println!("Hello {:width$}!", "x", width = 5);
//-----------------------------------
// 使用参数5为参数x指定宽度, 同时在结尾输出参数5 => Hello x !5
println!("Hello {:1$}!{}", "x", 5);
}< / code > < / pre >
< h4 id = "数字填充符号和-0" > < a class = "header" href = "#数字填充符号和-0" > 数字填充:符号和 0< / a > < / h4 >
< p > 数字格式化默认也是使用空格进行填充,但与字符串左对齐不同的是,数字是右对齐。< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
// 宽度是5 => Hello 5!
println!("Hello {:5}!", 5);
// 显式的输出正号 => Hello +5!
println!("Hello {:+}!", 5);
// 宽度5, 使用0进行填充 => Hello 00005!
println!("Hello {:05}!", 5);
// 负号也要占用一位宽度 => Hello -0005!
println!("Hello {:05}!", -5);
}< / code > < / pre >
< h3 id = "对齐" > < a class = "header" href = "#对齐" > 对齐< / a > < / h3 >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
// 以下全部都会补齐5个字符的长度
// 左对齐 => Hello x !
println!("Hello {:< 5}!", "x");
// 右对齐 => Hello x!
println!("Hello {:> 5}!", "x");
// 居中对齐 => Hello x !
println!("Hello {:^5}!", "x");
// 对齐并使用指定符号填充 => Hello x& & & & !
// 指定符号填充的前提条件是必须有对齐字符
println!("Hello {:& < 5}!", "x");
}< / code > < / pre >
< h3 id = "精度" > < a class = "header" href = "#精度" > 精度< / a > < / h3 >
< p > 精度可以用于控制浮点数的精度或者字符串的长度< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
let v = 3.1415926;
// 保留小数点后两位 => 3.14
println!("{:.2}", v);
// 带符号保留小数点后两位 => +3.14
println!("{:+.2}", v);
// 不带小数 => 3
println!("{:.0}", v);
// 通过参数来设定精度 => 3.1416,相当于{:.4}
println!("{:.1$}", v, 4);
let s = "hi我是Sunface孙飞";
// 保留字符串前三个字符 => hi我
println!("{:.3}", s);
// {:.*}接收两个参数,第一个是精度,第二个是被格式化的值 => Hello abc!
println!("Hello {:.*}!", 3, "abcdefg");
}< / code > < / pre >
< h3 id = "进制" > < a class = "header" href = "#进制" > 进制< / a > < / h3 >
< p > 可以使用 < code > #< / code > 号来控制数字的进制输出:< / p >
< ul >
< li > < code > #b< / code > , 二进制< / li >
< li > < code > #o< / code > , 八进制< / li >
< li > < code > #x< / code > , 小写十六进制< / li >
< li > < code > #X< / code > , 大写十六进制< / li >
< li > < code > x< / code > , 不带前缀的小写十六进制< / li >
< / ul >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
// 二进制 => 0b11011!
println!("{:#b}!", 27);
// 八进制 => 0o33!
println!("{:#o}!", 27);
// 十进制 => 27!
println!("{}!", 27);
// 小写十六进制 => 0x1b!
println!("{:#x}!", 27);
// 大写十六进制 => 0x1B!
println!("{:#X}!", 27);
// 不带前缀的十六进制 => 1b!
println!("{:x}!", 27);
// 使用0填充二进制, 宽度为10 => 0b00011011!
println!("{:#010b}!", 27);
}< / code > < / pre >
< h3 id = "指数" > < a class = "header" href = "#指数" > 指数< / a > < / h3 >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
println!("{:2e}", 1000000000); // => 1e9
println!("{:2E}", 1000000000); // => 1E9
}< / code > < / pre >
< h3 id = "指针地址" > < a class = "header" href = "#指针地址" > 指针地址< / a > < / h3 >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > let v= vec![1, 2, 3];
println!("{:p}", v.as_ptr()) // => 0x600002324050
< span class = "boring" > }< / span > < / code > < / pre >
< h3 id = "转义" > < a class = "header" href = "#转义" > 转义< / a > < / h3 >
< p > 有时需要输出 < code > {< / code > 和< code > }< / code > ,但这两个字符是特殊字符,需要进行转义:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn main() {
// "{{" 转义为 '{' "}}" 转义为 '}' "\"" 转义为 '"'
// => Hello "{World}"
println!(" Hello \"{{World}}\" ");
// 下面代码会报错,因为占位符{}只有一个右括号},左括号被转义成字符串的内容
// println!(" {{ Hello } ");
// 也不可使用 '\' 来转义 "{}"
// println!(" \{ Hello \} ")
}< / code > < / pre >
< h2 id = "在格式化字符串时捕获环境中的值rust-158-新增" > < a class = "header" href = "#在格式化字符串时捕获环境中的值rust-158-新增" > 在格式化字符串时捕获环境中的值( Rust 1.58 新增)< / a > < / h2 >
< p > 在以前,想要输出一个函数的返回值,你需要这么做:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn get_person() -> String {
String::from("sunface")
}
fn main() {
let p = get_person();
println!("Hello, {}!", p); // implicit position
println!("Hello, {0}!", p); // explicit index
println!("Hello, {person}!", person = p);
}< / code > < / pre >
< p > 问题倒也不大,但是一旦格式化字符串长了后,就会非常冗余,而在 1.58 后,我们可以这么写:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn get_person() -> String {
String::from("sunface")
}
fn main() {
let person = get_person();
println!("Hello, {person}!");
}< / code > < / pre >
< p > 是不是清晰、简洁了很多?甚至还可以将环境中的值用于格式化参数:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > < span class = "boring" > #![allow(unused)]
< / span > < span class = "boring" > fn main() {
< / span > let (width, precision) = get_format();
for (name, score) in get_scores() {
println!("{name}: {score:width$.precision$}");
}
< span class = "boring" > }< / span > < / code > < / pre >
< p > 但也有局限,它只能捕获普通的变量,对于更复杂的类型(例如表达式),可以先将它赋值给一个变量或使用以前的 < code > name = expression< / code > 形式的格式化参数。
目前除了 < code > panic!< / code > 外,其它接收格式化参数的宏,都可以使用新的特性。对于 < code > panic!< / code > 而言,如果还在使用 < code > 2015版本< / code > 或 < code > 2018版本< / code > ,那 < code > panic!("{ident}")< / code > 依然会被当成 正常的字符串来处理,同时编译器会给予 < code > warn< / code > 提示。而对于 < code > 2021版本< / code > ,则可以正常使用:< / p >
< pre class = "playground" > < code class = "language-rust edition2021" > fn get_person() -> String {
String::from("sunface")
}
fn main() {
let person = get_person();
panic!("Hello, {person}!");
}< / code > < / pre >
< p > 输出:< / p >
< pre > < code class = "language-console" > thread 'main' panicked at 'Hello, sunface!', src/main.rs:6:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
< / code > < / pre >
< h2 id = "课后练习" > < a class = "header" href = "#课后练习" > 课后练习< / a > < / h2 >
< blockquote >
< p > < a href = "https://practice-zh.course.rs/formatted-output.html" > Rust By Practice< / a > ,支持代码在线编辑和运行,并提供详细的习题解答。(本节暂无习题解答)< / p >
< / blockquote >
< h2 id = "总结" > < a class = "header" href = "#总结" > 总结< / a > < / h2 >
< p > 把这些格式化都牢记在脑中是不太现实的,也没必要,我们要做的就是知道 Rust 支持相应的格式化输出,在需要之时,读者再来查阅本文即可。< / p >
< p > 还是那句话,< a href = "https://github.com/sunface/rust-course" > < < Rust 语言圣经> > < / a > 不仅仅是 Rust 学习书籍,还是一本厚重的工具书!< / p >
< p > 至此, Rust 的基础内容学习已经全部完成,下面我们将学习 Rust 的高级进阶内容,正式开启你的高手之路。< / p >
< / main >
< nav class = "nav-wrapper" aria-label = "Page navigation" >
<!-- Mobile navigation buttons -->
< a rel = "prev" href = "../basic/comment.html" class = "mobile-nav-chapters previous" title = "Previous chapter" aria-label = "Previous chapter" aria-keyshortcuts = "Left" >
< span class = fa-svg > < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 320 512" > <!-- ! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --> < path d = "M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 246.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z" / > < / svg > < / span >
< / a >
< a rel = "next prefetch" href = "../basic-practice/intro.html" class = "mobile-nav-chapters next" title = "Next chapter" aria-label = "Next chapter" aria-keyshortcuts = "Right" >
< span class = fa-svg > < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 320 512" > <!-- ! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --> < path d = "M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z" / > < / svg > < / span >
< / a >
< div style = "clear: both" > < / div >
< / nav >
< / div >
< / div >
< nav class = "nav-wide-wrapper" aria-label = "Page navigation" >
< a rel = "prev" href = "../basic/comment.html" class = "nav-chapters previous" title = "Previous chapter" aria-label = "Previous chapter" aria-keyshortcuts = "Left" >
< span class = fa-svg > < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 320 512" > <!-- ! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --> < path d = "M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 246.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z" / > < / svg > < / span >
< / a >
< a rel = "next prefetch" href = "../basic-practice/intro.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 >