]> git.lizzy.rs Git - rust.git/blob - src/libcore/ops/range.rs
Skip checking for unused mutable locals that have no name
[rust.git] / src / libcore / ops / range.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use fmt;
12
13 /// An unbounded range (`..`).
14 ///
15 /// `RangeFull` is primarily used as a [slicing index], its shorthand is `..`.
16 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
17 ///
18 /// # Examples
19 ///
20 /// The `..` syntax is a `RangeFull`:
21 ///
22 /// ```
23 /// assert_eq!((..), std::ops::RangeFull);
24 /// ```
25 ///
26 /// It does not have an [`IntoIterator`] implementation, so you can't use it in
27 /// a `for` loop directly. This won't compile:
28 ///
29 /// ```compile_fail,E0277
30 /// for i in .. {
31 ///    // ...
32 /// }
33 /// ```
34 ///
35 /// Used as a [slicing index], `RangeFull` produces the full array as a slice.
36 ///
37 /// ```
38 /// let arr = [0, 1, 2, 3];
39 /// assert_eq!(arr[ .. ], [0,1,2,3]);  // RangeFull
40 /// assert_eq!(arr[ ..3], [0,1,2  ]);
41 /// assert_eq!(arr[1.. ], [  1,2,3]);
42 /// assert_eq!(arr[1..3], [  1,2  ]);
43 /// ```
44 ///
45 /// [`IntoIterator`]: ../iter/trait.Iterator.html
46 /// [`Iterator`]: ../iter/trait.IntoIterator.html
47 /// [slicing index]: ../slice/trait.SliceIndex.html
48 #[doc(alias = "..")]
49 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
50 #[stable(feature = "rust1", since = "1.0.0")]
51 pub struct RangeFull;
52
53 #[stable(feature = "rust1", since = "1.0.0")]
54 impl fmt::Debug for RangeFull {
55     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
56         write!(fmt, "..")
57     }
58 }
59
60 /// A (half-open) range bounded inclusively below and exclusively above
61 /// (`start..end`).
62 ///
63 /// The `Range` `start..end` contains all values with `x >= start` and
64 /// `x < end`.  It is empty unless `start < end`.
65 ///
66 /// # Examples
67 ///
68 /// ```
69 /// assert_eq!((3..5), std::ops::Range { start: 3, end: 5 });
70 /// assert_eq!(3 + 4 + 5, (3..6).sum());
71 ///
72 /// let arr = ['a', 'b', 'c', 'd'];
73 /// assert_eq!(arr[ .. ], ['a', 'b', 'c', 'd']);
74 /// assert_eq!(arr[ ..3], ['a', 'b', 'c',    ]);
75 /// assert_eq!(arr[1.. ], [     'b', 'c', 'd']);
76 /// assert_eq!(arr[1..3], [     'b', 'c'     ]);  // Range
77 /// ```
78 #[doc(alias = "..")]
79 #[derive(Clone, PartialEq, Eq, Hash)]  // not Copy -- see #27186
80 #[stable(feature = "rust1", since = "1.0.0")]
81 pub struct Range<Idx> {
82     /// The lower bound of the range (inclusive).
83     #[stable(feature = "rust1", since = "1.0.0")]
84     pub start: Idx,
85     /// The upper bound of the range (exclusive).
86     #[stable(feature = "rust1", since = "1.0.0")]
87     pub end: Idx,
88 }
89
90 #[stable(feature = "rust1", since = "1.0.0")]
91 impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
92     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
93         write!(fmt, "{:?}..{:?}", self.start, self.end)
94     }
95 }
96
97 impl<Idx: PartialOrd<Idx>> Range<Idx> {
98     /// Returns `true` if `item` is contained in the range.
99     ///
100     /// # Examples
101     ///
102     /// ```
103     /// #![feature(range_contains)]
104     ///
105     /// use std::f32;
106     ///
107     /// assert!(!(3..5).contains(&2));
108     /// assert!( (3..5).contains(&3));
109     /// assert!( (3..5).contains(&4));
110     /// assert!(!(3..5).contains(&5));
111     ///
112     /// assert!(!(3..3).contains(&3));
113     /// assert!(!(3..2).contains(&3));
114     ///
115     /// assert!( (0.0..1.0).contains(&0.5));
116     /// assert!(!(0.0..1.0).contains(&f32::NAN));
117     /// assert!(!(0.0..f32::NAN).contains(&0.5));
118     /// assert!(!(f32::NAN..1.0).contains(&0.5));
119     /// ```
120     #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
121     pub fn contains<U>(&self, item: &U) -> bool
122     where
123         Idx: PartialOrd<U>,
124         U: ?Sized + PartialOrd<Idx>,
125     {
126         <Self as RangeBounds<Idx>>::contains(self, item)
127     }
128
129     /// Returns `true` if the range contains no items.
130     ///
131     /// # Examples
132     ///
133     /// ```
134     /// #![feature(range_is_empty)]
135     ///
136     /// assert!(!(3..5).is_empty());
137     /// assert!( (3..3).is_empty());
138     /// assert!( (3..2).is_empty());
139     /// ```
140     ///
141     /// The range is empty if either side is incomparable:
142     ///
143     /// ```
144     /// #![feature(range_is_empty)]
145     ///
146     /// use std::f32::NAN;
147     /// assert!(!(3.0..5.0).is_empty());
148     /// assert!( (3.0..NAN).is_empty());
149     /// assert!( (NAN..5.0).is_empty());
150     /// ```
151     #[unstable(feature = "range_is_empty", reason = "recently added", issue = "48111")]
152     pub fn is_empty(&self) -> bool {
153         !(self.start < self.end)
154     }
155 }
156
157 /// A range only bounded inclusively below (`start..`).
158 ///
159 /// The `RangeFrom` `start..` contains all values with `x >= start`.
160 ///
161 /// *Note*: Currently, no overflow checking is done for the [`Iterator`]
162 /// implementation; if you use an integer range and the integer overflows, it
163 /// might panic in debug mode or create an endless loop in release mode. **This
164 /// overflow behavior might change in the future.**
165 ///
166 /// # Examples
167 ///
168 /// ```
169 /// assert_eq!((2..), std::ops::RangeFrom { start: 2 });
170 /// assert_eq!(2 + 3 + 4, (2..).take(3).sum());
171 ///
172 /// let arr = [0, 1, 2, 3];
173 /// assert_eq!(arr[ .. ], [0,1,2,3]);
174 /// assert_eq!(arr[ ..3], [0,1,2  ]);
175 /// assert_eq!(arr[1.. ], [  1,2,3]);  // RangeFrom
176 /// assert_eq!(arr[1..3], [  1,2  ]);
177 /// ```
178 ///
179 /// [`Iterator`]: ../iter/trait.IntoIterator.html
180 #[doc(alias = "..")]
181 #[derive(Clone, PartialEq, Eq, Hash)]  // not Copy -- see #27186
182 #[stable(feature = "rust1", since = "1.0.0")]
183 pub struct RangeFrom<Idx> {
184     /// The lower bound of the range (inclusive).
185     #[stable(feature = "rust1", since = "1.0.0")]
186     pub start: Idx,
187 }
188
189 #[stable(feature = "rust1", since = "1.0.0")]
190 impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
191     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
192         write!(fmt, "{:?}..", self.start)
193     }
194 }
195
196 impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
197     /// Returns `true` if `item` is contained in the range.
198     ///
199     /// # Examples
200     ///
201     /// ```
202     /// #![feature(range_contains)]
203     ///
204     /// use std::f32;
205     ///
206     /// assert!(!(3..).contains(&2));
207     /// assert!( (3..).contains(&3));
208     /// assert!( (3..).contains(&1_000_000_000));
209     ///
210     /// assert!( (0.0..).contains(&0.5));
211     /// assert!(!(0.0..).contains(&f32::NAN));
212     /// assert!(!(f32::NAN..).contains(&0.5));
213     /// ```
214     #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
215     pub fn contains<U>(&self, item: &U) -> bool
216     where
217         Idx: PartialOrd<U>,
218         U: ?Sized + PartialOrd<Idx>,
219     {
220         <Self as RangeBounds<Idx>>::contains(self, item)
221     }
222 }
223
224 /// A range only bounded exclusively above (`..end`).
225 ///
226 /// The `RangeTo` `..end` contains all values with `x < end`.
227 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
228 ///
229 /// # Examples
230 ///
231 /// The `..end` syntax is a `RangeTo`:
232 ///
233 /// ```
234 /// assert_eq!((..5), std::ops::RangeTo { end: 5 });
235 /// ```
236 ///
237 /// It does not have an [`IntoIterator`] implementation, so you can't use it in
238 /// a `for` loop directly. This won't compile:
239 ///
240 /// ```compile_fail,E0277
241 /// // error[E0277]: the trait bound `std::ops::RangeTo<{integer}>:
242 /// // std::iter::Iterator` is not satisfied
243 /// for i in ..5 {
244 ///     // ...
245 /// }
246 /// ```
247 ///
248 /// When used as a [slicing index], `RangeTo` produces a slice of all array
249 /// elements before the index indicated by `end`.
250 ///
251 /// ```
252 /// let arr = [0, 1, 2, 3];
253 /// assert_eq!(arr[ .. ], [0,1,2,3]);
254 /// assert_eq!(arr[ ..3], [0,1,2  ]);  // RangeTo
255 /// assert_eq!(arr[1.. ], [  1,2,3]);
256 /// assert_eq!(arr[1..3], [  1,2  ]);
257 /// ```
258 ///
259 /// [`IntoIterator`]: ../iter/trait.Iterator.html
260 /// [`Iterator`]: ../iter/trait.IntoIterator.html
261 /// [slicing index]: ../slice/trait.SliceIndex.html
262 #[doc(alias = "..")]
263 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
264 #[stable(feature = "rust1", since = "1.0.0")]
265 pub struct RangeTo<Idx> {
266     /// The upper bound of the range (exclusive).
267     #[stable(feature = "rust1", since = "1.0.0")]
268     pub end: Idx,
269 }
270
271 #[stable(feature = "rust1", since = "1.0.0")]
272 impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
273     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
274         write!(fmt, "..{:?}", self.end)
275     }
276 }
277
278 impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
279     /// Returns `true` if `item` is contained in the range.
280     ///
281     /// # Examples
282     ///
283     /// ```
284     /// #![feature(range_contains)]
285     ///
286     /// use std::f32;
287     ///
288     /// assert!( (..5).contains(&-1_000_000_000));
289     /// assert!( (..5).contains(&4));
290     /// assert!(!(..5).contains(&5));
291     ///
292     /// assert!( (..1.0).contains(&0.5));
293     /// assert!(!(..1.0).contains(&f32::NAN));
294     /// assert!(!(..f32::NAN).contains(&0.5));
295     /// ```
296     #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
297     pub fn contains<U>(&self, item: &U) -> bool
298     where
299         Idx: PartialOrd<U>,
300         U: ?Sized + PartialOrd<Idx>,
301     {
302         <Self as RangeBounds<Idx>>::contains(self, item)
303     }
304 }
305
306 /// An range bounded inclusively below and above (`start..=end`).
307 ///
308 /// The `RangeInclusive` `start..=end` contains all values with `x >= start`
309 /// and `x <= end`.  It is empty unless `start <= end`.
310 ///
311 /// This iterator is [fused], but the specific values of `start` and `end` after
312 /// iteration has finished are **unspecified** other than that [`.is_empty()`]
313 /// will return `true` once no more values will be produced.
314 ///
315 /// [fused]: ../iter/trait.FusedIterator.html
316 /// [`.is_empty()`]: #method.is_empty
317 ///
318 /// # Examples
319 ///
320 /// ```
321 /// #![feature(inclusive_range_fields)]
322 ///
323 /// assert_eq!((3..=5), std::ops::RangeInclusive { start: 3, end: 5 });
324 /// assert_eq!(3 + 4 + 5, (3..=5).sum());
325 ///
326 /// let arr = [0, 1, 2, 3];
327 /// assert_eq!(arr[ ..=2], [0,1,2  ]);
328 /// assert_eq!(arr[1..=2], [  1,2  ]);  // RangeInclusive
329 /// ```
330 #[doc(alias = "..=")]
331 #[derive(Clone, PartialEq, Eq, Hash)]  // not Copy -- see #27186
332 #[stable(feature = "inclusive_range", since = "1.26.0")]
333 pub struct RangeInclusive<Idx> {
334     /// The lower bound of the range (inclusive).
335     #[unstable(feature = "inclusive_range_fields", issue = "49022")]
336     pub start: Idx,
337     /// The upper bound of the range (inclusive).
338     #[unstable(feature = "inclusive_range_fields", issue = "49022")]
339     pub end: Idx,
340 }
341
342 #[stable(feature = "inclusive_range", since = "1.26.0")]
343 impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
344     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
345         write!(fmt, "{:?}..={:?}", self.start, self.end)
346     }
347 }
348
349 impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
350     /// Returns `true` if `item` is contained in the range.
351     ///
352     /// # Examples
353     ///
354     /// ```
355     /// #![feature(range_contains)]
356     ///
357     /// use std::f32;
358     ///
359     /// assert!(!(3..=5).contains(&2));
360     /// assert!( (3..=5).contains(&3));
361     /// assert!( (3..=5).contains(&4));
362     /// assert!( (3..=5).contains(&5));
363     /// assert!(!(3..=5).contains(&6));
364     ///
365     /// assert!( (3..=3).contains(&3));
366     /// assert!(!(3..=2).contains(&3));
367     ///
368     /// assert!( (0.0..=1.0).contains(&1.0));
369     /// assert!(!(0.0..=1.0).contains(&f32::NAN));
370     /// assert!(!(0.0..=f32::NAN).contains(&0.0));
371     /// assert!(!(f32::NAN..=1.0).contains(&1.0));
372     /// ```
373     #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
374     pub fn contains<U>(&self, item: &U) -> bool
375     where
376         Idx: PartialOrd<U>,
377         U: ?Sized + PartialOrd<Idx>,
378     {
379         <Self as RangeBounds<Idx>>::contains(self, item)
380     }
381
382     /// Returns `true` if the range contains no items.
383     ///
384     /// # Examples
385     ///
386     /// ```
387     /// #![feature(range_is_empty)]
388     ///
389     /// assert!(!(3..=5).is_empty());
390     /// assert!(!(3..=3).is_empty());
391     /// assert!( (3..=2).is_empty());
392     /// ```
393     ///
394     /// The range is empty if either side is incomparable:
395     ///
396     /// ```
397     /// #![feature(range_is_empty)]
398     ///
399     /// use std::f32::NAN;
400     /// assert!(!(3.0..=5.0).is_empty());
401     /// assert!( (3.0..=NAN).is_empty());
402     /// assert!( (NAN..=5.0).is_empty());
403     /// ```
404     ///
405     /// This method returns `true` after iteration has finished:
406     ///
407     /// ```
408     /// #![feature(range_is_empty)]
409     ///
410     /// let mut r = 3..=5;
411     /// for _ in r.by_ref() {}
412     /// // Precise field values are unspecified here
413     /// assert!(r.is_empty());
414     /// ```
415     #[unstable(feature = "range_is_empty", reason = "recently added", issue = "48111")]
416     pub fn is_empty(&self) -> bool {
417         !(self.start <= self.end)
418     }
419 }
420
421 /// A range only bounded inclusively above (`..=end`).
422 ///
423 /// The `RangeToInclusive` `..=end` contains all values with `x <= end`.
424 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
425 ///
426 /// # Examples
427 ///
428 /// The `..=end` syntax is a `RangeToInclusive`:
429 ///
430 /// ```
431 /// assert_eq!((..=5), std::ops::RangeToInclusive{ end: 5 });
432 /// ```
433 ///
434 /// It does not have an [`IntoIterator`] implementation, so you can't use it in a
435 /// `for` loop directly. This won't compile:
436 ///
437 /// ```compile_fail,E0277
438 /// // error[E0277]: the trait bound `std::ops::RangeToInclusive<{integer}>:
439 /// // std::iter::Iterator` is not satisfied
440 /// for i in ..=5 {
441 ///     // ...
442 /// }
443 /// ```
444 ///
445 /// When used as a [slicing index], `RangeToInclusive` produces a slice of all
446 /// array elements up to and including the index indicated by `end`.
447 ///
448 /// ```
449 /// let arr = [0, 1, 2, 3];
450 /// assert_eq!(arr[ ..=2], [0,1,2  ]);  // RangeToInclusive
451 /// assert_eq!(arr[1..=2], [  1,2  ]);
452 /// ```
453 ///
454 /// [`IntoIterator`]: ../iter/trait.Iterator.html
455 /// [`Iterator`]: ../iter/trait.IntoIterator.html
456 /// [slicing index]: ../slice/trait.SliceIndex.html
457 #[doc(alias = "..=")]
458 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
459 #[stable(feature = "inclusive_range", since = "1.26.0")]
460 pub struct RangeToInclusive<Idx> {
461     /// The upper bound of the range (inclusive)
462     #[stable(feature = "inclusive_range", since = "1.26.0")]
463     pub end: Idx,
464 }
465
466 #[stable(feature = "inclusive_range", since = "1.26.0")]
467 impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
468     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
469         write!(fmt, "..={:?}", self.end)
470     }
471 }
472
473 #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
474 impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
475     /// Returns `true` if `item` is contained in the range.
476     ///
477     /// # Examples
478     ///
479     /// ```
480     /// #![feature(range_contains)]
481     ///
482     /// use std::f32;
483     ///
484     /// assert!( (..=5).contains(&-1_000_000_000));
485     /// assert!( (..=5).contains(&5));
486     /// assert!(!(..=5).contains(&6));
487     ///
488     /// assert!( (..=1.0).contains(&1.0));
489     /// assert!(!(..=1.0).contains(&f32::NAN));
490     /// assert!(!(..=f32::NAN).contains(&0.5));
491     /// ```
492     #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
493     pub fn contains<U>(&self, item: &U) -> bool
494     where
495         Idx: PartialOrd<U>,
496         U: ?Sized + PartialOrd<Idx>,
497     {
498         <Self as RangeBounds<Idx>>::contains(self, item)
499     }
500 }
501
502 // RangeToInclusive<Idx> cannot impl From<RangeTo<Idx>>
503 // because underflow would be possible with (..0).into()
504
505 /// An endpoint of a range of keys.
506 ///
507 /// # Examples
508 ///
509 /// `Bound`s are range endpoints:
510 ///
511 /// ```
512 /// #![feature(collections_range)]
513 ///
514 /// use std::ops::Bound::*;
515 /// use std::ops::RangeBounds;
516 ///
517 /// assert_eq!((..100).start(), Unbounded);
518 /// assert_eq!((1..12).start(), Included(&1));
519 /// assert_eq!((1..12).end(), Excluded(&12));
520 /// ```
521 ///
522 /// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`].
523 /// Note that in most cases, it's better to use range syntax (`1..5`) instead.
524 ///
525 /// ```
526 /// use std::collections::BTreeMap;
527 /// use std::ops::Bound::{Excluded, Included, Unbounded};
528 ///
529 /// let mut map = BTreeMap::new();
530 /// map.insert(3, "a");
531 /// map.insert(5, "b");
532 /// map.insert(8, "c");
533 ///
534 /// for (key, value) in map.range((Excluded(3), Included(8))) {
535 ///     println!("{}: {}", key, value);
536 /// }
537 ///
538 /// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next());
539 /// ```
540 ///
541 /// [`BTreeMap::range`]: ../../std/collections/btree_map/struct.BTreeMap.html#method.range
542 #[stable(feature = "collections_bound", since = "1.17.0")]
543 #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
544 pub enum Bound<T> {
545     /// An inclusive bound.
546     #[stable(feature = "collections_bound", since = "1.17.0")]
547     Included(#[stable(feature = "collections_bound", since = "1.17.0")] T),
548     /// An exclusive bound.
549     #[stable(feature = "collections_bound", since = "1.17.0")]
550     Excluded(#[stable(feature = "collections_bound", since = "1.17.0")] T),
551     /// An infinite endpoint. Indicates that there is no bound in this direction.
552     #[stable(feature = "collections_bound", since = "1.17.0")]
553     Unbounded,
554 }
555
556 #[unstable(feature = "collections_range",
557            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
558            issue = "30877")]
559 /// `RangeBounds` is implemented by Rust's built-in range types, produced
560 /// by range syntax like `..`, `a..`, `..b` or `c..d`.
561 pub trait RangeBounds<T: ?Sized> {
562     /// Start index bound.
563     ///
564     /// Returns the start value as a `Bound`.
565     ///
566     /// # Examples
567     ///
568     /// ```
569     /// #![feature(collections_range)]
570     ///
571     /// # fn main() {
572     /// use std::ops::Bound::*;
573     /// use std::ops::RangeBounds;
574     ///
575     /// assert_eq!((..10).start(), Unbounded);
576     /// assert_eq!((3..10).start(), Included(&3));
577     /// # }
578     /// ```
579     fn start(&self) -> Bound<&T>;
580
581     /// End index bound.
582     ///
583     /// Returns the end value as a `Bound`.
584     ///
585     /// # Examples
586     ///
587     /// ```
588     /// #![feature(collections_range)]
589     ///
590     /// # fn main() {
591     /// use std::ops::Bound::*;
592     /// use std::ops::RangeBounds;
593     ///
594     /// assert_eq!((3..).end(), Unbounded);
595     /// assert_eq!((3..10).end(), Excluded(&10));
596     /// # }
597     /// ```
598     fn end(&self) -> Bound<&T>;
599
600
601     /// Returns `true` if `item` is contained in the range.
602     ///
603     /// # Examples
604     ///
605     /// ```
606     /// #![feature(range_contains)]
607     ///
608     /// use std::f32;
609     ///
610     /// assert!( (3..5).contains(&4));
611     /// assert!(!(3..5).contains(&2));
612     ///
613     /// assert!( (0.0..1.0).contains(&0.5));
614     /// assert!(!(0.0..1.0).contains(&f32::NAN));
615     /// assert!(!(0.0..f32::NAN).contains(&0.5));
616     /// assert!(!(f32::NAN..1.0).contains(&0.5));
617     #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
618     fn contains<U>(&self, item: &U) -> bool
619     where
620         T: PartialOrd<U>,
621         U: ?Sized + PartialOrd<T>,
622     {
623         (match self.start() {
624             Included(ref start) => *start <= item,
625             Excluded(ref start) => *start < item,
626             Unbounded => true,
627         })
628         &&
629         (match self.end() {
630             Included(ref end) => item <= *end,
631             Excluded(ref end) => item < *end,
632             Unbounded => true,
633         })
634     }
635 }
636
637 use self::Bound::{Excluded, Included, Unbounded};
638
639 #[unstable(feature = "collections_range",
640            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
641            issue = "30877")]
642 impl<T: ?Sized> RangeBounds<T> for RangeFull {
643     fn start(&self) -> Bound<&T> {
644         Unbounded
645     }
646     fn end(&self) -> Bound<&T> {
647         Unbounded
648     }
649 }
650
651 #[unstable(feature = "collections_range",
652            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
653            issue = "30877")]
654 impl<T> RangeBounds<T> for RangeFrom<T> {
655     fn start(&self) -> Bound<&T> {
656         Included(&self.start)
657     }
658     fn end(&self) -> Bound<&T> {
659         Unbounded
660     }
661 }
662
663 #[unstable(feature = "collections_range",
664            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
665            issue = "30877")]
666 impl<T> RangeBounds<T> for RangeTo<T> {
667     fn start(&self) -> Bound<&T> {
668         Unbounded
669     }
670     fn end(&self) -> Bound<&T> {
671         Excluded(&self.end)
672     }
673 }
674
675 #[unstable(feature = "collections_range",
676            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
677            issue = "30877")]
678 impl<T> RangeBounds<T> for Range<T> {
679     fn start(&self) -> Bound<&T> {
680         Included(&self.start)
681     }
682     fn end(&self) -> Bound<&T> {
683         Excluded(&self.end)
684     }
685 }
686
687 #[unstable(feature = "collections_range",
688            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
689            issue = "30877")]
690 impl<T> RangeBounds<T> for RangeInclusive<T> {
691     fn start(&self) -> Bound<&T> {
692         Included(&self.start)
693     }
694     fn end(&self) -> Bound<&T> {
695         Included(&self.end)
696     }
697 }
698
699 #[unstable(feature = "collections_range",
700            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
701            issue = "30877")]
702 impl<T> RangeBounds<T> for RangeToInclusive<T> {
703     fn start(&self) -> Bound<&T> {
704         Unbounded
705     }
706     fn end(&self) -> Bound<&T> {
707         Included(&self.end)
708     }
709 }
710
711 #[unstable(feature = "collections_range",
712            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
713            issue = "30877")]
714 impl<T> RangeBounds<T> for (Bound<T>, Bound<T>) {
715     fn start(&self) -> Bound<&T> {
716         match *self {
717             (Included(ref start), _) => Included(start),
718             (Excluded(ref start), _) => Excluded(start),
719             (Unbounded, _)           => Unbounded,
720         }
721     }
722
723     fn end(&self) -> Bound<&T> {
724         match *self {
725             (_, Included(ref end)) => Included(end),
726             (_, Excluded(ref end)) => Excluded(end),
727             (_, Unbounded)         => Unbounded,
728         }
729     }
730 }
731
732 #[unstable(feature = "collections_range",
733            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
734            issue = "30877")]
735 impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
736     fn start(&self) -> Bound<&T> {
737         self.0
738     }
739
740     fn end(&self) -> Bound<&T> {
741         self.1
742     }
743 }
744
745 #[unstable(feature = "collections_range",
746            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
747            issue = "30877")]
748 impl<'a, T> RangeBounds<T> for RangeFrom<&'a T> {
749     fn start(&self) -> Bound<&T> {
750         Included(self.start)
751     }
752     fn end(&self) -> Bound<&T> {
753         Unbounded
754     }
755 }
756
757 #[unstable(feature = "collections_range",
758            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
759            issue = "30877")]
760 impl<'a, T> RangeBounds<T> for RangeTo<&'a T> {
761     fn start(&self) -> Bound<&T> {
762         Unbounded
763     }
764     fn end(&self) -> Bound<&T> {
765         Excluded(self.end)
766     }
767 }
768
769 #[unstable(feature = "collections_range",
770            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
771            issue = "30877")]
772 impl<'a, T> RangeBounds<T> for Range<&'a T> {
773     fn start(&self) -> Bound<&T> {
774         Included(self.start)
775     }
776     fn end(&self) -> Bound<&T> {
777         Excluded(self.end)
778     }
779 }
780
781 #[unstable(feature = "collections_range",
782            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
783            issue = "30877")]
784 impl<'a, T> RangeBounds<T> for RangeInclusive<&'a T> {
785     fn start(&self) -> Bound<&T> {
786         Included(self.start)
787     }
788     fn end(&self) -> Bound<&T> {
789         Included(self.end)
790     }
791 }
792
793 #[unstable(feature = "collections_range",
794            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
795            issue = "30877")]
796 impl<'a, T> RangeBounds<T> for RangeToInclusive<&'a T> {
797     fn start(&self) -> Bound<&T> {
798         Unbounded
799     }
800     fn end(&self) -> Bound<&T> {
801         Included(self.end)
802     }
803 }