3 #![feature(slice_patterns)]
7 fn foldl<T, U, F>(values: &[T],
11 U: Clone+Debug, T:Debug,
14 &[ref head, ref tail @ ..] =>
15 foldl(tail, function(initial, head), function),
18 let res = initial.clone(); res
23 fn foldr<T, U, F>(values: &[T],
31 &[ref head @ .., ref tail] =>
32 foldr(head, function(tail, initial), function),
35 let res = initial.clone(); res
41 let x = &[1, 2, 3, 4, 5];
43 let product = foldl(x, 1, |a, b| a * *b);
44 assert_eq!(product, 120);
46 let sum = foldr(x, 0, |a, b| *a + b);