diff --git a/rustbyexample/031/src/main.rs b/rustbyexample/031/src/main.rs index 4515204..324e925 100644 --- a/rustbyexample/031/src/main.rs +++ b/rustbyexample/031/src/main.rs @@ -2,17 +2,26 @@ struct Nil; struct Pair(i32, f32); +#[derive(Debug)] struct Point { x: f32, y: f32, } -#[allow(dead_code)] +#[derive(Debug)] struct Rectangle { p1: Point, p2: Point, } +fn rect_area(rect: Rectangle) -> f32 { + let Rectangle { + p1: Point { x: x1, y: y1 }, + p2: Point { x: x2, y: y2 } + } = rect; + ((x1 - x2) * (y1 - y2)).abs() +} + fn main() { let point: Point = Point { x: 0.3, y: 0.4 }; @@ -34,4 +43,7 @@ fn main() { let Pair(integer, decimal) = pair; println!("pair contains {:?} and {:?}", integer, decimal); + + println!("rect is {:?}", _rectangle); + println!("rect area is {}", rect_area(_rectangle)); }