知识卡片
恐慌可展开或中止
panic 表示当前执行不能按原路径继续。栈展开沿函数栈回退并清理各层数据。若相应 Cargo profile 设置 panic = ‘abort’,则立即终止进程,操作系统负责回收进程内存,Rust 不再逐层清理。选择会影响二进制体积和清理行为。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 错误处理 Unrecoverable Errors with panic! · 查看原书依据
By default, when a panic occurs the program starts unwinding, which means Rust walks back up the stack and cleans up the data from each function it encounters. However, walking back and cleaning up is a lot of work. Rust, therefore, allows you to choose the alternative of immediately aborting, which ends the program without cleaning up.
参考来源
- 位置:《The Rust Programming Language(第2版)》第09章 错误处理(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c09.xhtml`,块 0009)
- 结论依据:原文把逐层清理与无逐层清理的中止直接对照。
- 原始内容:By default, when a panic occurs the program starts unwinding, which means Rust walks back up the stack and cleans up the data from each function it encounters. However, walking back and cleaning up is a lot of work. Rust, therefore, allows you to choose the alternative of immediately aborting, which ends the program without cleaning up.