User Tools

Site Tools


rust

This is an old revision of the document!


Rust

Debug attribute and println!

#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}
 
fn main() {
    let rect1 = Rectangle {
        width: 30,
        height: 50,
    };
 
    println!("rect1 is {:?}", rect1);
 
    // Or with pretty print :#?
    println!("rect1 is {:#?}", rect1);
}

Using dbg! macro in struct assignment can be done as dbg! returns ownership of the expression value.

#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}
 
fn main() {
    let scale = 2;
    let rect1 = Rectangle {
        width: dbg!(30 * scale),
        height: 50,
    };
 
    dbg!(&rect1);
}
rust.1720639006.txt.gz · Last modified: 2024/07/10 19:16 by utedass

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki