在运行时很容易崩溃unwrap:
fn main() {
c().unwrap();
}
fn c() -> Option<i64> {
None
}
Run Code Online (Sandbox Code Playgroud)
结果:
Compiling playground v0.0.1 (file:///playground)
Running `target/debug/playground`
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', ../src/libcore/option.rs:325
note: Run with `RUST_BACKTRACE=1` for a backtrace.
error: Process didn't exit successfully: `target/debug/playground` (exit code: 101)
Run Code Online (Sandbox Code Playgroud)
是unwrap唯一专为快速测试和证明的概念?
我不能肯定"我的程序不会崩溃,所以我可以使用unwrap",如果我真的想panic!在运行时避免,我认为避免panic!是我们想要的生产应用程序.
换句话说,如果我使用,我可以说我的程序是可靠的unwrap吗?或者unwrap即使案件看似简单,我也必须避免吗?
我读到了这个答案:
当您确定没有错误时,最好使用它.
但我认为我不能"肯定".
我不认为这是一个意见问题,而是一个关于Rust核心和编程的问题.