Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fmt;
- use itertools::Itertools;
- enum Object {
- Num(i32),
- List(Vec<Object>),
- }
- impl fmt::Display for Object {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self {
- Object::Num(x) => write!(f, "{}", x),
- Object::List(lst) => write!(f, "({})", lst.iter().join(" "))
- }
- }
- }
- fn main() {
- assert_eq!("()", Object::List(vec![]).to_string());
- assert_eq!("(42)", Object::List(vec![Object::Num(42)]).to_string());
- assert_eq!("(1 2 3)", Object::List((1..=3).map(Object::Num).collect()).to_string());
- assert_eq!(
- "((1) (2))",
- Object::List(vec![
- Object::List(vec![Object::Num(1)]),
- Object::List(vec![Object::Num(2)]),
- ]).to_string()
- )
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement