Console Input
This page is under construction. Please come back later.
/****************************************************************************** * This program demonstrates how to prompt the user for input. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ mod utils; fn main() { let mut name:&str = ""; name = &(Utils.prompt("What is your name? ")); let mut favoriteColor:&str = ""; favoriteColor = &(Utils.prompt("What is your favorite color? ")); println!("{}", format!("Hello, {0:s}! I like {1:s} too!", name, favoriteColor)); }
Output
File not found!: /kunden/homepages/39/d957328751/htdocs/pureprogrammer/rs/examples/output/ConsoleInput1.out
/****************************************************************************** * This program demonstrates how to prompt the user for input. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ mod utils; use std::error; use std::sync::Arc; fn main() { match (|| -> Result<(), Arc<dyn error::Error>>{ let mut favoriteInt:isize = 0; let mut favoriteLong:i64 = 0; let mut favoriteDouble:f64 = 0.0; let mut favoriteIntInput:&str = ""; favoriteIntInput = &(Utils.prompt("What is your favorite small integer? ")); favoriteInt = stoi(favoriteIntInput)?; let mut favoriteLongInput:&str = ""; favoriteLongInput = &(Utils.prompt("What is your favorite large integer? ")); favoriteLong = stol(favoriteLongInput)?; let mut favoriteDoubleInput:&str = ""; favoriteDoubleInput = &(Utils.prompt("What is your favorite floating point? ")); favoriteDouble = stod(favoriteDoubleInput)?; let sum:f64 = (favoriteInt) as f64 + (favoriteLong) as f64 + favoriteDouble; println!("{}", format!("All together they add up to {0:f}!", sum)); return Ok(()); })() { Ok(()) => {}, Err(ex) => { if let Some(ex) = utils::isCustomErrorType(ex.clone(), utils::CustomError::NumberFormatError("".to_string())){ println!("{}", String::from("Bad input! ") + &format!("{}", ex)); } else { println!("Don't know what went wrong!"); } } }; }
Output
File not found!: /kunden/homepages/39/d957328751/htdocs/pureprogrammer/rs/examples/output/ConsoleInput2.out
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Rust Language Reference]]
- [[Rust Compiler]]