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

Chapter 3.2: Example "Invalid Array Element Access" is incorrect #2417

Closed
andreastedile opened this issue Aug 1, 2020 · 5 comments · Fixed by #2446 or rust-lang/rust#81399
Closed
Assignees

Comments

@andreastedile
Copy link

Running the example's code doesn't produce the proposed result.
Because of this fact, the example's explanation is incorrect too.

Check related thread on r/learnrust.

@steveklabnik steveklabnik self-assigned this Aug 1, 2020
@Bert-King
Copy link

Bert-King commented Aug 20, 2020

Aha, I found out, too。
By the time I read this, I doubt it, because rust already knows a lot of information during compilation。

@Lonami
Copy link

Lonami commented Aug 23, 2020

Cross-posting from the above issue:

Rust got smarter constant evaluation since that was written it seems. The following doesn't fail to compile but errors at runtime:

fn get_index() -> usize {
    10
}

fn main() {
    let a = [1, 2, 3, 4, 5];
    let index = get_index();

    let element = a[index];

    println!("The value of element is: {}", element);
}

It does make the example a tiny bit worse, but showcases the point.

@Lonami
Copy link

Lonami commented Sep 9, 2020

Should this be closed given my comment #2446 (comment)?

@steveklabnik steveklabnik reopened this Sep 9, 2020
@Amjad50
Copy link
Contributor

Amjad50 commented Sep 9, 2020

Oh man, so sorry I didn't notice. I'll create a fix now. Thanks @Lonami

@carols10cents carols10cents pinned this issue Dec 29, 2020
steveklabnik added a commit that referenced this issue Jan 20, 2021
…ompiler

Fixes #2417. Get the index from user input instead of a const.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jan 26, 2021
Update books

## nomicon

7 commits in a8584998eacdea7106a1dfafcbf6c1c06fcdf925..bbf06ad39d1f45654047e9596b750cc6e6d1b693
2021-01-06 12:49:49 -0500 to 2021-01-22 07:07:31 -0800
- Fix alloc link in exotic-sizes for local docs (rust-lang/nomicon#255)
- Remove TODO
- Fix small punctuation error
- Arc revisions (Clone atomic explanation) (pt2/3(+?))
- Fix Arc Clone
- Arc revisions (pt1/2(+?))
- Simple Arc implementation (without Weak refs)

## reference

5 commits in 50af691f838937c300b47812d0507c6d88c14f97..f02b09eb6e8af340ad1256a54adb7aae2ff3163e
2021-01-12 21:19:20 -0800 to 2021-01-22 01:53:02 -0800
- Fix missing space (rust-lang/reference#941)
- Start documenting name resolution. (rust-lang/reference#937)
- Fix plural and delete spurious words in comparison ops (rust-lang/reference#932)
- Document execution order (rust-lang/reference#888)
- Compound operator expressions (rust-lang/reference#915)

## book

3 commits in ac57a0ddd23d173b26731ccf939f3ba729753275..e724bd826580ff95df48a8533af7dec1080693d4
2021-01-09 14:18:45 -0500 to 2021-01-20 08:19:49 -0600
- Fixes rust-lang/book#2417. Get the index from user input instead of a const. (rust-lang/book#2566)
- Turn off the playground in a bunch more lib.rs inclusions (rust-lang/book#2569)
- Merge pull request rust-lang/book#2567 from rust-lang/rust-1.49

## rust-by-example

1 commits in 03e23af01f0b4f83a3a513da280e1ca92587f2ec..f633769acef68574427a6fae6c06f13bc2199573
2021-01-09 10:20:28 -0300 to 2021-01-13 20:58:25 -0300
- Fixed styling on closure example (rust-lang/rust-by-example#1405)
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jan 26, 2021
Update books

## nomicon

7 commits in a8584998eacdea7106a1dfafcbf6c1c06fcdf925..bbf06ad39d1f45654047e9596b750cc6e6d1b693
2021-01-06 12:49:49 -0500 to 2021-01-22 07:07:31 -0800
- Fix alloc link in exotic-sizes for local docs (rust-lang/nomicon#255)
- Remove TODO
- Fix small punctuation error
- Arc revisions (Clone atomic explanation) (pt2/3(+?))
- Fix Arc Clone
- Arc revisions (pt1/2(+?))
- Simple Arc implementation (without Weak refs)

## reference

5 commits in 50af691f838937c300b47812d0507c6d88c14f97..f02b09eb6e8af340ad1256a54adb7aae2ff3163e
2021-01-12 21:19:20 -0800 to 2021-01-22 01:53:02 -0800
- Fix missing space (rust-lang/reference#941)
- Start documenting name resolution. (rust-lang/reference#937)
- Fix plural and delete spurious words in comparison ops (rust-lang/reference#932)
- Document execution order (rust-lang/reference#888)
- Compound operator expressions (rust-lang/reference#915)

## book

3 commits in ac57a0ddd23d173b26731ccf939f3ba729753275..e724bd826580ff95df48a8533af7dec1080693d4
2021-01-09 14:18:45 -0500 to 2021-01-20 08:19:49 -0600
- Fixes rust-lang/book#2417. Get the index from user input instead of a const. (rust-lang/book#2566)
- Turn off the playground in a bunch more lib.rs inclusions (rust-lang/book#2569)
- Merge pull request rust-lang/book#2567 from rust-lang/rust-1.49

## rust-by-example

1 commits in 03e23af01f0b4f83a3a513da280e1ca92587f2ec..f633769acef68574427a6fae6c06f13bc2199573
2021-01-09 10:20:28 -0300 to 2021-01-13 20:58:25 -0300
- Fixed styling on closure example (rust-lang/rust-by-example#1405)
@tculp
Copy link

tculp commented Mar 20, 2021

I just noticed that while cargo build now returns an error, cargo check doesn't. Is this just due caught in the final code generation step?

joeljpresent pushed a commit to joeljpresent/rust-book-fr that referenced this issue Apr 13, 2021
The compiler has gotten sufficiently smart regarding const evaluation to
catch many trivial instances of array-index-out-of-bounds at compile
time. However, the compiler can't read peoples' minds... yet.
@carols10cents carols10cents unpinned this issue Jul 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants