Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created May 26, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmizu/6c6582ee68594ac87f7c to your computer and use it in GitHub Desktop.
Save kmizu/6c6582ee68594ac87f7c to your computer and use it in GitHub Desktop.
use std::fmt;
use List::{Cons, Nil};
enum List<T> {
Cons(T, Box<List<T>>),
Nil
}
impl<T> fmt::Display for List<T> where T : fmt::Display {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Cons(ref head, ref tail) =>
{
write!(f, "{head} :: ", head = head);
write!(f, "{tail} ", tail = tail)
}
Nil => write!(f, "Nil")
}
}
}
fn new_list<T>(args: Vec<T>) -> Box<List<T>> where T: Copy {
let mut vec = args;
let mut x: Box<List<T>> = Box::new(Nil);
while vec.len() > 0 {
match vec.pop() {
Some(e) => x = Box::new(Cons(e, x)),
None => {}
}
}
x
}
fn main() {
let list = new_list(vec!["A", "B", "C"]);
println!("Hello, {rust}", rust=list);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment