|
17 | 17 | //! format!("The number is {}", 1); // => "The number is 1"
|
18 | 18 | //! format!("{:?}", (3, 4)); // => "(3, 4)"
|
19 | 19 | //! format!("{value}", value=4); // => "4"
|
| 20 | +//! let people = "Rustaceans"; |
| 21 | +//! format!("Hello {people}!"); // => "Hello Rustaceans!" |
20 | 22 | //! format!("{} {}", 1, 2); // => "1 2"
|
21 | 23 | //! format!("{:04}", 42); // => "0042" with leading zeros
|
22 | 24 | //! format!("{:#?}", (100, 200)); // => "(
|
|
80 | 82 | //! format!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
|
81 | 83 | //! ```
|
82 | 84 | //!
|
| 85 | +//! If a named parameter does not appear in the argument list, `format!` will |
| 86 | +//! reference a variable with that name in the current scope. |
| 87 | +//! |
| 88 | +//! ``` |
| 89 | +//! let argument = 2 + 2; |
| 90 | +//! format!("{argument}"); // => "4" |
| 91 | +//! |
| 92 | +//! fn make_string(a: u32, b: &str) -> String { |
| 93 | +//! format!("{b} {a}") |
| 94 | +//! } |
| 95 | +//! make_string(927, "label"); // => "label 927" |
| 96 | +//! ``` |
| 97 | +//! |
83 | 98 | //! It is not valid to put positional parameters (those without names) after
|
84 | 99 | //! arguments that have names. Like with positional parameters, it is not
|
85 | 100 | //! valid to provide named parameters that are unused by the format string.
|
|
98 | 113 | //! println!("Hello {:1$}!", "x", 5);
|
99 | 114 | //! println!("Hello {1:0$}!", 5, "x");
|
100 | 115 | //! println!("Hello {:width$}!", "x", width = 5);
|
| 116 | +//! let width = 5; |
| 117 | +//! println!("Hello {:width$}!", "x"); |
101 | 118 | //! ```
|
102 | 119 | //!
|
103 | 120 | //! This is a parameter for the "minimum width" that the format should take up.
|
|
0 commit comments