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::exitfunction with a parameter of1, which will cause the program to exit with a non-zero exit code.
Helpful links:
More of Rust
- How to match whitespace with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to use regex lookahead in Rust?
- How can I create a nullable string in Rust?
- How to replace all matches using Rust regex?
- Regex example to match multiline string in Rust?
- How to use regex lookbehind in Rust?
- How to match the end of a line in a Rust regex?
- How to use regex captures in Rust?
- How to replace strings using Rust regex?
See more codes...