-
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
12 changed files
with
222 additions
and
65 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[package] | ||
name = "180daysofrust" | ||
name = "_180daysofrust" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
|
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
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 |
---|---|---|
@@ -1,3 +1,10 @@ | ||
pub mod shadowing; | ||
pub mod ownership; | ||
pub mod borrowing; | ||
|
||
pub fn run() { | ||
println!("Days 4-6: Understanding Ownership"); | ||
shadowing::run(); | ||
ownership::run(); | ||
borrowing::run(); | ||
} |
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
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
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,24 @@ | ||
use std::sync::{Arc, Mutex}; | ||
use std::thread; | ||
|
||
pub fn examples() { | ||
println!("Advanced Concurrency Examples:"); | ||
|
||
let counter = Arc::new(Mutex::new(0)); | ||
let mut handles = vec![]; | ||
|
||
for _ in 0..10 { | ||
let counter = Arc::clone(&counter); | ||
let handle = thread::spawn(move || { | ||
let mut num = counter.lock().unwrap(); | ||
*num += 1; | ||
}); | ||
handles.push(handle); | ||
} | ||
|
||
for handle in handles { | ||
handle.join().unwrap(); | ||
} | ||
|
||
println!("Result: {}", *counter.lock().unwrap()); | ||
} |
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,27 @@ | ||
pub fn examples() { | ||
println!("Advanced Lifetimes Examples:"); | ||
|
||
let x = 5; | ||
let r = &x; | ||
print_ref(r); | ||
|
||
let context = Context("context"); | ||
let parser = Parser { context: &context }; | ||
parser.parse("input"); | ||
} | ||
|
||
fn print_ref<'a, T>(t: &'a T) where T: std::fmt::Debug + 'a { | ||
println!("t: {:?}", t); | ||
} | ||
|
||
struct Context<'a>(&'a str); | ||
|
||
struct Parser<'a> { | ||
context: &'a Context<'a>, | ||
} | ||
|
||
impl<'a> Parser<'a> { | ||
fn parse(&self, input: &str) { | ||
println!("Parsing {} in context {}", input, self.context.0); | ||
} | ||
} |
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,46 @@ | ||
use std::ops::Add; | ||
|
||
pub fn examples() { | ||
println!("Advanced Traits Examples:"); | ||
|
||
// Associated types | ||
let point1 = Point { x: 1, y: 0 }; | ||
let point2 = Point { x: 2, y: 3 }; | ||
let sum = point1 + point2; | ||
println!("Sum of points: {:?}", sum); | ||
|
||
// Default type parameters | ||
let millimeters = Millimeters(100); | ||
let meters = Meters(1); | ||
let sum = millimeters + meters; | ||
println!("Sum of lengths: {:?}", sum); | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Point { | ||
x: i32, | ||
y: i32, | ||
} | ||
|
||
impl Add for Point { | ||
type Output = Point; | ||
|
||
fn add(self, other: Point) -> Point { | ||
Point { | ||
x: self.x + other.x, | ||
y: self.y + other.y, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Millimeters(u32); | ||
struct Meters(u32); | ||
|
||
impl Add<Meters> for Millimeters { | ||
type Output = Millimeters; | ||
|
||
fn add(self, other: Meters) -> Millimeters { | ||
Millimeters(self.0 + (other.0 * 1000)) | ||
} | ||
} |
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,29 @@ | ||
macro_rules! say_hello { | ||
() => { | ||
println!("Hello!"); | ||
}; | ||
($name:expr) => { | ||
println!("Hello, {}!", $name); | ||
}; | ||
} | ||
|
||
macro_rules! create_function { | ||
($func_name:ident) => { | ||
fn $func_name() { | ||
println!("You called {:?}()", stringify!($func_name)); | ||
} | ||
}; | ||
} | ||
|
||
create_function!(foo); | ||
create_function!(bar); | ||
|
||
pub fn examples() { | ||
println!("Macros Examples:"); | ||
|
||
say_hello!(); | ||
say_hello!("Alice"); | ||
|
||
foo(); | ||
bar(); | ||
} |
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,14 @@ | ||
mod advanced_traits; | ||
mod advanced_lifetimes; | ||
mod unsafe_rust; | ||
mod macros; | ||
mod advanced_concurrency; | ||
|
||
pub fn run() { | ||
println!("Days 13-15: Advanced Rust Concepts"); | ||
advanced_traits::examples(); | ||
advanced_lifetimes::examples(); | ||
unsafe_rust::examples(); | ||
macros::examples(); | ||
advanced_concurrency::examples(); | ||
} |
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,24 @@ | ||
pub fn examples() { | ||
println!("Unsafe Rust Examples:"); | ||
|
||
let mut num = 5; | ||
|
||
let r1 = &num as *const i32; | ||
let r2 = &mut num as *mut i32; | ||
|
||
unsafe { | ||
println!("r1 is: {}", *r1); | ||
println!("r2 is: {}", *r2); | ||
|
||
*r2 = 10; | ||
println!("r2 is now: {}", *r2); | ||
} | ||
|
||
unsafe { | ||
dangerous(); | ||
} | ||
} | ||
|
||
unsafe fn dangerous() { | ||
println!("This function is unsafe"); | ||
} |
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 |
---|---|---|
@@ -1,76 +1,23 @@ | ||
//Day 01 to day03 | ||
pub mod day01_to_day03; | ||
|
||
use day01_to_day03::print; | ||
use day01_to_day03::strings; | ||
use day01_to_day03::functions; | ||
use day01_to_day03::types; | ||
use day01_to_day03::arrays; | ||
use day01_to_day03::vars; | ||
use day01_to_day03::cli; | ||
use day01_to_day03::loops; | ||
use day01_to_day03::vectors; | ||
use day01_to_day03::structs; | ||
use day01_to_day03::conditionals; | ||
use day01_to_day03::enums; | ||
use day01_to_day03::pointer_ref; | ||
|
||
//Day04 to day06 | ||
pub mod day04_to_day06; | ||
use day04_to_day06::shadowing; | ||
use day04_to_day06::ownership; | ||
use day04_to_day06::borrowing; | ||
|
||
|
||
//Day 07 to day 09 | ||
pub mod day07_to_day09; | ||
use day07_to_day09::fibonacci; | ||
use day07_to_day09::linear_search; | ||
use day07_to_day09::binary_search; | ||
use day07_to_day09::bubble_sort; | ||
use day07_to_day09::palindrome_checker; | ||
use day07_to_day09::fibonacci_sequence; | ||
|
||
//Day 10 to day 12 | ||
pub mod day10_to_day12; | ||
use day10_to_day12::closures; | ||
use day10_to_day12::error_handling; | ||
use day10_to_day12::generics; | ||
use day10_to_day12::iterators; | ||
use day10_to_day12::lifetimes; | ||
use day10_to_day12::smart_pointers; | ||
|
||
|
||
mod day13_to_day15; | ||
|
||
fn main() { | ||
//Hello world in rust | ||
// println!("Hello, world!"); | ||
// //Print from an another file! | ||
// print::run(); | ||
// strings::run(); | ||
// functions::run(); | ||
// types::run(); | ||
// arrays::run(); | ||
// vars::run(); | ||
// cli::run(); | ||
// loops::run(); | ||
// vectors::run(); | ||
// structs::run(); | ||
// conditionals::run(); | ||
// enums::run(); | ||
// pointer_ref::run(); | ||
// shadowing::run(); | ||
// ownership::run(); | ||
// borrowing::run(); | ||
// fibonacci::run(); | ||
// linear_search::run(); | ||
// binary_search::run(); | ||
// bubble_sort::run(); | ||
// palindrome_checker::run(); | ||
// fibonacci_sequence::run(); | ||
// closures::run(); | ||
// error_handling::run(); | ||
// generics::run(); | ||
// iterators::run(); | ||
// lifetimes::run(); | ||
// smart_pointers::run(); | ||
// day01_to_day03::run(); | ||
// day04_to_day06::run(); | ||
// day07_to_day09::run(); | ||
// day10_to_day12::run(); | ||
day13_to_day15::run(); | ||
|
||
} |