about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEvan Jones <ej@evanjones.ca>2022-12-17 13:43:08 -0500
committerEvan Jones <ej@evanjones.ca>2022-12-17 13:43:08 -0500
commitab2151cbf8270be61db8add6bfb1aea03230803f (patch)
treeaf54afd2be12f3a02e71ad7a8783ecfe8221c143
parent65c53c3bb6190319e210c94164b05a17997073f2 (diff)
downloadrust-ab2151cbf8270be61db8add6bfb1aea03230803f.tar.gz
rust-ab2151cbf8270be61db8add6bfb1aea03230803f.zip
std::fmt: Use args directly in example code
The lint "clippy::uninlined_format_args" recommends inline
variables in format strings. Fix two places in the docs that do
not do this. I noticed this because I copy/pasted one example in
to my project, then noticed this lint error. This fixes:

error: variables can be used directly in the `format!` string
  --> src/main.rs:30:22
   |
30 |         let string = format!("{:.*}", decimals, magnitude);
   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: variables can be used directly in the `format!` string
  --> src/main.rs:39:2
   |
39 |  write!(&mut io::stdout(), "{}", args).unwrap();
-rw-r--r--library/alloc/src/fmt.rs4
1 files changed, 2 insertions, 2 deletions
diff --git a/library/alloc/src/fmt.rs b/library/alloc/src/fmt.rs
index 799ce9d5daa..eadb35cb96d 100644
--- a/library/alloc/src/fmt.rs
+++ b/library/alloc/src/fmt.rs
@@ -419,7 +419,7 @@
 //!         // documentation for details, and the function `pad` can be used
 //!         // to pad strings.
 //!         let decimals = f.precision().unwrap_or(3);
-//!         let string = format!("{:.*}", decimals, magnitude);
+//!         let string = format!("{magnitude:.decimals$}");
 //!         f.pad_integral(true, "", &string)
 //!     }
 //! }
@@ -518,7 +518,7 @@
 //! write!(&mut some_writer, "{}", format_args!("print with a {}", "macro"));
 //!
 //! fn my_fmt_fn(args: fmt::Arguments) {
-//!     write!(&mut io::stdout(), "{}", args);
+//!     write!(&mut io::stdout(), "{args}");
 //! }
 //! my_fmt_fn(format_args!(", or a {} too", "function"));
 //! ```