X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/8f2ba670de8b8b29f9bbb95ba8fa6ac382e2b745..6a83fbe44cc324f35f99da3ad290f0c0ef71260c:/src/part03.rs?ds=inline diff --git a/src/part03.rs b/src/part03.rs index b9b7ce8..c468555 100644 --- a/src/part03.rs +++ b/src/part03.rs @@ -62,50 +62,15 @@ fn read_vec() -> Vec { // So much for `read_vec`. If there are any questions left, the documentation of the respective function // should be very helpful. I will not always provide the links, as the documentation is quite easy to navigate // and you should get used to that. -// -// The rest of the code dosn't change, so we just copy it. - -enum SomethingOrNothing { - Something(T), - Nothing, -} -use self::SomethingOrNothing::{Something,Nothing}; - -trait Minimum : Copy { - fn min(a: Self, b: Self) -> Self; -} - -fn vec_min(v: Vec) -> SomethingOrNothing { - let mut min = Nothing; - for e in v { - min = Something(match min { - Nothing => e, - Something(n) => T::min(n, e) - }); - } - min -} - -// `::std::cmp::min` is a way to refer to this function without importing `std`. -// We could also have done `use std::cmp;` and later called `cmp::min`. Try that! -impl Minimum for i32 { - fn min(a: Self, b: Self) -> Self { - ::std::cmp::min(a, b) - } -} -impl SomethingOrNothing { - fn print(self) { - match self { - Nothing => println!("The number is: "), - Something(n) => println!("The number is: {}", n), - }; - } -} +// For the rest of the code, we just re-use part 02 by importing it with `use`. +// I already sneaked a bunch of `pub` in the other module to make this possible: Only +// items declared public can be imported elsewhere. +use part02::{SomethingOrNothing,Something,Nothing,vec_min}; // If you update your `main.rs` to use part 03, `cargo run` should now ask you for some numbers, // and tell you the minimum. Neat, isn't it? -pub fn part_main() { +pub fn main() { let vec = read_vec(); let min = vec_min(vec); min.print(); @@ -123,7 +88,7 @@ pub fn part_main() { // as testing whether `(self, other)` is `(Nothing, Nothing)`, which is the case exactly if // both `self` and `other` are `Nothing`. Similar so for the second arm. impl SomethingOrNothing { - fn equals(self, other: Self) -> bool { + pub fn equals(self, other: Self) -> bool { match (self, other) { (Nothing , Nothing ) => true, (Something(n), Something(m)) => n == m, @@ -163,4 +128,4 @@ fn test_vec_min() { // *Hint*: You can figure out the trait bound `read_vec` needs from the documentation of `String::parse`. // Furthermore, `std::cmp::min` works not just for `i32`, but also for `f32`. -// [index](main.html) | [previous](part02.html) | next +// [index](main.html) | [previous](part02.html) | [next](part04.html)