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 replace a capture group using Rust regex?
- How to match whitespace with a regex in Rust?
- How to replace strings using Rust regex?
- How to match the end of a line in a Rust regex?
- How to match digits with regex in Rust?
- How to use regex captures in Rust?
- How to perform matrix operations in Rust?
- How to split a string with Rust regex?
- How to use regex lookbehind in Rust?
- How to get a capture group using Rust regex?
See more codes...