|
12 | 12 | //! Some examples of the [`format!`] extension are:
|
13 | 13 | //!
|
14 | 14 | //! ```
|
15 |
| -//! format!("Hello"); // => "Hello" |
16 |
| -//! format!("Hello, {}!", "world"); // => "Hello, world!" |
17 |
| -//! format!("The number is {}", 1); // => "The number is 1" |
18 |
| -//! format!("{:?}", (3, 4)); // => "(3, 4)" |
19 |
| -//! format!("{value}", value=4); // => "4" |
| 15 | +//! let _ = format!("Hello"); // => "Hello" |
| 16 | +//! let _ = format!("Hello, {}!", "world"); // => "Hello, world!" |
| 17 | +//! let _ = format!("The number is {}", 1); // => "The number is 1" |
| 18 | +//! let _ = format!("{:?}", (3, 4)); // => "(3, 4)" |
| 19 | +//! let _ = format!("{value}", value=4); // => "4" |
20 | 20 | //! let people = "Rustaceans";
|
21 |
| -//! format!("Hello {people}!"); // => "Hello Rustaceans!" |
22 |
| -//! format!("{} {}", 1, 2); // => "1 2" |
23 |
| -//! format!("{:04}", 42); // => "0042" with leading zeros |
24 |
| -//! format!("{:#?}", (100, 200)); // => "( |
25 |
| -//! // 100, |
26 |
| -//! // 200, |
27 |
| -//! // )" |
| 21 | +//! let _ = format!("Hello {people}!"); // => "Hello Rustaceans!" |
| 22 | +//! let _ = format!("{} {}", 1, 2); // => "1 2" |
| 23 | +//! let _ = format!("{:04}", 42); // => "0042" with leading zeros |
| 24 | +//! let _ = format!("{:#?}", (100, 200)); // => "( |
| 25 | +//! // 100, |
| 26 | +//! // 200, |
| 27 | +//! // )" |
28 | 28 | //! ```
|
29 | 29 | //!
|
30 | 30 | //! From these, you can see that the first argument is a format string. It is
|
|
50 | 50 | //! the iterator advances. This leads to behavior like this:
|
51 | 51 | //!
|
52 | 52 | //! ```
|
53 |
| -//! format!("{1} {} {0} {}", 1, 2); // => "2 1 1 2" |
| 53 | +//! let _ = format!("{1} {} {0} {}", 1, 2); // => "2 1 1 2" |
54 | 54 | //! ```
|
55 | 55 | //!
|
56 | 56 | //! The internal iterator over the argument has not been advanced by the time
|
|
77 | 77 | //! For example, the following [`format!`] expressions all use named arguments:
|
78 | 78 | //!
|
79 | 79 | //! ```
|
80 |
| -//! format!("{argument}", argument = "test"); // => "test" |
81 |
| -//! format!("{name} {}", 1, name = 2); // => "2 1" |
82 |
| -//! format!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b" |
| 80 | +//! let _ = format!("{argument}", argument = "test"); // => "test" |
| 81 | +//! let _ = format!("{name} {}", 1, name = 2); // => "2 1" |
| 82 | +//! let _ = format!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b" |
83 | 83 | //! ```
|
84 | 84 | //!
|
85 | 85 | //! If a named parameter does not appear in the argument list, `format!` will
|
86 | 86 | //! reference a variable with that name in the current scope.
|
87 | 87 | //!
|
88 | 88 | //! ```
|
89 | 89 | //! let argument = 2 + 2;
|
90 |
| -//! format!("{argument}"); // => "4" |
| 90 | +//! let _ = format!("{argument}"); // => "4" |
91 | 91 | //!
|
92 | 92 | //! fn make_string(a: u32, b: &str) -> String {
|
93 | 93 | //! format!("{b} {a}")
|
|
0 commit comments