Skip to content

Commit ccb5238

Browse files
rust basic [8]
1 parent 7546adf commit ccb5238

8 files changed

+29
-0
lines changed

src/_18_yo_slice.rs

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ pub fn main() {
66
let slice = &numbers[1..4]; //slice includes [20, 30, 40]
77
println!("Sum of slice : {}", calculate_sum(slice)) // expected result : 90
88
}
9+
10+
// Real Scenario: Efficiently process parts of large data structures without copying data.

src/_19_yo_slice_string.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ pub fn main() {
1010
let message = "YoRust programming is fun!";
1111
println!("First word: {}", extract_first_word(message)); // expected: "YoRust"
1212
}
13+
// Real Scenario: Parse text inputs for specific substrings or keywords.

src/_20_yo_struct.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ pub fn main() {
1616
user.username, user.age, user.active
1717
); // expected : user details printed
1818
}
19+
20+
// Real Scenario: Represent entities like users, products, or accounts.

src/_21_yo_method.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ pub fn main() {
1616
};
1717
println!("Rectangle area: {}", rect.area()) // expected: 50
1818
}
19+
20+
// Real Scenario: Use methods for data encapsulation and calculations.

src/_22_enum.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ pub fn main() {
1616
let current_status = YoStatus::Online;
1717
print_status(current_status); // Expected: "User is online"
1818
}
19+
// Real Scenario: Represent states or configurations, such as a network status.

src/_23_yo_pattern_matching.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub fn main() {
2+
let value = Some(42);
3+
match value {
4+
Some(num) if num > 10 => println!("Values is greater than 10: {}", num),
5+
Some(num) => println!("Value is 10 or less: {}", num),
6+
None => println!("No value"),
7+
}
8+
}
9+
// Real Scenario: Handle optional data, like parsing user inputs.

src/_24_yo_type_alias.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type YoKilometers = u32;
2+
3+
pub fn main() {
4+
let distance: YoKilometers = 120;
5+
println!("Distance traveled: {} km", distance); // Expected: "120 km"
6+
}
7+
8+
// Real Scenario: Use meaningful names for complex types like HashMap<String, Vec<u32>>.

src/main.rs

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ mod _1_variables;
1313
mod _20_yo_struct;
1414
mod _21_yo_method;
1515
mod _22_enum;
16+
mod _23_yo_pattern_matching;
17+
mod _24_yo_type_alias;
1618
mod _2_data_types;
1719
mod _3_numbers;
1820
mod _4_operators_comparison;
@@ -63,4 +65,6 @@ fn main() {
6365
_20_yo_struct::main();
6466
_21_yo_method::main();
6567
_22_enum::main();
68+
_23_yo_pattern_matching::main();
69+
_24_yo_type_alias::main();
6670
}

0 commit comments

Comments
 (0)