]> git.proxmox.com Git - rustc.git/blame - src/libcollections/fmt.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / src / libcollections / fmt.rs
CommitLineData
85aaf69f 1// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
1a4d82fc
JJ
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
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.
1a4d82fc 10
3157f602 11//! Utilities for formatting and printing `String`s
1a4d82fc
JJ
12//!
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
3157f602 15//! order to format arguments at runtime into strings.
1a4d82fc 16//!
c34b1796 17//! # Usage
1a4d82fc
JJ
18//!
19//! The `format!` macro is intended to be familiar to those coming from C's
3157f602 20//! printf/fprintf functions or Python's `str.format` function.
1a4d82fc
JJ
21//!
22//! Some examples of the `format!` extension are:
23//!
85aaf69f
SL
24//! ```
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"
3157f602 31//! format!("{:04}", 42); // => "0042" with leading zeros
1a4d82fc
JJ
32//! ```
33//!
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.
39//!
c34b1796 40//! ## Positional parameters
1a4d82fc
JJ
41//!
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.
47//!
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:
52//!
c34b1796 53//! ```
85aaf69f 54//! format!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
1a4d82fc
JJ
55//! ```
56//!
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.
62//!
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
65//! format string, although it must always be referred to with the same type.
66//!
c34b1796 67//! ## Named parameters
1a4d82fc
JJ
68//!
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:
73//!
74//! ```text
75//! identifier '=' expression
76//! ```
77//!
78//! For example, the following `format!` expressions all use named argument:
79//!
85aaf69f 80//! ```
1a4d82fc 81//! format!("{argument}", argument = "test"); // => "test"
a7813a04 82//! format!("{name} {}", 1, name = 2); // => "2 1"
85aaf69f 83//! format!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
1a4d82fc
JJ
84//! ```
85//!
c1a9b12d
SL
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.
1a4d82fc 89//!
c34b1796 90//! ## Argument types
1a4d82fc
JJ
91//!
92//! Each argument's type is dictated by the format string. It is a requirement
93//! that every argument is only ever referred to by one type. For example, this
94//! is an invalid format string:
95//!
96//! ```text
97//! {0:x} {0:o}
98//! ```
99//!
100//! This is invalid because the first argument is both referred to as a
101//! hexadecimal as well as an
102//! octal.
103//!
c1a9b12d 104//! There are various parameters which do require a particular type, however.
a7813a04
XL
105//! An example is the `{:.*}` syntax, which sets the number of decimal places
106//! in floating-point types:
c34b1796
AL
107//!
108//! ```
109//! let formatted_number = format!("{:.*}", 2, 1.234567);
110//!
111//! assert_eq!("1.23", formatted_number)
112//! ```
113//!
c1a9b12d
SL
114//! If this syntax is used, then the number of characters to print precedes the
115//! actual object being formatted, and the number of characters must have the
116//! type `usize`. Although a `usize` can be printed with `{}`, it is invalid to
117//! reference an argument as such. For example this is another invalid format
118//! string:
1a4d82fc
JJ
119//!
120//! ```text
121//! {:.*} {0}
122//! ```
123//!
c34b1796 124//! ## Formatting traits
1a4d82fc
JJ
125//!
126//! When requesting that an argument be formatted with a particular type, you
127//! are actually requesting that an argument ascribes to a particular trait.
128//! This allows multiple actual types to be formatted via `{:x}` (like `i8` as
85aaf69f 129//! well as `isize`). The current mapping of types to traits is:
1a4d82fc 130//!
c1a9b12d
SL
131//! * *nothing* ⇒ [`Display`](trait.Display.html)
132//! * `?` ⇒ [`Debug`](trait.Debug.html)
133//! * `o` ⇒ [`Octal`](trait.Octal.html)
134//! * `x` ⇒ [`LowerHex`](trait.LowerHex.html)
135//! * `X` ⇒ [`UpperHex`](trait.UpperHex.html)
136//! * `p` ⇒ [`Pointer`](trait.Pointer.html)
137//! * `b` ⇒ [`Binary`](trait.Binary.html)
138//! * `e` ⇒ [`LowerExp`](trait.LowerExp.html)
139//! * `E` ⇒ [`UpperExp`](trait.UpperExp.html)
1a4d82fc
JJ
140//!
141//! What this means is that any type of argument which implements the
85aaf69f 142//! `fmt::Binary` trait can then be formatted with `{:b}`. Implementations
1a4d82fc
JJ
143//! are provided for these traits for a number of primitive types by the
144//! standard library as well. If no format is specified (as in `{}` or `{:6}`),
85aaf69f 145//! then the format trait used is the `Display` trait.
1a4d82fc
JJ
146//!
147//! When implementing a format trait for your own type, you will have to
148//! implement a method of the signature:
149//!
c34b1796 150//! ```
92a42be0 151//! # #![allow(dead_code)]
1a4d82fc
JJ
152//! # use std::fmt;
153//! # struct Foo; // our custom type
85aaf69f
SL
154//! # impl fmt::Display for Foo {
155//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1a4d82fc
JJ
156//! # write!(f, "testing, testing")
157//! # } }
158//! ```
159//!
160//! Your type will be passed as `self` by-reference, and then the function
161//! should emit output into the `f.buf` stream. It is up to each format trait
162//! implementation to correctly adhere to the requested formatting parameters.
163//! The values of these parameters will be listed in the fields of the
164//! `Formatter` struct. In order to help with this, the `Formatter` struct also
165//! provides some helper methods.
166//!
167//! Additionally, the return value of this function is `fmt::Result` which is a
9e0c209e
SL
168//! type alias of `Result<(), std::fmt::Error>`. Formatting implementations
169//! should ensure that they propagate errors from the `Formatter` (e.g., when
170//! calling `write!`) however, they should never return errors spuriously. That
171//! is, a formatting implementation must and may only return an error if the
172//! passed-in `Formatter` returns an error. This is because, contrary to what
173//! the function signature might suggest, string formatting is an infallible
174//! operation. This function only returns a result because writing to the
175//! underlying stream might fail and it must provide a way to propagate the fact
176//! that an error has occurred back up the stack.
1a4d82fc
JJ
177//!
178//! An example of implementing the formatting traits would look
179//! like:
180//!
c34b1796 181//! ```
1a4d82fc 182//! use std::fmt;
1a4d82fc 183//!
85aaf69f 184//! #[derive(Debug)]
1a4d82fc 185//! struct Vector2D {
85aaf69f
SL
186//! x: isize,
187//! y: isize,
1a4d82fc
JJ
188//! }
189//!
85aaf69f 190//! impl fmt::Display for Vector2D {
1a4d82fc 191//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85aaf69f 192//! // The `f` value implements the `Write` trait, which is what the
1a4d82fc
JJ
193//! // write! macro is expecting. Note that this formatting ignores the
194//! // various flags provided to format strings.
195//! write!(f, "({}, {})", self.x, self.y)
196//! }
197//! }
198//!
199//! // Different traits allow different forms of output of a type. The meaning
200//! // of this format is to print the magnitude of a vector.
201//! impl fmt::Binary for Vector2D {
202//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
203//! let magnitude = (self.x * self.x + self.y * self.y) as f64;
204//! let magnitude = magnitude.sqrt();
205//!
206//! // Respect the formatting flags by using the helper method
9346a6ac
AL
207//! // `pad_integral` on the Formatter object. See the method
208//! // documentation for details, and the function `pad` can be used
209//! // to pad strings.
1a4d82fc 210//! let decimals = f.precision().unwrap_or(3);
9346a6ac 211//! let string = format!("{:.*}", decimals, magnitude);
c34b1796 212//! f.pad_integral(true, "", &string)
1a4d82fc
JJ
213//! }
214//! }
215//!
216//! fn main() {
217//! let myvector = Vector2D { x: 3, y: 4 };
218//!
219//! println!("{}", myvector); // => "(3, 4)"
85aaf69f 220//! println!("{:?}", myvector); // => "Vector2D {x: 3, y:4}"
1a4d82fc
JJ
221//! println!("{:10.3b}", myvector); // => " 5.000"
222//! }
223//! ```
224//!
b039eaaf 225//! ### `fmt::Display` vs `fmt::Debug`
1a4d82fc
JJ
226//!
227//! These two formatting traits have distinct purposes:
228//!
85aaf69f 229//! - `fmt::Display` implementations assert that the type can be faithfully
1a4d82fc 230//! represented as a UTF-8 string at all times. It is **not** expected that
85aaf69f
SL
231//! all types implement the `Display` trait.
232//! - `fmt::Debug` implementations should be implemented for **all** public types.
1a4d82fc 233//! Output will typically represent the internal state as faithfully as possible.
85aaf69f
SL
234//! The purpose of the `Debug` trait is to facilitate debugging Rust code. In
235//! most cases, using `#[derive(Debug)]` is sufficient and recommended.
1a4d82fc
JJ
236//!
237//! Some examples of the output from both traits:
238//!
239//! ```
c34b1796 240//! assert_eq!(format!("{} {:?}", 3, 4), "3 4");
1a4d82fc
JJ
241//! assert_eq!(format!("{} {:?}", 'a', 'b'), "a 'b'");
242//! assert_eq!(format!("{} {:?}", "foo\n", "bar\n"), "foo\n \"bar\\n\"");
243//! ```
244//!
c34b1796 245//! ## Related macros
1a4d82fc
JJ
246//!
247//! There are a number of related macros in the `format!` family. The ones that
248//! are currently implemented are:
249//!
250//! ```ignore
251//! format! // described above
9346a6ac 252//! write! // first argument is a &mut io::Write, the destination
1a4d82fc
JJ
253//! writeln! // same as write but appends a newline
254//! print! // the format string is printed to the standard output
255//! println! // same as print but appends a newline
256//! format_args! // described below.
257//! ```
258//!
c34b1796 259//! ### `write!`
1a4d82fc
JJ
260//!
261//! This and `writeln` are two macros which are used to emit the format string
262//! to a specified stream. This is used to prevent intermediate allocations of
263//! format strings and instead directly write the output. Under the hood, this
264//! function is actually invoking the `write` function defined in this module.
265//! Example usage is:
266//!
c34b1796 267//! ```
1a4d82fc 268//! # #![allow(unused_must_use)]
c34b1796 269//! use std::io::Write;
1a4d82fc 270//! let mut w = Vec::new();
85aaf69f 271//! write!(&mut w, "Hello {}!", "world");
1a4d82fc
JJ
272//! ```
273//!
c34b1796 274//! ### `print!`
1a4d82fc
JJ
275//!
276//! This and `println` emit their output to stdout. Similarly to the `write!`
277//! macro, the goal of these macros is to avoid intermediate allocations when
278//! printing output. Example usage is:
279//!
c34b1796 280//! ```
1a4d82fc
JJ
281//! print!("Hello {}!", "world");
282//! println!("I have a newline {}", "character at the end");
283//! ```
284//!
c34b1796
AL
285//! ### `format_args!`
286//!
1a4d82fc
JJ
287//! This is a curious macro which is used to safely pass around
288//! an opaque object describing the format string. This object
289//! does not require any heap allocations to create, and it only
290//! references information on the stack. Under the hood, all of
291//! the related macros are implemented in terms of this. First
292//! off, some example usage is:
293//!
294//! ```
92a42be0 295//! # #![allow(unused_must_use)]
1a4d82fc 296//! use std::fmt;
c34b1796 297//! use std::io::{self, Write};
1a4d82fc 298//!
c34b1796 299//! let mut some_writer = io::stdout();
85aaf69f 300//! write!(&mut some_writer, "{}", format_args!("print with a {}", "macro"));
1a4d82fc
JJ
301//!
302//! fn my_fmt_fn(args: fmt::Arguments) {
c34b1796 303//! write!(&mut io::stdout(), "{}", args);
1a4d82fc 304//! }
a7813a04 305//! my_fmt_fn(format_args!(", or a {} too", "function"));
1a4d82fc
JJ
306//! ```
307//!
308//! The result of the `format_args!` macro is a value of type `fmt::Arguments`.
309//! This structure can then be passed to the `write` and `format` functions
310//! inside this module in order to process the format string.
311//! The goal of this macro is to even further prevent intermediate allocations
312//! when dealing formatting strings.
313//!
314//! For example, a logging library could use the standard formatting syntax, but
315//! it would internally pass around this structure until it has been determined
316//! where output should go to.
317//!
c34b1796 318//! # Syntax
1a4d82fc
JJ
319//!
320//! The syntax for the formatting language used is drawn from other languages,
a7813a04 321//! so it should not be too alien. Arguments are formatted with Python-like
1a4d82fc
JJ
322//! syntax, meaning that arguments are surrounded by `{}` instead of the C-like
323//! `%`. The actual grammar for the formatting syntax is:
324//!
325//! ```text
326//! format_string := <text> [ format <text> ] *
327//! format := '{' [ argument ] [ ':' format_spec ] '}'
328//! argument := integer | identifier
329//!
330//! format_spec := [[fill]align][sign]['#'][0][width]['.' precision][type]
331//! fill := character
332//! align := '<' | '^' | '>'
333//! sign := '+' | '-'
334//! width := count
335//! precision := count | '*'
336//! type := identifier | ''
337//! count := parameter | integer
a7813a04 338//! parameter := argument '$'
1a4d82fc
JJ
339//! ```
340//!
c34b1796 341//! # Formatting Parameters
1a4d82fc
JJ
342//!
343//! Each argument being formatted can be transformed by a number of formatting
344//! parameters (corresponding to `format_spec` in the syntax above). These
345//! parameters affect the string representation of what's being formatted. This
346//! syntax draws heavily from Python's, so it may seem a bit familiar.
347//!
c34b1796 348//! ## Fill/Alignment
1a4d82fc
JJ
349//!
350//! The fill character is provided normally in conjunction with the `width`
351//! parameter. This indicates that if the value being formatted is smaller than
352//! `width` some extra characters will be printed around it. The extra
9cc50fc6
SL
353//! characters are specified by `fill`, and the alignment can be one of the
354//! following options:
1a4d82fc
JJ
355//!
356//! * `<` - the argument is left-aligned in `width` columns
357//! * `^` - the argument is center-aligned in `width` columns
358//! * `>` - the argument is right-aligned in `width` columns
359//!
62682a34
SL
360//! Note that alignment may not be implemented by some types. A good way
361//! to ensure padding is applied is to format your input, then use this
362//! resulting string to pad your output.
363//!
b039eaaf 364//! ## Sign/`#`/`0`
1a4d82fc
JJ
365//!
366//! These can all be interpreted as flags for a particular formatter.
367//!
b039eaaf 368//! * `+` - This is intended for numeric types and indicates that the sign
1a4d82fc
JJ
369//! should always be printed. Positive signs are never printed by
370//! default, and the negative sign is only printed by default for the
b039eaaf 371//! `Signed` trait. This flag indicates that the correct sign (`+` or `-`)
1a4d82fc 372//! should always be printed.
b039eaaf
SL
373//! * `-` - Currently not used
374//! * `#` - This flag is indicates that the "alternate" form of printing should
c1a9b12d
SL
375//! be used. The alternate forms are:
376//! * `#?` - pretty-print the `Debug` formatting
b039eaaf
SL
377//! * `#x` - precedes the argument with a `0x`
378//! * `#X` - precedes the argument with a `0x`
379//! * `#b` - precedes the argument with a `0b`
380//! * `#o` - precedes the argument with a `0o`
381//! * `0` - This is used to indicate for integer formats that the padding should
1a4d82fc 382//! both be done with a `0` character as well as be sign-aware. A format
c34b1796 383//! like `{:08}` would yield `00000001` for the integer `1`, while the
1a4d82fc
JJ
384//! same format would yield `-0000001` for the integer `-1`. Notice that
385//! the negative version has one fewer zero than the positive version.
386//!
c34b1796 387//! ## Width
1a4d82fc
JJ
388//!
389//! This is a parameter for the "minimum width" that the format should take up.
390//! If the value's string does not fill up this many characters, then the
391//! padding specified by fill/alignment will be used to take up the required
392//! space.
393//!
394//! The default fill/alignment for non-numerics is a space and left-aligned. The
395//! defaults for numeric formatters is also a space but with right-alignment. If
b039eaaf
SL
396//! the `0` flag is specified for numerics, then the implicit fill character is
397//! `0`.
1a4d82fc 398//!
85aaf69f 399//! The value for the width can also be provided as a `usize` in the list of
a7813a04
XL
400//! parameters by using the dollar syntax indicating that the second argument is
401//! a `usize` specifying the width, for example:
402//!
403//! ```
404//! // All of these print "Hello x !"
405//! println!("Hello {:5}!", "x");
406//! println!("Hello {:1$}!", "x", 5);
407//! println!("Hello {1:0$}!", 5, "x");
408//! println!("Hello {:width$}!", "x", width = 5);
409//! ```
410//!
411//! Referring to an argument with the dollar syntax does not affect the "next
412//! argument" counter, so it's usually a good idea to refer to arguments by
413//! position, or use named arguments.
1a4d82fc 414//!
c34b1796 415//! ## Precision
1a4d82fc 416//!
9346a6ac 417//! For non-numeric types, this can be considered a "maximum width". If the resulting string is
5bcae85e
SL
418//! longer than this width, then it is truncated down to this many characters and that truncated
419//! value is emitted with proper `fill`, `alignment` and `width` if those parameters are set.
1a4d82fc 420//!
d9579d0f 421//! For integral types, this is ignored.
1a4d82fc 422//!
9346a6ac
AL
423//! For floating-point types, this indicates how many digits after the decimal point should be
424//! printed.
425//!
426//! There are three possible ways to specify the desired `precision`:
427//!
c1a9b12d
SL
428//! 1. An integer `.N`:
429//!
430//! the integer `N` itself is the precision.
431//!
a7813a04 432//! 2. An integer or name followed by dollar sign `.N$`:
9346a6ac 433//!
c1a9b12d 434//! use format *argument* `N` (which must be a `usize`) as the precision.
9346a6ac 435//!
c1a9b12d 436//! 3. An asterisk `.*`:
9346a6ac 437//!
c1a9b12d
SL
438//! `.*` means that this `{...}` is associated with *two* format inputs rather than one: the
439//! first input holds the `usize` precision, and the second holds the value to print. Note that
440//! in this case, if one uses the format string `{<arg>:<spec>.*}`, then the `<arg>` part refers
441//! to the *value* to print, and the `precision` must come in the input preceding `<arg>`.
9346a6ac 442//!
5bcae85e 443//! For example, the following calls all print the same thing `Hello x is 0.01000`:
9346a6ac
AL
444//!
445//! ```
5bcae85e 446//! // Hello {arg 0 ("x")} is {arg 1 (0.01) with precision specified inline (5)}
9346a6ac
AL
447//! println!("Hello {0} is {1:.5}", "x", 0.01);
448//!
5bcae85e 449//! // Hello {arg 1 ("x")} is {arg 2 (0.01) with precision specified in arg 0 (5)}
9346a6ac
AL
450//! println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
451//!
5bcae85e 452//! // Hello {arg 0 ("x")} is {arg 2 (0.01) with precision specified in arg 1 (5)}
9346a6ac
AL
453//! println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
454//!
5bcae85e 455//! // Hello {next arg ("x")} is {second of next two args (0.01) with precision
9346a6ac
AL
456//! // specified in first of next two args (5)}
457//! println!("Hello {} is {:.*}", "x", 5, 0.01);
458//!
5bcae85e 459//! // Hello {next arg ("x")} is {arg 2 (0.01) with precision
9346a6ac
AL
460//! // specified in its predecessor (5)}
461//! println!("Hello {} is {2:.*}", "x", 5, 0.01);
a7813a04 462//!
5bcae85e 463//! // Hello {next arg ("x")} is {arg "number" (0.01) with precision specified
a7813a04
XL
464//! // in arg "prec" (5)}
465//! println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
9346a6ac
AL
466//! ```
467//!
9346a6ac
AL
468//! While these:
469//!
470//! ```
471//! println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
472//! println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
5bcae85e 473//! println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
9346a6ac
AL
474//! ```
475//!
476//! print two significantly different things:
477//!
478//! ```text
479//! Hello, `1234.560` has 3 fractional digits
480//! Hello, `123` has 3 characters
5bcae85e 481//! Hello, ` 123` has 3 right-aligned characters
9346a6ac 482//! ```
1a4d82fc 483//!
c34b1796 484//! # Escaping
1a4d82fc
JJ
485//!
486//! The literal characters `{` and `}` may be included in a string by preceding
487//! them with the same character. For example, the `{` character is escaped with
488//! `{{` and the `}` character is escaped with `}}`.
489
85aaf69f 490#![stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 491
92a42be0
SL
492#[unstable(feature = "fmt_internals", issue = "0")]
493pub use core::fmt::rt;
494#[stable(feature = "rust1", since = "1.0.0")]
495pub use core::fmt::{Formatter, Result, Write};
496#[stable(feature = "rust1", since = "1.0.0")]
c34b1796 497pub use core::fmt::{Octal, Binary};
92a42be0 498#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 499pub use core::fmt::{Display, Debug};
92a42be0 500#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 501pub use core::fmt::{LowerHex, UpperHex, Pointer};
92a42be0 502#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 503pub use core::fmt::{LowerExp, UpperExp};
92a42be0 504#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 505pub use core::fmt::Error;
92a42be0 506#[stable(feature = "rust1", since = "1.0.0")]
9cc50fc6 507pub use core::fmt::{ArgumentV1, Arguments, write};
92a42be0 508#[stable(feature = "rust1", since = "1.0.0")]
e9174d1e 509pub use core::fmt::{DebugList, DebugMap, DebugSet, DebugStruct, DebugTuple};
1a4d82fc 510
85aaf69f 511use string;
1a4d82fc
JJ
512
513/// The format function takes a precompiled format string and a list of
514/// arguments, to return the resulting formatted string.
515///
516/// # Arguments
517///
518/// * args - a structure of arguments generated via the `format_args!` macro.
519///
c34b1796 520/// # Examples
1a4d82fc 521///
a7813a04
XL
522/// Basic usage:
523///
c34b1796 524/// ```
1a4d82fc
JJ
525/// use std::fmt;
526///
527/// let s = fmt::format(format_args!("Hello, {}!", "world"));
a7813a04
XL
528/// assert_eq!(s, "Hello, world!");
529/// ```
530///
531/// Please note that using [`format!`][format!] might be preferrable.
532/// Example:
533///
534/// ```
535/// let s = format!("Hello, {}!", "world");
536/// assert_eq!(s, "Hello, world!");
1a4d82fc 537/// ```
a7813a04 538///
9e0c209e 539/// [format!]: ../macro.format.html
85aaf69f 540#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
541pub fn format(args: Arguments) -> string::String {
542 let mut output = string::String::new();
d9579d0f 543 let _ = output.write_fmt(args);
1a4d82fc
JJ
544 output
545}