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
if2 exercise and option1 fix #383
if2 exercise and option1 fix #383
Conversation
… an exercise about Option
@@ -18,6 +18,6 @@ fn main() { | |||
((iter * 5) + 2) / (4 * 16) | |||
}; | |||
|
|||
numbers[iter] = number_to_add; | |||
numbers[iter as usize] = number_to_add; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to discuss this; here's the compiler message you get:
21 | numbers[iter] = number_to_add;
| ^^^^^^^^^^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `std::slice::SliceIndex<[std::option::Option<u16>]>` is not implemented for `u16`
= note: required because of the requirements on the impl of `std::ops::Index<u16>` for `[std::option::Option<u16>]`
The message makes it sound like it could have something to do with a SliceIndex of Option, but really this is about the type of the slice index. While this is certainly a learning opportunity, it's not one that has anything to do with Option usage.
I don't feel super strongly about it, but it was the place I got stuck the longest in the exercises!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as the creator of the option1 exercise, you are correct and this change looks good. Looks like I forgot to add the "as usize" myself
Thanks all! |
@all-contributors add @wrobstory for code |
@fmoko I've put up a pull request to add @wrobstory! 🎉 |
This PR implements two things:
if2
exercise that shows the type of compiler message you get if your conditional branches don't return the same type as well as the usage ofelse if