diff --git a/src/exercises/easy_difficulty.rs b/src/exercises/easy_difficulty.rs index 1366055..6bb7a52 100644 --- a/src/exercises/easy_difficulty.rs +++ b/src/exercises/easy_difficulty.rs @@ -46,3 +46,20 @@ where } 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(mut list: Vec) -> Vec +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 +} diff --git a/src/exercises/mod.rs b/src/exercises/mod.rs index 4564d48..97746d4 100644 --- a/src/exercises/mod.rs +++ b/src/exercises/mod.rs @@ -27,4 +27,7 @@ pub fn run_easy() { ), Err(e) => println!("Error: {}", e), } + + let bubled_list = easy_difficulty::bubble_sort(list); + println!("The original list sorted is {:?}", bubled_list); } diff --git a/src/functions.rs b/src/functions.rs index d015e31..f628b52 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -91,4 +91,12 @@ pub fn functions_module() { my_point.x += x; 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: ! } diff --git a/src/main.rs b/src/main.rs index e92736a..2f42dd4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,8 +14,8 @@ // mod controlflow; // mod traits; // mod str_types; -// mod functions; -mod exercises; +mod functions; +// mod exercises; fn main() { // helloworld::hello_world_module(); @@ -27,6 +27,6 @@ fn main() { // controlflow::control_flow_module(); //traits::traits_exercise(); // str_types::str_types_module(); - // functions::functions_module(); - exercises::run_easy(); + functions::functions_module(); + // exercises::run_easy(); }