diff --git a/rustbyexample/custom_types/custom_types_structs.rs b/rustbyexample/custom_types/custom_types_structs.rs index 455f740..2dc992f 100644 --- a/rustbyexample/custom_types/custom_types_structs.rs +++ b/rustbyexample/custom_types/custom_types_structs.rs @@ -8,17 +8,22 @@ struct Unit; struct Pair(i32, f32); +#[derive(Debug)] struct Point { x: f32, y: f32, } -#[allow(dead_code)] +#[derive(Debug)] struct Rectangle { top_left: Point, bottom_right: Point, } +fn rect_area(r: Rectangle) -> f32 { + (r.top_left.x - r.bottom_right.x) * (r.top_left.y - r.bottom_right.y) +} + fn main() { let name = String::from("Peter"); let age = 27; @@ -30,7 +35,7 @@ fn main() { println!("point coordinates: ({}, {})", point.x, point.y); - let bottom_right = Point { x: 5.2, ..point }; + let bottom_right = Point { x: 5.2, y: 0.2 }; println!("second point: ({}, {})", bottom_right.x, bottom_right.y); @@ -39,7 +44,7 @@ fn main() { y: top_edge, } = point; - let _rectangle = Rectangle { + let rect = Rectangle { top_left: Point { x: left_edge, y: top_edge, @@ -56,4 +61,7 @@ fn main() { let Pair(integer, decimal) = pair; println!("pair contains {:?} and {:?}", integer, decimal); + + println!("rect in {:?}", rect); + println!("area of rect is {}", rect_area(rect)); }