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

rustdocs "The Book" Chapter 3 inaccuracy. #75822

Closed
kkh3049 opened this issue Aug 22, 2020 · 3 comments
Closed

rustdocs "The Book" Chapter 3 inaccuracy. #75822

kkh3049 opened this issue Aug 22, 2020 · 3 comments
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools

Comments

@kkh3049
Copy link

kkh3049 commented Aug 22, 2020

I'm not sure if this is the right place or labels for this and didn't find any specific format for errors in "The book", but hope someone can direct me if needed. I'm new to rust completely but excited to learn and contribute where I can.

In "The book" chapter 3 section two, towards the end of the section (https://doc.rust-lang.org/book/ch03-02-data-types.html#invalid-array-element-access), it claims that a certain code snippet will compile fine, but panic at runtime, but the code snippet actually doesn't compile. I imagine it's important to mention that panicking happens at runtime with similar conditions that aren't catchable at compile-time, but it's inaccurate. Here's the error that I get.

$ cargo run
   Compiling book_error v0.1.0 (D:\DDocuments\PersonalProjects\LearningRust\book_error)
error: this operation will panic at runtime
 --> src\main.rs:5:19
  |
5 |     let element = a[index];
  |                   ^^^^^^^^ index out of bounds: the len is 5 but the index is 10
  |
  = note: `#[deny(unconditional_panic)]` on by default

error: aborting due to previous error

error: could not compile `book_error`.

To learn more, run the command again with --verbose.

Here's the error mentioned in the book:

$ cargo run
   Compiling arrays v0.1.0 (file:///projects/arrays)
    Finished dev [unoptimized + debuginfo] target(s) in 0.31s
     Running `target/debug/arrays`
thread 'main' panicked at 'index out of bounds: the len is 5 but the index is 10', src/main.rs:5:19
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
@csmoe csmoe added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Aug 23, 2020
@Lonami
Copy link
Contributor

Lonami commented Aug 23, 2020

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.

@workingjubilee
Copy link
Member

This should probably be moved to https://github.com/rust-lang/book

@kkh3049
Copy link
Author

kkh3049 commented Aug 23, 2020

Turns out there's an open issue about this at rust-lang/book#2417
I'll close this. Thanks!

@kkh3049 kkh3049 closed this as completed Aug 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools
Projects
None yet
Development

No branches or pull requests

4 participants