Skip to content

Commit 6813ee2

Browse files
rust basic [7]
1 parent 614502b commit 6813ee2

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

src/_20_yo_struct.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
struct YoUser {
2+
username: String,
3+
age: u8,
4+
active: bool,
5+
}
6+
7+
pub fn main() {
8+
let user = YoUser {
9+
username: String::from("yo_rustacean"),
10+
age: 28,
11+
active: true,
12+
};
13+
14+
println!(
15+
"User : {}, Age : {}, Active : {}",
16+
user.username, user.age, user.active
17+
); // expected : user details printed
18+
}

src/_21_yo_method.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
struct YoRectangle {
2+
width: u32,
3+
height: u32,
4+
}
5+
6+
impl YoRectangle {
7+
fn area(&self) -> u32 {
8+
self.width * self.height // calculate the area
9+
}
10+
}
11+
12+
pub fn main() {
13+
let rect = YoRectangle {
14+
width: 10,
15+
height: 5,
16+
};
17+
println!("Rectangle area: {}", rect.area()) // expected: 50
18+
}

src/_22_enum.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
enum YoStatus {
2+
Online,
3+
Offline,
4+
Busy,
5+
}
6+
7+
fn print_status(status: YoStatus) {
8+
match status {
9+
YoStatus::Online => println!("User is online"),
10+
YoStatus::Offline => println!("User is offline"),
11+
YoStatus::Busy => println!("User is busy"),
12+
}
13+
}
14+
15+
pub fn main() {
16+
let current_status = YoStatus::Online;
17+
print_status(current_status); // Expected: "User is online"
18+
}

src/main.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ mod _17_yo_ownership_function;
1010
mod _18_yo_slice;
1111
mod _19_yo_slice_string;
1212
mod _1_variables;
13+
mod _20_yo_struct;
14+
mod _21_yo_method;
15+
mod _22_enum;
1316
mod _2_data_types;
1417
mod _3_numbers;
1518
mod _4_operators_comparison;
@@ -18,7 +21,6 @@ mod _6_char;
1821
mod _7_array;
1922
mod _8_constant;
2023
mod _9_variable_scope;
21-
2224
fn main() {
2325
println!("Hello, world!");
2426

@@ -58,4 +60,7 @@ fn main() {
5860
_17_yo_ownership_function::main();
5961
_18_yo_slice::main();
6062
_19_yo_slice_string::main();
63+
_20_yo_struct::main();
64+
_21_yo_method::main();
65+
_22_enum::main();
6166
}

0 commit comments

Comments
 (0)