I want to use Rust and once_cell to implement some static const struct instance, and one static const vector contains these static struct instances.
Here's the sample code:
use once_cell::sync::Lazy;
pub struct Kind {
name: String,
value: i32,
}
impl Kind {
pub fn new(name: &str, value: i32) -> Kind {
Kind {name: String::from(name), value}
}
}
const HYBRID: Lazy<Kind> = Lazy::new(|| Kind::new("a", 1));
const BOND: Lazy<Kind> = Lazy::new(|| Kind::new("b", 2));
// other instances
static KINDS: Lazy<Vec<Kind>> = Lazy::new(|| {
vec![
HYBRID,
BOND,
// other instances,
]
});
Here's the compiler error:
error[E0308]: mismatched types
--> src/quant/fund/data/fund_info.rs:50:9
|
50 | HYBRID,
| ^^^^^^ expected struct `Kind`, found struct `once_cell::sync::Lazy`
|
= note: expected struct `Kind`
found struct `once_cell::sync::Lazy<Kind>`
How should I get the real Kind
inside Lazy
?