|
| 1 | +// Copyright 2017 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 | +//! Collection types. |
| 12 | +//! |
| 13 | +//! See [`std::collections`](../../std/collections/index.html) for a detailed |
| 14 | +//! discussion of collections in Rust. |
| 15 | +
|
| 16 | +/// An endpoint of a range of keys. |
| 17 | +/// |
| 18 | +/// # Examples |
| 19 | +/// |
| 20 | +/// `Bound`s are range endpoints: |
| 21 | +/// |
| 22 | +/// ``` |
| 23 | +/// #![feature(collections_range)] |
| 24 | +/// |
| 25 | +/// use std::collections::range::RangeArgument; |
| 26 | +/// use std::collections::Bound::*; |
| 27 | +/// |
| 28 | +/// assert_eq!((..100).start(), Unbounded); |
| 29 | +/// assert_eq!((1..12).start(), Included(&1)); |
| 30 | +/// assert_eq!((1..12).end(), Excluded(&12)); |
| 31 | +/// ``` |
| 32 | +/// |
| 33 | +/// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`]. |
| 34 | +/// Note that in most cases, it's better to use range syntax (`1..5`) instead. |
| 35 | +/// |
| 36 | +/// ``` |
| 37 | +/// use std::collections::BTreeMap; |
| 38 | +/// use std::collections::Bound::{Excluded, Included, Unbounded}; |
| 39 | +/// |
| 40 | +/// let mut map = BTreeMap::new(); |
| 41 | +/// map.insert(3, "a"); |
| 42 | +/// map.insert(5, "b"); |
| 43 | +/// map.insert(8, "c"); |
| 44 | +/// |
| 45 | +/// for (key, value) in map.range((Excluded(3), Included(8))) { |
| 46 | +/// println!("{}: {}", key, value); |
| 47 | +/// } |
| 48 | +/// |
| 49 | +/// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next()); |
| 50 | +/// ``` |
| 51 | +/// |
| 52 | +/// [`BTreeMap::range`]: ../../collections/btree_map/struct.BTreeMap.html#method.range |
| 53 | +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] |
| 54 | +#[stable(feature = "collections_bound", since = "1.17.0")] |
| 55 | +pub enum Bound<T> { |
| 56 | + /// An inclusive bound. |
| 57 | + #[stable(feature = "collections_bound", since = "1.17.0")] |
| 58 | + Included( |
| 59 | + #[stable(feature = "collections_bound", since = "1.17.0")] // ??? |
| 60 | + T |
| 61 | + ), |
| 62 | + /// An exclusive bound. |
| 63 | + #[stable(feature = "collections_bound", since = "1.17.0")] |
| 64 | + Excluded( |
| 65 | + #[stable(feature = "collections_bound", since = "1.17.0")] // ??? |
| 66 | + T |
| 67 | + ), |
| 68 | + /// An infinite endpoint. Indicates that there is no bound in this direction. |
| 69 | + #[stable(feature = "collections_bound", since = "1.17.0")] |
| 70 | + Unbounded, |
| 71 | +} |
0 commit comments