Skip to content

Instantly share code, notes, and snippets.

@peschkaj
Created June 12, 2017 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peschkaj/049e00ab4185ec33eec143771b9d02eb to your computer and use it in GitHub Desktop.
Save peschkaj/049e00ab4185ec33eec143771b9d02eb to your computer and use it in GitHub Desktop.
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