Guest User

Untitled

a guest
Oct 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #[macro_export]
  2. macro_rules! static_ty {
  3. ($ty: ty) => {
  4. use std::sync::Once;
  5.  
  6. impl $ty {
  7. fn static_ref(self) -> &'static $ty {
  8. static mut TY: Option<$ty> = None;
  9. static INIT: Once = Once::new();
  10. unsafe {
  11. INIT.call_once(|| TY = Some(self));
  12. TY.as_ref().unwrap()
  13. }
  14. }
  15. }
  16. };
  17. }
  18. fn main() {
  19. println!("Hello, world!");
  20.  
  21. let b = B(16);
  22. let c = b.static_ref();
  23. println!("{:?}",c);
  24.  
  25. bb(*c);
  26. let a = A;
  27.  
  28.  
  29. let mut set = std::collections::HashSet::with_capacity(1);
  30.  
  31.  
  32. println!("{:?}", set.insert(1));
  33.  
  34. std::mem::drop(a);
  35.  
  36. println!("{:?}", set.insert(1));
  37.  
  38. println!( "{}", format_args!("Hello [{x:6}] h!", x="x1234"));
  39. println!("Hello {:1$} h!", "x", 5);
  40. println!("Hello {1:0$} h!", 5, "x");
  41. println!("Hello {:width$} h!", "x", width = 5);
  42.  
  43.  
  44. println!("{}", format!("0x{:0>width$}",123, width=8));
  45.  
  46. println!("{}", format!("{:0>64x}",123));
  47.  
  48. println!("{}", format!("{}", u64::from_str_radix(&"0x3eeb35"[1..], 16).unwrap()));
  49.  
  50. }
  51.  
  52.  
  53. use std::collections::BTreeMap;
  54.  
  55. struct A();
  56.  
  57. impl Drop for A {
  58. fn drop(&mut self) {
  59. println!("drop A");
  60. }
  61. }
  62.  
  63. #[derive(Debug, Clone, Default)]
  64. struct B(usize);
  65.  
  66. static_ty!(B);
  67.  
  68. fn bb(bb: B) {
  69. println!("{:?}",bb);
  70. }
Add Comment
Please, Sign In to add comment