Logo

Programming-Idioms

History of Idiom 120 > diff from v24 to v25

Edit summary for version 25 by programming-idioms.org:
[Rust] Comment emphasize

Version 24

2016-11-17, 14:33:33

Version 25

2016-11-18, 10:35:53

Idiom #120 Read integer from stdin

Read an integer value from the standard input into variable n.

Idiom #120 Read integer from stdin

Read an integer value from the standard input into variable n.

Code
fn get_input() -> String {
    let mut buffer = String::new();
    std::io::stdin().read_line(&mut buffer).expect("Failed");
    buffer
}

let x = get_input().trim().parse::<i64>().unwrap();
Code
fn get_input() -> String {
    let mut buffer = String::new();
    std::io::stdin().read_line(&mut buffer).expect("Failed");
    buffer
}

let x = get_input().trim().parse::<i64>().unwrap();
Comments bubble
Change i64 to what you expect to get. Do not use `unwrap()` if you're not sure it will be parsed correctly.
Comments bubble
Change i64 to what you expect to get. Do not use unwrap() if you're not sure it will be parsed correctly.