about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Hewitt <1939362+davidhewitt@users.noreply.github.com>2020-06-27 21:18:02 +0100
committerDavid Hewitt <1939362+davidhewitt@users.noreply.github.com>2020-06-27 22:02:01 +0100
commita1217cb29de22aae3cda717e78d1edd3e9d8ffd1 (patch)
tree6bc795b7820949e43796b90d5db7882dcf46a792
parent8caf60407033e84592821a3f7b3917fe80d343e0 (diff)
downloadrust-a1217cb29de22aae3cda717e78d1edd3e9d8ffd1.tar.gz
rust-a1217cb29de22aae3cda717e78d1edd3e9d8ffd1.zip
Add `format_args_capture` to the unstable book
-rw-r--r--src/doc/unstable-book/src/library-features/format-args-capture.md47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/doc/unstable-book/src/library-features/format-args-capture.md b/src/doc/unstable-book/src/library-features/format-args-capture.md
new file mode 100644
index 00000000000..64b1b3d81bd
--- /dev/null
+++ b/src/doc/unstable-book/src/library-features/format-args-capture.md
@@ -0,0 +1,47 @@
+# `format_args_capture`
+
+The tracking issue for this feature is: [#67984]
+
+[#67984]: https://github.com/rust-lang/rust/issues/67984
+
+------------------------
+
+Enables `format_args!` (and macros which use `format_args!` in their implementation, such
+as `format!`, `print!` and `panic!`) to capture variables from the surrounding scope.
+This avoids the need to pass named parameters when the binding in question
+already exists in scope.
+
+```rust
+#![feature(format_args_capture)]
+
+let (person, species, name) = ("Charlie Brown", "dog", "Snoopy");
+
+// captures named argument `person`
+print!("Hello {person}");
+
+// captures named arguments `species` and `name`
+format!("The {species}'s name is {name}.");
+```
+
+This also works for formatting parameters such as width and precision:
+
+```rust
+#![feature(format_args_capture)]
+
+let precision = 2;
+let s = format!("{:.precision$}", 1.324223);
+
+assert_eq!(&s, "1.32");
+```
+
+A non-exhaustive list of macros which benefit from this functionality include:
+- `format!`
+- `print!` and `println!`
+- `eprint!` and `eprintln!`
+- `write!` and `writeln!`
+- `panic!`
+- `unreachable!`
+- `unimplemented!`
+- `todo!`
+- `assert!` and similar
+- macros in many thirdparty crates, such as `log`