From af11c29d9c45260613ca8289af1331d8c296c04c Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 5 Sep 2016 22:53:07 -0400 Subject: [PATCH] Impl `rect_area` for 3.1 --- rustbyexample/031/src/main.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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)); }