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
Running this code using cargo run produces the following result:
$ cargo run
Compiling arrays v0.1.0 (file:///projects/arrays)
error: this operation will panic at runtime
--> src/main.rs:5:19
|
5 | let element = a[index];
| ^^^^^^^^ index out of bounds: the length is 5 but the index is 10
|
= note: `#[deny(unconditional_panic)]` on by default
error: aborting due to previous error
error: could not compile `arrays`
To learn more, run the command again with --verbose.
The compilation didn’t produce any errors, but the program resulted in a runtime error and didn’t exit successfully. When you attempt to access an element using indexing, Rust will check that the index you’ve specified is less than the array length. If the index is greater than or equal to the array length, Rust will panic.
The text says that this issue is caught at runtime and not compile time. That seems confusing given that running cargo build will also catch this issue, which doesn't run the binary. The error message says that this operation will panic at runtime, which implies that this error was caught before runtime.
Secondary question: This error is caught when running cargo build, but NOT when running cargo check. I'm guessing this is because of some kind of constant folding optimization that cargo check doesn't perform. Wondering if it's something that we can catch with cargo check as well.
The text was updated successfully, but these errors were encountered:
The text says that this issue is caught at runtime and not compile time. That seems confusing given that running
cargo build
will also catch this issue, which doesn't run the binary. The error message says thatthis operation will panic at runtime
, which implies that this error was caught before runtime.Secondary question: This error is caught when running
cargo build
, but NOT when runningcargo check
. I'm guessing this is because of some kind of constant folding optimization thatcargo check
doesn't perform. Wondering if it's something that we can catch withcargo check
as well.The text was updated successfully, but these errors were encountered: