I have the following function:
fn load_data() -> Result<MyData> {
// ...
Ok(my_data)
}
I want to call this function only once during the execution of my program and save the result in a static
variable.
It'd be nice if the result is of type Option<MyData>
, so the consumer just needs to check if the data is there or not, regardless of the reason of why it's not there.
I have came up with the following:
lazy_static! {
static ref DATA: Option<&'static MyData> = load_data().ok().as_ref();
}
However, that line fails:
35 | static ref DATA: Option<&'static MyData> = load_data().ok().as_ref();
| ----------------^^^^^^^^^
| | returns a value referencing data owned by the current function
| |
| |
| temporary value created here
Is there a way to achieve what I want or am I conceptually wrong?