Forked from
orestis.malaspin / rust-101
126 commits behind the upstream repository.
main.rs 1.26 KiB
/// Rust basics:
/// - Functions
/// - Arguments are "moved"
/// - if is an expression
/// - for loops revisited
/// - Tuples
/// - Destructuring
const SIZE: usize = 9;
fn read_command_line() -> [i32; SIZE] {
[10, 32, 12, 43, 52, 53, 83, 2, 9]
}
// Check if the size is large enough (more that 1 element)
fn check_size(size: usize) {
if size == 0 {
panic!("Size is of tab = 0.");
}
}
// 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], size: usize) -> ([i32; SIZE], i32) {
check_size(size);
let mut min = i32::MAX;
for t in tab {
min = min_i32(min, 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, SIZE);
// The first field is not used therefore we can replace it with "_"
println!("The minimal value is: {}", min);
}