Debug formatting using :?, :#?, and dbg!

:? :#? dbg! format! println! Debug

If we would like to print the content of a variable we can use the print! or println! macros, or, sepecially if we need it for debugging purposes we can use the dbg! macro. We could also use ther format! macro if we only wanted to format the string without immediately printing it to the screen.

  • The placeholder {} can be used for any primitive types, such as string, integer, floating point, boolean.
  • The placeholder {:?} can be used for both orimitive types and complex types such as tuple, vector, or struct. In order to be able to use it for a structwe need to add theDebug` trait to the struct.
  • the placeholder {:#?} can be used to pretty-print the content of any variable.
  • dbg! will print the valu in the same way as {:#?} does, but it will also include the name of the variable, the filename and the row-number. It will print to STDERR as would eprint! and eprintln! do.

See the example code:

examples/format-debug/src/main.rs

#[derive(Debug)]
#[allow(unused)]
struct Person {
    name: String,
    birthyear: u16,
    addresses: Vec<String>,
}

fn main() {
    let text = "Hello, world!";
    let integer = 42;
    let boolean = true;
    let tuple = ("text", 42);
    let vector = vec!["Aunt Em", "Uncle Henry", "Betsy Bobbin"];

    let person = Person {
        name: String::from("Mary Jane"),
        birthyear: 1997,
        addresses: vec![String::from("home"), String::from("work")],
    };

    println!("{} {} {}", text, integer, boolean);
    println!("{:?} {:?} {:?}", text, integer, boolean);
    println!("{:#?} {:#?} {:#?}", text, integer, boolean);
    println!();

    println!("{:?}", tuple);
    println!("{:?}", vector);
    println!("{:?}", person);
    println!();

    println!("{:#?}", tuple);
    println!("{:#?}", vector);
    println!("{:#?}", person);
    println!();

    dbg!(text);
    dbg!(integer);
    dbg!(boolean);
    dbg!(tuple);
    dbg!(vector);
    dbg!(person);
}

See the result from the example code:

Hello, world! 42 true
"Hello, world!" 42 true
"Hello, world!" 42 true

("text", 42)
["Aunt Em", "Uncle Henry", "Betsy Bobbin"]
Person { name: "Mary Jane", birthyear: 1997, addresses: ["home", "work"] }

(
    "text",
    42,
)
[
    "Aunt Em",
    "Uncle Henry",
    "Betsy Bobbin",
]
Person {
    name: "Mary Jane",
    birthyear: 1997,
    addresses: [
        "home",
        "work",
    ],
}

[src/main.rs:37:5] text = "Hello, world!"
[src/main.rs:38:5] integer = 42
[src/main.rs:39:5] boolean = true
[src/main.rs:40:5] tuple = (
    "text",
    42,
)
[src/main.rs:41:5] vector = [
    "Aunt Em",
    "Uncle Henry",
    "Betsy Bobbin",
]
[src/main.rs:42:5] person = Person {
    name: "Mary Jane",
    birthyear: 1997,
    addresses: [
        "home",
        "work",
    ],
}

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo