|
394 | 394 | //!
|
395 | 395 | //! ## Precision
|
396 | 396 | //!
|
397 |
| -//! For non-numeric types, this can be considered a "maximum width". If the |
398 |
| -//! resulting string is longer than this width, then it is truncated down to |
399 |
| -//! this many characters and only those are emitted. |
| 397 | +//! For non-numeric types, this can be considered a "maximum width". If the resulting string is |
| 398 | +//! longer than this width, then it is truncated down to this many characters and only those are |
| 399 | +//! emitted. |
400 | 400 | //!
|
401 | 401 | //! For integral types, this has no meaning currently.
|
402 | 402 | //!
|
403 |
| -//! For floating-point types, this indicates how many digits after the decimal |
404 |
| -//! point should be printed. |
| 403 | +//! For floating-point types, this indicates how many digits after the decimal point should be |
| 404 | +//! printed. |
| 405 | +//! |
| 406 | +//! There are three possible ways to specify the desired `precision`: |
| 407 | +//! |
| 408 | +//! There are three possible ways to specify the desired `precision`: |
| 409 | +//! 1. An integer `.N`, |
| 410 | +//! 2. an integer followed by dollar sign `.N$`, or |
| 411 | +//! 3. an asterisk `.*`. |
| 412 | +//! |
| 413 | +//! The first specification, `.N`, means the integer `N` itself is the precision. |
| 414 | +//! |
| 415 | +//! The second, `.N$`, means use format *argument* `N` (which must be a `usize`) as the precision. |
| 416 | +//! |
| 417 | +//! Finally, `.*` means that this `{...}` is associated with *two* format inputs rather than one: |
| 418 | +//! the first input holds the `usize` precision, and the second holds the value to print. Note |
| 419 | +//! that in this case, if one uses the format string `{<arg>:<spec>.*}`, then the `<arg>` part |
| 420 | +//! refers to the *value* to print, and the `precision` must come in the input preceding `<arg>`. |
| 421 | +//! |
| 422 | +//! For example, these: |
| 423 | +//! |
| 424 | +//! ``` |
| 425 | +//! // Hello {arg 0 (x)} is {arg 1 (0.01} with precision specified inline (5)} |
| 426 | +//! println!("Hello {0} is {1:.5}", "x", 0.01); |
| 427 | +//! |
| 428 | +//! // Hello {arg 1 (x)} is {arg 2 (0.01} with precision specified in arg 0 (5)} |
| 429 | +//! println!("Hello {1} is {2:.0$}", 5, "x", 0.01); |
| 430 | +//! |
| 431 | +//! // Hello {arg 0 (x)} is {arg 2 (0.01} with precision specified in arg 1 (5)} |
| 432 | +//! println!("Hello {0} is {2:.1$}", "x", 5, 0.01); |
| 433 | +//! |
| 434 | +//! // Hello {next arg (x)} is {second of next two args (0.01} with precision |
| 435 | +//! // specified in first of next two args (5)} |
| 436 | +//! println!("Hello {} is {:.*}", "x", 5, 0.01); |
| 437 | +//! |
| 438 | +//! // Hello {next arg (x)} is {arg 2 (0.01} with precision |
| 439 | +//! // specified in its predecessor (5)} |
| 440 | +//! println!("Hello {} is {2:.*}", "x", 5, 0.01); |
| 441 | +//! ``` |
| 442 | +//! |
| 443 | +//! All print the same thing: |
| 444 | +//! |
| 445 | +//! ```text |
| 446 | +//! Hello x is 0.01000 |
| 447 | +//! ``` |
| 448 | +//! |
| 449 | +//! While these: |
| 450 | +//! |
| 451 | +//! ``` |
| 452 | +//! println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56); |
| 453 | +//! println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56"); |
| 454 | +//! ``` |
| 455 | +//! |
| 456 | +//! print two significantly different things: |
| 457 | +//! |
| 458 | +//! ```text |
| 459 | +//! Hello, `1234.560` has 3 fractional digits |
| 460 | +//! Hello, `123` has 3 characters |
| 461 | +//! ``` |
405 | 462 | //!
|
406 | 463 | //! # Escaping
|
407 | 464 | //!
|
|
0 commit comments