Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. use std::fmt;
  2. use itertools::Itertools;
  3.  
  4. enum Object {
  5. Num(i32),
  6. List(Vec<Object>),
  7. }
  8.  
  9. impl fmt::Display for Object {
  10. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  11. match self {
  12. Object::Num(x) => write!(f, "{}", x),
  13. Object::List(lst) => write!(f, "({})", lst.iter().join(" "))
  14. }
  15. }
  16. }
  17.  
  18. fn main() {
  19. assert_eq!("()", Object::List(vec![]).to_string());
  20. assert_eq!("(42)", Object::List(vec![Object::Num(42)]).to_string());
  21. assert_eq!("(1 2 3)", Object::List((1..=3).map(Object::Num).collect()).to_string());
  22. assert_eq!(
  23. "((1) (2))",
  24. Object::List(vec![
  25. Object::List(vec![Object::Num(1)]),
  26. Object::List(vec![Object::Num(2)]),
  27. ]).to_string()
  28. )
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement