Skip to content
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

Fix ambiguous comment on 2.3 Arrays and Slices page #1885

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

maple19out
Copy link

@maple19out maple19out commented Sep 9, 2024

Comment from the page seems ambiguous since out of bound indexing on array with variables may cause runtime error.

// Out of bound indexing on array can cause runtime error
fn main() {
    let xs = [1, 2, 3, 4, 5];

    let idx = create_five();
    println!("{}", xs[idx]);
}

fn create_five() -> usize {
    5
}

The Rust Reference also mentions array indexing in the following link
https://doc.rust-lang.org/reference/expressions/array-expr.html#array-and-slice-indexing-expressions
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.

@rustbot
Copy link
Collaborator

rustbot commented Sep 9, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @marioidival (or someone else) some time within the next two weeks.

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