X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/943c00ca03ddc76177b4a16e19e8b831247e03f8..5f6e02d64e3789115ea4327a045b8ad3c39b1808:/workspace/src/part03.rs?ds=inline diff --git a/workspace/src/part03.rs b/workspace/src/part03.rs index 8d1c8b6..152b525 100644 --- a/workspace/src/part03.rs +++ b/workspace/src/part03.rs @@ -1 +1,71 @@ - +// Rust-101, Part 03: Input +// ======================== + + +// I/O is provided by the module `std::io`, so we first have import that with `use`. +// We also import the I/O *prelude*, which makes a bunch of commonly used I/O stuff +// directly available. +use std::io::prelude::*; +use std::io; + +fn read_vec() -> Vec { + let mut vec: Vec = Vec::::new(); + // The central handle to the standard input is made available by the function `io::stdin`. + let stdin = io::stdin(); + println!("Enter a list of numbers, one per line. End with Ctrl-D."); + for line in stdin.lock().lines() { + // Rust's type for (dynamic, growable) strings is `String`. However, our variable `line` + // here is not yet of that type: It has type `io::Result`. + + // I chose the same name (`line`) for the new variable to ensure that I will never, accidentally, + // access the "old" `line` again. + let line = line.unwrap(); + // Now that we have our `String`, we want to make it an `i32`. + + match line.trim().parse::() { + Ok(num) => { + unimplemented!() + }, + // We don't care about the particular error, so we ignore it with a `_`. + Err(_) => { + unimplemented!() + }, + } + } + + vec +} + + +// For the rest of the code, we just re-use part 02 by importing it with `use`. +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 main() { + let vec = read_vec(); + unimplemented!() +} + +// **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. +// 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 +// for `SomethingOrNothing`". +// +// Notice that I called the function on `SomethingOrNothing` `print2` to disambiguate from the `print` defined previously. +// +// *Hint*: There is a macro `print!` for printing without appending a newline. +trait Print { + /* Add things here */ +} +impl SomethingOrNothing { + fn print2(self) { + unimplemented!() + } +} + +// **Exercise 03.2**: Building on exercise 02.2, implement all the things you need on `f32` to make your +// program work with floating-point numbers. +