rectangles with structs

This commit is contained in:
laurens 2020-05-22 13:57:02 +02:00
parent 29f0bb891a
commit e9f349d55c

View file

@ -1,3 +1,9 @@
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
fn main() { fn main() {
let width1 = 30; let width1 = 30;
let height1 = 50; let height1 = 50;
@ -13,6 +19,16 @@ fn main() {
"The area of the rectangle is {} square pixels.", "The area of the rectangle is {} square pixels.",
area_tuple(rect) 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 { 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 { fn area_tuple(dimensions: (u32, u32)) -> u32 {
dimensions.0 * dimensions.1 dimensions.0 * dimensions.1
} }
fn area_struct(rect: &Rectangle) -> u32 {
rect.width * rect.height
}