X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/e7fafa765963c465b1601ae2204564c55ee11571..4cac8138d63e49c60d101bee79b9e60644b3de0a:/solutions/src/vec.rs?ds=inline diff --git a/solutions/src/vec.rs b/solutions/src/vec.rs index 8eff916..faf5171 100644 --- a/solutions/src/vec.rs +++ b/solutions/src/vec.rs @@ -37,7 +37,7 @@ pub mod part01 { } } -pub mod part0203 { +pub mod part02 { // A polymorphic (generic) "some value, or no value" pub enum SomethingOrNothing { Something(T), @@ -125,3 +125,63 @@ pub mod part0203 { } } +pub mod part03 { + use std::io::prelude::*; + use std::io; + + fn read_vec() -> Vec { + let mut vec: Vec = Vec::::new(); + let stdin = io::stdin(); + println!("Enter a list of numbers, one per line. End with Ctrl-D (Linux) or Ctrl-Z (Windows)."); + for line in stdin.lock().lines() { + let line = line.unwrap(); + match line.trim().parse::() { + Ok(num) => { + vec.push(num) + }, + // We don't care about the particular error, so we ignore it with a `_`. + Err(_) => { + println!("What did I say about numbers?") + }, + } + } + + vec + } + + use super::part02::{SomethingOrNothing,Something,Nothing,vec_min}; + + pub fn main() { + let vec = read_vec(); + let min = vec_min(vec); + min.print2(); + } + + pub trait Print { + fn print(self); + } + impl Print for i32 { + fn print(self) { + print!("{}", self); + } + } + + impl SomethingOrNothing { + fn print2(self) { + match self { + Nothing => println!("The number is: "), + Something(n) => { + print!("The number is: "); + n.print(); + println!(); + } + } + } + } + + impl Print for f32 { + fn print(self) { + print!("{}", self); + } + } +}