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