@@ -5,6 +5,7 @@ use std::panic;
5
5
use std:: rc:: Rc ;
6
6
use std:: sync:: atomic:: { Ordering :: Relaxed , AtomicUsize } ;
7
7
use std:: thread;
8
+ use std:: f64:: NAN ;
8
9
9
10
use rand:: { Rng , RngCore , thread_rng} ;
10
11
use rand:: seq:: SliceRandom ;
@@ -835,88 +836,88 @@ fn test_splitator() {
835
836
let xs = & [ 1 , 2 , 3 , 4 , 5 ] ;
836
837
837
838
let splits: & [ & [ _ ] ] = & [ & [ 1 ] , & [ 3 ] , & [ 5 ] ] ;
838
- assert_eq ! ( xs. split( |x| * x % 2 == 0 ) . collect:: <Vec <_>>( ) , splits) ;
839
+ assert_eq ! ( xs. split( |x: & i32 | * x % 2 == 0 ) . collect:: <Vec <_>>( ) , splits) ;
839
840
let splits: & [ & [ _ ] ] = & [ & [ ] , & [ 2 , 3 , 4 , 5 ] ] ;
840
- assert_eq ! ( xs. split( |x| * x == 1 ) . collect:: <Vec <_>>( ) , splits) ;
841
+ assert_eq ! ( xs. split( |x: & i32 | * x == 1 ) . collect:: <Vec <_>>( ) , splits) ;
841
842
let splits: & [ & [ _ ] ] = & [ & [ 1 , 2 , 3 , 4 ] , & [ ] ] ;
842
- assert_eq ! ( xs. split( |x| * x == 5 ) . collect:: <Vec <_>>( ) , splits) ;
843
+ assert_eq ! ( xs. split( |x: & i32 | * x == 5 ) . collect:: <Vec <_>>( ) , splits) ;
843
844
let splits: & [ & [ _ ] ] = & [ & [ 1 , 2 , 3 , 4 , 5 ] ] ;
844
- assert_eq ! ( xs. split( |x| * x == 10 ) . collect:: <Vec <_>>( ) , splits) ;
845
+ assert_eq ! ( xs. split( |x: & i32 | * x == 10 ) . collect:: <Vec <_>>( ) , splits) ;
845
846
let splits: & [ & [ _ ] ] = & [ & [ ] , & [ ] , & [ ] , & [ ] , & [ ] , & [ ] ] ;
846
- assert_eq ! ( xs. split( |_| true ) . collect:: <Vec <& [ i32 ] >>( ) , splits) ;
847
+ assert_eq ! ( xs. split( |_: & i32 | true ) . collect:: <Vec <& [ i32 ] >>( ) , splits) ;
847
848
848
849
let xs: & [ i32 ] = & [ ] ;
849
850
let splits: & [ & [ i32 ] ] = & [ & [ ] ] ;
850
- assert_eq ! ( xs. split( |x| * x == 5 ) . collect:: <Vec <& [ i32 ] >>( ) , splits) ;
851
+ assert_eq ! ( xs. split( |x: & i32 | * x == 5 ) . collect:: <Vec <& [ i32 ] >>( ) , splits) ;
851
852
}
852
853
853
854
#[ test]
854
855
fn test_splitnator ( ) {
855
856
let xs = & [ 1 , 2 , 3 , 4 , 5 ] ;
856
857
857
858
let splits: & [ & [ _ ] ] = & [ & [ 1 , 2 , 3 , 4 , 5 ] ] ;
858
- assert_eq ! ( xs. splitn( 1 , |x| * x % 2 == 0 ) . collect:: <Vec <_>>( ) , splits) ;
859
+ assert_eq ! ( xs. splitn( 1 , |x: & i32 | * x % 2 == 0 ) . collect:: <Vec <_>>( ) , splits) ;
859
860
let splits: & [ & [ _ ] ] = & [ & [ 1 ] , & [ 3 , 4 , 5 ] ] ;
860
- assert_eq ! ( xs. splitn( 2 , |x| * x % 2 == 0 ) . collect:: <Vec <_>>( ) , splits) ;
861
+ assert_eq ! ( xs. splitn( 2 , |x: & i32 | * x % 2 == 0 ) . collect:: <Vec <_>>( ) , splits) ;
861
862
let splits: & [ & [ _ ] ] = & [ & [ ] , & [ ] , & [ ] , & [ 4 , 5 ] ] ;
862
- assert_eq ! ( xs. splitn( 4 , |_| true ) . collect:: <Vec <_>>( ) , splits) ;
863
+ assert_eq ! ( xs. splitn( 4 , |_: & i32 | true ) . collect:: <Vec <_>>( ) , splits) ;
863
864
864
865
let xs: & [ i32 ] = & [ ] ;
865
866
let splits: & [ & [ i32 ] ] = & [ & [ ] ] ;
866
- assert_eq ! ( xs. splitn( 2 , |x| * x == 5 ) . collect:: <Vec <_>>( ) , splits) ;
867
+ assert_eq ! ( xs. splitn( 2 , |x: & i32 | * x == 5 ) . collect:: <Vec <_>>( ) , splits) ;
867
868
}
868
869
869
870
#[ test]
870
871
fn test_splitnator_mut ( ) {
871
872
let xs = & mut [ 1 , 2 , 3 , 4 , 5 ] ;
872
873
873
874
let splits: & [ & mut [ _ ] ] = & [ & mut [ 1 , 2 , 3 , 4 , 5 ] ] ;
874
- assert_eq ! ( xs. splitn_mut( 1 , |x| * x % 2 == 0 ) . collect:: <Vec <_>>( ) ,
875
+ assert_eq ! ( xs. splitn_mut( 1 , |x: & i32 | * x % 2 == 0 ) . collect:: <Vec <_>>( ) ,
875
876
splits) ;
876
877
let splits: & [ & mut [ _ ] ] = & [ & mut [ 1 ] , & mut [ 3 , 4 , 5 ] ] ;
877
- assert_eq ! ( xs. splitn_mut( 2 , |x| * x % 2 == 0 ) . collect:: <Vec <_>>( ) ,
878
+ assert_eq ! ( xs. splitn_mut( 2 , |x: & i32 | * x % 2 == 0 ) . collect:: <Vec <_>>( ) ,
878
879
splits) ;
879
880
let splits: & [ & mut [ _ ] ] = & [ & mut [ ] , & mut [ ] , & mut [ ] , & mut [ 4 , 5 ] ] ;
880
- assert_eq ! ( xs. splitn_mut( 4 , |_| true ) . collect:: <Vec <_>>( ) , splits) ;
881
+ assert_eq ! ( xs. splitn_mut( 4 , |_: & i32 | true ) . collect:: <Vec <_>>( ) , splits) ;
881
882
882
883
let xs: & mut [ i32 ] = & mut [ ] ;
883
884
let splits: & [ & mut [ i32 ] ] = & [ & mut [ ] ] ;
884
- assert_eq ! ( xs. splitn_mut( 2 , |x| * x == 5 ) . collect:: <Vec <_>>( ) , splits) ;
885
+ assert_eq ! ( xs. splitn_mut( 2 , |x: & i32 | * x == 5 ) . collect:: <Vec <_>>( ) , splits) ;
885
886
}
886
887
887
888
#[ test]
888
889
fn test_rsplitator ( ) {
889
890
let xs = & [ 1 , 2 , 3 , 4 , 5 ] ;
890
891
891
892
let splits: & [ & [ _ ] ] = & [ & [ 5 ] , & [ 3 ] , & [ 1 ] ] ;
892
- assert_eq ! ( xs. split( |x| * x % 2 == 0 ) . rev( ) . collect:: <Vec <_>>( ) , splits) ;
893
+ assert_eq ! ( xs. split( |x: & i32 | * x % 2 == 0 ) . rev( ) . collect:: <Vec <_>>( ) , splits) ;
893
894
let splits: & [ & [ _ ] ] = & [ & [ 2 , 3 , 4 , 5 ] , & [ ] ] ;
894
- assert_eq ! ( xs. split( |x| * x == 1 ) . rev( ) . collect:: <Vec <_>>( ) , splits) ;
895
+ assert_eq ! ( xs. split( |x: & i32 | * x == 1 ) . rev( ) . collect:: <Vec <_>>( ) , splits) ;
895
896
let splits: & [ & [ _ ] ] = & [ & [ ] , & [ 1 , 2 , 3 , 4 ] ] ;
896
- assert_eq ! ( xs. split( |x| * x == 5 ) . rev( ) . collect:: <Vec <_>>( ) , splits) ;
897
+ assert_eq ! ( xs. split( |x: & i32 | * x == 5 ) . rev( ) . collect:: <Vec <_>>( ) , splits) ;
897
898
let splits: & [ & [ _ ] ] = & [ & [ 1 , 2 , 3 , 4 , 5 ] ] ;
898
- assert_eq ! ( xs. split( |x| * x == 10 ) . rev( ) . collect:: <Vec <_>>( ) , splits) ;
899
+ assert_eq ! ( xs. split( |x: & i32 | * x == 10 ) . rev( ) . collect:: <Vec <_>>( ) , splits) ;
899
900
900
901
let xs: & [ i32 ] = & [ ] ;
901
902
let splits: & [ & [ i32 ] ] = & [ & [ ] ] ;
902
- assert_eq ! ( xs. split( |x| * x == 5 ) . rev( ) . collect:: <Vec <& [ i32 ] >>( ) , splits) ;
903
+ assert_eq ! ( xs. split( |x: & i32 | * x == 5 ) . rev( ) . collect:: <Vec <& [ i32 ] >>( ) , splits) ;
903
904
}
904
905
905
906
#[ test]
906
907
fn test_rsplitnator ( ) {
907
908
let xs = & [ 1 , 2 , 3 , 4 , 5 ] ;
908
909
909
910
let splits: & [ & [ _ ] ] = & [ & [ 1 , 2 , 3 , 4 , 5 ] ] ;
910
- assert_eq ! ( xs. rsplitn( 1 , |x| * x % 2 == 0 ) . collect:: <Vec <_>>( ) , splits) ;
911
+ assert_eq ! ( xs. rsplitn( 1 , |x: & i32 | * x % 2 == 0 ) . collect:: <Vec <_>>( ) , splits) ;
911
912
let splits: & [ & [ _ ] ] = & [ & [ 5 ] , & [ 1 , 2 , 3 ] ] ;
912
- assert_eq ! ( xs. rsplitn( 2 , |x| * x % 2 == 0 ) . collect:: <Vec <_>>( ) , splits) ;
913
+ assert_eq ! ( xs. rsplitn( 2 , |x: & i32 | * x % 2 == 0 ) . collect:: <Vec <_>>( ) , splits) ;
913
914
let splits: & [ & [ _ ] ] = & [ & [ ] , & [ ] , & [ ] , & [ 1 , 2 ] ] ;
914
- assert_eq ! ( xs. rsplitn( 4 , |_| true ) . collect:: <Vec <_>>( ) , splits) ;
915
+ assert_eq ! ( xs. rsplitn( 4 , |_: & i32 | true ) . collect:: <Vec <_>>( ) , splits) ;
915
916
916
917
let xs: & [ i32 ] = & [ ] ;
917
918
let splits: & [ & [ i32 ] ] = & [ & [ ] ] ;
918
- assert_eq ! ( xs. rsplitn( 2 , |x| * x == 5 ) . collect:: <Vec <& [ i32 ] >>( ) , splits) ;
919
- assert ! ( xs. rsplitn( 0 , |x| * x % 2 == 0 ) . next( ) . is_none( ) ) ;
919
+ assert_eq ! ( xs. rsplitn( 2 , |x: & i32 | * x == 5 ) . collect:: <Vec <& [ i32 ] >>( ) , splits) ;
920
+ assert ! ( xs. rsplitn( 0 , |x: & i32 | * x % 2 == 0 ) . next( ) . is_none( ) ) ;
920
921
}
921
922
922
923
#[ test]
@@ -1209,14 +1210,14 @@ fn test_ends_with() {
1209
1210
#[ test]
1210
1211
fn test_mut_splitator ( ) {
1211
1212
let mut xs = [ 0 , 1 , 0 , 2 , 3 , 0 , 0 , 4 , 5 , 0 ] ;
1212
- assert_eq ! ( xs. split_mut( |x| * x == 0 ) . count( ) , 6 ) ;
1213
- for slice in xs. split_mut ( |x| * x == 0 ) {
1213
+ assert_eq ! ( xs. split_mut( |x: & i32 | * x == 0 ) . count( ) , 6 ) ;
1214
+ for slice in xs. split_mut ( |x : & i32 | * x == 0 ) {
1214
1215
slice. reverse ( ) ;
1215
1216
}
1216
1217
assert ! ( xs == [ 0 , 1 , 0 , 3 , 2 , 0 , 0 , 5 , 4 , 0 ] ) ;
1217
1218
1218
1219
let mut xs = [ 0 , 1 , 0 , 2 , 3 , 0 , 0 , 4 , 5 , 0 , 6 , 7 ] ;
1219
- for slice in xs. split_mut ( |x| * x == 0 ) . take ( 5 ) {
1220
+ for slice in xs. split_mut ( |x : & i32 | * x == 0 ) . take ( 5 ) {
1220
1221
slice. reverse ( ) ;
1221
1222
}
1222
1223
assert ! ( xs == [ 0 , 1 , 0 , 3 , 2 , 0 , 0 , 5 , 4 , 0 , 6 , 7 ] ) ;
@@ -1225,7 +1226,7 @@ fn test_mut_splitator() {
1225
1226
#[ test]
1226
1227
fn test_mut_splitator_rev ( ) {
1227
1228
let mut xs = [ 1 , 2 , 0 , 3 , 4 , 0 , 0 , 5 , 6 , 0 ] ;
1228
- for slice in xs. split_mut ( |x| * x == 0 ) . rev ( ) . take ( 4 ) {
1229
+ for slice in xs. split_mut ( |x : & i32 | * x == 0 ) . rev ( ) . take ( 4 ) {
1229
1230
slice. reverse ( ) ;
1230
1231
}
1231
1232
assert ! ( xs == [ 1 , 2 , 0 , 4 , 3 , 0 , 0 , 6 , 5 , 0 ] ) ;
@@ -1646,3 +1647,56 @@ fn repeat_generic_slice() {
1646
1647
vec![ 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 ]
1647
1648
) ;
1648
1649
}
1650
+
1651
+ #[ test]
1652
+ fn test_match_indices_simple ( ) {
1653
+ let haystack = & [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 , 8.0 , 2.0 , 3.0 , 2.0 , 4.0 , 8.0 ] [ ..] ;
1654
+ let needle = & [ 2.0 , 3.0 ] [ ..] ;
1655
+
1656
+ assert_eq ! ( haystack. match_indices( needle) . collect:: <Vec <_>>( ) , vec![
1657
+ ( 1 , needle) ,
1658
+ ( 8 , needle) ,
1659
+ ] ) ;
1660
+ }
1661
+
1662
+ #[ test]
1663
+ fn test_match_indices_nan_haystack ( ) {
1664
+ let haystack = & [ 1.0 , 2.0 , NAN , 1.0 , 2.0 , NAN , 1.0 , NAN , NAN , NAN , 2.0 , 1.0 , 2.0 ] [ ..] ;
1665
+ let needle = & [ 1.0 , 2.0 ] [ ..] ;
1666
+
1667
+ assert_eq ! ( haystack. match_indices( needle) . collect:: <Vec <_>>( ) , vec![
1668
+ ( 0 , needle) ,
1669
+ ( 3 , needle) ,
1670
+ ( 11 , needle) ,
1671
+ ] ) ;
1672
+ }
1673
+
1674
+ #[ test]
1675
+ fn test_match_indices_nan_needle ( ) {
1676
+ let haystack = & [ 1.0 , 2.0 , NAN , 1.0 , 2.0 , NAN , 1.0 , NAN , NAN , NAN , 2.0 , 1.0 , 2.0 ] [ ..] ;
1677
+ let needle = & [ 2.0 , NAN ] [ ..] ;
1678
+
1679
+ assert_eq ! ( haystack. match_indices( needle) . collect:: <Vec <_>>( ) , vec![
1680
+ ] ) ;
1681
+ }
1682
+
1683
+ #[ test]
1684
+ fn test_match_indices_negative_zero ( ) {
1685
+ let haystack = & [ -0.0 , 0.0 , 0.0 , -0.0 , 0.0 ] [ ..] ;
1686
+ let needle = & [ 0.0 , -0.0 ] [ ..] ;
1687
+
1688
+ assert_eq ! ( haystack. match_indices( needle) . collect:: <Vec <_>>( ) , vec![
1689
+ ( 0 , needle) ,
1690
+ ( 2 , needle) ,
1691
+ ] ) ;
1692
+ }
1693
+
1694
+ #[ test]
1695
+ fn test_replace ( ) {
1696
+ let haystack = & b" empowering everyone to build reliable and efficient software." [ ..] ;
1697
+
1698
+ assert_eq ! (
1699
+ haystack. replace( & b" e" [ ..] , b" **E**" ) ,
1700
+ b" **E**mpowering **E**veryone to build reliable and **E**fficient software." . to_vec( )
1701
+ ) ;
1702
+ }
0 commit comments