@@ -100,13 +100,20 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
100
100
/// ```
101
101
/// #![feature(range_contains)]
102
102
///
103
+ /// use std::f32;
104
+ ///
103
105
/// assert!(!(3..5).contains(&2));
104
106
/// assert!( (3..5).contains(&3));
105
107
/// assert!( (3..5).contains(&4));
106
108
/// assert!(!(3..5).contains(&5));
107
109
///
108
110
/// assert!(!(3..3).contains(&3));
109
111
/// assert!(!(3..2).contains(&3));
112
+ ///
113
+ /// assert!( (0.0..1.0).contains(&0.5));
114
+ /// assert!(!(0.0..1.0).contains(&f32::NAN));
115
+ /// assert!(!(0.0..f32::NAN).contains(&0.5));
116
+ /// assert!(!(f32::NAN..1.0).contains(&0.5));
110
117
/// ```
111
118
#[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
112
119
pub fn contains < U > ( & self , item : & U ) -> bool
@@ -191,9 +198,15 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
191
198
/// ```
192
199
/// #![feature(range_contains)]
193
200
///
201
+ /// use std::f32;
202
+ ///
194
203
/// assert!(!(3..).contains(&2));
195
204
/// assert!( (3..).contains(&3));
196
205
/// assert!( (3..).contains(&1_000_000_000));
206
+ ///
207
+ /// assert!( (0.0..).contains(&0.5));
208
+ /// assert!(!(0.0..).contains(&f32::NAN));
209
+ /// assert!(!(f32::NAN..).contains(&0.5));
197
210
/// ```
198
211
#[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
199
212
pub fn contains < U > ( & self , item : & U ) -> bool
@@ -266,9 +279,15 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
266
279
/// ```
267
280
/// #![feature(range_contains)]
268
281
///
282
+ /// use std::f32;
283
+ ///
269
284
/// assert!( (..5).contains(&-1_000_000_000));
270
285
/// assert!( (..5).contains(&4));
271
286
/// assert!(!(..5).contains(&5));
287
+ ///
288
+ /// assert!( (..1.0).contains(&0.5));
289
+ /// assert!(!(..1.0).contains(&f32::NAN));
290
+ /// assert!(!(..f32::NAN).contains(&0.5));
272
291
/// ```
273
292
#[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
274
293
pub fn contains < U > ( & self , item : & U ) -> bool
@@ -330,6 +349,8 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
330
349
/// ```
331
350
/// #![feature(range_contains)]
332
351
///
352
+ /// use std::f32;
353
+ ///
333
354
/// assert!(!(3..=5).contains(&2));
334
355
/// assert!( (3..=5).contains(&3));
335
356
/// assert!( (3..=5).contains(&4));
@@ -338,6 +359,11 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
338
359
///
339
360
/// assert!( (3..=3).contains(&3));
340
361
/// assert!(!(3..=2).contains(&3));
362
+ ///
363
+ /// assert!( (0.0..=1.0).contains(&1.0));
364
+ /// assert!(!(0.0..=1.0).contains(&f32::NAN));
365
+ /// assert!(!(0.0..=f32::NAN).contains(&0.0));
366
+ /// assert!(!(f32::NAN..=1.0).contains(&1.0));
341
367
/// ```
342
368
#[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
343
369
pub fn contains < U > ( & self , item : & U ) -> bool
@@ -447,9 +473,15 @@ impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
447
473
/// ```
448
474
/// #![feature(range_contains)]
449
475
///
476
+ /// use std::f32;
477
+ ///
450
478
/// assert!( (..=5).contains(&-1_000_000_000));
451
479
/// assert!( (..=5).contains(&5));
452
480
/// assert!(!(..=5).contains(&6));
481
+ ///
482
+ /// assert!( (..=1.0).contains(&1.0));
483
+ /// assert!(!(..=1.0).contains(&f32::NAN));
484
+ /// assert!(!(..=f32::NAN).contains(&0.5));
453
485
/// ```
454
486
#[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
455
487
pub fn contains < U > ( & self , item : & U ) -> bool
@@ -559,34 +591,40 @@ pub trait RangeBounds<T: ?Sized> {
559
591
/// ```
560
592
fn end ( & self ) -> Bound < & T > ;
561
593
594
+
562
595
/// Returns `true` if `item` is contained in the range.
596
+ ///
597
+ /// # Examples
598
+ ///
599
+ /// ```
600
+ /// #![feature(range_contains)]
601
+ ///
602
+ /// use std::f32;
603
+ ///
604
+ /// assert!( (3..5).contains(&4));
605
+ /// assert!(!(3..5).contains(&2));
606
+ ///
607
+ /// assert!( (0.0..1.0).contains(&0.5));
608
+ /// assert!(!(0.0..1.0).contains(&f32::NAN));
609
+ /// assert!(!(0.0..f32::NAN).contains(&0.5));
610
+ /// assert!(!(f32::NAN..1.0).contains(&0.5));
563
611
#[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
564
612
fn contains < U > ( & self , item : & U ) -> bool
565
613
where
566
614
T : PartialOrd < U > ,
567
615
U : ?Sized ,
568
616
{
569
- match self . start ( ) {
570
- Included ( ref start) => if * start > item {
571
- return false ;
572
- } ,
573
- Excluded ( ref start) => if * start >= item {
574
- return false ;
575
- } ,
576
- Unbounded => ( ) ,
577
- } ;
578
-
579
- match self . end ( ) {
580
- Included ( ref end) => if * end < item {
581
- return false ;
582
- } ,
583
- Excluded ( ref end) => if * end <= item {
584
- return false ;
585
- } ,
586
- Unbounded => ( ) ,
587
- }
588
-
589
- true
617
+ ( match self . start ( ) {
618
+ Included ( ref start) => * start <= item,
619
+ Excluded ( ref start) => * start < item,
620
+ Unbounded => true ,
621
+ } )
622
+ &&
623
+ ( match self . end ( ) {
624
+ Included ( ref end) => * end >= item,
625
+ Excluded ( ref end) => * end > item,
626
+ Unbounded => true ,
627
+ } )
590
628
}
591
629
}
592
630
0 commit comments