Created
June 12, 2017 22:06
Reads a float from STDIN, parses it, and then displays it on STDOUT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::f32; | |
use std::io::prelude::*; | |
fn main() { | |
print!("I need a float: "); | |
std::io::stdout().flush().ok().expect("Could not flush stdout"); | |
let stdin = std::io::stdin(); | |
let mut buf = String::new(); | |
let num_bytes = stdin.read_line(&mut buf) | |
.expect("reading from stdin shouldn't fail"); | |
println!("Read {} bytes from STDIN", num_bytes); | |
let my_float_result = buf.trim().parse::<f32>(); | |
match my_float_result { | |
Ok(result) => println!("Your float is {}", result), | |
Err(error) => println!("WTF is wrong with you?\n{}", error), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment