From 90f6fdad977634f12d2744982794d59c551f0ff5 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 11 Sep 2021 20:00:34 -0400 Subject: [PATCH] RBE custom types structs activity 2 --- rustbyexample/custom_types/custom_types_structs.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rustbyexample/custom_types/custom_types_structs.rs b/rustbyexample/custom_types/custom_types_structs.rs index 2dc992f..090ff00 100644 --- a/rustbyexample/custom_types/custom_types_structs.rs +++ b/rustbyexample/custom_types/custom_types_structs.rs @@ -24,6 +24,13 @@ fn rect_area(r: Rectangle) -> f32 { (r.top_left.x - r.bottom_right.x) * (r.top_left.y - r.bottom_right.y) } +fn square(p: &Point, d: f32) -> Rectangle { + Rectangle { + top_left: Point { x: p.x + d, y: p.y }, + bottom_right: Point { x: p.x, y: p.y + d }, + } +} + fn main() { let name = String::from("Peter"); let age = 27; @@ -64,4 +71,9 @@ fn main() { println!("rect in {:?}", rect); println!("area of rect is {}", rect_area(rect)); + + let lower_left = Point { x: 4.1, y: 11.3 }; + let d = 7.7f32; + let sq = square(&lower_left, d); + println!("sq from point={:?} of d={} is {:?}", lower_left, d, sq); }