Showing
2 changed files
with
99 additions
and
0 deletions
rectangles/Cargo.toml
0 → 100644
rectangles/src/main.rs
0 → 100644
| 1 | +// | ||
| 2 | +// Examples related to structs… | ||
| 3 | +// | ||
| 4 | +// Georg Hopp <georg@steffers.org> | ||
| 5 | +// | ||
| 6 | +// Copyright © 2019 Georg Hopp | ||
| 7 | +// | ||
| 8 | +// This program is free software: you can redistribute it and/or modify | ||
| 9 | +// it under the terms of the GNU General Public License as published by | ||
| 10 | +// the Free Software Foundation, either version 3 of the License, or | ||
| 11 | +// (at your option) any later version. | ||
| 12 | +// | ||
| 13 | +// This program is distributed in the hope that it will be useful, | ||
| 14 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | +// GNU General Public License for more details. | ||
| 17 | +// | ||
| 18 | +// You should have received a copy of the GNU General Public License | ||
| 19 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 20 | +// | ||
| 21 | + | ||
| 22 | +#[derive(Debug)] | ||
| 23 | +struct Rectangle { | ||
| 24 | + width: u32, | ||
| 25 | + height: u32, | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +impl Rectangle { | ||
| 29 | + fn area(&self) -> u32 { | ||
| 30 | + self.width * self.height | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + fn can_hold(&self, inner: &Rectangle) -> bool { | ||
| 34 | + self.width >= inner.width && self.height >= inner.height | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + fn square(size: u32) -> Rectangle { | ||
| 38 | + Rectangle { width: size, height: size } | ||
| 39 | + } | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +fn main() { | ||
| 43 | + let width1 = 30; | ||
| 44 | + let height1 = 50; | ||
| 45 | + | ||
| 46 | + println!( | ||
| 47 | + "The area of the rectangle ist {} square pixels.", | ||
| 48 | + area(width1, height1)); | ||
| 49 | + | ||
| 50 | + let rect1 = (30, 50); | ||
| 51 | + | ||
| 52 | + println!( | ||
| 53 | + "The area of the rectangle ist {} square pixels.", | ||
| 54 | + _area(rect1)); | ||
| 55 | + | ||
| 56 | + let rect1 = Rectangle { width: 30, height: 50 }; | ||
| 57 | + | ||
| 58 | + println!( | ||
| 59 | + "The area of the rectangle ist {} square pixels.", | ||
| 60 | + __area(&rect1)); | ||
| 61 | + | ||
| 62 | + println!("_rect1 is {:?}", rect1); | ||
| 63 | + | ||
| 64 | + println!( | ||
| 65 | + "The area of the rectangle ist {} square pixels.", | ||
| 66 | + rect1.area()); | ||
| 67 | + | ||
| 68 | + let rect2 = Rectangle { width: 10, height: 40}; | ||
| 69 | + let rect3 = Rectangle { width: 60, height: 45}; | ||
| 70 | + | ||
| 71 | + println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2)); | ||
| 72 | + println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3)); | ||
| 73 | + | ||
| 74 | + let square1 = Rectangle::square(32); | ||
| 75 | + | ||
| 76 | + println!("Got the square rectange: {:?}", square1); | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +fn area(width: u32, height: u32) -> u32 { | ||
| 80 | + width * height | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +fn _area(dimensions: (u32, u32)) -> u32 { | ||
| 84 | + let (width, height) = dimensions; | ||
| 85 | + width * height | ||
| 86 | +} | ||
| 87 | + | ||
| 88 | +fn __area(rectangle: &Rectangle) -> u32 { | ||
| 89 | + rectangle.width * rectangle.height | ||
| 90 | +} |
Please
register
or
login
to post a comment