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

江枫思渺然 提交于 2021-02-17 21:34:05

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/38157335/what-does-mean-in-a-rust-format-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!