// As our first piece of Rust code, we want to write a function that computes the
// minimum of a list.
-// We are going to make use of the standard library, so let's import that:
-use std;
-
+// ## Getting started
// Let us start by thinking about the *type* of our function. Rust forces us to give the types of
// all arguments, and the return type, before we even start writing the body. In the case of our minimum
// function, we may be inclined to say that it returns a number. But then we would be in trouble: What's
NumberOrNothing::Nothing => {
min = NumberOrNothing::Number(el);
},
- // In this arm, `min` is currently the number `n`, so let's compute the new minimum and store it.
+ // In this arm, `min` is currently the number `n`, so let's compute the new minimum and store it. We will write
+ // the function `min_i32` just after we completed this one.
NumberOrNothing::Number(n) => {
- let new_min = std::cmp::min(n, el);
+ let new_min = min_i32(n, el);
min = NumberOrNothing::Number(new_min);
}
}
return min;
}
+// Now that we reduced the problem to computing the minimum of two integers, let's do that.
+fn min_i32(a: i32, b: i32) -> i32 {
+ if a < b {
+ return a;
+ } else {
+ return b;
+ }
+}
+
// Phew. We wrote our first Rust function! But all this `NumberOrNothing::` is getting kind of
// ugly. Can't we do that nicer?
}
// Putting it all together:
-pub fn part_main() {
+pub fn main() {
let vec = read_vec();
let min = vec_min(vec);
print_number_or_nothing(min);