Guest User

Untitled

a guest
Dec 10th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. enum Thing {
  2. V1,
  3. V2,
  4. Other(u32)
  5. }
  6.  
  7. impl From<Thing> for Option<u32> {
  8. fn from(thing: Thing) -> Option<u32> {
  9. match thing {
  10. Thing::V1 | Thing::V2 => None,
  11. Thing::Other(x) => Some(x),
  12. }
  13. }
  14. }
  15.  
  16. fn main() {
  17. let x = Thing::V1;
  18. let y = Thing::V2;
  19. let z = Thing::Other(5);
  20. let zz: Option<u32> = z.into();
  21.  
  22. println!(
  23. "{:?}, {:?}, {:?}",
  24. Option::<u32>::from(x),
  25. Option::<u32>::from(y),
  26. zz
  27. );
  28. }
Add Comment
Please, Sign In to add comment