1010
1111//! Utilities for formatting and printing `String`s
1212//!
13- //! This module contains the runtime support for the `format!` syntax extension.
13+ //! This module contains the runtime support for the [ `format!`] syntax extension.
1414//! This macro is implemented in the compiler to emit calls to this module in
1515//! order to format arguments at runtime into strings.
1616//!
1717//! # Usage
1818//!
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.
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.
2121//!
22- //! Some examples of the `format!` extension are:
22+ //! Some examples of the [ `format!`] extension are:
2323//!
2424//! ```
2525//! format!("Hello"); // => "Hello"
6767//! ## Named parameters
6868//!
6969//! 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
70+ //! function, but the [ `format!`] macro is a syntax extension which allows it to
7171//! leverage named parameters. Named parameters are listed at the end of the
7272//! argument list and have the syntax:
7373//!
7474//! ```text
7575//! identifier '=' expression
7676//! ```
7777//!
78- //! For example, the following `format!` expressions all use named argument:
78+ //! For example, the following [ `format!`] expressions all use named argument:
7979//!
8080//! ```
8181//! format!("{argument}", argument = "test"); // => "test"
102102//!
103103//! If this syntax is used, then the number of characters to print precedes the
104104//! actual object being formatted, and the number of characters must have the
105- //! type `usize`.
105+ //! type [ `usize`] .
106106//!
107107//! ## Formatting traits
108108//!
109109//! When requesting that an argument be formatted with a particular type, you
110110//! 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:
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:
113113//!
114- //! * *nothing* ⇒ [`Display`](trait.Display.html)
115- //! * `?` ⇒ [`Debug`](trait.Debug.html)
114+ //! * *nothing* ⇒ [`Display`]
115+ //! * `?` ⇒ [`Debug`]
116116//! * `o` ⇒ [`Octal`](trait.Octal.html)
117117//! * `x` ⇒ [`LowerHex`](trait.LowerHex.html)
118118//! * `X` ⇒ [`UpperHex`](trait.UpperHex.html)
119119//! * `p` ⇒ [`Pointer`](trait.Pointer.html)
120- //! * `b` ⇒ [`Binary`](trait.Binary.html)
120+ //! * `b` ⇒ [`Binary`]
121121//! * `e` ⇒ [`LowerExp`](trait.LowerExp.html)
122122//! * `E` ⇒ [`UpperExp`](trait.UpperExp.html)
123123//!
124124//! What this means is that any type of argument which implements the
125- //! `fmt::Binary` trait can then be formatted with `{:b}`. Implementations
125+ //! [ `fmt::Binary`][`Binary`] trait can then be formatted with `{:b}`. Implementations
126126//! are provided for these traits for a number of primitive types by the
127127//! standard library as well. If no format is specified (as in `{}` or `{:6}`),
128- //! then the format trait used is the `Display` trait.
128+ //! then the format trait used is the [ `Display`] trait.
129129//!
130130//! When implementing a format trait for your own type, you will have to
131131//! implement a method of the signature:
144144//! should emit output into the `f.buf` stream. It is up to each format trait
145145//! implementation to correctly adhere to the requested formatting parameters.
146146//! 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
147+ //! [ `Formatter`] struct. In order to help with this, the [ `Formatter`] struct also
148148//! provides some helper methods.
149149//!
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` (e.g., when
153- //! calling `write!`) however, they should never return errors spuriously. That
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
154154//! 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
155+ //! passed-in [ `Formatter`] returns an error. This is because, contrary to what
156156//! the function signature might suggest, string formatting is an infallible
157157//! operation. This function only returns a result because writing to the
158158//! underlying stream might fail and it must provide a way to propagate the fact
209209//!
210210//! These two formatting traits have distinct purposes:
211211//!
212- //! - `fmt::Display` implementations assert that the type can be faithfully
212+ //! - [ `fmt::Display`][`Display`] implementations assert that the type can be faithfully
213213//! represented as a UTF-8 string at all times. It is **not** expected that
214214//! all types implement the `Display` trait.
215- //! - `fmt::Debug` implementations should be implemented for **all** public types.
215+ //! - [ `fmt::Debug`][`Debug`] implementations should be implemented for **all** public types.
216216//! 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
217+ //! The purpose of the [ `Debug`] trait is to facilitate debugging Rust code. In
218218//! most cases, using `#[derive(Debug)]` is sufficient and recommended.
219219//!
220220//! Some examples of the output from both traits:
227227//!
228228//! ## Related macros
229229//!
230- //! There are a number of related macros in the `format!` family. The ones that
230+ //! There are a number of related macros in the [ `format!`] family. The ones that
231231//! are currently implemented are:
232232//!
233233//! ```ignore (only-for-syntax-highlight)
241241//!
242242//! ### `write!`
243243//!
244- //! This and `writeln` are two macros which are used to emit the format string
244+ //! This and [ `writeln!`] are two macros which are used to emit the format string
245245//! to a specified stream. This is used to prevent intermediate allocations of
246246//! 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:
247+ //! function is actually invoking the [ `write_fmt`] function defined on the
248+ //! [ `std::io::Write`] trait. Example usage is:
249249//!
250250//! ```
251251//! # #![allow(unused_must_use)]
256256//!
257257//! ### `print!`
258258//!
259- //! This and `println` emit their output to stdout. Similarly to the `write!`
259+ //! This and [ `println!`] emit their output to stdout. Similarly to the [ `write!`]
260260//! macro, the goal of these macros is to avoid intermediate allocations when
261261//! printing output. Example usage is:
262262//!
288288//! my_fmt_fn(format_args!(", or a {} too", "function"));
289289//! ```
290290//!
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
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
293293//! inside this module in order to process the format string.
294294//! The goal of this macro is to even further prevent intermediate allocations
295295//! when dealing formatting strings.
357357//! * `-` - Currently not used
358358//! * `#` - This flag is indicates that the "alternate" form of printing should
359359//! be used. The alternate forms are:
360- //! * `#?` - pretty-print the `Debug` formatting
360+ //! * `#?` - pretty-print the [ `Debug`] formatting
361361//! * `#x` - precedes the argument with a `0x`
362362//! * `#X` - precedes the argument with a `0x`
363363//! * `#b` - precedes the argument with a `0b`
384384//! the `0` flag is specified for numerics, then the implicit fill character is
385385//! `0`.
386386//!
387- //! The value for the width can also be provided as a `usize` in the list of
387+ //! The value for the width can also be provided as a [ `usize`] in the list of
388388//! parameters by using the dollar syntax indicating that the second argument is
389- //! a `usize` specifying the width, for example:
389+ //! a [ `usize`] specifying the width, for example:
390390//!
391391//! ```
392392//! // All of these print "Hello x !"
474474//! The literal characters `{` and `}` may be included in a string by preceding
475475//! them with the same character. For example, the `{` character is escaped with
476476//! `{{` and the `}` character is escaped with `}}`.
477+ //!
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
477500
478501#![ stable( feature = "rust1" , since = "1.0.0" ) ]
479502
@@ -498,10 +521,10 @@ pub use core::fmt::{DebugList, DebugMap, DebugSet, DebugStruct, DebugTuple};
498521
499522use string;
500523
501- /// The `format` function takes an `Arguments` struct and returns the resulting
524+ /// The `format` function takes an [ `Arguments`] struct and returns the resulting
502525/// formatted string.
503526///
504- /// The `Arguments` instance can be created with the `format_args!` macro.
527+ /// The [ `Arguments`] instance can be created with the [ `format_args!`] macro.
505528///
506529/// # Examples
507530///
@@ -514,15 +537,17 @@ use string;
514537/// assert_eq!(s, "Hello, world!");
515538/// ```
516539///
517- /// Please note that using [`format!`][format!] might be preferrable.
540+ /// Please note that using [`format!`] might be preferrable.
518541/// Example:
519542///
520543/// ```
521544/// let s = format!("Hello, {}!", "world");
522545/// assert_eq!(s, "Hello, world!");
523546/// ```
524547///
525- /// [format!]: ../macro.format.html
548+ /// [`Arguments`]: struct.Arguments.html
549+ /// [`format_args!`]: ../../std/macro.format_args.html
550+ /// [`format!`]: ../../std/macro.format.html
526551#[ stable( feature = "rust1" , since = "1.0.0" ) ]
527552pub fn format ( args : Arguments ) -> string:: String {
528553 let capacity = args. estimated_capacity ( ) ;
0 commit comments