Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fmt;
- use std::fmt::{Debug, Formatter};
- use std::mem::replace;
- #[allow(dead_code)]
- enum Dir<T> {
- None(T),
- A(T),
- B(T),
- }
- impl Debug for Dir<String> {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- match self {
- Dir::None(v) => write!(f, "None: {}", v),
- Dir::A(v) => write!(f, "A: {}", v),
- Dir::B(v) => write!(f, "B: {}", v),
- }
- }
- }
- impl<T> Dir<T>
- where
- T: Default,
- {
- pub fn a(&mut self) -> &Self {
- if let Dir::None(v) = self {
- *self = Dir::A(replace(v, T::default()));
- }
- self
- }
- }
- fn main() {
- let mut n = Dir::None("hello".to_string());
- let mut a = Dir::A("bye".to_string());
- std::mem::swap(&mut a, &mut n);
- a.a();
- dbg!(a, n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement