File tree 4 files changed +35
-5
lines changed
4 files changed +35
-5
lines changed Original file line number Diff line number Diff line change @@ -980,8 +980,8 @@ impl<T> [T] {
980
980
/// assert!(!v.contains(&50));
981
981
/// ```
982
982
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
983
- pub fn contains ( & self , x : & T ) -> bool
984
- where T : PartialEq
983
+ pub fn contains < U > ( & self , x : & U ) -> bool
984
+ where U : ? Sized , T : PartialEq < U >
985
985
{
986
986
core_slice:: SliceExt :: contains ( self , x)
987
987
}
Original file line number Diff line number Diff line change @@ -1051,6 +1051,20 @@ fn test_shrink_to_fit() {
1051
1051
assert_eq ! ( xs, ( 0 ..100 ) . collect:: <Vec <_>>( ) ) ;
1052
1052
}
1053
1053
1054
+ #[ test]
1055
+ fn test_contains ( ) {
1056
+ let numbers = [ 1 , 2 , 3 ] ;
1057
+ assert ! ( numbers. contains( & 2 ) ) ;
1058
+ assert ! ( !numbers. contains( & 4 ) ) ;
1059
+
1060
+ let strings = vec ! [ String :: from( "AB" ) , String :: from( "CD" ) ] ;
1061
+ assert ! ( strings. contains( & String :: from( "AB" ) ) ) ;
1062
+ assert ! ( !strings. contains( & String :: from( "A" ) ) ) ;
1063
+ assert ! ( strings. contains( & "AB" ) ) ;
1064
+ assert ! ( !strings. contains( & "BC" ) ) ;
1065
+ assert ! ( strings. contains( "AB" ) ) ;
1066
+ }
1067
+
1054
1068
#[ test]
1055
1069
fn test_starts_with ( ) {
1056
1070
assert ! ( b"foobar" . starts_with( b"foo" ) ) ;
Original file line number Diff line number Diff line change @@ -192,7 +192,7 @@ pub trait SliceExt {
192
192
fn as_mut_ptr ( & mut self ) -> * mut Self :: Item ;
193
193
194
194
#[ stable( feature = "core" , since = "1.6.0" ) ]
195
- fn contains ( & self , x : & Self :: Item ) -> bool where Self :: Item : PartialEq ;
195
+ fn contains < U > ( & self , x : & U ) -> bool where U : ? Sized , Self :: Item : PartialEq < U > ;
196
196
197
197
#[ stable( feature = "core" , since = "1.6.0" ) ]
198
198
fn starts_with ( & self , needle : & [ Self :: Item ] ) -> bool where Self :: Item : PartialEq ;
@@ -618,8 +618,8 @@ impl<T> SliceExt for [T] {
618
618
}
619
619
620
620
#[ inline]
621
- fn contains ( & self , x : & T ) -> bool where T : PartialEq {
622
- self . iter ( ) . any ( |elt| * x == * elt )
621
+ fn contains < U > ( & self , x : & U ) -> bool where U : ? Sized , T : PartialEq < U > {
622
+ self . iter ( ) . any ( |e| e == x )
623
623
}
624
624
625
625
#[ inline]
Original file line number Diff line number Diff line change @@ -1608,6 +1608,22 @@ mod traits {
1608
1608
fn ne ( & self , other : & str ) -> bool { !( * self ) . eq ( other) }
1609
1609
}
1610
1610
1611
+ #[ unstable( feature = "str_str_ref_partialeq" , issue = "46934" ) ]
1612
+ impl < ' a > PartialEq < & ' a str > for str {
1613
+ #[ inline]
1614
+ fn eq ( & self , other : & & ' a str ) -> bool { self == * other }
1615
+ #[ inline]
1616
+ fn ne ( & self , other : & & ' a str ) -> bool { self != * other }
1617
+ }
1618
+
1619
+ #[ unstable( feature = "str_str_ref_partialeq" , issue = "46934" ) ]
1620
+ impl < ' a > PartialEq < str > for & ' a str {
1621
+ #[ inline]
1622
+ fn eq ( & self , other : & str ) -> bool { * self == other }
1623
+ #[ inline]
1624
+ fn ne ( & self , other : & str ) -> bool { * self != other }
1625
+ }
1626
+
1611
1627
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1612
1628
impl Eq for str { }
1613
1629
You can’t perform that action at this time.
0 commit comments