-// With `BigInt` being about numbers, we should be able to write a version of `vec_min`
-// that computes the minimum of a list of `BigInt`. We start by writing `min` for
-// `BigInt`. Now our assumption of having no trailing zeros comes in handy!
-impl BigInt {
- fn min(self, other: Self) -> Self {
- // Just to be sure, we first check that both operands actually satisfy our invariant.
- // `debug_assert!` is a macro that checks that its argument (must be of type `bool`)
- // is `true`, and panics otherwise. It gets removed in release builds, which you do with
- // `cargo build --release`.
- //
- // If you carefully check the type of `BigInt::test_invariant`, you may be surprised that
- // we can call the function this way. Doesn't it take `self` in borrowed form? Indeed,
- // the explicit way to do that would be to call `(&other).test_invariant()`. However, the
- // `self` argument of a method is treated specially by Rust, and borrowing happens automatically here.
- debug_assert!(self.test_invariant() && other.test_invariant());
- // If the lengths of the two numbers differ, we already know which is larger.
- if self.data.len() < other.data.len() {
- self
- } else if self.data.len() > other.data.len() {
- other
- } else {
- // **Exercise**: Fill in this code.
- panic!("Not yet implemented.");
+// We can also make the type `SomethingOrNothing<T>` implement `Clone`. However, that
+// can only work if `T` is `Clone`! So we have to add this bound to `T` when we introduce
+// the type variable.
+use part02::{SomethingOrNothing,Something,Nothing};
+impl<T: Clone> Clone for SomethingOrNothing<T> {
+ fn clone(&self) -> Self {
+ match *self {
+ Nothing => Nothing,
+ // In the second arm of the match, we need to talk about the value `v`
+ // that's stored in `self`. However, if we would write the pattern as
+ // `Something(v)`, that would indicate that we *own* `v` in the code
+ // after the arrow. That can't work though, we have to leave `v` owned by
+ // whoever called us - after all, we don't even own `self`, we just borrowed it.
+ // By writing `Something(ref v)`, we borrow `v` for the duration of the match
+ // arm. That's good enough for cloning it.
+ Something(ref v) => Something(v.clone()),