1 // Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Utilities for formatting and printing `String`s
13 //! This module contains the runtime support for the [`format!`] syntax extension.
14 //! This macro is implemented in the compiler to emit calls to this module in
15 //! order to format arguments at runtime into strings.
19 //! The [`format!`] macro is intended to be familiar to those coming from C's
20 //! `printf`/`fprintf` functions or Python's `str.format` function.
22 //! Some examples of the [`format!`] extension are:
25 //! format!("Hello"); // => "Hello"
26 //! format!("Hello, {}!", "world"); // => "Hello, world!"
27 //! format!("The number is {}", 1); // => "The number is 1"
28 //! format!("{:?}", (3, 4)); // => "(3, 4)"
29 //! format!("{value}", value=4); // => "4"
30 //! format!("{} {}", 1, 2); // => "1 2"
31 //! format!("{:04}", 42); // => "0042" with leading zeros
34 //! From these, you can see that the first argument is a format string. It is
35 //! required by the compiler for this to be a string literal; it cannot be a
36 //! variable passed in (in order to perform validity checking). The compiler
37 //! will then parse the format string and determine if the list of arguments
38 //! provided is suitable to pass to this format string.
40 //! ## Positional parameters
42 //! Each formatting argument is allowed to specify which value argument it's
43 //! referencing, and if omitted it is assumed to be "the next argument". For
44 //! example, the format string `{} {} {}` would take three parameters, and they
45 //! would be formatted in the same order as they're given. The format string
46 //! `{2} {1} {0}`, however, would format arguments in reverse order.
48 //! Things can get a little tricky once you start intermingling the two types of
49 //! positional specifiers. The "next argument" specifier can be thought of as an
50 //! iterator over the argument. Each time a "next argument" specifier is seen,
51 //! the iterator advances. This leads to behavior like this:
54 //! format!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
57 //! The internal iterator over the argument has not been advanced by the time
58 //! the first `{}` is seen, so it prints the first argument. Then upon reaching
59 //! the second `{}`, the iterator has advanced forward to the second argument.
60 //! Essentially, parameters which explicitly name their argument do not affect
61 //! parameters which do not name an argument in terms of positional specifiers.
63 //! A format string is required to use all of its arguments, otherwise it is a
64 //! compile-time error. You may refer to the same argument more than once in the
67 //! ## Named parameters
69 //! Rust itself does not have a Python-like equivalent of named parameters to a
70 //! function, but the [`format!`] macro is a syntax extension which allows it to
71 //! leverage named parameters. Named parameters are listed at the end of the
72 //! argument list and have the syntax:
75 //! identifier '=' expression
78 //! For example, the following [`format!`] expressions all use named argument:
81 //! format!("{argument}", argument = "test"); // => "test"
82 //! format!("{name} {}", 1, name = 2); // => "2 1"
83 //! format!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
86 //! It is not valid to put positional parameters (those without names) after
87 //! arguments which have names. Like with positional parameters, it is not
88 //! valid to provide named parameters that are unused by the format string.
92 //! Each argument's type is dictated by the format string.
93 //! There are various parameters which require a particular type, however.
94 //! An example is the `{:.*}` syntax, which sets the number of decimal places
95 //! in floating-point types:
98 //! let formatted_number = format!("{:.*}", 2, 1.234567);
100 //! assert_eq!("1.23", formatted_number)
103 //! If this syntax is used, then the number of characters to print precedes the
104 //! actual object being formatted, and the number of characters must have the
107 //! ## Formatting traits
109 //! When requesting that an argument be formatted with a particular type, you
110 //! are actually requesting that an argument ascribes to a particular trait.
111 //! This allows multiple actual types to be formatted via `{:x}` (like [`i8`] as
112 //! well as [`isize`]). The current mapping of types to traits is:
114 //! * *nothing* ⇒ [`Display`]
115 //! * `?` ⇒ [`Debug`]
116 //! * `o` ⇒ [`Octal`](trait.Octal.html)
117 //! * `x` ⇒ [`LowerHex`](trait.LowerHex.html)
118 //! * `X` ⇒ [`UpperHex`](trait.UpperHex.html)
119 //! * `p` ⇒ [`Pointer`](trait.Pointer.html)
120 //! * `b` ⇒ [`Binary`]
121 //! * `e` ⇒ [`LowerExp`](trait.LowerExp.html)
122 //! * `E` ⇒ [`UpperExp`](trait.UpperExp.html)
124 //! What this means is that any type of argument which implements the
125 //! [`fmt::Binary`][`Binary`] trait can then be formatted with `{:b}`. Implementations
126 //! are provided for these traits for a number of primitive types by the
127 //! standard library as well. If no format is specified (as in `{}` or `{:6}`),
128 //! then the format trait used is the [`Display`] trait.
130 //! When implementing a format trait for your own type, you will have to
131 //! implement a method of the signature:
134 //! # #![allow(dead_code)]
136 //! # struct Foo; // our custom type
137 //! # impl fmt::Display for Foo {
138 //! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139 //! # write!(f, "testing, testing")
143 //! Your type will be passed as `self` by-reference, and then the function
144 //! should emit output into the `f.buf` stream. It is up to each format trait
145 //! implementation to correctly adhere to the requested formatting parameters.
146 //! The values of these parameters will be listed in the fields of the
147 //! [`Formatter`] struct. In order to help with this, the [`Formatter`] struct also
148 //! provides some helper methods.
150 //! Additionally, the return value of this function is [`fmt::Result`] which is a
151 //! type alias of [`Result`]`<(), `[`std::fmt::Error`]`>`. Formatting implementations
152 //! should ensure that they propagate errors from the [`Formatter`][`Formatter`] (e.g., when
153 //! calling [`write!`]) however, they should never return errors spuriously. That
154 //! is, a formatting implementation must and may only return an error if the
155 //! passed-in [`Formatter`] returns an error. This is because, contrary to what
156 //! the function signature might suggest, string formatting is an infallible
157 //! operation. This function only returns a result because writing to the
158 //! underlying stream might fail and it must provide a way to propagate the fact
159 //! that an error has occurred back up the stack.
161 //! An example of implementing the formatting traits would look
168 //! struct Vector2D {
173 //! impl fmt::Display for Vector2D {
174 //! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
175 //! // The `f` value implements the `Write` trait, which is what the
176 //! // write! macro is expecting. Note that this formatting ignores the
177 //! // various flags provided to format strings.
178 //! write!(f, "({}, {})", self.x, self.y)
182 //! // Different traits allow different forms of output of a type. The meaning
183 //! // of this format is to print the magnitude of a vector.
184 //! impl fmt::Binary for Vector2D {
185 //! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
186 //! let magnitude = (self.x * self.x + self.y * self.y) as f64;
187 //! let magnitude = magnitude.sqrt();
189 //! // Respect the formatting flags by using the helper method
190 //! // `pad_integral` on the Formatter object. See the method
191 //! // documentation for details, and the function `pad` can be used
192 //! // to pad strings.
193 //! let decimals = f.precision().unwrap_or(3);
194 //! let string = format!("{:.*}", decimals, magnitude);
195 //! f.pad_integral(true, "", &string)
200 //! let myvector = Vector2D { x: 3, y: 4 };
202 //! println!("{}", myvector); // => "(3, 4)"
203 //! println!("{:?}", myvector); // => "Vector2D {x: 3, y:4}"
204 //! println!("{:10.3b}", myvector); // => " 5.000"
208 //! ### `fmt::Display` vs `fmt::Debug`
210 //! These two formatting traits have distinct purposes:
212 //! - [`fmt::Display`][`Display`] implementations assert that the type can be faithfully
213 //! represented as a UTF-8 string at all times. It is **not** expected that
214 //! all types implement the [`Display`] trait.
215 //! - [`fmt::Debug`][`Debug`] implementations should be implemented for **all** public types.
216 //! Output will typically represent the internal state as faithfully as possible.
217 //! The purpose of the [`Debug`] trait is to facilitate debugging Rust code. In
218 //! most cases, using `#[derive(Debug)]` is sufficient and recommended.
220 //! Some examples of the output from both traits:
223 //! assert_eq!(format!("{} {:?}", 3, 4), "3 4");
224 //! assert_eq!(format!("{} {:?}", 'a', 'b'), "a 'b'");
225 //! assert_eq!(format!("{} {:?}", "foo\n", "bar\n"), "foo\n \"bar\\n\"");
228 //! ## Related macros
230 //! There are a number of related macros in the [`format!`] family. The ones that
231 //! are currently implemented are:
233 //! ```ignore (only-for-syntax-highlight)
234 //! format! // described above
235 //! write! // first argument is a &mut io::Write, the destination
236 //! writeln! // same as write but appends a newline
237 //! print! // the format string is printed to the standard output
238 //! println! // same as print but appends a newline
239 //! format_args! // described below.
244 //! This and [`writeln!`] are two macros which are used to emit the format string
245 //! to a specified stream. This is used to prevent intermediate allocations of
246 //! format strings and instead directly write the output. Under the hood, this
247 //! function is actually invoking the [`write_fmt`] function defined on the
248 //! [`std::io::Write`] trait. Example usage is:
251 //! # #![allow(unused_must_use)]
252 //! use std::io::Write;
253 //! let mut w = Vec::new();
254 //! write!(&mut w, "Hello {}!", "world");
259 //! This and [`println!`] emit their output to stdout. Similarly to the [`write!`]
260 //! macro, the goal of these macros is to avoid intermediate allocations when
261 //! printing output. Example usage is:
264 //! print!("Hello {}!", "world");
265 //! println!("I have a newline {}", "character at the end");
268 //! ### `format_args!`
270 //! This is a curious macro which is used to safely pass around
271 //! an opaque object describing the format string. This object
272 //! does not require any heap allocations to create, and it only
273 //! references information on the stack. Under the hood, all of
274 //! the related macros are implemented in terms of this. First
275 //! off, some example usage is:
278 //! # #![allow(unused_must_use)]
280 //! use std::io::{self, Write};
282 //! let mut some_writer = io::stdout();
283 //! write!(&mut some_writer, "{}", format_args!("print with a {}", "macro"));
285 //! fn my_fmt_fn(args: fmt::Arguments) {
286 //! write!(&mut io::stdout(), "{}", args);
288 //! my_fmt_fn(format_args!(", or a {} too", "function"));
291 //! The result of the [`format_args!`] macro is a value of type [`fmt::Arguments`].
292 //! This structure can then be passed to the [`write`] and [`format`] functions
293 //! inside this module in order to process the format string.
294 //! The goal of this macro is to even further prevent intermediate allocations
295 //! when dealing formatting strings.
297 //! For example, a logging library could use the standard formatting syntax, but
298 //! it would internally pass around this structure until it has been determined
299 //! where output should go to.
303 //! The syntax for the formatting language used is drawn from other languages,
304 //! so it should not be too alien. Arguments are formatted with Python-like
305 //! syntax, meaning that arguments are surrounded by `{}` instead of the C-like
306 //! `%`. The actual grammar for the formatting syntax is:
309 //! format_string := <text> [ maybe-format <text> ] *
310 //! maybe-format := '{' '{' | '}' '}' | <format>
311 //! format := '{' [ argument ] [ ':' format_spec ] '}'
312 //! argument := integer | identifier
314 //! format_spec := [[fill]align][sign]['#']['0'][width]['.' precision][type]
315 //! fill := character
316 //! align := '<' | '^' | '>'
317 //! sign := '+' | '-'
319 //! precision := count | '*'
320 //! type := identifier | ''
321 //! count := parameter | integer
322 //! parameter := argument '$'
325 //! # Formatting Parameters
327 //! Each argument being formatted can be transformed by a number of formatting
328 //! parameters (corresponding to `format_spec` in the syntax above). These
329 //! parameters affect the string representation of what's being formatted. This
330 //! syntax draws heavily from Python's, so it may seem a bit familiar.
332 //! ## Fill/Alignment
334 //! The fill character is provided normally in conjunction with the `width`
335 //! parameter. This indicates that if the value being formatted is smaller than
336 //! `width` some extra characters will be printed around it. The extra
337 //! characters are specified by `fill`, and the alignment can be one of the
338 //! following options:
340 //! * `<` - the argument is left-aligned in `width` columns
341 //! * `^` - the argument is center-aligned in `width` columns
342 //! * `>` - the argument is right-aligned in `width` columns
344 //! Note that alignment may not be implemented by some types. A good way
345 //! to ensure padding is applied is to format your input, then use this
346 //! resulting string to pad your output.
350 //! These can all be interpreted as flags for a particular formatter.
352 //! * `+` - This is intended for numeric types and indicates that the sign
353 //! should always be printed. Positive signs are never printed by
354 //! default, and the negative sign is only printed by default for the
355 //! `Signed` trait. This flag indicates that the correct sign (`+` or `-`)
356 //! should always be printed.
357 //! * `-` - Currently not used
358 //! * `#` - This flag is indicates that the "alternate" form of printing should
359 //! be used. The alternate forms are:
360 //! * `#?` - pretty-print the [`Debug`] formatting
361 //! * `#x` - precedes the argument with a `0x`
362 //! * `#X` - precedes the argument with a `0x`
363 //! * `#b` - precedes the argument with a `0b`
364 //! * `#o` - precedes the argument with a `0o`
365 //! * `0` - This is used to indicate for integer formats that the padding should
366 //! both be done with a `0` character as well as be sign-aware. A format
367 //! like `{:08}` would yield `00000001` for the integer `1`, while the
368 //! same format would yield `-0000001` for the integer `-1`. Notice that
369 //! the negative version has one fewer zero than the positive version.
370 //! Note that padding zeroes are always placed after the sign (if any)
371 //! and before the digits. When used together with the `#` flag, a similar
372 //! rule applies: padding zeroes are inserted after the prefix but before
377 //! This is a parameter for the "minimum width" that the format should take up.
378 //! If the value's string does not fill up this many characters, then the
379 //! padding specified by fill/alignment will be used to take up the required
382 //! The default fill/alignment for non-numerics is a space and left-aligned. The
383 //! defaults for numeric formatters is also a space but with right-alignment. If
384 //! the `0` flag is specified for numerics, then the implicit fill character is
387 //! The value for the width can also be provided as a [`usize`] in the list of
388 //! parameters by using the dollar syntax indicating that the second argument is
389 //! a [`usize`] specifying the width, for example:
392 //! // All of these print "Hello x !"
393 //! println!("Hello {:5}!", "x");
394 //! println!("Hello {:1$}!", "x", 5);
395 //! println!("Hello {1:0$}!", 5, "x");
396 //! println!("Hello {:width$}!", "x", width = 5);
399 //! Referring to an argument with the dollar syntax does not affect the "next
400 //! argument" counter, so it's usually a good idea to refer to arguments by
401 //! position, or use named arguments.
405 //! For non-numeric types, this can be considered a "maximum width". If the resulting string is
406 //! longer than this width, then it is truncated down to this many characters and that truncated
407 //! value is emitted with proper `fill`, `alignment` and `width` if those parameters are set.
409 //! For integral types, this is ignored.
411 //! For floating-point types, this indicates how many digits after the decimal point should be
414 //! There are three possible ways to specify the desired `precision`:
416 //! 1. An integer `.N`:
418 //! the integer `N` itself is the precision.
420 //! 2. An integer or name followed by dollar sign `.N$`:
422 //! use format *argument* `N` (which must be a `usize`) as the precision.
424 //! 3. An asterisk `.*`:
426 //! `.*` means that this `{...}` is associated with *two* format inputs rather than one: the
427 //! first input holds the `usize` precision, and the second holds the value to print. Note that
428 //! in this case, if one uses the format string `{<arg>:<spec>.*}`, then the `<arg>` part refers
429 //! to the *value* to print, and the `precision` must come in the input preceding `<arg>`.
431 //! For example, the following calls all print the same thing `Hello x is 0.01000`:
434 //! // Hello {arg 0 ("x")} is {arg 1 (0.01) with precision specified inline (5)}
435 //! println!("Hello {0} is {1:.5}", "x", 0.01);
437 //! // Hello {arg 1 ("x")} is {arg 2 (0.01) with precision specified in arg 0 (5)}
438 //! println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
440 //! // Hello {arg 0 ("x")} is {arg 2 (0.01) with precision specified in arg 1 (5)}
441 //! println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
443 //! // Hello {next arg ("x")} is {second of next two args (0.01) with precision
444 //! // specified in first of next two args (5)}
445 //! println!("Hello {} is {:.*}", "x", 5, 0.01);
447 //! // Hello {next arg ("x")} is {arg 2 (0.01) with precision
448 //! // specified in its predecessor (5)}
449 //! println!("Hello {} is {2:.*}", "x", 5, 0.01);
451 //! // Hello {next arg ("x")} is {arg "number" (0.01) with precision specified
452 //! // in arg "prec" (5)}
453 //! println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
459 //! println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
460 //! println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
461 //! println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
464 //! print two significantly different things:
467 //! Hello, `1234.560` has 3 fractional digits
468 //! Hello, `123` has 3 characters
469 //! Hello, ` 123` has 3 right-aligned characters
474 //! The literal characters `{` and `}` may be included in a string by preceding
475 //! them with the same character. For example, the `{` character is escaped with
476 //! `{{` and the `}` character is escaped with `}}`.
478 //! [`format!`]: ../../macro.format.html
479 //! [`usize`]: ../../std/primitive.usize.html
480 //! [`isize`]: ../../std/primitive.isize.html
481 //! [`i8`]: ../../std/primitive.i8.html
482 //! [`Display`]: trait.Display.html
483 //! [`Binary`]: trait.Binary.html
484 //! [`fmt::Result`]: type.Result.html
485 //! [`Result`]: ../../std/result/enum.Result.html
486 //! [`std::fmt::Error`]: struct.Error.html
487 //! [`Formatter`]: struct.Formatter.html
488 //! [`write!`]: ../../std/macro.write.html
489 //! [`Debug`]: trait.Debug.html
490 //! [`format!`]: ../../std/macro.format.html
491 //! [`writeln!`]: ../../std/macro.writeln.html
492 //! [`write_fmt`]: ../../std/io/trait.Write.html#method.write_fmt
493 //! [`std::io::Write`]: ../../std/io/trait.Write.html
494 //! [`println!`]: ../../std/macro.println.html
495 //! [`write!`]: ../../std/macro.write.html
496 //! [`format_args!`]: ../../std/macro.format_args.html
497 //! [`fmt::Arguments`]: struct.Arguments.html
498 //! [`write`]: fn.write.html
499 //! [`format`]: fn.format.html
501 #![stable(feature = "rust1", since = "1.0.0")]
503 #[unstable(feature = "fmt_internals", issue = "0")]
504 pub use core::fmt::rt;
505 #[stable(feature = "rust1", since = "1.0.0")]
506 pub use core::fmt::{Formatter, Result, Write};
507 #[stable(feature = "rust1", since = "1.0.0")]
508 pub use core::fmt::{Octal, Binary};
509 #[stable(feature = "rust1", since = "1.0.0")]
510 pub use core::fmt::{Display, Debug};
511 #[stable(feature = "rust1", since = "1.0.0")]
512 pub use core::fmt::{LowerHex, UpperHex, Pointer};
513 #[stable(feature = "rust1", since = "1.0.0")]
514 pub use core::fmt::{LowerExp, UpperExp};
515 #[stable(feature = "rust1", since = "1.0.0")]
516 pub use core::fmt::Error;
517 #[stable(feature = "rust1", since = "1.0.0")]
518 pub use core::fmt::{ArgumentV1, Arguments, write};
519 #[stable(feature = "rust1", since = "1.0.0")]
520 pub use core::fmt::{DebugList, DebugMap, DebugSet, DebugStruct, DebugTuple};
524 /// The `format` function takes an [`Arguments`] struct and returns the resulting
525 /// formatted string.
527 /// The [`Arguments`] instance can be created with the [`format_args!`] macro.
536 /// let s = fmt::format(format_args!("Hello, {}!", "world"));
537 /// assert_eq!(s, "Hello, world!");
540 /// Please note that using [`format!`] might be preferrable.
544 /// let s = format!("Hello, {}!", "world");
545 /// assert_eq!(s, "Hello, world!");
548 /// [`Arguments`]: struct.Arguments.html
549 /// [`format_args!`]: ../../std/macro.format_args.html
550 /// [`format!`]: ../../std/macro.format.html
551 #[stable(feature = "rust1", since = "1.0.0")]
552 pub fn format(args: Arguments) -> string::String {
553 let capacity = args.estimated_capacity();
554 let mut output = string::String::with_capacity(capacity);
555 output.write_fmt(args)
556 .expect("a formatting trait implementation returned an error");