// Structs group named fields. `impl` adds methods and associated functions. struct Rect { w: f64, h: f64, } impl Rect { fn new(w: f64, h: f64) -> Self { Rect { w, h } } fn square(side: f64) -> Self { Rect { w: side, h: side } } fn area(&self) -> f64 { self.w * self.h } fn fits_in(&self, other: &Rect) -> bool { self.w <= other.w && self.h <= other.h } fn scale(&mut self, k: f64) { self.w *= k self.h *= k } } fn main() { let mut r = Rect::new(3.0, 4.0) print(r) print(r.area()) r.scale(2.0) print(r) print(Rect::square(5.0).fits_in(&r)) r.h = 1.0 print("now {r.w} by {r.h}") }