Last active
May 2, 2023 02:00
-
-
Save oshea00/37825691c026df54b2a97cab123c782c to your computer and use it in GitHub Desktop.
This file contains hidden or 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::{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