feat: basic exercise + functions and mods

This commit is contained in:
2024-11-16 01:45:48 +01:00
parent a84316fd93
commit 2213d38728
4 changed files with 32 additions and 4 deletions

View File

@@ -46,3 +46,20 @@ where
} }
true true
} }
// TODO: read about good practices when using mutable parameters
// maybe the correct thing to do is to take an immutable one and create a copy inside the function.
pub fn bubble_sort<T>(mut list: Vec<T>) -> Vec<T>
where
T: PartialOrd,
{
for lap in 1..list.len() {
for index in (lap..list.len()).rev() {
if list[index] < list[index - 1] {
list.swap(index, index - 1);
}
}
}
list
}

View File

@@ -27,4 +27,7 @@ pub fn run_easy() {
), ),
Err(e) => println!("Error: {}", e), Err(e) => println!("Error: {}", e),
} }
let bubled_list = easy_difficulty::bubble_sort(list);
println!("The original list sorted is {:?}", bubled_list);
} }

View File

@@ -91,4 +91,12 @@ pub fn functions_module() {
my_point.x += x; my_point.x += x;
my_point.y += y; my_point.y += y;
}; };
// Rust provides higher order functions, such as
// - map : .map(|n| n * n)
// - filter : .filter(|&n| is_add(n))
// - take_while : .take_while(|&n| n < upper)
// Diverging functions
// Never return, marked with: !
} }

View File

@@ -14,8 +14,8 @@
// mod controlflow; // mod controlflow;
// mod traits; // mod traits;
// mod str_types; // mod str_types;
// mod functions; mod functions;
mod exercises; // mod exercises;
fn main() { fn main() {
// helloworld::hello_world_module(); // helloworld::hello_world_module();
@@ -27,6 +27,6 @@ fn main() {
// controlflow::control_flow_module(); // controlflow::control_flow_module();
//traits::traits_exercise(); //traits::traits_exercise();
// str_types::str_types_module(); // str_types::str_types_module();
// functions::functions_module(); functions::functions_module();
exercises::run_easy(); // exercises::run_easy();
} }