X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/f21475ab3d58a70db564410f2b81572a4dbb492c..943c00ca03ddc76177b4a16e19e8b831247e03f8:/src/part02.rs?ds=sidebyside diff --git a/src/part02.rs b/src/part02.rs index a9f9b05..b8641df 100644 --- a/src/part02.rs +++ b/src/part02.rs @@ -78,10 +78,9 @@ trait Minimum : Copy { // we cannot call `min`. Just try it! There is no reason to believe that `T` provides such an operation. // This is in strong contrast to C++, where the compiler only checks such details when the // function is actually used. -fn vec_min(v: &Vec) -> SomethingOrNothing { +fn vec_min(v: Vec) -> SomethingOrNothing { let mut min = Nothing; for e in v { - let e = *e; min = Something(match min { Nothing => e, Something(n) => T::min(n, e) @@ -125,7 +124,7 @@ fn read_vec() -> Vec { } pub fn part_main() { let vec = read_vec(); - let min = vec_min(&vec); + let min = vec_min(vec); min.print(); } @@ -136,7 +135,7 @@ pub fn part_main() { // *for an existing type*. With the hierarchical approach of, e.g., C++ or Java, // that's not possible: We cannot make an existing type suddenly also inherit from our abstract base class. -// **Exercise**: Define a trait "Print" to write a generic version of `SomethingOrNothing::print`. +// **Exercise**: Define a trait `Print` to write a generic version of `SomethingOrNothing::print`. // Implement that trait for `i32`, and change the code above to use it. // I will again provide a skeleton for this solution. It also shows how to attach bounds to generic // implementations (just compare it to the `impl` block from the previous exercise). @@ -155,4 +154,4 @@ impl SomethingOrNothing { } } -// [index](main.html) | [previous](part01.html) | next +// [index](main.html) | [previous](part01.html) | [next](part03.html)