'How to create a Uuid with static lifetime?
I want to have a static Uuid in my Rust program but I am unable to figure out how to do it.
I tried this but it does not work
fn type_id() -> &'static uuid::Uuid {
let tmp = "9cb4cf49-5c3d-4647-83b0-8f3515da7be1".as_bytes();
let tmp = uuid::Uuid::from_slice(tmp).unwrap();
&tmp
}
error: cannot return reference to local variable `tmp`
returns a reference to data owned by the current function (rustc E0515)
Solution 1:[1]
Thanks @PitaJ. Using once_cell:Lazy works. I am still curious to know if there is a simpler way of doing this so I won't mark the answer as accepted yet.
static TYPE_ID: Lazy<uuid::Uuid> = Lazy::new(|| {
let tmp = "9cb4cf49-5c3d-4647-83b0-8f3515da7be1";
let tmp = uuid::Uuid::from_str(tmp).unwrap();
tmp
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
