What does the dollar syntax in a Rust format string mean?

I am looking at Rust by example, and using $ (dollar sign) in this example is not clear to me:

 // You can right-align text with a specified width. This will output // " 1". 5 white spaces and a "1". println!("{number:>width$}", number=1, width=6); // You can pad numbers with extra zeroes. This will output "000001". println!("{number:>0width$}", number=1, width=6); 

I found this in the documentation for std::fmt , but it doesn’t explain anything to me:

 format_string := <text> [ maybe-format <text> ] * maybe-format := '{' '{' | '}' '}' | <format> format := '{' [ argument ] [ ':' format_spec ] '}' argument := integer | identifier format_spec := [[fill]align][sign]['#']['0'][width]['.' precision][type] fill := character align := '<' | '^' | '>' sign := '+' | '-' width := count precision := count | '*' type := identifier | '' count := parameter | integer parameter := argument '$' 

I bite a bit in the code, I found that it does not compile without a dollar sign, but " width " can be replaced with any arbitrary identifier. that is, the following is equivalent to the third line in the first code block:

 println!("{number:>test$}", number=1, test=6); 
+7
rust string-formatting
source share
1 answer

This allows for the width or precision of another formatted element as an argument, rather than hard-coded as part of a format string. The parameter can be specified using a numerical index or a name.

The documentation says :

The width value can also be specified as [ usize ] in the parameters list using dollar syntax indicating that the second argument is [ usize ] with the width, for example:

 // All of these print "Hello x !" println!("Hello {:5}!", "x"); println!("Hello {:1$}!", "x", 5); println!("Hello {1:0$}!", 5, "x"); println!("Hello {:width$}!", "x", width = 5); 

A reference to an argument with dollar syntax does not affect the “next Argument” counter “, so a position is usually a good idea, or use named arguments.

also says :

There are three possible ways to specify the desired precision :

[...]

  1. An integer or name followed by a dollar sign .N$ :

    for precision, use the N format argument (which should be usize ).

[...]

For example, all of the following calls print the same Hello x is 0.01000 :

 // Hello {arg 0 ("x")} is {arg 1 (0.01) with precision specified inline (5)} println!("Hello {0} is {1:.5}", "x", 0.01); // Hello {arg 1 ("x")} is {arg 2 (0.01) with precision specified in arg 0 (5)} println!("Hello {1} is {2:.0$}", 5, "x", 0.01); // Hello {arg 0 ("x")} is {arg 2 (0.01) with precision specified in arg 1 (5)} println!("Hello {0} is {2:.1$}", "x", 5, 0.01); // Hello {next arg ("x")} is {second of next two args (0.01) with precision // specified in first of next two args (5)} println!("Hello {} is {:.*}", "x", 5, 0.01); // Hello {next arg ("x")} is {arg 2 (0.01) with precision // specified in its predecessor (5)} println!("Hello {} is {2:.*}", "x", 5, 0.01); // Hello {next arg ("x")} is {arg "number" (0.01) with precision specified // in arg "prec" (5)} println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01); 
+6
source share

All Articles