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 display struct with new println! format in Rust?

In Rust 1.58, println!("{x}"); is supported (Captured identifiers in format strings), but I cannot print the struct because I don’t specify {:?}. Are there any ways to display struct with the new println!?

#[derive(Debug)]
struct Structure {
    name: String,
    version: u32
}

fn main() {
    let structure = Structure { name: "name".to_string(), version: 1 };
    println!("{:?}", structure); // working
    println!("{structure}");     // not working
}

>Solution :

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

You can add the :? debug modifier as part of the formatting as well:

println!("{structure:?}");

Playground

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