SHARE
TWEET

Untitled

a guest Aug 23rd, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::fmt;
  2. use std::fmt::{Debug, Formatter};
  3. use std::mem::replace;
  4.  
  5. #[allow(dead_code)]
  6. enum Dir<T> {
  7.     None(T),
  8.     A(T),
  9.     B(T),
  10. }
  11.  
  12. impl Debug for Dir<String> {
  13.     fn fmt(&self, f: &mut Formatter) -> fmt::Result {
  14.         match self {
  15.             Dir::None(v) => write!(f, "None: {}", v),
  16.             Dir::A(v) => write!(f, "A: {}", v),
  17.             Dir::B(v) => write!(f, "B: {}", v),
  18.         }
  19.     }
  20. }
  21.  
  22. impl<T> Dir<T>
  23. where
  24.     T: Default,
  25. {
  26.     pub fn a(&mut self) -> &Self {
  27.         if let Dir::None(v) = self {
  28.             *self = Dir::A(replace(v, T::default()));
  29.         }
  30.         self
  31.     }
  32. }
  33.  
  34. fn main() {
  35.     let mut n = Dir::None("hello".to_string());
  36.     let mut a = Dir::A("bye".to_string());
  37.     std::mem::swap(&mut a, &mut n);
  38.     a.a();
  39.     dbg!(a, n);
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top