diff --git a/codes/rust_lang/part01/src/main.rs b/codes/rust_lang/part01/src/main.rs index 5c668ee30c799ae01cbbfd19192b298455a143fc..44065235cad65b8b64c5d1418ed21233ec2e7ffa 100644 --- a/codes/rust_lang/part01/src/main.rs +++ b/codes/rust_lang/part01/src/main.rs @@ -2,6 +2,7 @@ /// - Functions /// - Arguments are "moved" /// - if is an expression +/// - for loops revisited /// - Tuples /// - Destructuring @@ -20,10 +21,9 @@ fn check_size(size: usize) { // Prints tab and returns tab. // Tab would be destructed at the end of the function otherwise. -fn print_tab(tab: [i32; SIZE], size: usize) -> [i32; SIZE] { - check_size(size); - for i in 0..size { - print!("{} ", tab[i]); +fn print_tab(tab: [i32; SIZE]) -> [i32; SIZE] { + for t in tab { + print!("{} ", t); } println!(); tab @@ -39,9 +39,9 @@ fn min_i32(lhs: i32, rhs: i32) -> i32 { fn find_min(tab: [i32; SIZE], size: usize) -> ([i32; SIZE], i32) { check_size(size); - let mut min = tab[0]; - for i in 1..size { - min = min_i32(min, tab[i]); + let mut min = i32::MAX; + for t in tab { + min = min_i32(min, t); } (tab, min) } @@ -50,7 +50,7 @@ fn main() { let tab = read_command_line(); 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 let (_, min) = find_min(tab, SIZE); diff --git a/codes/rust_lang/part02/Cargo.toml b/codes/rust_lang/part02/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..b5ed9e3d251f9a1c9ec68f4fceff4b119ac997dc --- /dev/null +++ b/codes/rust_lang/part02/Cargo.toml @@ -0,0 +1,8 @@ +[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] diff --git a/codes/rust_lang/part02/src/main.rs b/codes/rust_lang/part02/src/main.rs new file mode 100644 index 0000000000000000000000000000000000000000..acf2592df7195a2497a4bfbf987baf14a33ebf3c --- /dev/null +++ b/codes/rust_lang/part02/src/main.rs @@ -0,0 +1,68 @@ +/// 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(); +}