1 // Rust-101, Part 03: Input, Formatting
2 // ====================================
4 use std::io::prelude::*;
7 fn read_vec() -> Vec<i32> {
8 let mut vec = Vec::new();
10 let stdin = io::stdin();
11 println!("Enter a list of numbers, one per line. End with Ctrl-D.");
12 for line in stdin.lock().lines() {
13 let line = line.unwrap();
14 match line.parse::<i32>() {
15 Ok(num) => vec.push(num),
16 Err(_) => println!("What did I say about numbers?"),
23 enum SomethingOrNothing<T> {
27 use self::SomethingOrNothing::{Something,Nothing};
29 trait Minimum : Copy {
30 fn min(a: Self, b: Self) -> Self;
33 fn vec_min<T: Minimum>(v: &Vec<T>) -> SomethingOrNothing<T> {
34 let mut min = Nothing;
37 min = Something(match min {
39 Something(n) => T::min(n, e)
45 impl Minimum for i32 {
46 fn min(a: Self, b: Self) -> Self {
52 impl<T: fmt::Display> fmt::Display for SomethingOrNothing<T> {
53 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55 &Something(ref t) => t.fmt(f),
56 &Nothing => "Nothing".fmt(f),
63 let min = vec_min(&vec);
64 println!("The minimum is: {}", min);
67 // [index](main.html) | [previous](part02.html) | next