X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/c27b94f8e36d136f81f799ca1ae205988cf3d36e..d29f51c818176f4249179ceba33de1ff41db2c90:/src/part02.rs diff --git a/src/part02.rs b/src/part02.rs index 235e22d..2bf4d97 100644 --- a/src/part02.rs +++ b/src/part02.rs @@ -21,7 +21,6 @@ pub use self::SomethingOrNothing::*; //@ What this does is define an entire family of types: We can now write //@ `SomethingOrNothing` to get back our `NumberOrNothing`. type NumberOrNothing = SomethingOrNothing; - //@ However, we can also write `SomethingOrNothing` or even //@ `SomethingOrNothing>`. In fact, a type like `SomethingOrNothing` is so //@ useful that it is already present in the standard library: It's called an *option type*, @@ -78,7 +77,7 @@ pub trait Minimum : Copy { //@ Next, we write `vec_min` as a generic function over a type `T` that we demand to satisfy the `Minimum` trait. //@ This requirement is called a *trait bound*. //@ The only difference to the version from the previous part is that we call `e.min(n)` instead -//@ of `std::cmp::min(n, e)`. Rust automatically figures out that `n` is of type `T`, which implements +//@ of `min_i32(n, e)`. Rust automatically figures out that `e` is of type `T`, which implements //@ the `Minimum` trait, and hence we can call that function. //@ //@ There is a crucial difference to templates in C++: We actually have to declare which traits @@ -122,8 +121,6 @@ impl Minimum for i32 { } // 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. @@ -151,7 +148,7 @@ pub fn main() { //@ If this printed `3`, then your generic `vec_min` is working! So get ready for the next part. // **Exercise 02.1**: 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 +// `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) | [raw source](workspace/src/part02.rs) |