It's easy to crash at runtime with unwrap
:
fn main() {
c().unwrap();
}
fn c() -> Option<i64> {
None
}
Result:
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)
Is unwrap
only designed for quick tests and proofs-of-concept?
I can not affirm "My program will not crash here, so I can use unwrap
" if I really want to avoid panic!
at runtime, and I think avoiding panic!
is what we want in a production application.
In other words, can I say my program is reliable if I use unwrap
? Or must I avoid unwrap
even if the case seems simple?
I read this answer:
It is best used when you are positively sure that you don't have an error.
But I don't think I can be "positively sure".
I don't think this is an opinion question, but a question about Rust core and programming.