Skip to content
Snippets Groups Projects
Commit 2a7c5bc3 authored by Michaël El Kharroubi's avatar Michaël El Kharroubi :satellite:
Browse files

Merge branch 'part02' into 'main'

Adds part02 with enum and pattern matching

See merge request orestis.malaspin/rust-101!5
parents 87daa031 8e661fdb
No related branches found
No related tags found
No related merge requests found
......@@ -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);
......
[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