Why aren't enum variant names auto imported into the `match` scope?

⚓ Rust    📅 2025-06-26    👤 surdeus    👁️ 6      

surdeus

Warning

This post was published 47 days ago. The information described in this article may have changed.

Suppose you have an enum:

enum Enum {
  Foo,
  Bar,
}
let x : Enum = ...;

and you want to match on x. By default, you either use qualified variant name,

match x {
  Enum::Foo => ...,
  Enum::Bar => ...,
}

or you import those names into the current scope,

use Enum::*;
match x {
  Foo => ...,
  Bar => ...,
}

But I do not see why those names are not being imported automatically only in the match's scope. That is, allow this to work by default:

match x {
  Foo => ...,
  Bar => ...,
}

And the scope outside the match is not changed. This is both cleaner and more convenient.

My question is why it doesn't work this way? Is there something I missed? Thanks.

5 posts - 3 participants

Read full topic

🏷️ rust_feed