expr.array.index.const example code is outdated

Open
#2,119 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
50/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Stale
Tech stack
rust
Domain
documentation

Research direction

Open the array-indexing section at the linked Rust Reference URL and run its example with the documentation's interactive feature to reproduce the current warnings. Replace the middle example with a case where the compiler cannot determine the bounds at compile time, then verify that the explanation and resulting output distinguish compile-time warnings from runtime panics.

Written by the indexing model from the issue text.

Description

A-expressions Language Cleanup

At https://doc.rust-lang.org/reference/expressions/array-expr.html#r-expr.array.index.const the code is introduced with

Array access is a constant expression, so bounds can be checked at compile-time with a constant index value. Otherwise a check will be performed at run-time that will put the thread in a panicked state if it fails.

and then includes, in relevant part,

// lint is deny by default.
#![warn(unconditional_panic)]

//...
let x = (["a", "b"])[10]; // warning: index out of bounds

let n = 10;
let y = (["a", "b"])[n];  // panics

let arr = ["a", "b"];
arr[10];                  // warning: index out of bounds

The implication is that the two so-commented examples will have compile-time warnings, while the middle example with just "panics" will not have a compile time warning, but only a run-time panic (the cases with compile-time warnings will also have panics). But, using the "Run this code" feature in the interactive documentation, the compilation output log is

   Compiling playground v0.0.1 (/playground)
warning: this operation will panic at runtime
  --> src/main.rs:11:9
   |
11 | let x = (["a", "b"])[10]; // warning: index out of bounds
   |         ^^^^^^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 10
   |
note: the lint level is defined here
  --> src/main.rs:4:9
   |
 4 | #![warn(unconditional_panic)]
   |         ^^^^^^^^^^^^^^^^^^^

warning: this operation will panic at runtime
  --> src/main.rs:14:9
   |
14 | let y = (["a", "b"])[n];  // panics
   |         ^^^^^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 10

warning: this operation will panic at runtime
  --> src/main.rs:17:1
   |
17 | arr[10];                  // warning: index out of bounds
   | ^^^^^^^ index out of bounds: the length is 2 but the index is 10

warning: `playground` (bin "playground") generated 3 warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.64s
     Running `target/debug/playground`

thread 'main' (13) panicked at src/main.rs:11:9:
index out of bounds: the len is 2 but the index is 10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

specifically,

  --> src/main.rs:14:9
   |
14 | let y = (["a", "b"])[n];  // panics
   |         ^^^^^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 10

This indicates that the compiler now emits a warning in the case were it presumably did not use to.

Can the example be updated to use a case where the compiler cannot perform the checks at compile-time?

Dominant language
Rust
Stars
1.6k
Forks
607
Avg merge
1d 1h
Merged PRs (30d)
12

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rust-lang/reference

All issues in rust-lang/reference

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.