avoid using std::cmp::min, for it doesn't support f32...
[rust-101.git] / src / part00.rs
index 4deb82946709e1b5944e9076e4c3db69bd52b98f..1648ba177295beec84ba5f711ab775577ed7bad8 100644 (file)
@@ -4,9 +4,6 @@
 // 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
@@ -48,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);
             }
         }
@@ -59,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?