1
#[derive(Serialize)]
pub struct SLPInfoVersion {
    name: String,
    protocol: i32
}

impl SLPInfoVersion {
    lazy_static! {
        pub static ref V1_13: MyStruct = (SLPInfoVersion {
            name: "1.13".to_string(),
            protocol: 404
        });
    }
}

The lazy_static! call gives me this error:

error: expected one of `(`, `async`, `const`, `default`, `existential`, `extern`, `fn`, `type`, or `unsafe`, found `struct`                      
   --> src\protocol\packet\mod.rs:244:2                                                                                                          
    |                                                                                                                                            
244 |         lazy_static! {                                                                                                                     
    |    _____^                                                                                                                                  
    |   |_____|                                                                                                                                  
    |  ||_____|                                                                                                                                  
    | |||                                                                                                                                        
245 | |||         pub static ref V1_13: SLPInfoVersion = (SLPInfoVersion {                                                                       
246 | |||             name: "1.13".to_string(),                                                                                                  
247 | |||             protocol: 404                                                                                                              
248 | |||         });                                                                                                                            
249 | |||     }                                                                                                                                  
    | |||     ^                                                                                                                                  
    | |||_____|                                                                                                                                  
    | ||______expected one of 9 possible tokens here                                                                                             
    | |_______unexpected token                                                                                                                   
    |         in this macro invocation                                                                                                           
    |                                                                                                                                            
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

I am using Rust 1.32.0.

1 Answer 1

1

You cannot. lazy-static works by creating a new hidden type as well as a static variable of that type. Neither of those are allowed to be created in an impl block:

struct Foo;

impl Foo {
    static BAR: u8;

    struct Bar;
}
error: expected one of `async`, `const`, `crate`, `default`, `existential`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `static`
 --> src/lib.rs:4:5
  |
3 | impl Foo {
  |           - expected one of 11 possible tokens here
4 |     static BAR: u8;
  |     ^^^^^^ unexpected token

error: expected one of `async`, `const`, `crate`, `default`, `existential`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `struct`
 --> src/lib.rs:6:5
  |
4 |     static BAR: u8;
  |                    - expected one of 11 possible tokens here
5 |     
6 |     struct Bar;
  |     ^^^^^^ unexpected token

Instead, use it outside of the impl block or inside of a function.

See also:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.