]> git.lizzy.rs Git - rust.git/blobdiff - src/test/run-pass/vec-matching-fold.rs
cleanup: s/impl Copy/#[derive(Copy)]/g
[rust.git] / src / test / run-pass / vec-matching-fold.rs
index 63914a8df31cdc52e7474c5ce18646d45dd45d9f..576601833330d4f746cd0965b6b78fdba325baf8 100644 (file)
 
 #![feature(advanced_slice_patterns)]
 
-fn foldl<T,U:Clone>(values: &[T],
-                    initial: U,
-                    function: |partial: U, element: &T| -> U)
-                    -> U {
+fn foldl<T, U, F>(values: &[T],
+                  initial: U,
+                  mut function: F)
+                  -> U where
+    U: Clone,
+    F: FnMut(U, &T) -> U,
+{
     match values {
         [ref head, tail..] =>
             foldl(tail, function(initial, head), function),
@@ -21,10 +24,13 @@ fn foldl<T,U:Clone>(values: &[T],
     }
 }
 
-fn foldr<T,U:Clone>(values: &[T],
-                    initial: U,
-                    function: |element: &T, partial: U| -> U)
-                    -> U {
+fn foldr<T, U, F>(values: &[T],
+                  initial: U,
+                  mut function: F)
+                  -> U where
+    U: Clone,
+    F: FnMut(&T, U) -> U,
+{
     match values {
         [head.., ref tail] =>
             foldr(head, function(tail, initial), function),
@@ -33,7 +39,7 @@ fn foldr<T,U:Clone>(values: &[T],
 }
 
 pub fn main() {
-    let x = [1i, 2, 3, 4, 5];
+    let x = &[1i, 2, 3, 4, 5];
 
     let product = foldl(x, 1i, |a, b| a * *b);
     assert_eq!(product, 120);