Implementing structs
You can also implement structs
, which means you can attach functions to instances of structs
(Very similar to classes in TS)
Passing in &self as the first argument to a function
struct Rect { width: u32, height: u32,}
impl Rect { fn area(&self) -> u32 { self.width * self.height }}
fn main() { let rect = Rect { width: 30, height: 50, }; print!("The area of the rectangle is {}", rect.area());}
Not passing in &self as the first argument
struct Rect { width: u32, height: u32, }
impl Rect { fn print_str() { println!("Inside the rect struct"); } }
fn main() { Rect::print_str(); }