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

Could not force a panic with arrays without adding a trait #55

Open
tjarratt opened this issue Jul 30, 2020 · 2 comments
Open

Could not force a panic with arrays without adding a trait #55

tjarratt opened this issue Jul 30, 2020 · 2 comments

Comments

@tjarratt
Copy link

tjarratt commented Jul 30, 2020

Hello, I'd like to thank you for all the hard work that has gone into building this codebase. I've been telling myself for years now that I should learn rust, and with your help I've just started to write my first rust code. There's a lot I still don't know about rust, so this issue may be very naive.

After installing rust version 1.45.0 I began working through the koans until I got to arrays.rs which has a test to cause a panic with an invalid array index, as you can see below :

// Attempting to access an array at an index that is
// out of its bounds will cause an error. Let's cause
// that error in this example.
#[test]
#[should_panic]
#[allow(const_err)]
fn out_of_index() {
    let arr: [&'static str; 5] = ["rust", "is", "mostly", "for", "nerds"];
    arr[__];
}

However, I was unable to make this pass by simply using an invalid index such as 6. With a little bit of googling I was able to find out that you can add a trait to allow for unconditional panics.

// Attempting to access an array at an index that is
// out of its bounds will cause an error. Let's cause
// that error in this example.
#[test]
#[should_panic]
#[allow(const_err)]
#[alllow(unconditional_panic)] // <<<< this is the line I needed to add
fn out_of_index() {
    let arr: [&'static str; 5] = ["rust", "is", "mostly", "for", "nerds"];
    arr[666];
}

It seems like one of the following is true

  • I don't fully understand this koan
  • the trait [allow(const_err)] should be changed to #[alllow(unconditional_panic)]

If the second is true I would be more than happy to issue you a pull request.

buchananta added a commit to buchananta/rust-koans that referenced this issue Feb 5, 2021
@gentooza
Copy link

Also, const_err seems to be deprecated now rust-lang/rust#71800

@yrmo
Copy link

yrmo commented Mar 29, 2024

Thank you also ran into this issue, just in case anyone else copies and pastes this like me:

  • Remove the #[allow(const_err)] line
  • Typo in #[alllow(unconditional_panic)] should be allow

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

No branches or pull requests

3 participants