Skip to content

Commit af05b56

Browse files
committedMay 3, 2016
Rollup merge of rust-lang#33277 - birkenfeld:fmt-named-dollar-args, r=steveklabnik
Fix std::fmt format spec: named args are allowed with "$" syntax
2 parents 3157691 + 84bd1ce commit af05b56

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed
 

‎src/libcollections/fmt.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@
333333
//! precision := count | '*'
334334
//! type := identifier | ''
335335
//! count := parameter | integer
336-
//! parameter := integer '$'
336+
//! parameter := argument '$'
337337
//! ```
338338
//!
339339
//! # Formatting Parameters
@@ -403,11 +403,12 @@
403403
//! println!("Hello {:5}!", "x");
404404
//! println!("Hello {:1$}!", "x", 5);
405405
//! println!("Hello {1:0$}!", 5, "x");
406+
//! println!("Hello {:width$}!", "x", width = 5);
406407
//! ```
407408
//!
408409
//! 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.
410+
//! argument" counter, so it's usually a good idea to refer to arguments by
411+
//! position, or use named arguments.
411412
//!
412413
//! ## Precision
413414
//!
@@ -426,7 +427,7 @@
426427
//!
427428
//! the integer `N` itself is the precision.
428429
//!
429-
//! 2. An integer followed by dollar sign `.N$`:
430+
//! 2. An integer or name followed by dollar sign `.N$`:
430431
//!
431432
//! use format *argument* `N` (which must be a `usize`) as the precision.
432433
//!
@@ -456,6 +457,10 @@
456457
//! // Hello {next arg (x)} is {arg 2 (0.01) with precision
457458
//! // specified in its predecessor (5)}
458459
//! println!("Hello {} is {2:.*}", "x", 5, 0.01);
460+
//!
461+
//! // Hello {next arg (x)} is {arg "number" (0.01) with precision specified
462+
//! // in arg "prec" (5)}
463+
//! println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
459464
//! ```
460465
//!
461466
//! All print the same thing:

0 commit comments

Comments
 (0)