-
Book Overview & Buying
-
Table Of Contents
Rust for C++ Developers
By :
While the Rust compiler works very hard to keep problems from occurring, a significant part of Rust's safety strategy is deciding what happens when something does go wrong. For time failures from which a program cannot reasonably recover, Rust uses panics as an error-handling method of last resort. In this section, we'll examine what panics do, what kinds of issues can cause them, and how we can write programs to avoid them.
In Chapter 1, we talked about how the kinds of bugs caused by reading and writing outside of allocated memory have plagued C and C++ software. Buffer overruns are one of the most dangerous of all security issues. Let's see how Rust handles reading outside of an array:
fn main() {
let values = [0; 10];
let index = 11;
println!("Element at index {} is: {}", index, values[index]);
}
When run, this program produces the following output:
thread 'main&apos...