Skip to content

Commit 1288883

Browse files
committed
std::fmt: Fixed documentation for specifying precision via .*
The documentation stated that in case of the syntax `{<arg>:<spec>.*}`, "the `<arg>` part refers to the value to print, and the precision must come in the input preceding `<arg>`". This is not correct: the <arg> part does indeed refer to the value to print, but the precision does not come in the input preciding arg, but in the next implicit input (as if specified with {}). Fixes #96413.
1 parent a707f40 commit 1288883

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

library/alloc/src/fmt.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,12 @@
221221
//!
222222
//! 3. An asterisk `.*`:
223223
//!
224-
//! `.*` means that this `{...}` is associated with *two* format inputs rather than one: the
225-
//! first input holds the `usize` precision, and the second holds the value to print. Note that
226-
//! in this case, if one uses the format string `{<arg>:<spec>.*}`, then the `<arg>` part refers
227-
//! to the *value* to print, and the `precision` must come in the input preceding `<arg>`.
224+
//! `.*` means that this `{...}` is associated with *two* format inputs rather than one:
225+
//! - If a format string in the fashion of `{:<spec>.*}` is used, then the first input holds
226+
//! the `usize` precision, and the second holds the value to print.
227+
//! - If a format string in the fashion of `{<arg>:<spec>.*}` is used, then the `<arg>` part
228+
//! refers to the value to print, and the `precision` is taken like it was specified with an
229+
//! omitted positional parameter (`{}` instead of `{<arg>:}`).
228230
//!
229231
//! For example, the following calls all print the same thing `Hello x is 0.01000`:
230232
//!
@@ -242,8 +244,12 @@
242244
//! // specified in first of next two args (5)}
243245
//! println!("Hello {} is {:.*}", "x", 5, 0.01);
244246
//!
247+
//! // Hello {arg 1 ("x")} is {arg 2 (0.01) with precision
248+
//! // specified in next arg (5)}
249+
//! println!("Hello {1} is {2:.*}", 5, "x", 0.01);
250+
//!
245251
//! // Hello {next arg ("x")} is {arg 2 (0.01) with precision
246-
//! // specified in its predecessor (5)}
252+
//! // specified in next arg (5)}
247253
//! println!("Hello {} is {2:.*}", "x", 5, 0.01);
248254
//!
249255
//! // Hello {next arg ("x")} is {arg "number" (0.01) with precision specified

0 commit comments

Comments
 (0)