src/error-handling/panics.md
In case of a fatal runtime error, Rust triggers a "panic":
# // Copyright 2022 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
fn main() {
let v = vec![10, 20, 30];
dbg!(v[100]);
}
assert!) panic on failure.panic! macro.Vec::get) if crashing is not acceptable.By default, a panic will cause the stack to unwind. The unwinding can be caught:
# // Copyright 2022 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
use std::panic;
fn main() {
let result = panic::catch_unwind(|| "No problem here!");
dbg!(result);
let result = panic::catch_unwind(|| {
panic!("oh no!");
});
dbg!(result);
}
catch_unwind!panic = 'abort' is set in your Cargo.toml.