Skip to content

Commit 3437865

Browse files
committed
Rollup merge of #23493 - steveklabnik:gh22927, r=alexcrichton
And do some formatting while I'm here.
2 parents 30718dd + 8a8b2ce commit 3437865

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

src/libcollections/fmt.rs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! This macro is implemented in the compiler to emit calls to this module in
1717
//! order to format arguments at runtime into strings and streams.
1818
//!
19-
//! ## Usage
19+
//! # Usage
2020
//!
2121
//! The `format!` macro is intended to be familiar to those coming from C's
2222
//! printf/fprintf functions or Python's `str.format` function. In its current
@@ -41,7 +41,7 @@
4141
//! will then parse the format string and determine if the list of arguments
4242
//! provided is suitable to pass to this format string.
4343
//!
44-
//! ### Positional parameters
44+
//! ## Positional parameters
4545
//!
4646
//! Each formatting argument is allowed to specify which value argument it's
4747
//! referencing, and if omitted it is assumed to be "the next argument". For
@@ -54,7 +54,7 @@
5454
//! iterator over the argument. Each time a "next argument" specifier is seen,
5555
//! the iterator advances. This leads to behavior like this:
5656
//!
57-
//! ```rust
57+
//! ```
5858
//! format!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
5959
//! ```
6060
//!
@@ -68,7 +68,7 @@
6868
//! compile-time error. You may refer to the same argument more than once in the
6969
//! format string, although it must always be referred to with the same type.
7070
//!
71-
//! ### Named parameters
71+
//! ## Named parameters
7272
//!
7373
//! Rust itself does not have a Python-like equivalent of named parameters to a
7474
//! function, but the `format!` macro is a syntax extension which allows it to
@@ -91,7 +91,7 @@
9191
//! arguments which have names. Like with positional parameters, it is illegal
9292
//! to provide named parameters that are unused by the format string.
9393
//!
94-
//! ### Argument types
94+
//! ## Argument types
9595
//!
9696
//! Each argument's type is dictated by the format string. It is a requirement
9797
//! that every argument is only ever referred to by one type. For example, this
@@ -105,18 +105,25 @@
105105
//! hexadecimal as well as an
106106
//! octal.
107107
//!
108-
//! There are various parameters which do require a particular type, however.
109-
//! Namely if the syntax `{:.*}` is used, then the number of characters to print
110-
//! precedes the actual object being formatted, and the number of characters
111-
//! must have the type `usize`. Although a `usize` can be printed with `{}`, it is
112-
//! illegal to reference an argument as such. For example this is another
108+
//! There are various parameters which do require a particular type, however. Namely, the `{:.*}`
109+
//! syntax, which sets the number of numbers after the decimal in floating-point types:
110+
//!
111+
//! ```
112+
//! let formatted_number = format!("{:.*}", 2, 1.234567);
113+
//!
114+
//! assert_eq!("1.23", formatted_number)
115+
//! ```
116+
//!
117+
//! If this syntax is used, then the number of characters to print precedes the actual object being
118+
//! formatted, and the number of characters must have the type `usize`. Although a `usize` can be
119+
//! printed with `{}`, it is illegal to reference an argument as such. For example this is another
113120
//! invalid format string:
114121
//!
115122
//! ```text
116123
//! {:.*} {0}
117124
//! ```
118125
//!
119-
//! ### Formatting traits
126+
//! ## Formatting traits
120127
//!
121128
//! When requesting that an argument be formatted with a particular type, you
122129
//! are actually requesting that an argument ascribes to a particular trait.
@@ -142,7 +149,7 @@
142149
//! When implementing a format trait for your own type, you will have to
143150
//! implement a method of the signature:
144151
//!
145-
//! ```rust
152+
//! ```
146153
//! # use std::fmt;
147154
//! # struct Foo; // our custom type
148155
//! # impl fmt::Display for Foo {
@@ -166,7 +173,7 @@
166173
//! An example of implementing the formatting traits would look
167174
//! like:
168175
//!
169-
//! ```rust
176+
//! ```
170177
//! use std::fmt;
171178
//! use std::f64;
172179
//! use std::num::Float;
@@ -211,7 +218,7 @@
211218
//! }
212219
//! ```
213220
//!
214-
//! #### fmt::Display vs fmt::Debug
221+
//! ### fmt::Display vs fmt::Debug
215222
//!
216223
//! These two formatting traits have distinct purposes:
217224
//!
@@ -231,7 +238,7 @@
231238
//! assert_eq!(format!("{} {:?}", "foo\n", "bar\n"), "foo\n \"bar\\n\"");
232239
//! ```
233240
//!
234-
//! ### Related macros
241+
//! ## Related macros
235242
//!
236243
//! There are a number of related macros in the `format!` family. The ones that
237244
//! are currently implemented are:
@@ -245,32 +252,33 @@
245252
//! format_args! // described below.
246253
//! ```
247254
//!
248-
//! #### `write!`
255+
//! ### `write!`
249256
//!
250257
//! This and `writeln` are two macros which are used to emit the format string
251258
//! to a specified stream. This is used to prevent intermediate allocations of
252259
//! format strings and instead directly write the output. Under the hood, this
253260
//! function is actually invoking the `write` function defined in this module.
254261
//! Example usage is:
255262
//!
256-
//! ```rust
263+
//! ```
257264
//! # #![allow(unused_must_use)]
258265
//! let mut w = Vec::new();
259266
//! write!(&mut w, "Hello {}!", "world");
260267
//! ```
261268
//!
262-
//! #### `print!`
269+
//! ### `print!`
263270
//!
264271
//! This and `println` emit their output to stdout. Similarly to the `write!`
265272
//! macro, the goal of these macros is to avoid intermediate allocations when
266273
//! printing output. Example usage is:
267274
//!
268-
//! ```rust
275+
//! ```
269276
//! print!("Hello {}!", "world");
270277
//! println!("I have a newline {}", "character at the end");
271278
//! ```
272279
//!
273-
//! #### `format_args!`
280+
//! ### `format_args!`
281+
//!
274282
//! This is a curious macro which is used to safely pass around
275283
//! an opaque object describing the format string. This object
276284
//! does not require any heap allocations to create, and it only
@@ -303,7 +311,7 @@
303311
//! it would internally pass around this structure until it has been determined
304312
//! where output should go to.
305313
//!
306-
//! ## Syntax
314+
//! # Syntax
307315
//!
308316
//! The syntax for the formatting language used is drawn from other languages,
309317
//! so it should not be too alien. Arguments are formatted with python-like
@@ -326,14 +334,14 @@
326334
//! parameter := integer '$'
327335
//! ```
328336
//!
329-
//! ## Formatting Parameters
337+
//! # Formatting Parameters
330338
//!
331339
//! Each argument being formatted can be transformed by a number of formatting
332340
//! parameters (corresponding to `format_spec` in the syntax above). These
333341
//! parameters affect the string representation of what's being formatted. This
334342
//! syntax draws heavily from Python's, so it may seem a bit familiar.
335343
//!
336-
//! ### Fill/Alignment
344+
//! ## Fill/Alignment
337345
//!
338346
//! The fill character is provided normally in conjunction with the `width`
339347
//! parameter. This indicates that if the value being formatted is smaller than
@@ -345,7 +353,7 @@
345353
//! * `^` - the argument is center-aligned in `width` columns
346354
//! * `>` - the argument is right-aligned in `width` columns
347355
//!
348-
//! ### Sign/#/0
356+
//! ## Sign/#/0
349357
//!
350358
//! These can all be interpreted as flags for a particular formatter.
351359
//!
@@ -368,7 +376,7 @@
368376
//! same format would yield `-0000001` for the integer `-1`. Notice that
369377
//! the negative version has one fewer zero than the positive version.
370378
//!
371-
//! ### Width
379+
//! ## Width
372380
//!
373381
//! This is a parameter for the "minimum width" that the format should take up.
374382
//! If the value's string does not fill up this many characters, then the
@@ -384,7 +392,7 @@
384392
//! parameters by using the `2$` syntax indicating that the second argument is a
385393
//! `usize` specifying the width.
386394
//!
387-
//! ### Precision
395+
//! ## Precision
388396
//!
389397
//! For non-numeric types, this can be considered a "maximum width". If the
390398
//! resulting string is longer than this width, then it is truncated down to
@@ -395,7 +403,7 @@
395403
//! For floating-point types, this indicates how many digits after the decimal
396404
//! point should be printed.
397405
//!
398-
//! ## Escaping
406+
//! # Escaping
399407
//!
400408
//! The literal characters `{` and `}` may be included in a string by preceding
401409
//! them with the same character. For example, the `{` character is escaped with

0 commit comments

Comments
 (0)