2

Shouldn't the variant of an enum be fully qualified?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Zhe Chen
  • 2,918
  • 4
  • 23
  • 39

1 Answers1

6

The variant of an enum can be imported into the current namespace too.

enum Test {
    A,
    B,
}

use Test::{A,B};

fn main() {
    let t = A;
    match t {
        A => println!("A"),
        B => println!("B"),
    };
}

And it turns out that the prelude of Rust, on top of importing Option, also imports Some and None.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • Could you explain the `prelude` a little more? I think it's useful in this context and I really like the question :) – Lukas Kalbertodt Apr 02 '16 at 19:23
  • @LukasKalbertodt: Well, just as I was looking for an existing question explaining the prelude that I could link to I found a clear duplicate http://stackoverflow.com/questions/30546644/why-dont-options-some-and-none-variants-need-to-be-qualified/30546727#30546727 ... – Matthieu M. Apr 03 '16 at 10:57
  • 1
    @LukasKalbertodt: Actually, I could not find a single explanation of what the prelude was and what its role was on SO, so I gave it a shot in a dedicated question [here](http://stackoverflow.com/q/36384840/147192). I would appreciate your review, if you have the time. – Matthieu M. Apr 03 '16 at 11:29
  • Oops, perfect duplicate, indeed :P Thanks for opening a new question, I upvoted an commented. – Lukas Kalbertodt Apr 03 '16 at 13:19