Pārlūkot izejas kodu

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 gadi atpakaļ
vecāks
revīzija
11b0425b75
1 mainītis faili ar 21 papildinājumiem un 0 dzēšanām
  1. 21 0
      rust/pointers.rs

+ 21 - 0
rust/pointers.rs

@ -50,6 +50,20 @@ mod list_with_box {
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 67
    pub fn run() {
54 68
        println!("\nlist_with_box:");
55 69
@ -60,9 +74,16 @@ mod list_with_box {
60 74
        println!("l = {}", l);
61 75
        println!("cons(3, l)  = {:?}", cons(3, l));
62 76
        println!("cons(4, l2) = {:?}", cons(4, l2));
77
        println!("");
63 78
64 79
        // println!("cons_with_ref(3, &l) = {:?}", cons_with_ref(3, &l));
65 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