]> git.lizzy.rs Git - rust.git/blob - src/test/ui/array-slice-vec/vec-matching-fold.rs
slice_patterns: remove gates in tests
[rust.git] / src / test / ui / array-slice-vec / vec-matching-fold.rs
1 // run-pass
2
3 use std::fmt::Debug;
4
5 fn foldl<T, U, F>(values: &[T],
6                   initial: U,
7                   mut function: F)
8                   -> U where
9     U: Clone+Debug, T:Debug,
10     F: FnMut(U, &T) -> U,
11 {    match values {
12         &[ref head, ref tail @ ..] =>
13             foldl(tail, function(initial, head), function),
14         &[] => {
15             // FIXME: call guards
16             let res = initial.clone(); res
17         }
18     }
19 }
20
21 fn foldr<T, U, F>(values: &[T],
22                   initial: U,
23                   mut function: F)
24                   -> U where
25     U: Clone,
26     F: FnMut(&T, U) -> U,
27 {
28     match values {
29         &[ref head @ .., ref tail] =>
30             foldr(head, function(tail, initial), function),
31         &[] => {
32             // FIXME: call guards
33             let res = initial.clone(); res
34         }
35     }
36 }
37
38 pub fn main() {
39     let x = &[1, 2, 3, 4, 5];
40
41     let product = foldl(x, 1, |a, b| a * *b);
42     assert_eq!(product, 120);
43
44     let sum = foldr(x, 0, |a, b| *a + b);
45     assert_eq!(sum, 15);
46 }