about summary refs log tree commit diff
path: root/src/doc
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2020-07-03 17:16:52 -0700
committerGitHub <noreply@github.com>2020-07-03 17:16:52 -0700
commit4a8d9ea80ff8d7ec4dcc6da7d36fe60ecaa55539 (patch)
tree95dcbc65f6c216af2314d646227859c0573d4522 /src/doc
parentdf8f551e796e3dd86a1ea8fb23b96cefbc7cdf85 (diff)
parent93d662fd9d0e39145ac45fc08f35a619e0cb3f8c (diff)
downloadrust-4a8d9ea80ff8d7ec4dcc6da7d36fe60ecaa55539.tar.gz
rust-4a8d9ea80ff8d7ec4dcc6da7d36fe60ecaa55539.zip
Rollup merge of #73670 - davidhewitt:format-args-capture, r=varkor
Add `format_args_capture` feature

This is the initial implementation PR for [RFC 2795](https://github.com/rust-lang/rfcs/pull/2795).

Note that, as dicussed in the tracking issue (#67984), the feature gate has been called `format_args_capture`.

Next up I guess I need to add documentation for this feature. I've not written any docs before for rustc / std so I would appreciate suggestions on where I should add docs.
Diffstat (limited to 'src/doc')
-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`