feat: basic exercise + functions and mods
This commit is contained in:
@@ -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<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
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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: !
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user