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;
36 min = Something(match min {
38 Something(n) => T::min(n, e)
44 impl Minimum for i32 {
45 fn min(a: Self, b: Self) -> Self {
50 impl SomethingOrNothing<i32> {
53 Nothing => println!("The number is: <nothing>"),
54 Something(n) => println!("The number is: {}", n),
60 let min = vec_min(vec);
64 impl SomethingOrNothing<i32> {
65 fn equals(self, other: Self) -> bool {
67 (Nothing , Nothing ) => true,
68 (Something(n), Something (m)) => n == m,
76 assert!(vec_min(vec![6,325,33,532,5,7]).equals(Something(5)));
77 assert!(vec_min(vec![]).equals(Nothing));
80 // [index](main.html) | [previous](part02.html) | next