Skip to content

Instantly share code, notes, and snippets.

@oshea00
Last active May 2, 2023 02:00
Show Gist options
  • Save oshea00/37825691c026df54b2a97cab123c782c to your computer and use it in GitHub Desktop.
Save oshea00/37825691c026df54b2a97cab123c782c to your computer and use it in GitHub Desktop.
use std::{io::{self, Write}, str::FromStr};
fn main() {
let name = input("What's your name? ");
let number: f64 = input_num("What's your favorite number? ");
println!("{name}'s favorite number is {number:.2}.");
}
fn input(prompt: &str) -> String {
let mut line = String::new();
print!("{prompt}");
io::stdout().flush().expect("success");
io::stdin().read_line(&mut line).expect("success");
String::from(line.trim())
}
fn input_num<T>(prompt: &str) -> T
where
T:FromStr + Default
{
input(prompt).parse().unwrap_or_default()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment