1 # `format_args_capture`
3 The tracking issue for this feature is: [#67984]
5 [#67984]: https://github.com/rust-lang/rust/issues/67984
7 ------------------------
9 Enables `format_args!` (and macros which use `format_args!` in their implementation, such
10 as `format!`, `print!` and `panic!`) to capture variables from the surrounding scope.
11 This avoids the need to pass named parameters when the binding in question
12 already exists in scope.
15 #![feature(format_args_capture)]
17 let (person, species, name) = ("Charlie Brown", "dog", "Snoopy");
19 // captures named argument `person`
20 print!("Hello {person}");
22 // captures named arguments `species` and `name`
23 format!("The {species}'s name is {name}.");
26 This also works for formatting parameters such as width and precision:
29 #![feature(format_args_capture)]
32 let s = format!("{:.precision$}", 1.324223);
34 assert_eq!(&s, "1.32");
37 A non-exhaustive list of macros which benefit from this functionality include:
39 - `print!` and `println!`
40 - `eprint!` and `eprintln!`
41 - `write!` and `writeln!`
46 - `assert!` and similar
47 - macros in many thirdparty crates, such as `log`