Skip to content

Point at match discriminant on type error in match arm pattern #57279

Closed
@estebank

Description

@estebank

From https://www.reddit.com/r/rust/comments/abjzi7/expected_usize_found_enum_stdresultresult/

When encountering code like the following, where a match arm pattern is not the expected type, point at the match expression to provide more context

fn main() {
    let a: usize = 3;
    let b: usize = 3;
    let temp: usize = match a + b {
        Ok(num) => num,
        Err(_) => 0,
    };
}
error[E0308]: mismatched types
 --> src/main.rs:5:9
  |
4 |    let temp: usize = match a + b {
  |                            ----- this `match` expression is of type `usize`
5 |         Ok(num) => num,
  |         ^^^^^^^ expected usize, found enum `std::result::Result`
  |
  = note: expected type `usize`
             found type `std::result::Result<_, _>`

error[E0308]: mismatched types
 --> src/main.rs:6:9
  |
4 |    let temp: usize = match a + b {
  |                            ----- this `match` expression is of type `usize`
5 |         Ok(num) => num,
6 |         Err(_) => 0,
  |         ^^^^^^ expected usize, found enum `std::result::Result`
  |
  = note: expected type `usize`
             found type `std::result::Result<_, _>`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions