From 7e8ee5ed9cad6484e9f13f81731b102ced58402e Mon Sep 17 00:00:00 2001 From: Adam Carpenter Date: Tue, 9 Jul 2019 15:14:04 -0400 Subject: Init. --- rust-book/structs/src/main.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 rust-book/structs/src/main.rs (limited to 'rust-book/structs/src/main.rs') diff --git a/rust-book/structs/src/main.rs b/rust-book/structs/src/main.rs new file mode 100755 index 0000000..cf93866 --- /dev/null +++ b/rust-book/structs/src/main.rs @@ -0,0 +1,32 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +impl Rectangle { + + fn area(&self) -> u32 { + self.width * self.height + } + + fn can_hold(&self, other: &Rectangle ) -> bool { + self.width > other.width && self.height > other.height + } + +} + +fn main() { + let rect1 = Rectangle { + width: 30, + height: 50, + }; + let rect2 = Rectangle { + width: 3, + height: 5, + }; + println!("{:#?}", rect1); + println!("The area of the rectangle is {} square pixels.", rect1.area()); + println!("{}", rect1.can_hold(&rect2)); +} + -- cgit v1.2.3