X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/23c658ed26c424dd6f38468108dd8ac34534fe1e..35c4d2161ea07cfbb4085d7e5242ab9939889afa:/src/part02.rs diff --git a/src/part02.rs b/src/part02.rs index 233fa0f..e4f50be 100644 --- a/src/part02.rs +++ b/src/part02.rs @@ -1,12 +1,11 @@ // 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. +// ## Generic datatypes // The solution to this is called *generics* or *polymorphism* (the latter is Greek, // meaning "many shapes"). You may know something similar from C++ (where it's called @@ -22,16 +21,16 @@ pub use self::SomethingOrNothing::*; // `SomethingOrNothing` to get back our `NumberOrNothing`. type NumberOrNothing = SomethingOrNothing; // However, we can also write `SomethingOrNothing` or even `SomethingOrNothing>`. -// 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`. -// 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`. 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` // from an `Option`, and vice versa. - -// **Exercise**: Implement such functions! I provided a skeleton of the solution. Here, -// `panic!` is another macro. This one terminates execution with the given message. +// **Exercise 02.1**: Implement such functions! I provided a skeleton of the solution. Here, +// `unimplemented!` is another macro. This one terminates execution saying that something has not yet +// been implemented. // // Notice the syntax for giving generic implementations to generic types: Think of the first `` // as *declaring* a type variable ("I am doing something for all types `T`"), and the second `` as @@ -42,11 +41,11 @@ type NumberOrNothing = SomethingOrNothing; // Remember that `self` is the `this` of Rust, and implicitly has type `Self`. impl SomethingOrNothing { fn new(o: Option) -> Self { - panic!("Not yet implemented.") + unimplemented!() } fn to_option(self) -> Option { - panic!("Not yet implemented.") + unimplemented!() } } // Observe how `new` does *not* have a `self` parameter. This corresponds to a `static` method @@ -58,6 +57,7 @@ fn call_constructor(x: i32) -> SomethingOrNothing { SomethingOrNothing::new(Some(x)) } +// ## Traits // Now that we have a generic `SomethingOrNothing`, wouldn't it be nice to also gave a generic // `vec_min`? Of course, we can't take the minimum of a vector of *any* type. It has to be a type // supporting a `min` operation. Rust calls such properties that we may demand of types *traits*. @@ -108,18 +108,18 @@ pub fn vec_min(v: Vec) -> SomethingOrNothing { // This behavior is similar to C++ templates. The optimizer (Rust is using LLVM) then has all the // information it could want to, e.g., inline function calls. +// ## Trait implementations // To make the function usable with a `Vec`, we implement the `Minimum` trait for `i32`. impl Minimum for i32 { fn min(self, b: Self) -> Self { - std::cmp::min(self, b) + if self < b { self } else { b } } } -// In order to run our code and see the result, we again provide a `print` function. -// This also shows that we can have multiple `impl` blocks for the same type (remember -// that `NumberOrNothing` is just a type alias for `SomethingOrNothing`), and we -// can provide some methods only for certain instances of a generic type. -impl NumberOrNothing{ +// We again provide a `print` function. This also shows that we can have multiple `impl` blocks +// for the same type (remember that `NumberOrNothing` is just a type alias for `SomethingOrNothing`), +// and we can provide some methods only for certain instances of a generic type. +impl NumberOrNothing { pub fn print(self) { match self { Nothing => println!("The number is: "), @@ -142,4 +142,7 @@ pub fn main() { // If this printed `3`, then you generic `vec_min` is working! So get ready for the next part. +// **Exercise 02.2**: Change your program such that it computes the minimum of a `Vec` (where `f32` is the type +// of 32-bit floating-point numbers). You should not change `vec_min` in any way, obviously! + // [index](main.html) | [previous](part01.html) | [next](part03.html)