Skip to content

Commit

Permalink
refactor: re-build api
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBoji committed Oct 6, 2024
1 parent 63cb2af commit 67530a7
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/day01_to_day03/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pub fn run() {
// Infinite Loop Example
#[allow(dead_code)]
fn infinite_loop_example() {
let mut count = 0;
loop {
Expand All @@ -14,6 +15,7 @@ pub fn run() {
}

// While Loop Example with FizzBuzz
#[allow(dead_code)]
fn while_loop_fizzbuzz() {
let mut count = 1;
while count <= 100 {
Expand Down
11 changes: 11 additions & 0 deletions src/day04_to_day06/shadowing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

pub fn run() {
// Example 1: Basic shadowing
let x = 5;
Expand All @@ -24,6 +26,11 @@ pub fn run() {

println!("Outer y still: {}", y);

// Example 4: Shadowing with functions
let message = String::from("Hello");
let message = announce(message);
println!("Announced message: {}", message);

/*
Shadowing in Rust allows you to declare a new variable with the same name as a previous variable.
This is different from mutability:
Expand All @@ -37,3 +44,7 @@ pub fn run() {
- Create clearer, more readable code in certain scenarios.
*/
}

fn announce<T: Display>(value: T) -> String {
format!("Announcing: {}", value)
}
1 change: 1 addition & 0 deletions src/day10_to_day12/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct Point {
}

impl Point {
#[allow(dead_code)]
fn new(x: i32, y: i32) -> Self {
Point { x, y }
}
Expand Down
10 changes: 8 additions & 2 deletions src/day10_to_day12/lifetimes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

pub fn examples() {
println!("Lifetimes Examples:");

Expand All @@ -11,15 +13,19 @@ pub fn examples() {
let string3 = String::from("very long string");
let result2;
{
let string4 = String::from("tiny");
result2 = longest(&string3, &string4);
let _string4 = String::from("tiny");
result2 = longest(&string3, &string3);
}
println!("Longest string from example 2: {}", result2);

// Example 3: Using 'static lifetime
let static_string: &'static str = "I have a static lifetime";
let result3 = longest(static_string, "Short lived");
println!("Longest string with static lifetime: {}", result3);

// Example 4: Using longest_with_announcement
let result4 = longest_with_announcement(&string1, &string2, "Comparing strings");
println!("Longest string with announcement: {}", result4);
}

// The lifetime parameter 'a specifies that the references x and y
Expand Down
2 changes: 1 addition & 1 deletion src/day10_to_day12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod traits;
pub fn run() {
println!("Days 10-12: Advanced Rust Concepts");
closures::examples();
error_handling::examples();
let _ = error_handling::examples();
generics::examples();
iterators::examples();
lifetimes::examples();
Expand Down
4 changes: 2 additions & 2 deletions src/day10_to_day12/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Shape for Rectangle {
}

// Function to print shape information
fn print_shape_info(shape: &impl Shape) {
fn print_shape_info(shape: &dyn Shape) {
println!("{} area: {:.2}", shape.name(), shape.area());
}

Expand All @@ -56,6 +56,6 @@ pub fn examples() {
];

for shape in shapes.iter() {
print_shape_info(shape.as_ref());
print_shape_info(&**shape);
}
}
2 changes: 1 addition & 1 deletion src/day13_to_day15/advanced_lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ fn multiple_lifetime_example() {

/// A function that takes two string slices with different lifetimes and returns the longer one.
/// The returned reference is guaranteed to live at least as long as the shorter of 'a and 'b.
fn longest<'a, 'b>(x: &'a str, y: &'b str) -> &'a str {
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() { x } else { y }
}
1 change: 1 addition & 0 deletions src/day13_to_day15/advanced_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ trait Animal {
struct Dog;

impl Dog {
#[allow(dead_code)]
fn baby_name() -> String {
String::from("Spot")
}
Expand Down
2 changes: 1 addition & 1 deletion src/day16_to_day18/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ mod web_development;

pub fn run() {
println!("Days 16-18: Web Development");
web_development::main();
let _ = web_development::main();
}

0 comments on commit 67530a7

Please sign in to comment.