Selaa lähdekoodia

cons that does not copy objects.

Lucas Stadler 11 vuotta sitten
vanhempi
commit
d3ec0107b9
1 muutettua tiedostoa jossa 7 lisäystä ja 7 poistoa
  1. 7 7
      rust/list.rs

+ 7 - 7
rust/list.rs

4
    Cons(A, Box<List<A>>)
4
    Cons(A, Box<List<A>>)
5
}
5
}
6
6
7
pub fn cons<A>(x: A, xs: List<A>) -> List<A> {
8
    match xs {
9
        Nil => Cons(x, box Nil),
10
        l => Cons(x, box l)
7
pub fn cons<A>(x: A, xs: Box<List<A>>) -> Box<List<A>> {
8
    match *xs {
9
        Nil => box Cons(x, box Nil),
10
        l => box Cons(x, box l)
11
    }
11
    }
12
}
12
}
13
13
19
}
19
}
20
20
21
pub fn main() {
21
pub fn main() {
22
	let nil: List<int> = Nil;
23
	let ns:  List<int> = cons(1i, cons(2, cons(3, Nil)));
22
	let nil: List<int>      = Nil;
23
	let ns:  Box<List<int>> = cons(1i, cons(2, cons(3, box Nil)));
24
24
25
	println!("nil = {}, ns = {}", nil, ns);
25
	println!("nil = {}, ns = {}", nil, ns);
26
	println!("first(nil) = {}, first(ns) = {}", first(nil), first(ns));
26
	println!("first(nil) = {}, first(ns) = {}", first(nil), first(*ns));
27
}
27
}