Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. use std::mem::MaybeUninit;
  2. use std::sync::Once;
  3.  
  4. static mut VAL: MaybeUninit<usize> = MaybeUninit::uninit();
  5. static INIT: Once = Once::new();
  6.  
  7. pub fn init() {
  8. INIT.call_once(|| {
  9. unsafe {
  10. VAL = MaybeUninit::new(5);
  11. }
  12. });
  13. }
  14.  
  15. fn main() {
  16. init(); // init is always called before access, I just care for the safety of using MaybeUninit here
  17.  
  18. let a = unsafe {
  19. VAL.assume_init()
  20. };
  21.  
  22. println!("Value: {}", a);
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement