Skip to content

Commit 89c3d84

Browse files
committed
Give examples of format args capture in the fmt module documentation
1 parent 4205481 commit 89c3d84

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

library/alloc/src/fmt.rs

+17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
//! format!("The number is {}", 1); // => "The number is 1"
1818
//! format!("{:?}", (3, 4)); // => "(3, 4)"
1919
//! format!("{value}", value=4); // => "4"
20+
//! let people = "Rustaceans";
21+
//! format!("Hello {people}!"); // => "Hello Rustaceans!"
2022
//! format!("{} {}", 1, 2); // => "1 2"
2123
//! format!("{:04}", 42); // => "0042" with leading zeros
2224
//! format!("{:#?}", (100, 200)); // => "(
@@ -80,6 +82,19 @@
8082
//! format!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
8183
//! ```
8284
//!
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+
//!
8398
//! It is not valid to put positional parameters (those without names) after
8499
//! arguments that have names. Like with positional parameters, it is not
85100
//! valid to provide named parameters that are unused by the format string.
@@ -98,6 +113,8 @@
98113
//! println!("Hello {:1$}!", "x", 5);
99114
//! println!("Hello {1:0$}!", 5, "x");
100115
//! println!("Hello {:width$}!", "x", width = 5);
116+
//! let width = 5;
117+
//! println!("Hello {:width$}!", "x");
101118
//! ```
102119
//!
103120
//! This is a parameter for the "minimum width" that the format should take up.

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
#![feature(fmt_internals)]
106106
#![feature(fn_traits)]
107107
#![feature(inherent_ascii_escape)]
108+
#![feature(format_args_capture)]
108109
#![feature(inplace_iteration)]
109110
#![feature(iter_advance_by)]
110111
#![feature(iter_zip)]

0 commit comments

Comments
 (0)