Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pretty-print inlined variables when formatting strings

Normally one can print strings the following way: println!("{:#?}", foo) where the {:#?} syntax will make a pretty-print of the value. But I know it’s also possible to inline the variable directly in the string between the curly braces, instead of listing it as a second argument to the macro, like so: println!("{foo}").

My question is – can I combine the pretty-print syntax and inlining the variable in the string?

I found out about the shorthand syntax from clippy’s docs, but I couldn’t find (or understand) how to combine it with pretty-print (if it’s at all possible).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Simply place the variable name before the colon:

fn main() {
    let foo = 3;
    println!("{foo:#?}");
}

Note:

  • :#? is pretty-printed Debug output
  • :? is normal Debug output
  • no modifier is Display output

Display is for user-facing output

Debug is for output when debugging, also used for panic messages

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading