use std::rc::Rc;

enum List {
    Cons(i32, Rc<List>),
    Nil,
}

use List::{ Cons, Nil, };

fn print_list(list: &List) {
    match list {
        Cons(i, list) => {
            println!("i -> {}", i);
            print_list(&list);
        },
        Nil => (),
    }
}

fn print_list2(mut list: &List) {
    while let Cons(i, next_list) = list {
        println!("i --> {}", i);
        list = next_list;
    }
}

fn main() {
    let a = Rc::new(Cons(5, Rc::new(Cons(10, Rc::new(Nil)))));
    let b = Cons(3, Rc::clone(&a));
    let c = Cons(4, a.clone());

    println!("print a");
    print_list(&a);
    println!("print b");
    print_list(&b);
    println!("print c");
    print_list2(&c);
}
