@@ -3562,6 +3562,47 @@ fn test_intersperse_size_hint() {
3562
3562
assert_eq ! ( [ ] . iter( ) . intersperse( & ( ) ) . size_hint( ) , ( 0 , Some ( 0 ) ) ) ;
3563
3563
}
3564
3564
3565
+ #[ test]
3566
+ fn test_intersperse_fold ( ) {
3567
+ let v = ( 1 ..4 ) . intersperse ( 9 ) . fold ( Vec :: new ( ) , |mut acc, x| {
3568
+ acc. push ( x) ;
3569
+ acc
3570
+ } ) ;
3571
+ assert_eq ! ( v. as_slice( ) , [ 1 , 9 , 2 , 9 , 3 ] ) ;
3572
+
3573
+ let mut iter = ( 1 ..4 ) . intersperse ( 9 ) ;
3574
+ assert_eq ! ( iter. next( ) , Some ( 1 ) ) ;
3575
+ let v = iter. fold ( Vec :: new ( ) , |mut acc, x| {
3576
+ acc. push ( x) ;
3577
+ acc
3578
+ } ) ;
3579
+ assert_eq ! ( v. as_slice( ) , [ 9 , 2 , 9 , 3 ] ) ;
3580
+
3581
+ struct NoneAtStart ( i32 ) ; // Produces: None, Some(2), Some(3), None, ...
3582
+ impl Iterator for NoneAtStart {
3583
+ type Item = i32 ;
3584
+ fn next ( & mut self ) -> Option < i32 > {
3585
+ self . 0 += 1 ;
3586
+ Some ( self . 0 ) . filter ( |i| i % 3 != 1 )
3587
+ }
3588
+ }
3589
+
3590
+ let v = NoneAtStart ( 0 ) . intersperse ( 1000 ) . fold ( 0 , |a, b| a + b) ;
3591
+ assert_eq ! ( v, 0 ) ;
3592
+ }
3593
+
3594
+ #[ test]
3595
+ fn test_intersperse_collect_string ( ) {
3596
+ let contents = vec ! [ 1 , 2 , 3 ] ;
3597
+
3598
+ let contents_string = contents
3599
+ . into_iter ( )
3600
+ . map ( |id| id. to_string ( ) )
3601
+ . intersperse ( ", " . to_owned ( ) )
3602
+ . collect :: < String > ( ) ;
3603
+ assert_eq ! ( contents_string, "1, 2, 3" ) ;
3604
+ }
3605
+
3565
3606
#[ test]
3566
3607
fn test_fold_specialization_intersperse ( ) {
3567
3608
let mut iter = ( 1 ..2 ) . intersperse ( 0 ) ;
0 commit comments