Skip to content

Catch-all match arm variable used without explanation #1868

@xStrom

Description

@xStrom

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions