|
333 | 333 | //! precision := count | '*'
|
334 | 334 | //! type := identifier | ''
|
335 | 335 | //! count := parameter | integer
|
336 |
| -//! parameter := integer '$' |
| 336 | +//! parameter := argument '$' |
337 | 337 | //! ```
|
338 | 338 | //!
|
339 | 339 | //! # Formatting Parameters
|
|
403 | 403 | //! println!("Hello {:5}!", "x");
|
404 | 404 | //! println!("Hello {:1$}!", "x", 5);
|
405 | 405 | //! println!("Hello {1:0$}!", 5, "x");
|
| 406 | +//! println!("Hello {:width$}!", "x", width = 5); |
406 | 407 | //! ```
|
407 | 408 | //!
|
408 | 409 | //! Referring to an argument with the dollar syntax does not affect the "next
|
409 |
| -//! argument" counter, so it's usually a good idea to refer to all arguments by |
410 |
| -//! their position explicitly. |
| 410 | +//! argument" counter, so it's usually a good idea to refer to arguments by |
| 411 | +//! position, or use named arguments. |
411 | 412 | //!
|
412 | 413 | //! ## Precision
|
413 | 414 | //!
|
|
426 | 427 | //!
|
427 | 428 | //! the integer `N` itself is the precision.
|
428 | 429 | //!
|
429 |
| -//! 2. An integer followed by dollar sign `.N$`: |
| 430 | +//! 2. An integer or name followed by dollar sign `.N$`: |
430 | 431 | //!
|
431 | 432 | //! use format *argument* `N` (which must be a `usize`) as the precision.
|
432 | 433 | //!
|
|
456 | 457 | //! // Hello {next arg (x)} is {arg 2 (0.01) with precision
|
457 | 458 | //! // specified in its predecessor (5)}
|
458 | 459 | //! println!("Hello {} is {2:.*}", "x", 5, 0.01);
|
| 460 | +//! |
| 461 | +//! // Hello {next arg (x)} is {arg "number" (0.01) with precision specified |
| 462 | +//! // in arg "prec" (5)} |
| 463 | +//! println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01); |
459 | 464 | //! ```
|
460 | 465 | //!
|
461 | 466 | //! All print the same thing:
|
|
0 commit comments