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

Catch-all match arm variable used without explanation #1868

Closed
xStrom opened this issue Mar 19, 2019 · 7 comments · Fixed by rust-lang/rust#88277
Closed

Catch-all match arm variable used without explanation #1868

xStrom opened this issue Mar 19, 2019 · 7 comments · Fixed by rust-lang/rust#88277
Milestone

Comments

@xStrom
Copy link

xStrom commented Mar 19, 2019

Chapter 6.2, at the end, does discuss the _ placeholder pattern. However it makes it sound super special, as opposed to just being an unused variable. No mention that you can use an arbitrary variable name to capture the value.

Then in chapter 9.2 in listing 9-5 the catch-all is used again but with a named variable other_error.

match error.kind() {
    ErrorKind::NotFound => match File::create("hello.txt") {
        Ok(fc) => fc,
        Err(e) => panic!("Tried to create file but there was a problem: {:?}", e),
    },
    other_error => panic!("There was a problem opening the file: {:?}", other_error),
},

The only explanation given is The last arm of the outer match stays the same so the program panics on any error besides the missing file error. It claims that the last arm stays the same, but that arm pattern is Err(error) in the previous listing (9-4). So while the functionality might be the same, the match arm pattern definitely isn't.

When I got to this as I was originally reading the book, this situation confused me. I was wondering if this is a special error matching generic or something. After some playing around with code I figured out you can just use arbitrary variables as the catch-all arm.

I think this situation could be greatly improved with just a minor note either in chapter 6.2 near the _ placeholder pattern to say that you can actually capture the value and use it. Or a similar description near listing 9-5, instead of the claim that the last arm stays the same.

@L0uisc
Copy link
Contributor

L0uisc commented Apr 8, 2019

I agree! I understand this for the first time now that I have read your post! Definitely something which can be improved.

@izackwu
Copy link

izackwu commented May 22, 2020

I agree!

I'm now reading Chapter 9.2 and find other_err confusing. So I google it and find this issue. Gladly I'm not the only one that gets confused here but sadly this part of The Book is so confusing!

@medeotl
Copy link

medeotl commented Nov 11, 2020

I agree, and is also find confusing this part: "The () is just the unit value, so nothing will happen in the _ case". The unit value was not properly explained before, and the sentence let me think that () work always (instead, it work for String but not for other types, such float)

@ttxs69
Copy link

ttxs69 commented Feb 24, 2021

agree

@marcus302
Copy link

I didn't get it while reading chapter 9 either, and Google led me here. Agree.

@carols10cents carols10cents added this to the ch6 milestone Jul 17, 2021
@kneep
Copy link

kneep commented Jul 25, 2021

Interesting post. I was led here after I googled for 'rust other_error'. I was confused when reading chapter 9.2 just like you guys were.

@SoloCoGo
Copy link

+1 google here

LeSeulArtichaut added a commit to LeSeulArtichaut/rust that referenced this issue Aug 25, 2021
Update books

## reference

1 commits in 4884fe45c14f8b22121760fb117181bb4da8dfe0..da6ea9b03f74cae0a292f40315723d7a3a973637
2021-07-28 21:31:28 -0700 to 2021-08-19 21:28:10 -0700
- Allow users to change status labels (rust-lang/reference#1083)

## book

7 commits in 7e49659102f0977d9142190e1ba23345c0f00eb1..687e21bde2ea10c261f79fa14797c5137425098d
2021-08-03 21:41:35 -0400 to 2021-08-18 20:48:38 -0400
- Small tweaks to Ferris size and position
- Retain previous height: auto just in case
- Shrink and move ferris when possible
- Snapshot chapter 6 for nostarch
- Demonstrate variable as catch-all for match. Fixes rust-lang/book#1868.
- Improve the if let example to have a binding pattern. Fixes rust-lang/book#1401.
- Fixes typo (rust-lang/book#2816)

## rust-by-example

1 commits in 0dc9cd4e89f00cb5230f120e1a083916386e422b..04f489c889235fe3b6dfe678ae5410d07deda958
2021-07-23 09:14:27 -0300 to 2021-08-17 08:01:20 -0300
- Grammar mistake (rust-lang/rust-by-example#1456)

## rustc-dev-guide

5 commits in c4644b4..cf0e151
2021-08-10 20:41:44 +0900 to 2021-08-22 11:47:02 -0300
- Fix typo “a Rc” → “an Rc” (rust-lang/rustc-dev-guide#1191)
- Expand THIR section with more details (rust-lang/rustc-dev-guide#1183)
- Remove docs for old -Z profile-queries flag
- update mdbook version to latest
- allow to quickly edit a page directly on github
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants