This commit is contained in:
2026-04-29 12:02:30 +07:00
parent f64b473e2d
commit 21056bc624
6 changed files with 246 additions and 0 deletions

1
Rectangle/rust/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
target/

7
Rectangle/rust/Cargo.lock generated Normal file
View 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"

View File

@@ -0,0 +1,6 @@
[package]
name = "rust"
version = "0.1.0"
edition = "2024"
[dependencies]

0
Rectangle/rust/README.md Normal file
View File

116
Rectangle/rust/src/main.rs Normal file
View 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}");
}