Why Does an Out-of-Bounds Array Index Fail at Compile Time in One Case but at Runtime in Another?
The Rust Programming Language Forum
Why Does an Out-of-Bounds Array Index Fail at Compile Time in One Case but at Runtime in Another?
I'm learning Rust and trying to understand the difference between compile-time errors and runtime panics when accessing array elements. Consider the following code: fn main() { let a: [u32; 5] = [1, 2, 3, 4, 5]; let b = [1, 2, 3]; println!("b is {:?}", b); println!("b at 0 is {}", b[4]); println!("Please enter an array index."); let mut index = String::new(); std::io::stdin() .read_line(&mut index) .expect("Failed to read line"); let index: ...
0 comments
No comments yet.