Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reads a float from STDIN, parses it, and then displays it on STDOUT
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