update
This commit is contained in:
1
Rectangle/rust/.gitignore
vendored
Normal file
1
Rectangle/rust/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
target/
|
||||
7
Rectangle/rust/Cargo.lock
generated
Normal file
7
Rectangle/rust/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
6
Rectangle/rust/Cargo.toml
Normal file
6
Rectangle/rust/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
0
Rectangle/rust/README.md
Normal file
0
Rectangle/rust/README.md
Normal file
116
Rectangle/rust/src/main.rs
Normal file
116
Rectangle/rust/src/main.rs
Normal file
@@ -0,0 +1,116 @@
|
||||
// Problem 4: Rectangle with Methods
|
||||
// Concepts: Structs, methods, associated functions
|
||||
|
||||
// Define Rectangle struct with width and height. Implement:
|
||||
|
||||
// area(&self) -> f64
|
||||
|
||||
// can_hold(&self, other: &Rectangle) -> bool
|
||||
|
||||
// square(size: f64) -> Rectangle (associated function, a.k.a "static method")
|
||||
|
||||
/* --------------------------------------------- 100 -------------------------------------------- */
|
||||
|
||||
struct Rectangle {
|
||||
width:u64,
|
||||
hight:u64,
|
||||
}
|
||||
|
||||
|
||||
impl Rectangle {
|
||||
fn square_rectangle(l:u64) -> Rectangle {
|
||||
return Rectangle { width: l, hight: l };
|
||||
}
|
||||
|
||||
fn can_hold(& self, rect_insert:& Rectangle) -> bool {
|
||||
let mut width_holdable = false;
|
||||
let mut hight_holdable = false;
|
||||
if self.width > rect_insert.width || self.width > rect_insert.hight {
|
||||
width_holdable = true;
|
||||
}
|
||||
if self.hight > rect_insert.width || self.hight > rect_insert.hight {
|
||||
hight_holdable = true;
|
||||
}
|
||||
|
||||
let mut hold_result = false;
|
||||
if width_holdable == true && hight_holdable == true {
|
||||
hold_result = true;
|
||||
}
|
||||
|
||||
|
||||
return hold_result;
|
||||
}
|
||||
|
||||
fn area(& self) -> u64 {
|
||||
return self.hight * self.width
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
fn main() {
|
||||
let rec1 = Rectangle::square_rectangle(20);
|
||||
let h = rec1.hight;
|
||||
let w = rec1.width;
|
||||
println!("{w} {h}");
|
||||
|
||||
let rec2 = Rectangle{width:6, hight:10};
|
||||
let holdable = rec1.can_hold(&rec2);
|
||||
|
||||
println!("{holdable}");
|
||||
|
||||
let rec2area = rec2.area();
|
||||
println!("rec2 area {rec2area}");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user