Snippets Collections
// for compile error
#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
    sqr_rec: () => {}
}

fn main() {
    let rect1 = Rectangle {
        width: 30,
        height: 50,
    };
    // ? tells compiler to output to console
    println!("rect1 is {:#?}", rect1);
      
    println!(
        "The area of the rectangle is {} square pixels.",
        area(&rect1)
    );
}


fn area(rectangle: &Rectangle) -> u32 {
    rectangle.width * rectangle.height
}
  
// 'Class' Method and Constructor (associated method (doesn't have self as fitst arg))
// Self = Struct Name ( Rectangle in this case)
impl Rectangle {
  fn new() -> Self {
    Self {
      width: 100,
      height: 100,
      fn sqr_rec(&self)
      fn sqr_rec(mut &self)
    }
  }
}
  
let rec1 = Rectangle::new();
let width = rec1.width;
let height = rect.height;
star

Wed Jul 20 2022 22:37:05 GMT+0000 (Coordinated Universal Time) https://doc.rust-lang.org/book/ch05-02-example-structs.html

#println #customtypes #debug

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension