mirror of
https://github.com/LukasKalbertodt/programmieren-in-rust.git
synced 2024-11-18 02:48:58 +01:00
Add file from the lecture
This commit is contained in:
parent
9b40a4d6c2
commit
2c0beb56a4
28
materialien/read.rs
Executable file
28
materialien/read.rs
Executable file
@ -0,0 +1,28 @@
|
|||||||
|
use std::str::FromStr;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// let x = read::<usize>();
|
||||||
|
let x: usize = read();
|
||||||
|
println!("{}", x);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read<T>() -> 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user