Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does {:?} mean in a Rust format string?

Tags:

I found out that {:?} prints an entire array in Rust. I want to know what is it called and how exactly it works. Is it only limited to printing arrays or could it also be used elsewhere for other purposes?

like image 207
7_R3X Avatar asked Jul 02 '16 06:07

7_R3X


People also ask

What does string formatting mean?

String formatting uses a process of string interpolation (variable substitution) to evaluate a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

What is format macro in Rust?

Macro std::formatCreates a String using interpolation of runtime expressions. The first argument format! receives is a format string. This must be a string literal. The power of the formatting string is in the {} s contained.

Does Rust have string interpolation?

Short answer: no, not that type of interpolation. There is a Rust RFC for this.

What is Rust How is it format?

Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH), Fe(OH)3), and is typically associated with the corrosion of refined iron.


1 Answers

This is explained (along with the rest of the formatting syntax) in the std::fmt documentation.

{...} surrounds all formatting directives. : separates the name or ordinal of the thing being formatted (which in this case is omitted, and thus means "the next thing") from the formatting options. The ? is a formatting option that triggers the use of the std::fmt::Debug implementation of the thing being formatted, as opposed to the default Display trait, or one of the other traits (like UpperHex or Octal).

Thus, {:?} formats the "next" value passed to a formatting macro, and supports anything that implements Debug.

like image 195
DK. Avatar answered Oct 07 '22 05:10

DK.