23 ms·
{:#x?} omg!
by ohazi 6y ago
{:#x?}
omg!
- deleted 6y ago[deleted]
- kibwen 6y agoIt's a formatting sublanguage based on the one from C#. In the formatting sublanguage, braces denote a value to be formatted: `format!("{foo}", foo=42)` Instead of a name you can identify values positionally, counting from zero: `format!("{0}", 42)` You can omit the identifier entirely and the arguments will likewise be interpreted as positional: `format!("{}", 42)` To tell the formatter the manner in which you would like to format the value, you can give format specifiers. These are separated from the value's identifier by a colon. An example of a format specifier is `b`, which prints the value in binary: `format!("{foo:b}", foo=42)` As before, you can leave out the name to refer to the value positionally: `format!("{:b}", 42)` Other format specifiers are `?`, which tells the formatter to print the "debug" output (i.e. autogenerated output useful for debugging by programmers), `#` which tells the formatter to insert line breaks and indentation for easier reading, and `x` which tells the formatter to print numbers as hex.
- masklinn 6y ago> autogenerated output useful for debugging by programmers Nit: not necessarily autogenerated, we can implement Debug by hand. It's just that, as the name indicates, this is aimed at debugging output rather than the possibly-user-facing output of Display.
- kibwen 6y agoIndeed, though it's worth driving home that the salient difference between Debug and Display is that the former, unlike the latter, can be autogenerated via derive (well, okay, I've seen crates that add the ability to derive Display based on doc comments, which is pretty cool: https://github.com/yaahc/displaydoc https://github.com/yaahc/displaydoc ).
- jabedude 6y agoRight! I've been using Rust for ~2 years now and just found out about the pretty print format.