re-do deepaksirone's fix on the actual source file
[rust-101.git] / workspace / src / part07.rs
1 // Rust-101, Part 07: Operator Overloading, Tests, Formatting
2 // ==========================================================
3
4 pub use part05::BigInt;
5
6 // With our new knowledge of lifetimes, we are now able to write down the desired type of `min`:
7 pub trait Minimum {
8     fn min<'a>(&'a self, other: &'a Self) -> &'a Self;
9 }
10
11 pub fn vec_min<T: Minimum>(v: &Vec<T>) -> Option<&T> {
12     let mut min: Option<&T> = None;
13     for e in v {
14         min = Some(match min {
15             None => e,
16             Some(n) => n.min(e)
17         });
18     }
19     min
20 }
21
22 // **Exercise 07.1**: For our `vec_min` to be usable with `BigInt`, you will have to provide an implementation of
23 // `Minimum`. You should be able to pretty much copy the code you wrote for exercise 06.1. You should *not*
24 // make any copies of `BigInt`!
25 impl Minimum for BigInt {
26     fn min<'a>(&'a self, other: &'a Self) -> &'a Self {
27         unimplemented!()
28     }
29 }
30
31 // ## Operator Overloading
32
33 impl PartialEq for BigInt {
34     #[inline]
35     fn eq(&self, other: &BigInt) -> bool {
36         debug_assert!(self.test_invariant() && other.test_invariant());
37         unimplemented!()
38     }
39 }
40
41
42 // Now we can compare `BigInt`s. Rust treats `PartialEq` special in that it is wired to the operator `==`:
43 fn compare_big_ints() {
44     let b1 = BigInt::new(13);
45     let b2 = BigInt::new(37);
46     println!("b1 == b1: {} ; b1 == b2: {}; b1 != b2: {}", b1 == b1, b1 == b2, b1 != b2);
47 }
48
49 // ## Testing
50 // With our equality test written, we are now ready to write our first testcase.
51 // the `test` attribute. `assert!` is like `debug_assert!`, but does not get compiled away in a release build.
52 #[test]
53 fn test_min() {
54     let b1 = BigInt::new(1);
55     let b2 = BigInt::new(42);
56     let b3 = BigInt::from_vec(vec![0, 1]);
57
58     unimplemented!()
59 }
60 // Now run `cargo test` to execute the test. If you implemented `min` correctly, it should all work!
61
62 // ## Formatting
63
64 // All formating is handled by [`std::fmt`](https://doc.rust-lang.org/std/fmt/index.html). I won't explain
65 // all the details, and refer you to the documentation instead.
66 use std::fmt;
67
68 impl fmt::Debug for BigInt {
69     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70         self.data.fmt(f)
71     }
72 }
73
74 // Now we are ready to use `assert_eq!` to test `vec_min`.
75 /*#[test]*/
76 fn test_vec_min() {
77     let b1 = BigInt::new(1);
78     let b2 = BigInt::new(42);
79     let b3 = BigInt::from_vec(vec![0, 1]);
80
81     let v1 = vec![b2.clone(), b1.clone(), b3.clone()];
82     let v2 = vec![b2.clone(), b3.clone()];
83     unimplemented!()
84 }
85
86 // **Exercise 07.1**: Add some more testcases. In particular, make sure you test the behavior of
87 // `vec_min` on an empty vector. Also add tests for `BigInt::from_vec` (in particular, removing
88 // trailing zeros). Finally, break one of your functions in a subtle way and watch the test fail.
89 // 
90 // **Exercise 07.2**: Go back to your good ol' `SomethingOrNothing`, and implement `Display` for it. (This will,
91 // of course, need a `Display` bound on `T`.) Then you should be able to use them with `println!` just like you do
92 // with numbers, and get rid of the inherent functions to print `SomethingOrNothing<i32>` and `SomethingOrNothing<f32>`.
93