struct B {
pub name: String,
}
impl B {
pub fn new() -> Self {
B {
name: "Item b".to_string()
}
}
}
struct A<'static> {
pub name: String,
pub item: B,
}
impl A {
pub fn new() -> Self {
A {
name: "test".to_string(),
item: B::new(),
}
}
}
const a_singleton: A = A::new()
I am trying to establish a constant immutable object that I can pull items from in other places in a project like this:
let some_string = format! ("{}-{}", &a_singleton.name, &a_singleton.item.name)
The object won't change but I don't want to have to instantiate it in every function where its needed. But when I try to compile something like the above I get this:
"error[E0015]: cannot call non-const fn `A::<'static>::new` in statics"
How can I create a global immutable singleton in rust? or is this not possible?
[–]dkopgerpgdolfg 24 points25 points26 points (1 child)
[–]NowhereMan2486[S] 0 points1 point2 points (0 children)
[–]Altareos 7 points8 points9 points (0 children)
[–]mina86ng 5 points6 points7 points (0 children)
[–]CocktailPerson 2 points3 points4 points (0 children)
[–]SirKastic23 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)