From: Ralf Jung Date: Sat, 4 Feb 2023 19:29:27 +0000 (+0100) Subject: add part03 solutions X-Git-Url: https://git.ralfj.de/rust-101.git/commitdiff_plain/ff92eaa5332ba1ac1efab2d6be3a227774fb5946?ds=inline;hp=2eb15e9ad4c5f980bb007347146568fd41223011 add part03 solutions --- 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); + } + } +} diff --git a/src/part02.rs b/src/part02.rs index a24a9d0..2bf4d97 100644 --- a/src/part02.rs +++ b/src/part02.rs @@ -148,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) | diff --git a/src/part03.rs b/src/part03.rs index 44431e6..25f9386 100644 --- a/src/part03.rs +++ b/src/part03.rs @@ -101,7 +101,7 @@ pub fn main() { // **Exercise 03.1**: 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. +// Implement that trait for `i32`, and change `main` above to use the new generic `print` function. // 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). // You can read this as "For all types `T` satisfying the `Print` trait, I provide an implementation