Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::mem::MaybeUninit;
- use std::sync::Once;
- static mut VAL: MaybeUninit<usize> = MaybeUninit::uninit();
- static INIT: Once = Once::new();
- pub fn init() {
- INIT.call_once(|| {
- unsafe {
- VAL = MaybeUninit::new(5);
- }
- });
- }
- fn main() {
- init(); // init is always called before access, I just care for the safety of using MaybeUninit here
- let a = unsafe {
- VAL.assume_init()
- };
- println!("Value: {}", a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement