Skip to content

Update speaker notes for arrays.md with clarification on runtime check optimization #2781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 25, 2025

Conversation

daivinhtran
Copy link
Collaborator

According to the error I got from trying out-of-bound index, array accesses seem to be checked at compile time.

   Compiling playground v0.0.1 (/playground)
error: this operation will panic at runtime
 --> src/main.rs:3:5
  |
3 |     a[7] = 0;
  |     ^^^^ index out of bounds: the length is 5 but the index is 7
  |
  = note: `#[deny(unconditional_panic)]` on by default

error: could not compile `playground` (bin "playground") due to 1 previous error

According to the error I got 

```
   Compiling playground v0.0.1 (/playground)
error: this operation will panic at runtime
 --> src/main.rs:3:5
  |
3 |     a[7] = 0;
  |     ^^^^ index out of bounds: the length is 5 but the index is 7
  |
  = note: `#[deny(unconditional_panic)]` on by default

error: could not compile `playground` (bin "playground") due to 1 previous error
```

Array accesses seem to be checked at compile time.
@@ -27,8 +27,8 @@ fn main() {
later.

- Try accessing an out-of-bounds array element. Array accesses are checked at
runtime. Rust can usually optimize these checks away, and they can be avoided
using unsafe Rust.
compile time. Rust can usually optimize these checks away, and they can be
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original meaning was correct, and important. In more sophisticated cases than this simple example, Rust may very well leave the runtime bounds-checks in place, and these can result in a substantial performance overhead above unchecked languages like C++.

It's difficult to demonstrate this in a short code sample because the checks are often optimized away at compile time -- in this case to an unconditional panic. I suspect black_box could be useful to avoid this optimization. Do you want to try adjusting the example so that it shows the runtime bounds check?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see what you mean. I tried using black_box but it seems to not prevent the compile-time safety checks. I changed the out-of-bound index to be returned from a function and add that example to below the note. PTAL. Let me know what you think.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! The speaker note still says "compile time" though. It is also a good idea to add a sentence about how the optimization is so good that it's hard to give an example of runtime checks failing, and here requires a function to generate the index.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. PTAL again.

Copy link
Collaborator

@djmitche djmitche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks!!

@djmitche djmitche enabled auto-merge (squash) June 25, 2025 19:12
@djmitche
Copy link
Collaborator

(just needs formatting fixed)

@daivinhtran daivinhtran changed the title Fix typo in arrays.md Update speaker notes for arrays.md with clarification on runtime check optimization Jun 25, 2025
@djmitche djmitche merged commit f51f0db into main Jun 25, 2025
38 checks passed
@djmitche djmitche deleted the daivinhtran-patch-1 branch June 25, 2025 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants