fn main() {
println!("{}", (1, (2, (4, Empty()))).get(2));
}
struct Empty ();
trait Cons<T> {
type Tail: Cons<T>;
fn get_head(&self) -> &T;
fn get_tail(&self) -> &Self::Tail;
fn get(&self, index: usize) -> &T {
match index {
0 => self.get_head(),
x => self.get_tail().get(x - 1)
}
}
}
impl<T, C:Cons<T>> Cons<T> for (T, C) {
type Tail = C;
fn get_head(&self) -> &T {
&self.0
}
fn get_tail(&self) -> &Self::Tail {
&self.1
}
}
impl<T> Cons<T> for Empty {
type Tail = Empty;
fn get_head(&self) -> &T {
panic!()
}
fn get_tail(&self) -> &Self::Tail {
panic!()
}
}
Comments