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

two more tries (boxed list and reference to boxed list).

both don't work as desired, boxed list moves the value and the reference
to it has the same error as all the other reference things.
Lucas Stadler лет назад: 11
Родитель
Сommit
11b0425b75
1 измененных файлов с 21 добавлено и 0 удалено
  1. 21 0
      rust/pointers.rs

+ 21 - 0
rust/pointers.rs

50
    //     }
50
    //     }
51
    // }
51
    // }
52
52
53
    pub fn cons_with_box<A>(x: A, xs: Box<List<A>>) -> List<A> {
54
        match *xs {
55
            Nil => Cons(x, xs),
56
            _   => Cons(x, xs)
57
        }
58
    }
59
60
    // pub fn cons_with_referenced_box<A>(x: A, xs: &Box<List<A>>) -> List<A> {
61
    //     match **xs {
62
    //         Nil     => Cons(x, box Nil),
63
    //         ref l   => Cons(x, box *l) // "cannot move ...", again
64
    //     }
65
    // }
66
53
    pub fn run() {
67
    pub fn run() {
54
        println!("\nlist_with_box:");
68
        println!("\nlist_with_box:");
55
69
60
        println!("l = {}", l);
74
        println!("l = {}", l);
61
        println!("cons(3, l)  = {:?}", cons(3, l));
75
        println!("cons(3, l)  = {:?}", cons(3, l));
62
        println!("cons(4, l2) = {:?}", cons(4, l2));
76
        println!("cons(4, l2) = {:?}", cons(4, l2));
77
        println!("");
63
78
64
        // println!("cons_with_ref(3, &l) = {:?}", cons_with_ref(3, &l));
79
        // println!("cons_with_ref(3, &l) = {:?}", cons_with_ref(3, &l));
65
        //                                                        // ^    "use of moved value: `l`"
80
        //                                                        // ^    "use of moved value: `l`"
81
82
        let lb: Box<List<int>> = box cons_with_box(1, box cons_with_box(2, box Nil));
83
        println!("lb = {:?}", lb);
84
        println!("cons_with_box(3, lb) = {:?}", cons_with_box(3, lb));
85
        // println!("cons_with_box(4, lb) = {:?}", cons_with_box(4, lb));
86
        //                                                       // ^     "use of moved value: `l`"
66
    }
87
    }
67
}
88
}
68
89