-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
164 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
pub fn examples() { | ||
println!("Closures Examples:"); | ||
|
||
let add_one = |x| x + 1; | ||
println!("Add one to 5: {}", add_one(5)); | ||
|
||
let numbers = vec![1, 2, 3, 4, 5]; | ||
let even_numbers: Vec<i32> = numbers.into_iter().filter(|&x| x % 2 == 0).collect(); | ||
println!("Even numbers: {:?}", even_numbers); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use std::fs::File; | ||
use std::io::Read; | ||
|
||
pub fn examples() { | ||
println!("Error Handling Examples:"); | ||
result_example(); | ||
custom_error_example(); | ||
} | ||
|
||
fn result_example() { | ||
let file_result = File::open("nonexistent.txt"); | ||
match file_result { | ||
Ok(mut file) => { | ||
let mut content = String::new(); | ||
file.read_to_string(&mut content).unwrap(); | ||
println!("File content: {}", content); | ||
} | ||
Err(error) => println!("Error opening file: {:?}", error), | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct CustomError(String); | ||
|
||
fn custom_error_example() { | ||
fn may_fail(fail: bool) -> Result<(), CustomError> { | ||
if fail { | ||
Err(CustomError("Something went wrong".to_string())) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
println!("Success: {:?}", may_fail(false)); | ||
println!("Failure: {:?}", may_fail(true)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
pub fn examples() { | ||
println!("Generics Examples:"); | ||
|
||
let numbers = vec![1, 2, 3, 4, 5]; | ||
println!("Largest number: {}", largest(&numbers)); | ||
|
||
let chars = vec!['a', 'b', 'c', 'd', 'e']; | ||
println!("Largest char: {}", largest(&chars)); | ||
} | ||
|
||
fn largest<T: PartialOrd>(list: &[T]) -> &T { | ||
let mut largest = &list[0]; | ||
|
||
for item in list.iter() { | ||
if item > largest { | ||
largest = item; | ||
} | ||
} | ||
|
||
largest | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
pub fn examples() { | ||
println!("Iterators Examples:"); | ||
|
||
let numbers = vec![1, 2, 3, 4, 5]; | ||
|
||
let doubled: Vec<i32> = numbers.iter().map(|&x| x * 2).collect(); | ||
println!("Doubled numbers: {:?}", doubled); | ||
|
||
let sum: i32 = numbers.iter().sum(); | ||
println!("Sum of numbers: {}", sum); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
pub fn examples() { | ||
println!("Lifetimes Examples:"); | ||
|
||
let string1 = String::from("short"); | ||
let string2 = String::from("longer"); | ||
let result = longest(&string1, &string2); | ||
println!("Longest string: {}", result); | ||
} | ||
|
||
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { | ||
if x.len() > y.len() { | ||
x | ||
} else { | ||
y | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pub mod closures; | ||
pub mod error_handling; | ||
pub mod generics; | ||
pub mod iterators; | ||
pub mod lifetimes; | ||
pub mod smart_pointers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use std::rc::Rc; | ||
use std::cell::RefCell; | ||
|
||
pub fn examples() { | ||
println!("Smart Pointers Examples:"); | ||
|
||
// Box<T> | ||
let boxed_value = Box::new(5); | ||
println!("Boxed value: {}", boxed_value); | ||
|
||
// Rc<T> | ||
let shared_value = Rc::new(10); | ||
let cloned_value = Rc::clone(&shared_value); | ||
println!("Shared value: {}, Cloned value: {}", shared_value, cloned_value); | ||
|
||
// RefCell<T> | ||
let mutable_value = RefCell::new(15); | ||
*mutable_value.borrow_mut() += 5; | ||
println!("Mutable value: {}", mutable_value.borrow()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
pub fn examples() { | ||
println!("Traits Examples:"); | ||
|
||
let circle = Circle { radius: 5.0 }; | ||
let rectangle = Rectangle { width: 4.0, height: 6.0 }; | ||
|
||
println!("Circle area: {}", circle.area()); | ||
println!("Rectangle area: {}", rectangle.area()); | ||
} | ||
|
||
trait Shape { | ||
fn area(&self) -> f64; | ||
} | ||
|
||
struct Circle { | ||
radius: f64, | ||
} | ||
|
||
impl Shape for Circle { | ||
fn area(&self) -> f64 { | ||
std::f64::consts::PI * self.radius * self.radius | ||
} | ||
} | ||
|
||
struct Rectangle { | ||
width: f64, | ||
height: f64, | ||
} | ||
|
||
impl Shape for Rectangle { | ||
fn area(&self) -> f64 { | ||
self.width * self.height | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters