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 get a capture group using Rust regex?
- How to use regex with bytes in Rust?
- How to match the end of a line in a Rust regex?
- Hashshet example in Rust
- How to create a new Rust HashMap with values?
- How do I use a variable from another file in Rust?
- How to get the length of a Rust HashMap?
- How to get an element from a HashSet in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to convert the keys of a Rust HashMap to a vector?
See more codes...