// Rust-101, Part 03: Input // ======================== // I/O is provided by the module `std::io`, so we first have to 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 (Linux) or Ctrl-Z (Windows)."); 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**: The goal is to write a generic version of `SomethingOrNothing::print`. // To this end, define a trait `Print` that provides (simple) generic printing, and implement // that trait for `i32`. Then define `SomethingOrNothing::print2` to use that trait, and change // `main` above to use the new generic `print2` 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 // 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. pub trait Print { /* Add things here */ } impl Print for i32 { /* 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.