From e9f349d55c40b88daaaa53294d4455d93c825ced Mon Sep 17 00:00:00 2001 From: laurens Date: Fri, 22 May 2020 13:57:02 +0200 Subject: [PATCH] rectangles with structs --- rectangles/src/main.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/rectangles/src/main.rs b/rectangles/src/main.rs index b6b4fe1..9a1568c 100644 --- a/rectangles/src/main.rs +++ b/rectangles/src/main.rs @@ -1,3 +1,9 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + fn main() { let width1 = 30; let height1 = 50; @@ -13,6 +19,16 @@ fn main() { "The area of the rectangle is {} square pixels.", area_tuple(rect) ); + + let rect = Rectangle { + width: 30, + height: 50, + }; + + println!( + "The area of the rectangle is {} square pixels.", + area_struct(&rect) + ); } fn area(width: u32, height: u32) -> u32 { @@ -22,3 +38,7 @@ fn area(width: u32, height: u32) -> u32 { fn area_tuple(dimensions: (u32, u32)) -> u32 { dimensions.0 * dimensions.1 } + +fn area_struct(rect: &Rectangle) -> u32 { + rect.width * rect.height +}