|
221 | 221 | //!
|
222 | 222 | //! 3. An asterisk `.*`:
|
223 | 223 | //!
|
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>:}`). |
228 | 230 | //!
|
229 | 231 | //! For example, the following calls all print the same thing `Hello x is 0.01000`:
|
230 | 232 | //!
|
|
238 | 240 | //! // Hello {arg 0 ("x")} is {arg 2 (0.01) with precision specified in arg 1 (5)}
|
239 | 241 | //! println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
|
240 | 242 | //!
|
241 |
| -//! // Hello {next arg ("x")} is {second of next two args (0.01) with precision |
242 |
| -//! // specified in first of next two args (5)} |
| 243 | +//! // Hello {next arg -> arg 0 ("x")} is {second of next two args -> arg 2 (0.01) with precision |
| 244 | +//! // specified in first of next two args -> arg 1 (5)} |
243 | 245 | //! println!("Hello {} is {:.*}", "x", 5, 0.01);
|
244 | 246 | //!
|
245 |
| -//! // Hello {next arg ("x")} is {arg 2 (0.01) with precision |
246 |
| -//! // specified in its predecessor (5)} |
| 247 | +//! // Hello {arg 1 ("x")} is {arg 2 (0.01) with precision |
| 248 | +//! // specified in next arg -> arg 0 (5)} |
| 249 | +//! println!("Hello {1} is {2:.*}", 5, "x", 0.01); |
| 250 | +//! |
| 251 | +//! // Hello {next arg -> arg 0 ("x")} is {arg 2 (0.01) with precision |
| 252 | +//! // specified in next arg -> arg 1 (5)} |
247 | 253 | //! println!("Hello {} is {2:.*}", "x", 5, 0.01);
|
248 | 254 | //!
|
249 |
| -//! // Hello {next arg ("x")} is {arg "number" (0.01) with precision specified |
| 255 | +//! // Hello {next arg -> arg 0 ("x")} is {arg "number" (0.01) with precision specified |
250 | 256 | //! // in arg "prec" (5)}
|
251 | 257 | //! println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
|
252 | 258 | //! ```
|
|
304 | 310 | //! ```text
|
305 | 311 | //! format_string := text [ maybe_format text ] *
|
306 | 312 | //! maybe_format := '{' '{' | '}' '}' | format
|
307 |
| -//! format := '{' [ argument ] [ ':' format_spec ] '}' |
| 313 | +//! format := '{' [ argument ] [ ':' format_spec ] [ ws ] * '}' |
308 | 314 | //! argument := integer | identifier
|
309 | 315 | //!
|
310 | 316 | //! format_spec := [[fill]align][sign]['#']['0'][width]['.' precision]type
|
|
317 | 323 | //! count := parameter | integer
|
318 | 324 | //! parameter := argument '$'
|
319 | 325 | //! ```
|
320 |
| -//! In the above grammar, `text` must not contain any `'{'` or `'}'` characters. |
| 326 | +//! In the above grammar, |
| 327 | +//! - `text` must not contain any `'{'` or `'}'` characters, |
| 328 | +//! - `ws` is any character for which [`char::is_whitespace`] returns `true`, has no semantic |
| 329 | +//! meaning and is completely optional, |
| 330 | +//! - `integer` is a decimal integer that may contain leading zeroes and |
| 331 | +//! - `identifier` is an `IDENTIFIER_OR_KEYWORD` (not an `IDENTIFIER`) as defined by the [Rust language reference](https://doc.rust-lang.org/reference/identifiers.html). |
321 | 332 | //!
|
322 | 333 | //! # Formatting traits
|
323 | 334 | //!
|
|
358 | 369 | //! ```
|
359 | 370 | //!
|
360 | 371 | //! Your type will be passed as `self` by-reference, and then the function
|
361 |
| -//! should emit output into the `f.buf` stream. It is up to each format trait |
362 |
| -//! implementation to correctly adhere to the requested formatting parameters. |
363 |
| -//! The values of these parameters will be listed in the fields of the |
| 372 | +//! should emit output into the Formatter `f` which implements `fmt::Write`. It is up to each |
| 373 | +//! format trait implementation to correctly adhere to the requested formatting parameters. |
| 374 | +//! The values of these parameters can be accessed with methods of the |
364 | 375 | //! [`Formatter`] struct. In order to help with this, the [`Formatter`] struct also
|
365 | 376 | //! provides some helper methods.
|
366 | 377 | //!
|
|
449 | 460 | //!
|
450 | 461 | //! ```ignore (only-for-syntax-highlight)
|
451 | 462 | //! format! // described above
|
452 |
| -//! write! // first argument is a &mut io::Write, the destination |
| 463 | +//! write! // first argument is either a &mut io::Write or a &mut fmt::Write, the destination |
453 | 464 | //! writeln! // same as write but appends a newline
|
454 | 465 | //! print! // the format string is printed to the standard output
|
455 | 466 | //! println! // same as print but appends a newline
|
|
460 | 471 | //!
|
461 | 472 | //! ### `write!`
|
462 | 473 | //!
|
463 |
| -//! This and [`writeln!`] are two macros which are used to emit the format string |
| 474 | +//! [`write!`] and [`writeln!`] are two macros which are used to emit the format string |
464 | 475 | //! to a specified stream. This is used to prevent intermediate allocations of
|
465 | 476 | //! format strings and instead directly write the output. Under the hood, this
|
466 | 477 | //! function is actually invoking the [`write_fmt`] function defined on the
|
467 |
| -//! [`std::io::Write`] trait. Example usage is: |
| 478 | +//! [`std::io::Write`] and the [`std::fmt::Write`] trait. Example usage is: |
468 | 479 | //!
|
469 | 480 | //! ```
|
470 | 481 | //! # #![allow(unused_must_use)]
|
|
491 | 502 | //!
|
492 | 503 | //! ### `format_args!`
|
493 | 504 | //!
|
494 |
| -//! This is a curious macro used to safely pass around |
| 505 | +//! [`format_args!`] is a curious macro used to safely pass around |
495 | 506 | //! an opaque object describing the format string. This object
|
496 | 507 | //! does not require any heap allocations to create, and it only
|
497 | 508 | //! references information on the stack. Under the hood, all of
|
|
529 | 540 | //! [`to_string`]: crate::string::ToString::to_string "ToString::to_string"
|
530 | 541 | //! [`write_fmt`]: ../../std/io/trait.Write.html#method.write_fmt
|
531 | 542 | //! [`std::io::Write`]: ../../std/io/trait.Write.html
|
| 543 | +//! [`std::fmt::Write`]: ../../std/fmt/trait.Write.html |
532 | 544 | //! [`print!`]: ../../std/macro.print.html "print!"
|
533 | 545 | //! [`println!`]: ../../std/macro.println.html "println!"
|
534 | 546 | //! [`eprint!`]: ../../std/macro.eprint.html "eprint!"
|
535 | 547 | //! [`eprintln!`]: ../../std/macro.eprintln.html "eprintln!"
|
| 548 | +//! [`format_args!`]: ../../std/macro.format_args.html "format_args!" |
536 | 549 | //! [`fmt::Arguments`]: Arguments "fmt::Arguments"
|
537 | 550 | //! [`format`]: format() "fmt::format"
|
538 | 551 |
|
|
0 commit comments