|
10 | 10 |
|
11 | 11 | //! Utilities for formatting and printing `String`s
|
12 | 12 | //!
|
13 |
| -//! This module contains the runtime support for the `format!` syntax extension. |
| 13 | +//! This module contains the runtime support for the [`format!`] syntax extension. |
14 | 14 | //! This macro is implemented in the compiler to emit calls to this module in
|
15 | 15 | //! order to format arguments at runtime into strings.
|
16 | 16 | //!
|
17 | 17 | //! # Usage
|
18 | 18 | //!
|
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. |
21 | 21 | //!
|
22 |
| -//! Some examples of the `format!` extension are: |
| 22 | +//! Some examples of the [`format!`] extension are: |
23 | 23 | //!
|
24 | 24 | //! ```
|
25 | 25 | //! format!("Hello"); // => "Hello"
|
|
67 | 67 | //! ## Named parameters
|
68 | 68 | //!
|
69 | 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 |
| 70 | +//! function, but the [`format!`] macro is a syntax extension which allows it to |
71 | 71 | //! leverage named parameters. Named parameters are listed at the end of the
|
72 | 72 | //! argument list and have the syntax:
|
73 | 73 | //!
|
74 | 74 | //! ```text
|
75 | 75 | //! identifier '=' expression
|
76 | 76 | //! ```
|
77 | 77 | //!
|
78 |
| -//! For example, the following `format!` expressions all use named argument: |
| 78 | +//! For example, the following [`format!`] expressions all use named argument: |
79 | 79 | //!
|
80 | 80 | //! ```
|
81 | 81 | //! format!("{argument}", argument = "test"); // => "test"
|
|
102 | 102 | //!
|
103 | 103 | //! If this syntax is used, then the number of characters to print precedes the
|
104 | 104 | //! actual object being formatted, and the number of characters must have the
|
105 |
| -//! type `usize`. |
| 105 | +//! type [`usize`]. |
106 | 106 | //!
|
107 | 107 | //! ## Formatting traits
|
108 | 108 | //!
|
109 | 109 | //! When requesting that an argument be formatted with a particular type, you
|
110 | 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: |
| 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: |
113 | 113 | //!
|
114 |
| -//! * *nothing* ⇒ [`Display`](trait.Display.html) |
115 |
| -//! * `?` ⇒ [`Debug`](trait.Debug.html) |
| 114 | +//! * *nothing* ⇒ [`Display`] |
| 115 | +//! * `?` ⇒ [`Debug`] |
116 | 116 | //! * `o` ⇒ [`Octal`](trait.Octal.html)
|
117 | 117 | //! * `x` ⇒ [`LowerHex`](trait.LowerHex.html)
|
118 | 118 | //! * `X` ⇒ [`UpperHex`](trait.UpperHex.html)
|
119 | 119 | //! * `p` ⇒ [`Pointer`](trait.Pointer.html)
|
120 |
| -//! * `b` ⇒ [`Binary`](trait.Binary.html) |
| 120 | +//! * `b` ⇒ [`Binary`] |
121 | 121 | //! * `e` ⇒ [`LowerExp`](trait.LowerExp.html)
|
122 | 122 | //! * `E` ⇒ [`UpperExp`](trait.UpperExp.html)
|
123 | 123 | //!
|
124 | 124 | //! 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 |
126 | 126 | //! are provided for these traits for a number of primitive types by the
|
127 | 127 | //! 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. |
129 | 129 | //!
|
130 | 130 | //! When implementing a format trait for your own type, you will have to
|
131 | 131 | //! implement a method of the signature:
|
|
144 | 144 | //! should emit output into the `f.buf` stream. It is up to each format trait
|
145 | 145 | //! implementation to correctly adhere to the requested formatting parameters.
|
146 | 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 |
| 147 | +//! [`Formatter`] struct. In order to help with this, the [`Formatter`] struct also |
148 | 148 | //! provides some helper methods.
|
149 | 149 | //!
|
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`] (e.g., when |
| 153 | +//! calling [`write!`]) however, they should never return errors spuriously. That |
154 | 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 |
| 155 | +//! passed-in [`Formatter`] returns an error. This is because, contrary to what |
156 | 156 | //! the function signature might suggest, string formatting is an infallible
|
157 | 157 | //! operation. This function only returns a result because writing to the
|
158 | 158 | //! underlying stream might fail and it must provide a way to propagate the fact
|
|
209 | 209 | //!
|
210 | 210 | //! These two formatting traits have distinct purposes:
|
211 | 211 | //!
|
212 |
| -//! - `fmt::Display` implementations assert that the type can be faithfully |
| 212 | +//! - [`fmt::Display`][`Display] implementations assert that the type can be faithfully |
213 | 213 | //! represented as a UTF-8 string at all times. It is **not** expected that
|
214 | 214 | //! 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. |
216 | 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 |
| 217 | +//! The purpose of the [`Debug`] trait is to facilitate debugging Rust code. In |
218 | 218 | //! most cases, using `#[derive(Debug)]` is sufficient and recommended.
|
219 | 219 | //!
|
220 | 220 | //! Some examples of the output from both traits:
|
|
227 | 227 | //!
|
228 | 228 | //! ## Related macros
|
229 | 229 | //!
|
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 |
231 | 231 | //! are currently implemented are:
|
232 | 232 | //!
|
233 | 233 | //! ```ignore (only-for-syntax-highlight)
|
|
241 | 241 | //!
|
242 | 242 | //! ### `write!`
|
243 | 243 | //!
|
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 |
245 | 245 | //! to a specified stream. This is used to prevent intermediate allocations of
|
246 | 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: |
| 247 | +//! function is actually invoking the [`write_fmt`] function defined on the |
| 248 | +//! [`std::io::Write`] trait. Example usage is: |
249 | 249 | //!
|
250 | 250 | //! ```
|
251 | 251 | //! # #![allow(unused_must_use)]
|
|
256 | 256 | //!
|
257 | 257 | //! ### `print!`
|
258 | 258 | //!
|
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!`] |
260 | 260 | //! macro, the goal of these macros is to avoid intermediate allocations when
|
261 | 261 | //! printing output. Example usage is:
|
262 | 262 | //!
|
|
288 | 288 | //! my_fmt_fn(format_args!(", or a {} too", "function"));
|
289 | 289 | //! ```
|
290 | 290 | //!
|
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 |
293 | 293 | //! inside this module in order to process the format string.
|
294 | 294 | //! The goal of this macro is to even further prevent intermediate allocations
|
295 | 295 | //! when dealing formatting strings.
|
|
384 | 384 | //! the `0` flag is specified for numerics, then the implicit fill character is
|
385 | 385 | //! `0`.
|
386 | 386 | //!
|
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 |
388 | 388 | //! 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: |
390 | 390 | //!
|
391 | 391 | //! ```
|
392 | 392 | //! // All of these print "Hello x !"
|
|
474 | 474 | //! The literal characters `{` and `}` may be included in a string by preceding
|
475 | 475 | //! them with the same character. For example, the `{` character is escaped with
|
476 | 476 | //! `{{` and the `}` character is escaped with `}}`.
|
| 477 | +//! |
| 478 | +//! [`format!`]: ../macro.format.html |
| 479 | +//! [`usize`]: ../primitive.usize.html |
| 480 | +//! [`isize`]: ../primitive.isize.html |
| 481 | +//! [`i8`]: ../primitive.i8.html |
| 482 | +//! [`Display`]: trait.Display.html |
| 483 | +//! [`Binary`]: trait.Binary.html |
| 484 | +//! [`fmt::Result`]: type.Result.html |
| 485 | +//! [`Result`]: ../result/enum.Result.html |
| 486 | +//! [`std::fmt::Error`]: struct.Error.html |
| 487 | +//! [`Formatter`]: struct.Formatter.html |
| 488 | +//! [`write!`]: ../macro.write.html |
| 489 | +//! [`Debug`]: trait.Debug.html |
| 490 | +//! [`format!`]: ../macro.format.html |
| 491 | +//! [`writeln!`]: ../macro.writeln.html |
| 492 | +//! [`write_fmt`]: ../io/trait.Write.html#method.write_fmt |
| 493 | +//! [`std::io::Write`]: ../io/trait.Write.html |
| 494 | +//! [`println!`]: ../macro.println.html |
| 495 | +//! [`write!`]: ../macro.write.html |
| 496 | +//! [`format_args!`]: ../macro.format_args.html |
| 497 | +//! [`fmt::Arguments`]: struct.Arguments.html |
| 498 | +//! [`write`]: fn.write.html |
| 499 | +//! [`format`]: fn.format.html |
477 | 500 |
|
478 | 501 | #![stable(feature = "rust1", since = "1.0.0")]
|
479 | 502 |
|
|
0 commit comments