Просмотр исходного кода

demonstrate that plain cons moves the value it receives.

Lucas Stadler лет назад: 11
Родитель
Сommit
c9796e1f77
1 измененных файлов с 5 добавлено и 2 удалено
  1. 5 2
      rust/pointers.rs

+ 5 - 2
rust/pointers.rs

27
}
27
}
28
28
29
mod list_with_box {
29
mod list_with_box {
30
    #[deriving(Show)]
30
    #[deriving(Clone, Show)]
31
    pub enum List<A> {
31
    pub enum List<A> {
32
        Nil,
32
        Nil,
33
        Cons(A, Box<List<A>>)
33
        Cons(A, Box<List<A>>)
53
        println!("\nlist_with_box:");
53
        println!("\nlist_with_box:");
54
54
55
        let l: List<int> = cons(1, cons(2, Nil));
55
        let l: List<int> = cons(1, cons(2, Nil));
56
        // must be cloned before use as parameter to cons, which moves it
57
        let l2 = l.clone();
56
58
57
        println!("l = {}", l);
59
        println!("l = {}", l);
58
        println!("cons(3, l) = {:?}", cons(3, l));
60
        println!("cons(3, l)  = {:?}", cons(3, l));
61
        println!("cons(4, l2) = {:?}", cons(4, l2));
59
62
60
        // println!("cons_with_ref(3, &l) = {:?}", cons_with_ref(3, &l));
63
        // println!("cons_with_ref(3, &l) = {:?}", cons_with_ref(3, &l));
61
        //                                                        // ^    "use of moved value: `l`"
64
        //                                                        // ^    "use of moved value: `l`"