I\'ve got a struct defined that has a function which defines a static lifetime:
impl MyStruct {
pub fn doSomething(&\'static self) {
// Some
The naive way to do this is with a static
variable, but it will require unsafe code if you need to actually set the value inside your main
function:
static mut OBJ: MyStruct = MyStruct;
fn main() {
unsafe {
OBJ = MyStruct {};
OBJ.doSomething();
}
}
It's also unsafe
to do pretty much anything with a mutable static thereafter.
The much better way to do it is to let a library (lazy_static) take care of the unsafe code.
#[macro_use]
extern crate lazy_static;
fn main() {
lazy_static!{
static ref OBJ: MyStruct = MyStruct {};
}
OBJ.doSomething();
}