I want when user type "n" program will quit
How to do that?
std::io;
fn main() {
println!("Would you like to continue? (y/n)");
let mut is_start: String = String::new();
io::stdin()
.read_line(&mut is_start)
.expect("Something went wrong");
let is_start: &str = is_start.trim();
match is_start {
"y" => start(),
"n" => /* quit the program */,
_ => println!("Invalid command"),
}
}
fn start() {
println!("Here we go :)");
}
Can you give me the working code
Literally, either return
or std::process::exit(0)
in place of /* quit the program */
will do.
use std::io;
fn main() {
println!("Would you like to continue? (y/n)");
let mut is_start: String = String::new();
io::stdin()
.read_line(&mut is_start)
.expect("Something went wrong");
let is_start: &str = is_start.trim();
match is_start {
"y" => start(),
"n" => return,
_ => println!("Invalid command"),
}
}
fn start() {
println!("Here we go :)");
}
or
use std::io;
fn main() {
println!("Would you like to continue? (y/n)");
let mut is_start: String = String::new();
io::stdin()
.read_line(&mut is_start)
.expect("Something went wrong");
let is_start: &str = is_start.trim();
match is_start {
"y" => start(),
"n" => std::process::exit(0),
_ => println!("Invalid command"),
}
}
fn start() {
println!("Here we go :)");
}