exercises

Log | Files | Refs | README

commit eb3a1fd5b3a2d838b0532c2d8f76699730548e1d
parent e1fe241540b76fbce2066c66478c5bd78fd9cf04
Author: ling0x <ling0x@users.noreply.github.com>
Date:   Thu, 11 Jun 2026 14:26:30 +0100

refactor: exercises

Diffstat:
AREADME.md | 1+
Aasync_programming_in_rust/os_threads/Cargo.lock | 7+++++++
Aasync_programming_in_rust/os_threads/Cargo.toml | 6++++++
Aasync_programming_in_rust/os_threads/src/main.rs | 34++++++++++++++++++++++++++++++++++
Adesign_patterns_in_rust/behavioral_patterns/Cargo.lock | 7+++++++
Adesign_patterns_in_rust/behavioral_patterns/Cargo.toml | 6++++++
Adesign_patterns_in_rust/behavioral_patterns/src/calculator.rs | 9+++++++++
Adesign_patterns_in_rust/behavioral_patterns/src/command_pattern.rs | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adesign_patterns_in_rust/behavioral_patterns/src/expression.rs | 11+++++++++++
Adesign_patterns_in_rust/behavioral_patterns/src/lib.rs | 3+++
Adesign_patterns_in_rust/behavioral_patterns/src/main.rs | 3+++
Dindex.md | 5-----
12 files changed, 142 insertions(+), 5 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1 @@ +Coding exercises in rust and typescript diff --git a/async_programming_in_rust/os_threads/Cargo.lock b/async_programming_in_rust/os_threads/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "os_threads" +version = "0.1.0" diff --git a/async_programming_in_rust/os_threads/Cargo.toml b/async_programming_in_rust/os_threads/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "os_threads" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/async_programming_in_rust/os_threads/src/main.rs b/async_programming_in_rust/os_threads/src/main.rs @@ -0,0 +1,34 @@ +use std::{ + thread::{self, sleep}, + time::Duration, +}; + +/// A short example of how to use OS threads +/// +/// In this example we simply spawn several OS threads and put them to sleep. +/// Sleeping is essentially the same as yielding to the OS scheduler with +/// a request to be re-scheduled to run after a certain time has passed. +/// +/// Using OS threads aren't always the best choice, there are many +/// alternatives, such as fibers and green threads, etc. +fn main() { + println!("So, we start the program here!"); + let thread_1 = thread::spawn(move || { + sleep(Duration::from_millis(200)); + println!("The long running tasks finish last!"); + }); + + let thread_2 = thread::spawn(move || { + sleep(Duration::from_millis(100)); + println!("We can chain callbacks..."); + let thread_3 = thread::spawn(move || { + sleep(Duration::from_millis(50)); + println!("...like this!"); + }); + thread_3.join().unwrap(); + }); + println!("The tasks run concurrently!"); + + thread_1.join().unwrap(); + thread_2.join().unwrap(); +} diff --git a/design_patterns_in_rust/behavioral_patterns/Cargo.lock b/design_patterns_in_rust/behavioral_patterns/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "behavioral_patterns" +version = "0.1.0" diff --git a/design_patterns_in_rust/behavioral_patterns/Cargo.toml b/design_patterns_in_rust/behavioral_patterns/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "behavioral_patterns" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/design_patterns_in_rust/behavioral_patterns/src/calculator.rs b/design_patterns_in_rust/behavioral_patterns/src/calculator.rs @@ -0,0 +1,9 @@ +use std::collections::HashMap; + +pub struct Calculator { + pub variables: HashMap<String, f64>, + pub history: Vec<Calculation>, + pub last_result: Option<f64>, +} + +pub struct Calculation; diff --git a/design_patterns_in_rust/behavioral_patterns/src/command_pattern.rs b/design_patterns_in_rust/behavioral_patterns/src/command_pattern.rs @@ -0,0 +1,55 @@ +//! The Command Pattern transforms operations into objects, enabling us +//! to store, pass, and, manipulate operations just like any other data + +use crate::{calculator::Calculator, expression::Expression}; + +/// Define the Command pattern through a trait in Rust +/// This trait establishes the contract that each command must follow: +pub trait Command { + /// Commands receive the calculator by mutable reference rather than + /// owning it + fn execute(&mut self, calculator: &mut Calculator) -> Result<Option<f64>, String>; + fn undo(&self, calculator: &mut Calculator) -> Result<(), String>; + fn description(&self) -> String; +} + +/// Implement concrete commands +pub struct EvaluateCommand { + expression: String, + expr_tree: Box<dyn Expression>, + previous_result: Option<f64>, +} + +impl Command for EvaluateCommand { + fn execute(&mut self, calculator: &mut Calculator) -> Result<Option<f64>, String> { + todo!() + } + + fn undo(&self, calculator: &mut Calculator) -> Result<(), String> { + todo!() + } + + fn description(&self) -> String { + todo!() + } +} + +pub struct SetVariableCommand { + name: String, + value: f64, + previous_value: Option<f64>, +} + +impl Command for SetVariableCommand { + fn execute(&mut self, calculator: &mut Calculator) -> Result<Option<f64>, String> { + todo!() + } + + fn undo(&self, calculator: &mut Calculator) -> Result<(), String> { + todo!() + } + + fn description(&self) -> String { + todo!() + } +} diff --git a/design_patterns_in_rust/behavioral_patterns/src/expression.rs b/design_patterns_in_rust/behavioral_patterns/src/expression.rs @@ -0,0 +1,11 @@ +use std::collections::HashMap; + +pub trait Expression { + fn evaluate(&self, variables: &HashMap<String, f64>) -> Result<f64, String>; + fn to_string(&self) -> String; + + // For debugging and visualization + fn precedence(&self) -> u8 { + 0 // Leaf nodes have lowest precedence by default + } +} diff --git a/design_patterns_in_rust/behavioral_patterns/src/lib.rs b/design_patterns_in_rust/behavioral_patterns/src/lib.rs @@ -0,0 +1,3 @@ +mod calculator; +mod command_pattern; +mod expression; diff --git a/design_patterns_in_rust/behavioral_patterns/src/main.rs b/design_patterns_in_rust/behavioral_patterns/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/index.md b/index.md @@ -1,5 +0,0 @@ ---- -title: Exercises ---- - -Exercises in rust programming language for various software engineering topics