tune and tweak: part 05-07
[rust-101.git] / src / part07.rs
index c12081ef976604f9be6a116c8dac5fed9e552357..8fad5a8001a8998e67b99ce8c1e957ff98597389 100644 (file)
@@ -3,7 +3,7 @@
 
 pub use part05::BigInt;
 
-// With our new knowledge on Lifetimes, we are now able to write down the desired type
+// With our new knowledge of lifetimes, we are now able to write down the desired type
 // of `min`: We want the function to take two borrows *of the same lifetime*, and then
 // return a borrow of that lifetime. If the two input lifetimes would be different, we
 // would not know which lifetime to use for the result.
@@ -36,7 +36,7 @@ pub fn vec_min<T: Minimum>(v: &Vec<T>) -> Option<&T> {
 // a pointer in C(++), if you look at what happens during execution - but it's much safer to use.
 
 // For our `vec_min` to be usable with `BigInt`, we need to provide an implementation of
-// `minimum`. You should be able to pretty much copy the code you wrote for exercise 06.1.
+// `Minimum`. You should be able to pretty much copy the code you wrote for exercise 06.1.
 impl Minimum for BigInt {
     fn min<'a>(&'a self, other: &'a Self) -> &'a Self {
         unimplemented!()
@@ -46,10 +46,9 @@ impl Minimum for BigInt {
 // ## Operator Overloading
 // How can we know that our `min` function actually does what we want it to do? One possibility
 // here is to do *testing*. Rust comes with nice build-in support for both unit tests and integration
-// tests. However, before we go there, we need to have a way of checking whether the results are
+// tests. However, before we go there, we need to have a way of checking whether the results of function calls are
 // correct. In other words, we need to define how to test equality of `BigInt`. Being able to
-// test equality is a property of a type, that - you guessed it - Rust expresses as a trait:
-// `PartialEq`. Once a type implements that trait, one can use the `==` operator on it.
+// test equality is a property of a type, that - you guessed it - Rust expresses as a trait: `PartialEq`.
 
 // Doing this for `BigInt` is fairly easy, thanks to our requirement that there be no trailing zeros.
 // The `inline` attribute tells Rust that we will typically want this function to be inlined.
@@ -63,14 +62,14 @@ impl PartialEq for BigInt {
 // Since implementing `PartialEq` is a fairly mechanical business, you can let Rust automate this
 // by adding the attribute `derive(PartialEq)` to the type definition. In case you wonder about
 // the "partial", I suggest you check out the documentation of [`PartialEq`](http://doc.rust-lang.org/std/cmp/trait.PartialEq.html)
-// and [`Eq`](http://doc.rust-lang.org/std/cmp/trait.Eq.html). Again, `Eq` can be automatically derived.
+// and [`Eq`](http://doc.rust-lang.org/std/cmp/trait.Eq.html). `Eq` can be automatically derived as well.
 
-// Now we can compare `BigInt`s! Speaking in C++ terms, we just overloaded the `==` operator
+// Now we can compare `BigInt`s using `==`! Speaking in C++ terms, we just overloaded the `==` operator
 // for `BigInt`. Rust does not have function overloading (i.e., it will not dispatch to different
 // functions depending on the type of the argument). Instead, one typically finds (or defines) a
 // trait that catches the core characteristic common to all the overloads, and writes a single
 // function that's generic in the trait. For example, instead of overloading a function for all
-// the ways a string can be represented, one write a generic functions over [ToString](http://doc.rust-lang.org/std/string/trait.ToString.html).
+// the ways a string can be represented, one writes a generic functions over [ToString](http://doc.rust-lang.org/std/string/trait.ToString.html).
 // Usually, there is a trait like this that fits the purpose - and if there is, this has the great
 // advantage that any type *you* write, that can convert to a string, just has to implement
 // that trait to be immediately usable with all the functions out there that generalize over `ToString`.
@@ -83,7 +82,7 @@ fn compare_big_ints() {
 }
 
 // ## Testing
-// With our equality test written, we are now ready to write out first testcase. It doesn't get much
+// With our equality test written, we are now ready to write our first testcase. It doesn't get much
 // simpler: You just write a function (with no arguments or return value), and give it the `test` attribute.
 // `assert!` is like `debug_assert!`, but does not get compiled away in a release build.
 #[test]
@@ -105,7 +104,7 @@ fn test_min() {
 // that users can understand, while `Debug` is meant to show the internal state of data and targeted at
 // the programmer. The latter is what we want for `assert_eq!`, so let's get started.
 
-// Al formating is handled by [`std::fmt`](http://doc.rust-lang.org/std/fmt/index.html). I won't explain
+// All formating is handled by [`std::fmt`](http://doc.rust-lang.org/std/fmt/index.html). I won't explain
 // all the details, and refer you to the documentation instead.
 use std::fmt;
 
@@ -118,30 +117,22 @@ impl fmt::Debug for BigInt {
 }
 // `Debug` implementations can be automatically generated using the `derive(Debug)` attribute.
 
-// Now we are ready to use `assert_eq!` to test `vec_min`. While we are at it, let's also follow the usual
-// Rust style of putting tests into a *submodule*, to avoid polluting the namespace. The attribute `cfg(test)`
-// at the submodule means that it will only be compiled when building the tests.
-#[cfg(test)]
-mod tests {
-    use super::*;
-
-    #[test]
-    fn test_vec_min() {
-        let b1 = BigInt::new(1);
-        let b2 = BigInt::new(42);
-        let b3 = BigInt::from_vec(vec![0, 1]);
-
-        let v1 = vec![b2.clone(), b1.clone(), b3.clone()];
-        let v2 = vec![b2.clone(), b3.clone()];
-        assert_eq!(vec_min(&v1), Some(&b1));
-        assert_eq!(vec_min(&v2), Some(&b2));
-    }
+// Now we are ready to use `assert_eq!` to test `vec_min`.
+#[test]
+fn test_vec_min() {
+    let b1 = BigInt::new(1);
+    let b2 = BigInt::new(42);
+    let b3 = BigInt::from_vec(vec![0, 1]);
+
+    let v1 = vec![b2.clone(), b1.clone(), b3.clone()];
+    let v2 = vec![b2.clone(), b3.clone()];
+    assert_eq!(vec_min(&v1), Some(&b1));
+    assert_eq!(vec_min(&v2), Some(&b2));
 }
 
 // **Exercise 07.1**: Add some more testcases. In particular, make sure you test the behavior of
 // `vec_min` on an empty vector. Also add tests for `BigInt::from_vec` (in particular, removing
-// trailing zeros) and the functions you wrote for exercise 05.1. Finally, break one of your
-// functions in a subtle way and watch the test fail.
+// trailing zeros). Finally, break one of your functions in a subtle way and watch the test fail.
 // 
 // **Exercise 07.2**: Go back to your good ol' `SomethingOrNothing`, and implement `Display` for it. (This will,
 // of course, need a `Display` bound on `T`.) Then you should be able to use them with `println!` just like you do with numbers.