#[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。
答案 0 :(得分:0)
您不能。惰性静态通过创建新的隐藏类型以及该类型的impl
变量来工作。不允许在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
相反,请在{{1}}块之外或函数内部使用它。
另请参阅: