Home > Articles

Getting Started

This chapter is from the book

This chapter is from the book

Main Function

The main function is the entry point function for executable crates. Execution starts at this function. The main function prototype has no parameters and returns either a unit or the Termination trait. In Rust, a unit is an empty tuple, which is (). This is the default return.

From the main function, you can return an integer value to the operating system using the Termination trait. The default implementation of the trait returns libc::EXIT_SUCCESS or libc::EXIT_FAILURE as an exit code. These values are most likely 1 and 0, but that is implementation specific.

Instead of returning from main, you can call the exit function (std::process::exit) to prematurely exit an application. However, the exit function immediately terminates the application without performing cleanup for the functions currently on the call stack, possibly preventing an orderly shutdown. The parameter of the exit function sets the process return value for the operating system.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.