rust-lang-nursery / lazy-static.rs

A small macro for defining lazy evaluated static variables in Rust.
Apache License 2.0
1.9k stars 108 forks source link

cannot use match on lazy_static initiated self-defined enum #183

Closed orbli closed 3 years ago

orbli commented 3 years ago

as title.

cargo version
cargo 1.46.0 (149022b1d 2020-07-17)

rustc --version
rustc 1.46.0 (04488afe3 2020-08-24)

code sample:

#[macro_use]
extern crate lazy_static;

lazy_static! {
    static ref THE_TYPE: TheType = TheType::A;
}

enum TheType {
    A,
}

fn main() {
    match THE_TYPE {
        TheType::A => {
            println!("working");
        },
        _ => {
            println!("fail");
        }
    }
}

compiler console:

cargo run
   Compiling rust_test v0.1.0 (/root/rust_test)
error[E0308]: mismatched types
  --> src/main.rs:14:9
   |
9  |     A,
   |     - unit variant defined here
...
13 |     match THE_TYPE {
   |           -------- this expression has type `THE_TYPE`
14 |         TheType::A => {
   |         ^^^^^^^^^^ expected struct `THE_TYPE`, found enum `TheType`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `rust_test`.

To learn more, run the command again with --verbose.
okkero commented 3 years ago

You have to dereference it: match *THE_TYPE { ... }

orbli commented 3 years ago

thanks!