Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exit a Rust program early from outside the main function?

Tags:

rust

system

I am in the process of writing a bash clone in Rust. I need to have my program exit when the user types exit. In previous iterations of my program, before I added more complicated features, I used return to get out of the loop that was prompting the user for input. This logic is now in a function, because of the way I am implementing built in shell functions, so when I return it just jumps out of the function back into the control loop, instead of short-circuiting the control loop and ending the program.

I realize that I could probably return a boolean when the user types exit and exit the loop, but I would like to at least know if Rust has a way to terminate programs early, similar to Java's System.exit(), as this is useful for certain types of programs.

like image 749
zephyrthenoble Avatar asked Feb 05 '14 06:02

zephyrthenoble


People also ask

How do you exit a program in rust?

pub fn exit(code: i32) -> ! Terminates the current process with the specified exit code. This function will never return and will immediately terminate the current process. The exit code is passed through to the underlying OS and will be available for consumption by another process.

How do you pause in Rust?

One way to pause your program, for some particular time is by using sleep() which takes Duration struct as an input. This program should pause for 1 second and then terminate.


2 Answers

Rust 1.0 stable

std::process::exit() does exactly that - it terminates the program with the specified exit code:

use std::process;  fn main() {     for i in 0..10 {         if i == 5 {             process::exit(1);         }         println!("{}", i);     } } 

This function causes the program to terminate immediately, without unwinding and running destructors, so it should be used sparingly.

Alternative (not recommended) solution

You can use C API directly. Add libc = "0.2" to Cargo.toml, and:

fn main() {     for i in 0..10 {         if i == 5 {             unsafe { libc::exit(1); }         }         println!("{}", i);     } } 

Calling C functions cannot be verified by the Rust compiler, so this requires the unsafe block. Resources used by the program will not be freed properly. This may cause problems such as hanging sockets. As far as I understand, the proper way to exit from the program is to terminate all threads somehow, then the process will exit automatically.

like image 89
Vladimir Matveev Avatar answered Oct 24 '22 01:10

Vladimir Matveev


panic!("Oh no something bad has happened!") 

Example:

    if a * g < 0f32 { panic!("The arithmetric-geometric mean is undefined for numbers less than zero!"); } 

In older documentation, you will see this as fail!("Oh no something bad here has happened.")

For some reason, this macro was changed from fail to panic. Panic is the way to fail, if you must.

[edit] I am sorry. It looks like you should be testing input for the string "exit," which would depend on how you are taking input (by line or by args). Then you can have the program break out of the loop on the condition that the exit is detected.

Example:

loop {     if exit_found { break }     else {         // your thing, which also looks for exit_found     } } 
like image 25
Rentsy Avatar answered Oct 24 '22 00:10

Rentsy