Skip to content

Commit c5caf83

Browse files
closure
1 parent 5094b53 commit c5caf83

7 files changed

+122
-0
lines changed

src/_29_yo_overloadable_operator.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::ops::Add;
2+
3+
// Define a custom struct to represent a point in 2D space.
4+
#[derive(Debug)]
5+
struct Point {
6+
x: i32,
7+
y: i32,
8+
}
9+
10+
// Implement the Add trait to allow adding two Points together.
11+
impl Add for Point {
12+
type Output = Point;
13+
14+
fn add(self, other: Self) -> Point {
15+
Point {
16+
x: self.x + other.x,
17+
y: self.y + other.y,
18+
}
19+
}
20+
}
21+
22+
pub fn main() {
23+
let point1 = Point { x: 1, y: 2 };
24+
let point2 = Point { x: 3, y: 4 };
25+
26+
let result = point1 + point2; // Overloaded operator
27+
println!("{:?}", result); // Expected output: Point { x: 4, y: 6 }
28+
}

src/_30_yo_optional.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fn find_item(value: i32) -> Option<String> {
2+
if value > 10 {
3+
Some("Item found!".to_string())
4+
} else {
5+
None
6+
}
7+
}
8+
9+
pub fn main() {
10+
let result = find_item(15);
11+
12+
match result {
13+
Some(message) => println!("{}", message), // Output: Item found !
14+
None => println!("No item found."),
15+
}
16+
}

src/_31_yo_comparing.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::cmp::Ordering;
2+
3+
#[derive(Debug, PartialOrd, PartialEq)]
4+
struct Product {
5+
price: f64,
6+
name: String,
7+
}
8+
9+
pub fn main() {
10+
let product1 = Product {
11+
price: 19.99,
12+
name: "Laptop".to_string(),
13+
};
14+
let product2 = Product {
15+
price: 29.99,
16+
name: "Smartphone".to_string(),
17+
};
18+
19+
if product1 < product2 {
20+
println!("{} is cheaper than {}", product1.name, product2.name);
21+
} else {
22+
println!("{} is more expensive than {}", product1.name, product2.name);
23+
}
24+
}

src/_32_yo_string_manipulation.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pub fn main() {
2+
let first_name = "Yogi";
3+
let last_name = "Arif Widodo";
4+
5+
// Concatenate strings
6+
let full_name = format!("{} {}", first_name, last_name);
7+
println!("{}", full_name); // Output: Yogi Arif Widodo
8+
9+
// String manipulation: slicing
10+
let first_char = &full_name[0..1];
11+
println!("{}", first_char); // Output: Y
12+
}

src/_33_yo_formatting.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::fmt;
2+
3+
struct Product {
4+
name: String,
5+
price: f64,
6+
}
7+
8+
impl fmt::Display for Product {
9+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10+
write!(f, "{}: ${:.2}", self.name, self.price)
11+
}
12+
}
13+
14+
pub fn main() {
15+
let product = Product {
16+
name: "Laptop".to_string(),
17+
price: 999.99,
18+
};
19+
20+
println!("{}", product); // Output: Laptop: $999.99
21+
}

src/_34_yo_closure.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// A closure is an anonymous function that can capture its environment. They are useful for things like callbacks or operations that require an external context.
2+
3+
// Example: Using Closure for Summation
4+
pub fn main() {
5+
let add = |x, y| x + y;
6+
7+
let result = add(5, 10);
8+
println!("{}", result); // Output: 15
9+
}

src/main.rs

+12
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ mod _25_yo_module;
1919
mod _26_yo_crate;
2020
mod _27_yo_trait;
2121
mod _28_generic;
22+
mod _29_yo_overloadable_operator;
2223
mod _2_data_types;
24+
mod _30_yo_optional;
25+
mod _31_yo_comparing;
26+
mod _32_yo_string_manipulation;
27+
mod _33_yo_formatting;
28+
mod _34_yo_closure;
2329
mod _3_numbers;
2430
mod _4_operators_comparison;
2531
mod _5_operator_boolean;
@@ -75,4 +81,10 @@ fn main() {
7581
_26_yo_crate::main();
7682
_27_yo_trait::main();
7783
_28_generic::main();
84+
_29_yo_overloadable_operator::main();
85+
_30_yo_optional::main();
86+
_31_yo_comparing::main();
87+
_32_yo_string_manipulation::main();
88+
_33_yo_formatting::main();
89+
_34_yo_closure::main();
7890
}

0 commit comments

Comments
 (0)