What does `$e` mean in the invocation of macro format?

Occasionally, I found the code that wrote

format!("{:Precision$e}",10.01, Precision = 10);

The Precision specifies the valid number of the fraction part of 10.01. However, what does the symbol $e mean in this macro invocation, the symbol $e must immediately follow the variable Precision, and if without $e, it won't be compiled. I haven't yet seen this usage in the common code until now. I'm curious about what the symbol means and its effect in this macro.

This is the formatter telling Rust that 10.01 should be displayed with a precision (digits after decimal point) of Precision which is 10 in your case (assigned in your argument). See here.

This is the formatter that tells Rust to format your number 10.01 in scientific notation with the LowerExp trait (see here).


I'm quite sure that Precision$ has no effect on your number when you use it together with the e formatter, so I guess

format!("{:e}", 10.01);

is equivalent to your version.

1 Like

Thank. IIUC, we cannot place any whitespace between formatting parameters, right?

format!("{: .1 e}", 10.01); // this will be ill-formed.

According to the documented syntax, whitespace within the formatting {}s is permitted almost nowhere, except for one place that indicates some optional whitespace, “[ ws ] *” in the grammar. Here's a link to the relevant section std::fmt - Rust

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.