From 2c0beb56a4f49e9b7ef820e2420b44320ca1f8c2 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Wed, 30 Nov 2016 12:42:00 +0100 Subject: [PATCH] Add file from the lecture --- materialien/read.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 materialien/read.rs diff --git a/materialien/read.rs b/materialien/read.rs new file mode 100755 index 0000000..df3616a --- /dev/null +++ b/materialien/read.rs @@ -0,0 +1,28 @@ +use std::str::FromStr; +use std::fmt::Display; + +fn main() { + // let x = read::(); + let x: usize = read(); + println!("{}", x); + +} + +fn read() -> T + where T: FromStr, + T::Err: Display +{ + use std::io; + + loop { + let mut s = String::new(); + io::stdin() + .read_line(&mut s) + .expect("unexpected io error"); + + match s.trim_right().parse() { + Ok(value) => return value, + Err(e) => println!("error: {}", e), + } + } +}