在Rust 1.0中有没有返回退出代码的方法?
我try 了env::set_exit_status(exit_code);
次,但这会产生一个编译器错误.
还有一个问题:Exit Rust program early,这是类似的,但询问的情况下,该进程必须提前退出.
EDIT: I'm looking for a solution that will also allow the process to tidy up the stack, call destructors, etc.
在Rust 1.0中有没有返回退出代码的方法?
我try 了env::set_exit_status(exit_code);
次,但这会产生一个编译器错误.
还有一个问题:Exit Rust program early,这是类似的,但询问的情况下,该进程必须提前退出.
EDIT: I'm looking for a solution that will also allow the process to tidy up the stack, call destructors, etc.
在@FrancisGagné 's answer条注释的基础上构建,如果您正在搜索与C's return exit_code
等效的注释,可以通过以下方式人工构建:
fn main() {
let exit_code = real_main();
std::process::exit(exit_code);
}
fn real_main() -> i32 {
// the real program here
}
这样,程序的所有对象都将在real_main()
函数的范围内,您可以安全地使用main
中的return exit_code;
,同时仍能正确运行所有析构函数.