Skip to content

Instantly share code, notes, and snippets.

@helmutgranda
Created January 23, 2020 22:00
Show Gist options
  • Save helmutgranda/f764c1dbd1fb8b36e10bbf908c0b5095 to your computer and use it in GitHub Desktop.
Save helmutgranda/f764c1dbd1fb8b36e10bbf908c0b5095 to your computer and use it in GitHub Desktop.
Asking for phone number until entered.
use std::io;
use std::process;
fn main() {
println!("Please enter your name: ");
let mut name_input = String::new();
io::stdin().read_line(&mut name_input).unwrap();
let name: String;
match name_input.trim().parse() {
Ok(value) => {
name = value;
},
Err(_error) => {
eprintln!("There was an error with your name input");
process::exit(1);
}
}
let mut phone: String = "".to_string();
while phone.is_empty() {
println!("Please enter your phone number: ");
let mut phone_input = String::new();
io::stdin().read_line(&mut phone_input).unwrap();
match phone_input.trim().parse() {
Ok(phone_value) => {
phone = phone_value;
},
Err(_error) => {
eprintln!("There was an error with your name input");
process::exit(1);
}
}
}
println!("Your Name: {} and phone: {}", name, phone)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment