Migration from GitHub
This commit is contained in:
5
chapter_05/rectangles/Cargo.lock
generated
Normal file
5
chapter_05/rectangles/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "rectangles"
|
||||
version = "0.1.0"
|
||||
9
chapter_05/rectangles/Cargo.toml
Normal file
9
chapter_05/rectangles/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "rectangles"
|
||||
version = "0.1.0"
|
||||
authors = ["rarmknecht <rarmknecht@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
45
chapter_05/rectangles/src/main.rs
Normal file
45
chapter_05/rectangles/src/main.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
#[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 square(size: u32) -> Rectangle {
|
||||
Rectangle {
|
||||
width: size,
|
||||
height: size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let rect1 = Rectangle {
|
||||
width: 30,
|
||||
height: 50,
|
||||
};
|
||||
|
||||
println!(
|
||||
"The area of the rectangle is {} square pixels.",
|
||||
rect1.area()
|
||||
);
|
||||
|
||||
println!(
|
||||
"This rectangle fits inside: {}",
|
||||
rect1.can_hold(&Rectangle {width: 20, height: 30,})
|
||||
);
|
||||
|
||||
println!(
|
||||
"This is a square! {:?}",
|
||||
Rectangle::square(32)
|
||||
);
|
||||
|
||||
}
|
||||
16
chapter_05/rectangles/src/main.rs.basic
Normal file
16
chapter_05/rectangles/src/main.rs.basic
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
fn main() {
|
||||
|
||||
let widthl = 30;
|
||||
let heightl = 50;
|
||||
|
||||
println!(
|
||||
"The area of the rectangle is {} square pixels",
|
||||
area(widthl, heightl)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
fn area(width: u32, height: u32) -> u32 {
|
||||
width * height
|
||||
}
|
||||
20
chapter_05/rectangles/src/main.rs.struct
Normal file
20
chapter_05/rectangles/src/main.rs.struct
Normal file
@@ -0,0 +1,20 @@
|
||||
struct Rectangle {
|
||||
width: u32,
|
||||
height: u32,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let rect1 = Rectangle {
|
||||
width: 30,
|
||||
height: 50,
|
||||
};
|
||||
|
||||
println!(
|
||||
"The area of the rectangle is {} square pixels.",
|
||||
area(&rect1)
|
||||
);
|
||||
}
|
||||
|
||||
fn area(rectangle: &Rectangle) -> u32 {
|
||||
rectangle.width * rectangle.height
|
||||
}
|
||||
Reference in New Issue
Block a user