Skip to content
Snippets Groups Projects
Commit 8e661fdb authored by orestis.malaspin's avatar orestis.malaspin Committed by Michaël El Kharroubi
Browse files

Adds part02 with enum and pattern matching

parent 858ec252
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
/// - Functions /// - Functions
/// - Arguments are "moved" /// - Arguments are "moved"
/// - if is an expression /// - if is an expression
/// - for loops revisited
/// - Tuples /// - Tuples
/// - Destructuring /// - Destructuring
...@@ -20,10 +21,9 @@ fn check_size(size: usize) { ...@@ -20,10 +21,9 @@ fn check_size(size: usize) {
// Prints tab and returns tab. // Prints tab and returns tab.
// Tab would be destructed at the end of the function otherwise. // Tab would be destructed at the end of the function otherwise.
fn print_tab(tab: [i32; SIZE], size: usize) -> [i32; SIZE] { fn print_tab(tab: [i32; SIZE]) -> [i32; SIZE] {
check_size(size); for t in tab {
for i in 0..size { print!("{} ", t);
print!("{} ", tab[i]);
} }
println!(); println!();
tab tab
...@@ -39,9 +39,9 @@ fn min_i32(lhs: i32, rhs: i32) -> i32 { ...@@ -39,9 +39,9 @@ fn min_i32(lhs: i32, rhs: i32) -> i32 {
fn find_min(tab: [i32; SIZE], size: usize) -> ([i32; SIZE], i32) { fn find_min(tab: [i32; SIZE], size: usize) -> ([i32; SIZE], i32) {
check_size(size); check_size(size);
let mut min = tab[0]; let mut min = i32::MAX;
for i in 1..size { for t in tab {
min = min_i32(min, tab[i]); min = min_i32(min, t);
} }
(tab, min) (tab, min)
} }
...@@ -50,7 +50,7 @@ fn main() { ...@@ -50,7 +50,7 @@ fn main() {
let tab = read_command_line(); let tab = read_command_line();
println!("Among the numbers in the list:"); println!("Among the numbers in the list:");
let tab = print_tab(tab, SIZE); let tab = print_tab(tab);
// There are alternatives to access fields of tuples // There are alternatives to access fields of tuples
let (_, min) = find_min(tab, SIZE); let (_, min) = find_min(tab, SIZE);
......
[package]
name = "part02"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
/// In part03 we introduce `Enums` (also known as `Algebraic Data Types`), `Pattern Matching`,
/// and `Static Functions`.
enum NumberOrNothing {
Nothing,
Number(i32),
}
impl NumberOrNothing {
fn new(val: i32) -> Self {
NumberOrNothing::Number(val)
}
// Static function
fn print(self) {
match self {
NumberOrNothing::Nothing => println!("No number."),
NumberOrNothing::Number(val) => println!("The number is: {}", val),
}
}
}
const SIZE: usize = 9;
fn read_command_line() -> [i32; SIZE] {
[10, 32, 12, 43, 52, 53, 83, 2, 9]
}
// Prints tab and returns tab.
// Tab would be destructed at the end of the function otherwise.
fn print_tab(tab: [i32; SIZE]) -> [i32; SIZE] {
for t in tab {
print!("{} ", t);
}
println!();
tab
}
fn min_i32(lhs: i32, rhs: i32) -> i32 {
if lhs < rhs {
lhs
} else {
rhs
}
}
fn find_min(tab: [i32; SIZE]) -> ([i32; SIZE], NumberOrNothing) {
let mut min = NumberOrNothing::Nothing;
for t in tab {
match min {
NumberOrNothing::Nothing => min = NumberOrNothing::new(t),
NumberOrNothing::Number(val) => min = NumberOrNothing::new(min_i32(val, t)),
}
}
(tab, min)
}
fn main() {
let tab = read_command_line();
println!("Among the numbers in the list:");
let tab = print_tab(tab);
// There are alternatives to access fields of tuples
let (_, min) = find_min(tab);
// The first field is not used therefore we can replace it with "_"
min.print();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment