知识卡片
入口结果映射退出状态
main 返回 () 时不能让 Result 的 ? 提前传出错误。可将入口声明为 Result<(), E> 并在成功时返回 Ok(())。main 的 Ok 对应退出状态 0,Err 对应非零状态。Box 可以充当多种兼容错误的共同返回类型。
相关卡片 [[提前退出须匹配返回类型]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 错误处理 Where the ? Operator Can Be Used · 查看原书依据
When a main function returns a Result<(), E>, the executable will exit with a value of 0 if main returns Ok(()) and will exit with a nonzero value if main returns an Err value. Executables written in C return integers when they exit: programs that exit successfully return the integer 0, and programs that error return some integer other than 0.
参考来源
- 位置:《The Rust Programming Language(第2版)》第09章 错误处理(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c09.xhtml`,块 0132)
- 结论依据:原文把入口返回值与可执行程序的零或非零退出状态逐一对应。
- 原始内容:When a main function returns a Result<(), E>, the executable will exit with a value of 0 if main returns Ok(()) and will exit with a nonzero value if main returns an Err value. Executables written in C return integers when they exit: programs that exit successfully return the integer 0, and programs that error return some integer other than 0.