avoid using std::cmp::min, for it doesn't support f32...
[rust-101.git] / src / part00.rs
index 90b04ef75bd6bf15d1ccc66b4cbf496c168e70ac..1648ba177295beec84ba5f711ab775577ed7bad8 100644 (file)
@@ -4,9 +4,7 @@
 // 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
@@ -47,9 +45,10 @@ fn vec_min(vec: Vec<i32>) -> NumberOrNothing {
             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);
             }
         }
@@ -58,6 +57,15 @@ fn vec_min(vec: Vec<i32>) -> NumberOrNothing {
     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?
 
@@ -91,7 +99,7 @@ fn print_number_or_nothing(n: NumberOrNothing) {
 }
 
 // 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);