// Rust-101, Part 02: Generic types, Traits
// ========================================
// Rust-101, Part 02: Generic types, Traits
// ========================================
-use std;
-
-// Let us for a moment reconsider the type `NumberOrNothing`. Isn't it a bit
-// annoying that we had to hard-code the type `i32` in there? What if tomorrow,
-// we want a `CharOrNothing`, and later a `FloatOrNothing`? Certainly we don't
-// want to re-write the type and all its inherent methods.
+// Let us for a moment reconsider the type `NumberOrNothing`. Isn't it a bit annoying that we
+// had to hard-code the type `i32` in there? What if tomorrow, we want a `CharOrNothing`, and
+// later a `FloatOrNothing`? Certainly we don't want to re-write the type and all its inherent methods.
// `SomethingOrNothing<i32>` to get back our `NumberOrNothing`.
type NumberOrNothing = SomethingOrNothing<i32>;
// However, we can also write `SomethingOrNothing<bool>` or even `SomethingOrNothing<SomethingOrNothing<i32>>`.
// `SomethingOrNothing<i32>` to get back our `NumberOrNothing`.
type NumberOrNothing = SomethingOrNothing<i32>;
// However, we can also write `SomethingOrNothing<bool>` or even `SomethingOrNothing<SomethingOrNothing<i32>>`.
-// In fact, such a type is so useful that it is already present in the standard
-// library: It's called an *option type*, written `Option<T>`.
-// Go check out its [documentation](http://doc.rust-lang.org/stable/std/option/index.html)!
+// In fact, such a type is so useful that it is already present in the standard library: It's called an
+// *option type*, written `Option<T>`. Go check out its [documentation](http://doc.rust-lang.org/stable/std/option/index.html)!
// (And don't worry, there's indeed lots of material mentioned there that we did not cover yet.)
// ## Generic `impl`, Static functions
// The types are so similar, that we can provide a generic function to construct a `SomethingOrNothing<T>`
// from an `Option<T>`, and vice versa.
// **Exercise 02.1**: Implement such functions! I provided a skeleton of the solution. Here,
// (And don't worry, there's indeed lots of material mentioned there that we did not cover yet.)
// ## Generic `impl`, Static functions
// The types are so similar, that we can provide a generic function to construct a `SomethingOrNothing<T>`
// from an `Option<T>`, and vice versa.
// **Exercise 02.1**: Implement such functions! I provided a skeleton of the solution. Here,
//
// Notice the syntax for giving generic implementations to generic types: Think of the first `<T>`
// as *declaring* a type variable ("I am doing something for all types `T`"), and the second `<T>` as
//
// Notice the syntax for giving generic implementations to generic types: Think of the first `<T>`
// as *declaring* a type variable ("I am doing something for all types `T`"), and the second `<T>` as
// Remember that `self` is the `this` of Rust, and implicitly has type `Self`.
impl<T> SomethingOrNothing<T> {
fn new(o: Option<T>) -> Self {
// Remember that `self` is the `this` of Rust, and implicitly has type `Self`.
impl<T> SomethingOrNothing<T> {
fn new(o: Option<T>) -> Self {
// To make the function usable with a `Vec<i32>`, we implement the `Minimum` trait for `i32`.
impl Minimum for i32 {
fn min(self, b: Self) -> Self {
// To make the function usable with a `Vec<i32>`, we implement the `Minimum` trait for `i32`.
impl Minimum for i32 {
fn min(self, b: Self) -> Self {