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

feat(options2): better test for layered_option #1487

Merged
merged 1 commit into from
May 17, 2023

Conversation

lionel-rowe
Copy link
Contributor

The existing test can be solved with the following:

while let Some(integer) = optional_integers.pop() {
    assert_eq!(integer.unwrap(), range);

Similarly with expect(...), unwrap_or(0), unwrap_or_default(), etc. However, none of these solutions use the learning point of stacking Option<T>s.

The updated test can only be solved by stacking Option<T>s:

while let Some(Some(integer)) = optional_integers.pop() {
    assert_eq!(integer, cursor);

With the updated test, using unwrap or expect will panic when it hits the None value, and using unwrap_or or unwrap_or_default will cause the final assert_eq!(cursor, 0) to panic.

The existing test can be solved with the following:

```rs
        while let Some(integer) = optional_integers.pop() {
            assert_eq!(integer.unwrap(), range);
```

Similarly with `expect(...)`, `unwrap_or(0)`, `unwrap_or_default()`, etc. However, none of these solutions use the learning point of stacking `Option<T>`s.

The updated test can _only_ be solved by stacking `Option<T>`s:

```rs
        while let Some(Some(integer)) = optional_integers.pop() {
            assert_eq!(integer, cursor);
```

With the updated test, using `unwrap` or `expect` will panic when it hits the `None` value, and using `unwrap_or` or `unwrap_or_default` will cause the final `assert_eq!(cursor, 0)`  to panic.
@shadows-withal shadows-withal merged commit 81cd979 into rust-lang:main May 17, 2023
@shadows-withal
Copy link
Member

@all-contributors please add @lionel-rowe for content

@allcontributors
Copy link
Contributor

@shadows-withal

I've put up a pull request to add @lionel-rowe! 🎉

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.

2 participants