rust
This is an old revision of the document!
Table of Contents
Rust
Struct methods
struct Rectangle { width: u32, height: u32, } impl Rectangle { fn area(&self) -> u32 { self.width * self.height } }
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.1720642361.txt.gz · Last modified: 2024/07/10 20:12 by utedass
