// 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<T: Minimum>(v: &Vec<T>) -> SomethingOrNothing<T> {
+fn vec_min<T: Minimum>(v: Vec<T>) -> SomethingOrNothing<T> {
let mut min = Nothing;
for e in v {
- let e = *e;
min = Something(match min {
Nothing => e,
Something(n) => T::min(n, e)
}
pub fn part_main() {
let vec = read_vec();
- let min = vec_min(&vec);
+ let min = vec_min(vec);
min.print();
}
// *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).
}
}
-// [index](main.html) | [previous](part01.html) | next
+// [index](main.html) | [previous](part01.html) | [next](part03.html)