rustHow to return error from main in Rust
The main function in Rust is the entry point of a program and it is required to return a value of type std::process::ExitCode
. To return an error from main, you can use the std::process::exit
function to return a non-zero value.
Code example:
fn main() -> std::process::ExitCode {
// Do something
std::process::exit(1);
}
Output
The program will exit with a non-zero exit code.
Explanation of code parts:
fn main() -> std::process::ExitCode
: This declares the main function and specifies that it returns a value of typestd::process::ExitCode
.std::process::exit(1)
: This calls thestd::process::exit
function with a parameter of1
, which will cause the program to exit with a non-zero exit code.
Helpful links:
More of Rust
- How to use regex to match a double quote in Rust?
- How to match a URL with a regex in Rust?
- How to match the end of a line in a Rust regex?
- How to use regex with bytes in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to print a Rust HashMap?
- How to use an enum in a Rust HashMap?
- How to perform matrix operations in Rust?
- How to parse JSON string in Rust?
- How to get an element from a HashSet in Rust?
See more codes...