rust - 如何使用 lazy_static!在结构实现中?

标签 rust

#[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
        });
    }
}

lazy_static! 调用给我这个错误:

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)

我正在使用 Rust 1.32.0。

最佳答案

你不能。 lazy-static 通过创建一个新的隐藏类型以及该类型的 static 变量来工作。这些都不允许在 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

相反,在 impl block 外部或函数内部使用它。

另见:

关于rust - 如何使用 lazy_static!在结构实现中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55984384/

相关文章:

rust - 如何指定生命周期以使本地引用值与传入引用不同?

rust - 为什么存储到 AVX2 256 位向量和从中加载在调试和 Release模式下有不同的结果?

rust - 在作用域线程池中生成线程是否需要额外的时间?

使用rust 柴油机 : the trait bound `NaiveDateTime: Deserialize<' _>` is not satisfied

multithreading - 为什么我看不到 thread::spawn 内部打印的任何输出?

rust - 有没有其他方法可以接受整数或 float 输入而不是接受字符串输入并进行转换?

enums - 匹配内部可变枚举的预期方法是什么?

multithreading - 如何在 Rust 中访问外部线程局部全局变量?

rust - 删除不可变借用以进行可变借用

rust - Rust结构/枚举的可复制加密哈希