You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fnmain(){let x = vec![1,2,3];println!("Got: {}", x[88]);}
This code compiles just fine, but, when I run it, I get an ICE, or something similar:
thread '<main>' panicked at 'index out of bounds: the len is 3 but the index is 88', ../src/libcollections/vec.rs:1047
Note that this is the same problem as in #28217. Here are the lines it references:
#[stable(feature = "rust1", since = "1.0.0")]impl<T>Index<usize>forVec<T>{typeOutput = T;#[inline]fnindex(&self,index:usize) -> &T{// NB built-in indexing via `&[T]`&(**self)[index]}}
I'm not sure if this is intended behavior or not, but it seems odd.
The text was updated successfully, but these errors were encountered:
alexispurslane
changed the title
Rust panics if index out of bounds, not in a compile-time error.
Rust panics if index out of bounds
Sep 4, 2015
This is intended behaviour - the [x] form of indexing panics, because it cannot return a reference to an element that doesn't exist. .get(x) returns an Option<T>, so it doesn't panic. The panicking behaviour is documented at https://doc.rust-lang.org/nightly/reference.html#index-expressions .
This code compiles just fine, but, when I run it, I get an ICE, or something similar:
Note that this is the same problem as in #28217. Here are the lines it references:
I'm not sure if this is intended behavior or not, but it seems odd.
The text was updated successfully, but these errors were encountered: