[Rust] Exit a program using std::process in Rust

In this lesson we'll learn how to exit a program using the std::process module in Rust and it's exit() method.

 
use std::io;
use std::process;

fn main() {
    let mut first = String::new();
    io::stdin().read_line(&mut first).unwrap();
    
    let a:u32;
    
    match first.trim().parse() {
        Ok(val) => {
           a = val;
        },
        Err(_err) => {
            println!("This is not a valid number");
            process::exit(1);
        } 
    }
}

 

posted @ 2024-02-23 14:38  Zhentiw  阅读(10)  评论(0编辑  收藏  举报