Skip to content

Commit f30058f

Browse files
committed
Rollup merge of #33258 - birkenfeld:fmt-dollar-syntax, r=brson
Clarify std::fmt width docs w.r.t. dollar syntax and give example. The previous version only said "the `2$` syntax", which while introduced in the grammar is not very self-explanatory.
2 parents 3a0efff + 815987b commit f30058f

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/libcollections/fmt.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,19 @@
395395
//! `0`.
396396
//!
397397
//! The value for the width can also be provided as a `usize` in the list of
398-
//! parameters by using the `2$` syntax indicating that the second argument is a
399-
//! `usize` specifying the width.
398+
//! parameters by using the dollar syntax indicating that the second argument is
399+
//! a `usize` specifying the width, for example:
400+
//!
401+
//! ```
402+
//! // All of these print "Hello x !"
403+
//! println!("Hello {:5}!", "x");
404+
//! println!("Hello {:1$}!", "x", 5);
405+
//! println!("Hello {1:0$}!", 5, "x");
406+
//! ```
407+
//!
408+
//! Referring to an argument with the dollar syntax does not affect the "next
409+
//! argument" counter, so it's usually a good idea to refer to all arguments by
410+
//! their position explicitly.
400411
//!
401412
//! ## Precision
402413
//!

0 commit comments

Comments
 (0)