<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `fmt` mod in crate `std`."><meta name="keywords" content="rust, rustlang, rust-lang, fmt"><title>std::fmt - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../std/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a><p class='location'>Module fmt</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</a></li><li><a href="#types">Type Definitions</a></li></ul></div><p class='location'><a href='../index.html'>std</a></p><script>window.sidebarCurrent = {name: 'fmt', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='in-band'>Module <a href='../index.html'>std</a>::<wbr><a class="mod" href=''>fmt</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.0</span><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../../src/alloc/lib.rs.html#192' title='goto source code'>[src]</a></span></h1><div class='docblock'><p>Utilities for formatting and printing <code>String</code>s.</p> <p>This module contains the runtime support for the <a href="../../std/macro.format.html"><code>format!</code></a> syntax extension. This macro is implemented in the compiler to emit calls to this module in order to format arguments at runtime into strings.</p> <h1 id="usage" class="section-header"><a href="#usage">Usage</a></h1> <p>The <a href="../../std/macro.format.html"><code>format!</code></a> macro is intended to be familiar to those coming from C's <code>printf</code>/<code>fprintf</code> functions or Python's <code>str.format</code> function.</p> <p>Some examples of the <a href="../../std/macro.format.html"><code>format!</code></a> extension are:</p> <pre class="rust rust-example-rendered"> <span class="macro">format</span><span class="macro">!</span>(<span class="string">"Hello"</span>); <span class="comment">// => "Hello"</span> <span class="macro">format</span><span class="macro">!</span>(<span class="string">"Hello, {}!"</span>, <span class="string">"world"</span>); <span class="comment">// => "Hello, world!"</span> <span class="macro">format</span><span class="macro">!</span>(<span class="string">"The number is {}"</span>, <span class="number">1</span>); <span class="comment">// => "The number is 1"</span> <span class="macro">format</span><span class="macro">!</span>(<span class="string">"{:?}"</span>, (<span class="number">3</span>, <span class="number">4</span>)); <span class="comment">// => "(3, 4)"</span> <span class="macro">format</span><span class="macro">!</span>(<span class="string">"{value}"</span>, <span class="ident">value</span><span class="op">=</span><span class="number">4</span>); <span class="comment">// => "4"</span> <span class="macro">format</span><span class="macro">!</span>(<span class="string">"{} {}"</span>, <span class="number">1</span>, <span class="number">2</span>); <span class="comment">// => "1 2"</span> <span class="macro">format</span><span class="macro">!</span>(<span class="string">"{:04}"</span>, <span class="number">42</span>); <span class="comment">// => "0042" with leading zeros</span><a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aformat!(%22Hello%22)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20%3D%3E%20%22Hello%22%0Aformat!(%22Hello%2C%20%7B%7D!%22%2C%20%22world%22)%3B%20%20%20%2F%2F%20%3D%3E%20%22Hello%2C%20world!%22%0Aformat!(%22The%20number%20is%20%7B%7D%22%2C%201)%3B%20%20%20%2F%2F%20%3D%3E%20%22The%20number%20is%201%22%0Aformat!(%22%7B%3A%3F%7D%22%2C%20(3%2C%204))%3B%20%20%20%20%20%20%20%20%20%20%2F%2F%20%3D%3E%20%22(3%2C%204)%22%0Aformat!(%22%7Bvalue%7D%22%2C%20value%3D4)%3B%20%20%20%20%20%20%2F%2F%20%3D%3E%20%224%22%0Aformat!(%22%7B%7D%20%7B%7D%22%2C%201%2C%202)%3B%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20%3D%3E%20%221%202%22%0Aformat!(%22%7B%3A04%7D%22%2C%2042)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20%3D%3E%20%220042%22%20with%20leading%20zeros%0A%7D">Run</a></pre> <p>From these, you can see that the first argument is a format string. It is required by the compiler for this to be a string literal; it cannot be a variable passed in (in order to perform validity checking). The compiler will then parse the format string and determine if the list of arguments provided is suitable to pass to this format string.</p> <h2 id="positional-parameters" class="section-header"><a href="#positional-parameters">Positional parameters</a></h2> <p>Each formatting argument is allowed to specify which value argument it's referencing, and if omitted it is assumed to be "the next argument". For example, the format string <code>{} {} {}</code> would take three parameters, and they would be formatted in the same order as they're given. The format string <code>{2} {1} {0}</code>, however, would format arguments in reverse order.</p> <p>Things can get a little tricky once you start intermingling the two types of positional specifiers. The "next argument" specifier can be thought of as an iterator over the argument. Each time a "next argument" specifier is seen, the iterator advances. This leads to behavior like this:</p> <pre class="rust rust-example-rendered"> <span class="macro">format</span><span class="macro">!</span>(<span class="string">"{1} {} {0} {}"</span>, <span class="number">1</span>, <span class="number">2</span>); <span class="comment">// => "2 1 1 2"</span><a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aformat!(%22%7B1%7D%20%7B%7D%20%7B0%7D%20%7B%7D%22%2C%201%2C%202)%3B%20%2F%2F%20%3D%3E%20%222%201%201%202%22%0A%7D">Run</a></pre> <p>The internal iterator over the argument has not been advanced by the time the first <code>{}</code> is seen, so it prints the first argument. Then upon reaching the second <code>{}</code>, the iterator has advanced forward to the second argument. Essentially, parameters which explicitly name their argument do not affect parameters which do not name an argument in terms of positional specifiers.</p> <p>A format string is required to use all of its arguments, otherwise it is a compile-time error. You may refer to the same argument more than once in the format string.</p> <h2 id="named-parameters" class="section-header"><a href="#named-parameters">Named parameters</a></h2> <p>Rust itself does not have a Python-like equivalent of named parameters to a function, but the <a href="../../std/macro.format.html"><code>format!</code></a> macro is a syntax extension which allows it to leverage named parameters. Named parameters are listed at the end of the argument list and have the syntax:</p> <pre><code class="language-text">identifier '=' expression </code></pre> <p>For example, the following <a href="../../std/macro.format.html"><code>format!</code></a> expressions all use named argument:</p> <pre class="rust rust-example-rendered"> <span class="macro">format</span><span class="macro">!</span>(<span class="string">"{argument}"</span>, <span class="ident">argument</span> <span class="op">=</span> <span class="string">"test"</span>); <span class="comment">// => "test"</span> <span class="macro">format</span><span class="macro">!</span>(<span class="string">"{name} {}"</span>, <span class="number">1</span>, <span class="ident">name</span> <span class="op">=</span> <span class="number">2</span>); <span class="comment">// => "2 1"</span> <span class="macro">format</span><span class="macro">!</span>(<span class="string">"{a} {c} {b}"</span>, <span class="ident">a</span><span class="op">=</span><span class="string">"a"</span>, <span class="ident">b</span><span class="op">=</span><span class="string">'b'</span>, <span class="ident">c</span><span class="op">=</span><span class="number">3</span>); <span class="comment">// => "a 3 b"</span><a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aformat!(%22%7Bargument%7D%22%2C%20argument%20%3D%20%22test%22)%3B%20%20%20%2F%2F%20%3D%3E%20%22test%22%0Aformat!(%22%7Bname%7D%20%7B%7D%22%2C%201%2C%20name%20%3D%202)%3B%20%20%20%20%20%20%20%20%20%20%2F%2F%20%3D%3E%20%222%201%22%0Aformat!(%22%7Ba%7D%20%7Bc%7D%20%7Bb%7D%22%2C%20a%3D%22a%22%2C%20b%3D'b'%2C%20c%3D3)%3B%20%20%2F%2F%20%3D%3E%20%22a%203%20b%22%0A%7D">Run</a></pre> <p>It is not valid to put positional parameters (those without names) after arguments which have names. Like with positional parameters, it is not valid to provide named parameters that are unused by the format string.</p> <h2 id="argument-types" class="section-header"><a href="#argument-types">Argument types</a></h2> <p>Each argument's type is dictated by the format string. There are various parameters which require a particular type, however. An example is the <code>{:.*}</code> syntax, which sets the number of decimal places in floating-point types:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">formatted_number</span> <span class="op">=</span> <span class="macro">format</span><span class="macro">!</span>(<span class="string">"{:.*}"</span>, <span class="number">2</span>, <span class="number">1.234567</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"1.23"</span>, <span class="ident">formatted_number</span>)<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20formatted_number%20%3D%20format!(%22%7B%3A.*%7D%22%2C%202%2C%201.234567)%3B%0A%0Aassert_eq!(%221.23%22%2C%20formatted_number)%0A%7D">Run</a></pre> <p>If this syntax is used, then the number of characters to print precedes the actual object being formatted, and the number of characters must have the type <a href="../../std/primitive.usize.html"><code>usize</code></a>.</p> <h2 id="formatting-traits" class="section-header"><a href="#formatting-traits">Formatting traits</a></h2> <p>When requesting that an argument be formatted with a particular type, you are actually requesting that an argument ascribes to a particular trait. This allows multiple actual types to be formatted via <code>{:x}</code> (like <a href="../../std/primitive.i8.html"><code>i8</code></a> as well as <a href="../../std/primitive.isize.html"><code>isize</code></a>). The current mapping of types to traits is:</p> <ul> <li><em>nothing</em> ⇒ <a href="trait.Display.html"><code>Display</code></a></li> <li><code>?</code> ⇒ <a href="trait.Debug.html"><code>Debug</code></a></li> <li><code>x?</code> ⇒ <a href="trait.Debug.html"><code>Debug</code></a> with lower-case hexadecimal integers</li> <li><code>X?</code> ⇒ <a href="trait.Debug.html"><code>Debug</code></a> with upper-case hexadecimal integers</li> <li><code>o</code> ⇒ <a href="trait.Octal.html"><code>Octal</code></a></li> <li><code>x</code> ⇒ <a href="trait.LowerHex.html"><code>LowerHex</code></a></li> <li><code>X</code> ⇒ <a href="trait.UpperHex.html"><code>UpperHex</code></a></li> <li><code>p</code> ⇒ <a href="trait.Pointer.html"><code>Pointer</code></a></li> <li><code>b</code> ⇒ <a href="trait.Binary.html"><code>Binary</code></a></li> <li><code>e</code> ⇒ <a href="trait.LowerExp.html"><code>LowerExp</code></a></li> <li><code>E</code> ⇒ <a href="trait.UpperExp.html"><code>UpperExp</code></a></li> </ul> <p>What this means is that any type of argument which implements the <a href="trait.Binary.html"><code>fmt::Binary</code></a> trait can then be formatted with <code>{:b}</code>. Implementations are provided for these traits for a number of primitive types by the standard library as well. If no format is specified (as in <code>{}</code> or <code>{:6}</code>), then the format trait used is the <a href="trait.Display.html"><code>Display</code></a> trait.</p> <p>When implementing a format trait for your own type, you will have to implement a method of the signature:</p> <pre class="rust rust-example-rendered"> <span class="kw">fn</span> <span class="ident">fmt</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">f</span>: <span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">fmt</span>::<span class="ident">Formatter</span>) <span class="op">-></span> <span class="ident">fmt</span>::<span class="prelude-ty">Result</span> {<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0A%23!%5Ballow(dead_code)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Afmt%3B%0Astruct%20Foo%3B%20%2F%2F%20our%20custom%20type%0Aimpl%20fmt%3A%3ADisplay%20for%20Foo%20%7B%0Afn%20fmt(%26self%2C%20f%3A%20%26mut%20fmt%3A%3AFormatter)%20-%3E%20fmt%3A%3AResult%20%7B%0Awrite!(f%2C%20%22testing%2C%20testing%22)%0A%7D%20%7D%0A%7D">Run</a></pre> <p>Your type will be passed as <code>self</code> by-reference, and then the function should emit output into the <code>f.buf</code> stream. It is up to each format trait implementation to correctly adhere to the requested formatting parameters. The values of these parameters will be listed in the fields of the <a href="struct.Formatter.html"><code>Formatter</code></a> struct. In order to help with this, the <a href="struct.Formatter.html"><code>Formatter</code></a> struct also provides some helper methods.</p> <p>Additionally, the return value of this function is <a href="type.Result.html"><code>fmt::Result</code></a> which is a type alias of <a href="../../std/result/enum.Result.html"><code>Result</code></a><code><(),</code><a href="struct.Error.html"><code>std::fmt::Error</code></a><code>></code>. Formatting implementations should ensure that they propagate errors from the <a href="struct.Formatter.html"><code>Formatter</code></a> (e.g., when calling <a href="../../std/macro.write.html"><code>write!</code></a>) however, they should never return errors spuriously. That is, a formatting implementation must and may only return an error if the passed-in <a href="struct.Formatter.html"><code>Formatter</code></a> returns an error. This is because, contrary to what the function signature might suggest, string formatting is an infallible operation. This function only returns a result because writing to the underlying stream might fail and it must provide a way to propagate the fact that an error has occurred back up the stack.</p> <p>An example of implementing the formatting traits would look like:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">fmt</span>; <span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>)]</span> <span class="kw">struct</span> <span class="ident">Vector2D</span> { <span class="ident">x</span>: <span class="ident">isize</span>, <span class="ident">y</span>: <span class="ident">isize</span>, } <span class="kw">impl</span> <span class="ident">fmt</span>::<span class="ident">Display</span> <span class="kw">for</span> <span class="ident">Vector2D</span> { <span class="kw">fn</span> <span class="ident">fmt</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">f</span>: <span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">fmt</span>::<span class="ident">Formatter</span>) <span class="op">-></span> <span class="ident">fmt</span>::<span class="prelude-ty">Result</span> { <span class="comment">// The `f` value implements the `Write` trait, which is what the</span> <span class="comment">// write! macro is expecting. Note that this formatting ignores the</span> <span class="comment">// various flags provided to format strings.</span> <span class="macro">write</span><span class="macro">!</span>(<span class="ident">f</span>, <span class="string">"({}, {})"</span>, <span class="self">self</span>.<span class="ident">x</span>, <span class="self">self</span>.<span class="ident">y</span>) } } <span class="comment">// Different traits allow different forms of output of a type. The meaning</span> <span class="comment">// of this format is to print the magnitude of a vector.</span> <span class="kw">impl</span> <span class="ident">fmt</span>::<span class="ident">Binary</span> <span class="kw">for</span> <span class="ident">Vector2D</span> { <span class="kw">fn</span> <span class="ident">fmt</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">f</span>: <span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">fmt</span>::<span class="ident">Formatter</span>) <span class="op">-></span> <span class="ident">fmt</span>::<span class="prelude-ty">Result</span> { <span class="kw">let</span> <span class="ident">magnitude</span> <span class="op">=</span> (<span class="self">self</span>.<span class="ident">x</span> <span class="op">*</span> <span class="self">self</span>.<span class="ident">x</span> <span class="op">+</span> <span class="self">self</span>.<span class="ident">y</span> <span class="op">*</span> <span class="self">self</span>.<span class="ident">y</span>) <span class="kw">as</span> <span class="ident">f64</span>; <span class="kw">let</span> <span class="ident">magnitude</span> <span class="op">=</span> <span class="ident">magnitude</span>.<span class="ident">sqrt</span>(); <span class="comment">// Respect the formatting flags by using the helper method</span> <span class="comment">// `pad_integral` on the Formatter object. See the method</span> <span class="comment">// documentation for details, and the function `pad` can be used</span> <span class="comment">// to pad strings.</span> <span class="kw">let</span> <span class="ident">decimals</span> <span class="op">=</span> <span class="ident">f</span>.<span class="ident">precision</span>().<span class="ident">unwrap_or</span>(<span class="number">3</span>); <span class="kw">let</span> <span class="ident">string</span> <span class="op">=</span> <span class="macro">format</span><span class="macro">!</span>(<span class="string">"{:.*}"</span>, <span class="ident">decimals</span>, <span class="ident">magnitude</span>); <span class="ident">f</span>.<span class="ident">pad_integral</span>(<span class="bool-val">true</span>, <span class="string">""</span>, <span class="kw-2">&</span><span class="ident">string</span>) } } <span class="kw">fn</span> <span class="ident">main</span>() { <span class="kw">let</span> <span class="ident">myvector</span> <span class="op">=</span> <span class="ident">Vector2D</span> { <span class="ident">x</span>: <span class="number">3</span>, <span class="ident">y</span>: <span class="number">4</span> }; <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">myvector</span>); <span class="comment">// => "(3, 4)"</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{:?}"</span>, <span class="ident">myvector</span>); <span class="comment">// => "Vector2D {x: 3, y:4}"</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{:10.3b}"</span>, <span class="ident">myvector</span>); <span class="comment">// => " 5.000"</span> }<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Ause%20std%3A%3Afmt%3B%0A%0A%23%5Bderive(Debug)%5D%0Astruct%20Vector2D%20%7B%0A%20%20%20%20x%3A%20isize%2C%0A%20%20%20%20y%3A%20isize%2C%0A%7D%0A%0Aimpl%20fmt%3A%3ADisplay%20for%20Vector2D%20%7B%0A%20%20%20%20fn%20fmt(%26self%2C%20f%3A%20%26mut%20fmt%3A%3AFormatter)%20-%3E%20fmt%3A%3AResult%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20The%20%60f%60%20value%20implements%20the%20%60Write%60%20trait%2C%20which%20is%20what%20the%0A%20%20%20%20%20%20%20%20%2F%2F%20write!%20macro%20is%20expecting.%20Note%20that%20this%20formatting%20ignores%20the%0A%20%20%20%20%20%20%20%20%2F%2F%20various%20flags%20provided%20to%20format%20strings.%0A%20%20%20%20%20%20%20%20write!(f%2C%20%22(%7B%7D%2C%20%7B%7D)%22%2C%20self.x%2C%20self.y)%0A%20%20%20%20%7D%0A%7D%0A%0A%2F%2F%20Different%20traits%20allow%20different%20forms%20of%20output%20of%20a%20type.%20The%20meaning%0A%2F%2F%20of%20this%20format%20is%20to%20print%20the%20magnitude%20of%20a%20vector.%0Aimpl%20fmt%3A%3ABinary%20for%20Vector2D%20%7B%0A%20%20%20%20fn%20fmt(%26self%2C%20f%3A%20%26mut%20fmt%3A%3AFormatter)%20-%3E%20fmt%3A%3AResult%20%7B%0A%20%20%20%20%20%20%20%20let%20magnitude%20%3D%20(self.x%20*%20self.x%20%2B%20self.y%20*%20self.y)%20as%20f64%3B%0A%20%20%20%20%20%20%20%20let%20magnitude%20%3D%20magnitude.sqrt()%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Respect%20the%20formatting%20flags%20by%20using%20the%20helper%20method%0A%20%20%20%20%20%20%20%20%2F%2F%20%60pad_integral%60%20on%20the%20Formatter%20object.%20See%20the%20method%0A%20%20%20%20%20%20%20%20%2F%2F%20documentation%20for%20details%2C%20and%20the%20function%20%60pad%60%20can%20be%20used%0A%20%20%20%20%20%20%20%20%2F%2F%20to%20pad%20strings.%0A%20%20%20%20%20%20%20%20let%20decimals%20%3D%20f.precision().unwrap_or(3)%3B%0A%20%20%20%20%20%20%20%20let%20string%20%3D%20format!(%22%7B%3A.*%7D%22%2C%20decimals%2C%20magnitude)%3B%0A%20%20%20%20%20%20%20%20f.pad_integral(true%2C%20%22%22%2C%20%26string)%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20myvector%20%3D%20Vector2D%20%7B%20x%3A%203%2C%20y%3A%204%20%7D%3B%0A%0A%20%20%20%20println!(%22%7B%7D%22%2C%20myvector)%3B%20%20%20%20%20%20%20%2F%2F%20%3D%3E%20%22(3%2C%204)%22%0A%20%20%20%20println!(%22%7B%3A%3F%7D%22%2C%20myvector)%3B%20%20%20%20%20%2F%2F%20%3D%3E%20%22Vector2D%20%7Bx%3A%203%2C%20y%3A4%7D%22%0A%20%20%20%20println!(%22%7B%3A10.3b%7D%22%2C%20myvector)%3B%20%2F%2F%20%3D%3E%20%22%20%20%20%20%205.000%22%0A%7D">Run</a></pre> <h3 id="fmtdisplay-vs-fmtdebug" class="section-header"><a href="#fmtdisplay-vs-fmtdebug"><code>fmt::Display</code> vs <code>fmt::Debug</code></a></h3> <p>These two formatting traits have distinct purposes:</p> <ul> <li><a href="trait.Display.html"><code>fmt::Display</code></a> implementations assert that the type can be faithfully represented as a UTF-8 string at all times. It is <strong>not</strong> expected that all types implement the <a href="trait.Display.html"><code>Display</code></a> trait.</li> <li><a href="trait.Debug.html"><code>fmt::Debug</code></a> implementations should be implemented for <strong>all</strong> public types. Output will typically represent the internal state as faithfully as possible. The purpose of the <a href="trait.Debug.html"><code>Debug</code></a> trait is to facilitate debugging Rust code. In most cases, using <code>#[derive(Debug)]</code> is sufficient and recommended.</li> </ul> <p>Some examples of the output from both traits:</p> <pre class="rust rust-example-rendered"> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="macro">format</span><span class="macro">!</span>(<span class="string">"{} {:?}"</span>, <span class="number">3</span>, <span class="number">4</span>), <span class="string">"3 4"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="macro">format</span><span class="macro">!</span>(<span class="string">"{} {:?}"</span>, <span class="string">'a'</span>, <span class="string">'b'</span>), <span class="string">"a 'b'"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="macro">format</span><span class="macro">!</span>(<span class="string">"{} {:?}"</span>, <span class="string">"foo\n"</span>, <span class="string">"bar\n"</span>), <span class="string">"foo\n \"bar\\n\""</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aassert_eq!(format!(%22%7B%7D%20%7B%3A%3F%7D%22%2C%203%2C%204)%2C%20%223%204%22)%3B%0Aassert_eq!(format!(%22%7B%7D%20%7B%3A%3F%7D%22%2C%20'a'%2C%20'b')%2C%20%22a%20'b'%22)%3B%0Aassert_eq!(format!(%22%7B%7D%20%7B%3A%3F%7D%22%2C%20%22foo%5Cn%22%2C%20%22bar%5Cn%22)%2C%20%22foo%5Cn%20%5C%22bar%5C%5Cn%5C%22%22)%3B%0A%7D">Run</a></pre> <h2 id="related-macros" class="section-header"><a href="#related-macros">Related macros</a></h2> <p>There are a number of related macros in the <a href="../../std/macro.format.html"><code>format!</code></a> family. The ones that are currently implemented are:</p> <div class='information'><div class='tooltip ignore'>ⓘ<span class='tooltiptext'>This example is not tested</span></div></div><pre class="rust rust-example-rendered ignore"> <span class="macro">format</span><span class="macro">!</span> <span class="comment">// described above</span> <span class="macro">write</span><span class="macro">!</span> <span class="comment">// first argument is a &mut io::Write, the destination</span> <span class="macro">writeln</span><span class="macro">!</span> <span class="comment">// same as write but appends a newline</span> <span class="macro">print</span><span class="macro">!</span> <span class="comment">// the format string is printed to the standard output</span> <span class="macro">println</span><span class="macro">!</span> <span class="comment">// same as print but appends a newline</span> <span class="macro">eprint</span><span class="macro">!</span> <span class="comment">// the format string is printed to the standard error</span> <span class="macro">eprintln</span><span class="macro">!</span> <span class="comment">// same as eprint but appends a newline</span> <span class="macro">format_args</span><span class="macro">!</span> <span class="comment">// described below.</span><a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aformat!%20%20%20%20%20%20%2F%2F%20described%20above%0Awrite!%20%20%20%20%20%20%20%2F%2F%20first%20argument%20is%20a%20%26mut%20io%3A%3AWrite%2C%20the%20destination%0Awriteln!%20%20%20%20%20%2F%2F%20same%20as%20write%20but%20appends%20a%20newline%0Aprint!%20%20%20%20%20%20%20%2F%2F%20the%20format%20string%20is%20printed%20to%20the%20standard%20output%0Aprintln!%20%20%20%20%20%2F%2F%20same%20as%20print%20but%20appends%20a%20newline%0Aeprint!%20%20%20%20%20%20%2F%2F%20the%20format%20string%20is%20printed%20to%20the%20standard%20error%0Aeprintln!%20%20%20%20%2F%2F%20same%20as%20eprint%20but%20appends%20a%20newline%0Aformat_args!%20%2F%2F%20described%20below.%0A%7D">Run</a></pre> <h3 id="write" class="section-header"><a href="#write"><code>write!</code></a></h3> <p>This and <a href="../../std/macro.writeln.html"><code>writeln!</code></a> are two macros which are used to emit the format string to a specified stream. This is used to prevent intermediate allocations of format strings and instead directly write the output. Under the hood, this function is actually invoking the <a href="../../std/io/trait.Write.html#method.write_fmt"><code>write_fmt</code></a> function defined on the <a href="../../std/io/trait.Write.html"><code>std::io::Write</code></a> trait. Example usage is:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">Write</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">w</span> <span class="op">=</span> <span class="ident">Vec</span>::<span class="ident">new</span>(); <span class="macro">write</span><span class="macro">!</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">w</span>, <span class="string">"Hello {}!"</span>, <span class="string">"world"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0A%23!%5Ballow(unused_must_use)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aio%3A%3AWrite%3B%0Alet%20mut%20w%20%3D%20Vec%3A%3Anew()%3B%0Awrite!(%26mut%20w%2C%20%22Hello%20%7B%7D!%22%2C%20%22world%22)%3B%0A%7D">Run</a></pre> <h3 id="print" class="section-header"><a href="#print"><code>print!</code></a></h3> <p>This and <a href="../../std/macro.println.html"><code>println!</code></a> emit their output to stdout. Similarly to the <a href="../../std/macro.write.html"><code>write!</code></a> macro, the goal of these macros is to avoid intermediate allocations when printing output. Example usage is:</p> <pre class="rust rust-example-rendered"> <span class="macro">print</span><span class="macro">!</span>(<span class="string">"Hello {}!"</span>, <span class="string">"world"</span>); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"I have a newline {}"</span>, <span class="string">"character at the end"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aprint!(%22Hello%20%7B%7D!%22%2C%20%22world%22)%3B%0Aprintln!(%22I%20have%20a%20newline%20%7B%7D%22%2C%20%22character%20at%20the%20end%22)%3B%0A%7D">Run</a></pre> <h3 id="eprint" class="section-header"><a href="#eprint"><code>eprint!</code></a></h3> <p>The <a href="../../std/macro.eprint.html"><code>eprint!</code></a> and <a href="../../std/macro.eprintln.html"><code>eprintln!</code></a> macros are identical to <a href="../../std/macro.print.html"><code>print!</code></a> and <a href="../../std/macro.println.html"><code>println!</code></a>, respectively, except they emit their output to stderr.</p> <h3 id="format_args" class="section-header"><a href="#format_args"><code>format_args!</code></a></h3> <p>This is a curious macro which is used to safely pass around an opaque object describing the format string. This object does not require any heap allocations to create, and it only references information on the stack. Under the hood, all of the related macros are implemented in terms of this. First off, some example usage is:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">fmt</span>; <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::{<span class="self">self</span>, <span class="ident">Write</span>}; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">some_writer</span> <span class="op">=</span> <span class="ident">io</span>::<span class="ident">stdout</span>(); <span class="macro">write</span><span class="macro">!</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">some_writer</span>, <span class="string">"{}"</span>, <span class="macro">format_args</span><span class="macro">!</span>(<span class="string">"print with a {}"</span>, <span class="string">"macro"</span>)); <span class="kw">fn</span> <span class="ident">my_fmt_fn</span>(<span class="ident">args</span>: <span class="ident">fmt</span>::<span class="ident">Arguments</span>) { <span class="macro">write</span><span class="macro">!</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">io</span>::<span class="ident">stdout</span>(), <span class="string">"{}"</span>, <span class="ident">args</span>); } <span class="ident">my_fmt_fn</span>(<span class="macro">format_args</span><span class="macro">!</span>(<span class="string">", or a {} too"</span>, <span class="string">"function"</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0A%23!%5Ballow(unused_must_use)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Afmt%3B%0Ause%20std%3A%3Aio%3A%3A%7Bself%2C%20Write%7D%3B%0A%0Alet%20mut%20some_writer%20%3D%20io%3A%3Astdout()%3B%0Awrite!(%26mut%20some_writer%2C%20%22%7B%7D%22%2C%20format_args!(%22print%20with%20a%20%7B%7D%22%2C%20%22macro%22))%3B%0A%0Afn%20my_fmt_fn(args%3A%20fmt%3A%3AArguments)%20%7B%0A%20%20%20%20write!(%26mut%20io%3A%3Astdout()%2C%20%22%7B%7D%22%2C%20args)%3B%0A%7D%0Amy_fmt_fn(format_args!(%22%2C%20or%20a%20%7B%7D%20too%22%2C%20%22function%22))%3B%0A%7D">Run</a></pre> <p>The result of the <a href="../../std/macro.format_args.html"><code>format_args!</code></a> macro is a value of type <a href="struct.Arguments.html"><code>fmt::Arguments</code></a>. This structure can then be passed to the <a href="fn.write.html"><code>write</code></a> and <a href="fn.format.html"><code>format</code></a> functions inside this module in order to process the format string. The goal of this macro is to even further prevent intermediate allocations when dealing formatting strings.</p> <p>For example, a logging library could use the standard formatting syntax, but it would internally pass around this structure until it has been determined where output should go to.</p> <h1 id="syntax" class="section-header"><a href="#syntax">Syntax</a></h1> <p>The syntax for the formatting language used is drawn from other languages, so it should not be too alien. Arguments are formatted with Python-like syntax, meaning that arguments are surrounded by <code>{}</code> instead of the C-like <code>%</code>. The actual grammar for the formatting syntax is:</p> <pre><code class="language-text">format_string := <text> [ maybe-format <text> ] * maybe-format := '{' '{' | '}' '}' | <format> format := '{' [ argument ] [ ':' format_spec ] '}' argument := integer | identifier format_spec := [[fill]align][sign]['#']['0'][width]['.' precision][type] fill := character align := '<' | '^' | '>' sign := '+' | '-' width := count precision := count | '*' type := identifier | '?' | '' count := parameter | integer parameter := argument '$' </code></pre> <h1 id="formatting-parameters" class="section-header"><a href="#formatting-parameters">Formatting Parameters</a></h1> <p>Each argument being formatted can be transformed by a number of formatting parameters (corresponding to <code>format_spec</code> in the syntax above). These parameters affect the string representation of what's being formatted. This syntax draws heavily from Python's, so it may seem a bit familiar.</p> <h2 id="fillalignment" class="section-header"><a href="#fillalignment">Fill/Alignment</a></h2> <p>The fill character is provided normally in conjunction with the <code>width</code> parameter. This indicates that if the value being formatted is smaller than <code>width</code> some extra characters will be printed around it. The extra characters are specified by <code>fill</code>, and the alignment can be one of the following options:</p> <ul> <li><code><</code> - the argument is left-aligned in <code>width</code> columns</li> <li><code>^</code> - the argument is center-aligned in <code>width</code> columns</li> <li><code>></code> - the argument is right-aligned in <code>width</code> columns</li> </ul> <p>Note that alignment may not be implemented by some types. A good way to ensure padding is applied is to format your input, then use this resulting string to pad your output.</p> <h2 id="sign0" class="section-header"><a href="#sign0">Sign/<code>#</code>/<code>0</code></a></h2> <p>These can all be interpreted as flags for a particular formatter.</p> <ul> <li><code>+</code> - This is intended for numeric types and indicates that the sign should always be printed. Positive signs are never printed by default, and the negative sign is only printed by default for the <code>Signed</code> trait. This flag indicates that the correct sign (<code>+</code> or <code>-</code>) should always be printed.</li> <li><code>-</code> - Currently not used</li> <li><code>#</code> - This flag is indicates that the "alternate" form of printing should be used. The alternate forms are: <ul> <li><code>#?</code> - pretty-print the <a href="trait.Debug.html"><code>Debug</code></a> formatting</li> <li><code>#x</code> - precedes the argument with a <code>0x</code></li> <li><code>#X</code> - precedes the argument with a <code>0x</code></li> <li><code>#b</code> - precedes the argument with a <code>0b</code></li> <li><code>#o</code> - precedes the argument with a <code>0o</code></li> </ul> </li> <li><code>0</code> - This is used to indicate for integer formats that the padding should both be done with a <code>0</code> character as well as be sign-aware. A format like <code>{:08}</code> would yield <code>00000001</code> for the integer <code>1</code>, while the same format would yield <code>-0000001</code> for the integer <code>-1</code>. Notice that the negative version has one fewer zero than the positive version. Note that padding zeroes are always placed after the sign (if any) and before the digits. When used together with the <code>#</code> flag, a similar rule applies: padding zeroes are inserted after the prefix but before the digits.</li> </ul> <h2 id="width" class="section-header"><a href="#width">Width</a></h2> <p>This is a parameter for the "minimum width" that the format should take up. If the value's string does not fill up this many characters, then the padding specified by fill/alignment will be used to take up the required space.</p> <p>The default fill/alignment for non-numerics is a space and left-aligned. The defaults for numeric formatters is also a space but with right-alignment. If the <code>0</code> flag is specified for numerics, then the implicit fill character is <code>0</code>.</p> <p>The value for the width can also be provided as a <a href="../../std/primitive.usize.html"><code>usize</code></a> in the list of parameters by using the dollar syntax indicating that the second argument is a <a href="../../std/primitive.usize.html"><code>usize</code></a> specifying the width, for example:</p> <pre class="rust rust-example-rendered"> <span class="comment">// All of these print "Hello x !"</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello {:5}!"</span>, <span class="string">"x"</span>); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello {:1$}!"</span>, <span class="string">"x"</span>, <span class="number">5</span>); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello {1:0$}!"</span>, <span class="number">5</span>, <span class="string">"x"</span>); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello {:width$}!"</span>, <span class="string">"x"</span>, <span class="ident">width</span> <span class="op">=</span> <span class="number">5</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0A%2F%2F%20All%20of%20these%20print%20%22Hello%20x%20%20%20%20!%22%0Aprintln!(%22Hello%20%7B%3A5%7D!%22%2C%20%22x%22)%3B%0Aprintln!(%22Hello%20%7B%3A1%24%7D!%22%2C%20%22x%22%2C%205)%3B%0Aprintln!(%22Hello%20%7B1%3A0%24%7D!%22%2C%205%2C%20%22x%22)%3B%0Aprintln!(%22Hello%20%7B%3Awidth%24%7D!%22%2C%20%22x%22%2C%20width%20%3D%205)%3B%0A%7D">Run</a></pre> <p>Referring to an argument with the dollar syntax does not affect the "next argument" counter, so it's usually a good idea to refer to arguments by position, or use named arguments.</p> <h2 id="precision" class="section-header"><a href="#precision">Precision</a></h2> <p>For non-numeric types, this can be considered a "maximum width". If the resulting string is longer than this width, then it is truncated down to this many characters and that truncated value is emitted with proper <code>fill</code>, <code>alignment</code> and <code>width</code> if those parameters are set.</p> <p>For integral types, this is ignored.</p> <p>For floating-point types, this indicates how many digits after the decimal point should be printed.</p> <p>There are three possible ways to specify the desired <code>precision</code>:</p> <ol> <li> <p>An integer <code>.N</code>:</p> <p>the integer <code>N</code> itself is the precision.</p> </li> <li> <p>An integer or name followed by dollar sign <code>.N$</code>:</p> <p>use format <em>argument</em> <code>N</code> (which must be a <code>usize</code>) as the precision.</p> </li> <li> <p>An asterisk <code>.*</code>:</p> <p><code>.*</code> means that this <code>{...}</code> is associated with <em>two</em> format inputs rather than one: the first input holds the <code>usize</code> precision, and the second holds the value to print. Note that in this case, if one uses the format string <code>{<arg>:<spec>.*}</code>, then the <code><arg></code> part refers to the <em>value</em> to print, and the <code>precision</code> must come in the input preceding <code><arg></code>.</p> </li> </ol> <p>For example, the following calls all print the same thing <code>Hello x is 0.01000</code>:</p> <pre class="rust rust-example-rendered"> <span class="comment">// Hello {arg 0 ("x")} is {arg 1 (0.01) with precision specified inline (5)}</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello {0} is {1:.5}"</span>, <span class="string">"x"</span>, <span class="number">0.01</span>); <span class="comment">// Hello {arg 1 ("x")} is {arg 2 (0.01) with precision specified in arg 0 (5)}</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello {1} is {2:.0$}"</span>, <span class="number">5</span>, <span class="string">"x"</span>, <span class="number">0.01</span>); <span class="comment">// Hello {arg 0 ("x")} is {arg 2 (0.01) with precision specified in arg 1 (5)}</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello {0} is {2:.1$}"</span>, <span class="string">"x"</span>, <span class="number">5</span>, <span class="number">0.01</span>); <span class="comment">// Hello {next arg ("x")} is {second of next two args (0.01) with precision</span> <span class="comment">// specified in first of next two args (5)}</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello {} is {:.*}"</span>, <span class="string">"x"</span>, <span class="number">5</span>, <span class="number">0.01</span>); <span class="comment">// Hello {next arg ("x")} is {arg 2 (0.01) with precision</span> <span class="comment">// specified in its predecessor (5)}</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello {} is {2:.*}"</span>, <span class="string">"x"</span>, <span class="number">5</span>, <span class="number">0.01</span>); <span class="comment">// Hello {next arg ("x")} is {arg "number" (0.01) with precision specified</span> <span class="comment">// in arg "prec" (5)}</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello {} is {number:.prec$}"</span>, <span class="string">"x"</span>, <span class="ident">prec</span> <span class="op">=</span> <span class="number">5</span>, <span class="ident">number</span> <span class="op">=</span> <span class="number">0.01</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0A%2F%2F%20Hello%20%7Barg%200%20(%22x%22)%7D%20is%20%7Barg%201%20(0.01)%20with%20precision%20specified%20inline%20(5)%7D%0Aprintln!(%22Hello%20%7B0%7D%20is%20%7B1%3A.5%7D%22%2C%20%22x%22%2C%200.01)%3B%0A%0A%2F%2F%20Hello%20%7Barg%201%20(%22x%22)%7D%20is%20%7Barg%202%20(0.01)%20with%20precision%20specified%20in%20arg%200%20(5)%7D%0Aprintln!(%22Hello%20%7B1%7D%20is%20%7B2%3A.0%24%7D%22%2C%205%2C%20%22x%22%2C%200.01)%3B%0A%0A%2F%2F%20Hello%20%7Barg%200%20(%22x%22)%7D%20is%20%7Barg%202%20(0.01)%20with%20precision%20specified%20in%20arg%201%20(5)%7D%0Aprintln!(%22Hello%20%7B0%7D%20is%20%7B2%3A.1%24%7D%22%2C%20%22x%22%2C%205%2C%200.01)%3B%0A%0A%2F%2F%20Hello%20%7Bnext%20arg%20(%22x%22)%7D%20is%20%7Bsecond%20of%20next%20two%20args%20(0.01)%20with%20precision%0A%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20specified%20in%20first%20of%20next%20two%20args%20(5)%7D%0Aprintln!(%22Hello%20%7B%7D%20is%20%7B%3A.*%7D%22%2C%20%20%20%20%22x%22%2C%205%2C%200.01)%3B%0A%0A%2F%2F%20Hello%20%7Bnext%20arg%20(%22x%22)%7D%20is%20%7Barg%202%20(0.01)%20with%20precision%0A%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20specified%20in%20its%20predecessor%20(5)%7D%0Aprintln!(%22Hello%20%7B%7D%20is%20%7B2%3A.*%7D%22%2C%20%20%20%22x%22%2C%205%2C%200.01)%3B%0A%0A%2F%2F%20Hello%20%7Bnext%20arg%20(%22x%22)%7D%20is%20%7Barg%20%22number%22%20(0.01)%20with%20precision%20specified%0A%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20in%20arg%20%22prec%22%20(5)%7D%0Aprintln!(%22Hello%20%7B%7D%20is%20%7Bnumber%3A.prec%24%7D%22%2C%20%22x%22%2C%20prec%20%3D%205%2C%20number%20%3D%200.01)%3B%0A%7D">Run</a></pre> <p>While these:</p> <pre class="rust rust-example-rendered"> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}, `{name:.*}` has 3 fractional digits"</span>, <span class="string">"Hello"</span>, <span class="number">3</span>, <span class="ident">name</span><span class="op">=</span><span class="number">1234.56</span>); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}, `{name:.*}` has 3 characters"</span>, <span class="string">"Hello"</span>, <span class="number">3</span>, <span class="ident">name</span><span class="op">=</span><span class="string">"1234.56"</span>); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}, `{name:>8.*}` has 3 right-aligned characters"</span>, <span class="string">"Hello"</span>, <span class="number">3</span>, <span class="ident">name</span><span class="op">=</span><span class="string">"1234.56"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aprintln!(%22%7B%7D%2C%20%60%7Bname%3A.*%7D%60%20has%203%20fractional%20digits%22%2C%20%22Hello%22%2C%203%2C%20name%3D1234.56)%3B%0Aprintln!(%22%7B%7D%2C%20%60%7Bname%3A.*%7D%60%20has%203%20characters%22%2C%20%22Hello%22%2C%203%2C%20name%3D%221234.56%22)%3B%0Aprintln!(%22%7B%7D%2C%20%60%7Bname%3A%3E8.*%7D%60%20has%203%20right-aligned%20characters%22%2C%20%22Hello%22%2C%203%2C%20name%3D%221234.56%22)%3B%0A%7D">Run</a></pre> <p>print two significantly different things:</p> <pre><code class="language-text">Hello, `1234.560` has 3 fractional digits Hello, `123` has 3 characters Hello, ` 123` has 3 right-aligned characters </code></pre> <h1 id="escaping" class="section-header"><a href="#escaping">Escaping</a></h1> <p>The literal characters <code>{</code> and <code>}</code> may be included in a string by preceding them with the same character. For example, the <code>{</code> character is escaped with <code>{{</code> and the <code>}</code> character is escaped with <code>}}</code>.</p> </div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2> <table> <tr class=' module-item'> <td><a class="struct" href="struct.Arguments.html" title='struct std::fmt::Arguments'>Arguments</a></td> <td class='docblock-short'> <p>This structure represents a safely precompiled version of a format string and its arguments. This cannot be generated at runtime because it cannot safely be done, so no constructors are given and the fields are private to prevent modification.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.DebugList.html" title='struct std::fmt::DebugList'>DebugList</a></td> <td class='docblock-short'> <p>A struct to help with <a href="trait.Debug.html"><code>fmt::Debug</code></a> implementations.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.DebugMap.html" title='struct std::fmt::DebugMap'>DebugMap</a></td> <td class='docblock-short'> <p>A struct to help with <a href="trait.Debug.html"><code>fmt::Debug</code></a> implementations.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.DebugSet.html" title='struct std::fmt::DebugSet'>DebugSet</a></td> <td class='docblock-short'> <p>A struct to help with <a href="trait.Debug.html"><code>fmt::Debug</code></a> implementations.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.DebugStruct.html" title='struct std::fmt::DebugStruct'>DebugStruct</a></td> <td class='docblock-short'> <p>A struct to help with <a href="trait.Debug.html"><code>fmt::Debug</code></a> implementations.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.DebugTuple.html" title='struct std::fmt::DebugTuple'>DebugTuple</a></td> <td class='docblock-short'> <p>A struct to help with <a href="trait.Debug.html"><code>fmt::Debug</code></a> implementations.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.Error.html" title='struct std::fmt::Error'>Error</a></td> <td class='docblock-short'> <p>The error type which is returned from formatting a message into a stream.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.Formatter.html" title='struct std::fmt::Formatter'>Formatter</a></td> <td class='docblock-short'> <p>A struct to represent both where to emit formatting strings to and how they should be formatted. A mutable version of this is passed to all formatting traits.</p> </td> </tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2> <table> <tr class=' module-item'> <td><a class="trait" href="trait.Binary.html" title='trait std::fmt::Binary'>Binary</a></td> <td class='docblock-short'> <p><code>b</code> formatting.</p> </td> </tr> <tr class=' module-item'> <td><a class="trait" href="trait.Debug.html" title='trait std::fmt::Debug'>Debug</a></td> <td class='docblock-short'> <p><code>?</code> formatting.</p> </td> </tr> <tr class=' module-item'> <td><a class="trait" href="trait.Display.html" title='trait std::fmt::Display'>Display</a></td> <td class='docblock-short'> <p>Format trait for an empty format, <code>{}</code>.</p> </td> </tr> <tr class=' module-item'> <td><a class="trait" href="trait.LowerExp.html" title='trait std::fmt::LowerExp'>LowerExp</a></td> <td class='docblock-short'> <p><code>e</code> formatting.</p> </td> </tr> <tr class=' module-item'> <td><a class="trait" href="trait.LowerHex.html" title='trait std::fmt::LowerHex'>LowerHex</a></td> <td class='docblock-short'> <p><code>x</code> formatting.</p> </td> </tr> <tr class=' module-item'> <td><a class="trait" href="trait.Octal.html" title='trait std::fmt::Octal'>Octal</a></td> <td class='docblock-short'> <p><code>o</code> formatting.</p> </td> </tr> <tr class=' module-item'> <td><a class="trait" href="trait.Pointer.html" title='trait std::fmt::Pointer'>Pointer</a></td> <td class='docblock-short'> <p><code>p</code> formatting.</p> </td> </tr> <tr class=' module-item'> <td><a class="trait" href="trait.UpperExp.html" title='trait std::fmt::UpperExp'>UpperExp</a></td> <td class='docblock-short'> <p><code>E</code> formatting.</p> </td> </tr> <tr class=' module-item'> <td><a class="trait" href="trait.UpperHex.html" title='trait std::fmt::UpperHex'>UpperHex</a></td> <td class='docblock-short'> <p><code>X</code> formatting.</p> </td> </tr> <tr class=' module-item'> <td><a class="trait" href="trait.Write.html" title='trait std::fmt::Write'>Write</a></td> <td class='docblock-short'> <p>A collection of methods that are required to format a message into a stream.</p> </td> </tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2> <table> <tr class=' module-item'> <td><a class="fn" href="fn.format.html" title='fn std::fmt::format'>format</a></td> <td class='docblock-short'> <p>The <code>format</code> function takes an <a href="struct.Arguments.html"><code>Arguments</code></a> struct and returns the resulting formatted string.</p> </td> </tr> <tr class=' module-item'> <td><a class="fn" href="fn.write.html" title='fn std::fmt::write'>write</a></td> <td class='docblock-short'> <p>The <code>write</code> function takes an output stream, and an <code>Arguments</code> struct that can be precompiled with the <code>format_args!</code> macro.</p> </td> </tr></table><h2 id='types' class='section-header'><a href="#types">Type Definitions</a></h2> <table> <tr class=' module-item'> <td><a class="type" href="type.Result.html" title='type std::fmt::Result'>Result</a></td> <td class='docblock-short'> <p>The type returned by formatter methods.</p> </td> </tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "std";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>