-//@ 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
+//@ We want the function to take two references *with the same lifetime*, and then
+//@ return a reference with that lifetime. If the two input lifetimes would be different, we
//@ would not know which lifetime to use for the result.
pub trait Minimum {
fn min<'a>(&'a self, other: &'a Self) -> &'a Self;
//@ would not know which lifetime to use for the result.
pub trait Minimum {
fn min<'a>(&'a self, other: &'a Self) -> &'a Self;
}
min
}
//@ Notice that the return type `Option<&T>` is technically (leaving the borrowing story aside) a
//@ pointer to a `T`, that could optionally be invalid. In other words, it's just like a pointer in
//@ C(++) or Java that can be `NULL`! However, thanks to `Option` being an `enum`, we cannot forget
}
min
}
//@ Notice that the return type `Option<&T>` is technically (leaving the borrowing story aside) a
//@ pointer to a `T`, that could optionally be invalid. In other words, it's just like a pointer in
//@ C(++) or Java that can be `NULL`! However, thanks to `Option` being an `enum`, we cannot forget
//@ Also, if you are worried about wasting space, notice that Rust knows that `&T` can never be
//@ `NULL`, and hence optimizes `Option<&T>` to be no larger than `&T`. The `None` case is represented
//@ as `NULL`. This is another great example of a zero-cost abstraction: `Option<&T>` is exactly like
//@ Also, if you are worried about wasting space, notice that Rust knows that `&T` can never be
//@ `NULL`, and hence optimizes `Option<&T>` to be no larger than `&T`. The `None` case is represented
//@ as `NULL`. This is another great example of a zero-cost abstraction: `Option<&T>` is exactly like
// **Exercise 07.1**: For our `vec_min` to be usable with `BigInt`, you will have to provide an implementation of
// `Minimum`. You should be able to pretty much copy the code you wrote for exercise 06.1. You should *not*
// **Exercise 07.1**: For our `vec_min` to be usable with `BigInt`, you will have to provide an implementation of
// `Minimum`. You should be able to pretty much copy the code you wrote for exercise 06.1. You should *not*
impl Minimum for BigInt {
fn min<'a>(&'a self, other: &'a Self) -> &'a Self {
unimplemented!()
impl Minimum for BigInt {
fn min<'a>(&'a self, other: &'a Self) -> &'a Self {
unimplemented!()
//@ 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
//@ 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). `Eq` can be automatically derived as well.
+//@ the "partial", I suggest you check out the documentation of [`PartialEq`](https://doc.rust-lang.org/std/cmp/trait.PartialEq.html)
+//@ and [`Eq`](https://doc.rust-lang.org/std/cmp/trait.Eq.html). `Eq` can be automatically derived as well.
-// Now we can compare `BigInt`s. Rust treats `PratialEq` special in that it is wired to the operator `==`:
-//@ That operator can not be used on our numbers! Speaking in C++ terms, we just overloaded the `==` operator
+// Now we can compare `BigInt`s. Rust treats `PartialEq` special in that it is wired to the operator `==`:
+//@ That operator can now be used on our numbers! 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
//@ 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
//@ 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`.
//@ 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`.
//@ 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.
//@ 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.
// of course, need a `Display` bound on `T`.) Then you should be able to use them with `println!` just like you do
// with numbers, and get rid of the inherent functions to print `SomethingOrNothing<i32>` and `SomethingOrNothing<f32>`.
// of course, need a `Display` bound on `T`.) Then you should be able to use them with `println!` just like you do
// with numbers, and get rid of the inherent functions to print `SomethingOrNothing<i32>` and `SomethingOrNothing<f32>`.