mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-01 06:51:58 +00:00
Rollup merge of #96568 - EliasHolzmann:fmt_doc_fixes, r=joshtriplett
std::fmt: Various fixes and improvements to documentation
This PR contains the following changes:
- **Added argument index comments to examples for specifying precision**
The examples for specifying the precision have comments explaining which
argument the specifier is referring to. However, for implicit positional
arguments, the examples simply refer to "next arg". To simplify following the
comments, "next arg" was supplemented with the actual resulting argument index.
- **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.
- **Fix the grammar documentation**
According to the grammar documented, the format specifier `{: }` should not be
legal because of the whitespace it contains. However, in reality, this is
perfectly fine because the actual implementation allows spaces before the
closing brace. Fixes #71088.
Also, the exact meaning of most of the terminal symbols was not specified, for
example the meaning of `identifier`.
- **Removed reference to Formatter::buf and other private fields**
Formatter::buf is not a public field and therefore isn't very helpful in user-
facing documentation. Also, the other public fields of Formatter were removed
during stabilization of std::fmt (4af3494bb0
) and can only be accessed via
getters.
- **Improved list of formatting macros**
Two improvements:
1. write! can not only receive a `io::Write`, but also a `fmt::Write` as first argument.
2. The description texts now contain links to the actual macros for easier
navigation.
This commit is contained in:
commit
ab1ae48cce
@ -221,10 +221,12 @@
|
||||
//!
|
||||
//! 3. An asterisk `.*`:
|
||||
//!
|
||||
//! `.*` means that this `{...}` is associated with *two* format inputs rather than one: the
|
||||
//! first input holds the `usize` precision, and the second holds the value to print. Note that
|
||||
//! in this case, if one uses the format string `{<arg>:<spec>.*}`, then the `<arg>` part refers
|
||||
//! to the *value* to print, and the `precision` must come in the input preceding `<arg>`.
|
||||
//! `.*` means that this `{...}` is associated with *two* format inputs rather than one:
|
||||
//! - If a format string in the fashion of `{:<spec>.*}` is used, then the first input holds
|
||||
//! the `usize` precision, and the second holds the value to print.
|
||||
//! - If a format string in the fashion of `{<arg>:<spec>.*}` is used, then the `<arg>` part
|
||||
//! refers to the value to print, and the `precision` is taken like it was specified with an
|
||||
//! omitted positional parameter (`{}` instead of `{<arg>:}`).
|
||||
//!
|
||||
//! For example, the following calls all print the same thing `Hello x is 0.01000`:
|
||||
//!
|
||||
@ -238,15 +240,19 @@
|
||||
//! // Hello {arg 0 ("x")} is {arg 2 (0.01) with precision specified in arg 1 (5)}
|
||||
//! println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
|
||||
//!
|
||||
//! // Hello {next arg ("x")} is {second of next two args (0.01) with precision
|
||||
//! // specified in first of next two args (5)}
|
||||
//! // Hello {next arg -> arg 0 ("x")} is {second of next two args -> arg 2 (0.01) with precision
|
||||
//! // specified in first of next two args -> arg 1 (5)}
|
||||
//! println!("Hello {} is {:.*}", "x", 5, 0.01);
|
||||
//!
|
||||
//! // Hello {next arg ("x")} is {arg 2 (0.01) with precision
|
||||
//! // specified in its predecessor (5)}
|
||||
//! // Hello {arg 1 ("x")} is {arg 2 (0.01) with precision
|
||||
//! // specified in next arg -> arg 0 (5)}
|
||||
//! println!("Hello {1} is {2:.*}", 5, "x", 0.01);
|
||||
//!
|
||||
//! // Hello {next arg -> arg 0 ("x")} is {arg 2 (0.01) with precision
|
||||
//! // specified in next arg -> arg 1 (5)}
|
||||
//! println!("Hello {} is {2:.*}", "x", 5, 0.01);
|
||||
//!
|
||||
//! // Hello {next arg ("x")} is {arg "number" (0.01) with precision specified
|
||||
//! // Hello {next arg -> arg 0 ("x")} is {arg "number" (0.01) with precision specified
|
||||
//! // in arg "prec" (5)}
|
||||
//! println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
|
||||
//! ```
|
||||
@ -304,7 +310,7 @@
|
||||
//! ```text
|
||||
//! format_string := text [ maybe_format text ] *
|
||||
//! maybe_format := '{' '{' | '}' '}' | format
|
||||
//! format := '{' [ argument ] [ ':' format_spec ] '}'
|
||||
//! format := '{' [ argument ] [ ':' format_spec ] [ ws ] * '}'
|
||||
//! argument := integer | identifier
|
||||
//!
|
||||
//! format_spec := [[fill]align][sign]['#']['0'][width]['.' precision]type
|
||||
@ -317,7 +323,12 @@
|
||||
//! count := parameter | integer
|
||||
//! parameter := argument '$'
|
||||
//! ```
|
||||
//! In the above grammar, `text` must not contain any `'{'` or `'}'` characters.
|
||||
//! In the above grammar,
|
||||
//! - `text` must not contain any `'{'` or `'}'` characters,
|
||||
//! - `ws` is any character for which [`char::is_whitespace`] returns `true`, has no semantic
|
||||
//! meaning and is completely optional,
|
||||
//! - `integer` is a decimal integer that may contain leading zeroes and
|
||||
//! - `identifier` is an `IDENTIFIER_OR_KEYWORD` (not an `IDENTIFIER`) as defined by the [Rust language reference](https://doc.rust-lang.org/reference/identifiers.html).
|
||||
//!
|
||||
//! # Formatting traits
|
||||
//!
|
||||
@ -358,9 +369,9 @@
|
||||
//! ```
|
||||
//!
|
||||
//! Your type will be passed as `self` by-reference, and then the function
|
||||
//! should emit output into the `f.buf` stream. It is up to each format trait
|
||||
//! implementation to correctly adhere to the requested formatting parameters.
|
||||
//! The values of these parameters will be listed in the fields of the
|
||||
//! should emit output into the Formatter `f` which implements `fmt::Write`. It is up to each
|
||||
//! format trait implementation to correctly adhere to the requested formatting parameters.
|
||||
//! The values of these parameters can be accessed with methods of the
|
||||
//! [`Formatter`] struct. In order to help with this, the [`Formatter`] struct also
|
||||
//! provides some helper methods.
|
||||
//!
|
||||
@ -449,7 +460,7 @@
|
||||
//!
|
||||
//! ```ignore (only-for-syntax-highlight)
|
||||
//! format! // described above
|
||||
//! write! // first argument is a &mut io::Write, the destination
|
||||
//! write! // first argument is either a &mut io::Write or a &mut fmt::Write, the destination
|
||||
//! writeln! // same as write but appends a newline
|
||||
//! print! // the format string is printed to the standard output
|
||||
//! println! // same as print but appends a newline
|
||||
@ -460,11 +471,11 @@
|
||||
//!
|
||||
//! ### `write!`
|
||||
//!
|
||||
//! This and [`writeln!`] are two macros which are used to emit the format string
|
||||
//! [`write!`] and [`writeln!`] are two macros which are used to emit the format string
|
||||
//! to a specified stream. This is used to prevent intermediate allocations of
|
||||
//! format strings and instead directly write the output. Under the hood, this
|
||||
//! function is actually invoking the [`write_fmt`] function defined on the
|
||||
//! [`std::io::Write`] trait. Example usage is:
|
||||
//! [`std::io::Write`] and the [`std::fmt::Write`] trait. Example usage is:
|
||||
//!
|
||||
//! ```
|
||||
//! # #![allow(unused_must_use)]
|
||||
@ -491,7 +502,7 @@
|
||||
//!
|
||||
//! ### `format_args!`
|
||||
//!
|
||||
//! This is a curious macro used to safely pass around
|
||||
//! [`format_args!`] is a curious macro used to safely pass around
|
||||
//! an opaque object describing the format string. This object
|
||||
//! does not require any heap allocations to create, and it only
|
||||
//! references information on the stack. Under the hood, all of
|
||||
@ -529,10 +540,12 @@
|
||||
//! [`to_string`]: crate::string::ToString::to_string "ToString::to_string"
|
||||
//! [`write_fmt`]: ../../std/io/trait.Write.html#method.write_fmt
|
||||
//! [`std::io::Write`]: ../../std/io/trait.Write.html
|
||||
//! [`std::fmt::Write`]: ../../std/fmt/trait.Write.html
|
||||
//! [`print!`]: ../../std/macro.print.html "print!"
|
||||
//! [`println!`]: ../../std/macro.println.html "println!"
|
||||
//! [`eprint!`]: ../../std/macro.eprint.html "eprint!"
|
||||
//! [`eprintln!`]: ../../std/macro.eprintln.html "eprintln!"
|
||||
//! [`format_args!`]: ../../std/macro.format_args.html "format_args!"
|
||||
//! [`fmt::Arguments`]: Arguments "fmt::Arguments"
|
||||
//! [`format`]: format() "fmt::format"
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user