Struct

PHOTO EMBED

Wed Jul 20 2022 22:37:05 GMT+0000 (Coordinated Universal Time)

Saved by @Polly #println #customtypes #debug

// 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;
content_copyCOPY

Similar to a Class - can have properties and functions/methods see more Attributes for Custom types: [doc](https://doc.rust-lang.org/reference/attributes.html)
https://doc.rust-lang.org/book/ch05-02-example-structs.html