|
147 | 147 |
|
148 | 148 | #![stable(feature = "rust1", since = "1.0.0")]
|
149 | 149 |
|
150 |
| -use fmt; |
| 150 | +mod range; |
| 151 | + |
| 152 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 153 | +pub use self::range::{Range, RangeFrom, RangeFull, RangeTo}; |
| 154 | + |
| 155 | +#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] |
| 156 | +pub use self::range::{RangeInclusive, RangeToInclusive}; |
| 157 | + |
151 | 158 | use marker::Unsize;
|
152 | 159 |
|
153 | 160 | /// The `Drop` trait is used to run some code when a value goes out of scope.
|
@@ -2083,361 +2090,6 @@ pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
|
2083 | 2090 | fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
|
2084 | 2091 | }
|
2085 | 2092 |
|
2086 |
| -/// An unbounded range. Use `..` (two dots) for its shorthand. |
2087 |
| -/// |
2088 |
| -/// Its primary use case is slicing index. It cannot serve as an iterator |
2089 |
| -/// because it doesn't have a starting point. |
2090 |
| -/// |
2091 |
| -/// # Examples |
2092 |
| -/// |
2093 |
| -/// The `..` syntax is a `RangeFull`: |
2094 |
| -/// |
2095 |
| -/// ``` |
2096 |
| -/// assert_eq!((..), std::ops::RangeFull); |
2097 |
| -/// ``` |
2098 |
| -/// |
2099 |
| -/// It does not have an `IntoIterator` implementation, so you can't use it in a |
2100 |
| -/// `for` loop directly. This won't compile: |
2101 |
| -/// |
2102 |
| -/// ```ignore |
2103 |
| -/// for i in .. { |
2104 |
| -/// // ... |
2105 |
| -/// } |
2106 |
| -/// ``` |
2107 |
| -/// |
2108 |
| -/// Used as a slicing index, `RangeFull` produces the full array as a slice. |
2109 |
| -/// |
2110 |
| -/// ``` |
2111 |
| -/// let arr = [0, 1, 2, 3]; |
2112 |
| -/// assert_eq!(arr[ .. ], [0,1,2,3]); // RangeFull |
2113 |
| -/// assert_eq!(arr[ ..3], [0,1,2 ]); |
2114 |
| -/// assert_eq!(arr[1.. ], [ 1,2,3]); |
2115 |
| -/// assert_eq!(arr[1..3], [ 1,2 ]); |
2116 |
| -/// ``` |
2117 |
| -#[derive(Copy, Clone, PartialEq, Eq, Hash)] |
2118 |
| -#[stable(feature = "rust1", since = "1.0.0")] |
2119 |
| -pub struct RangeFull; |
2120 |
| - |
2121 |
| -#[stable(feature = "rust1", since = "1.0.0")] |
2122 |
| -impl fmt::Debug for RangeFull { |
2123 |
| - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
2124 |
| - write!(fmt, "..") |
2125 |
| - } |
2126 |
| -} |
2127 |
| - |
2128 |
| -/// A (half-open) range which is bounded at both ends: { x | start <= x < end }. |
2129 |
| -/// Use `start..end` (two dots) for its shorthand. |
2130 |
| -/// |
2131 |
| -/// See the [`contains`](#method.contains) method for its characterization. |
2132 |
| -/// |
2133 |
| -/// # Examples |
2134 |
| -/// |
2135 |
| -/// ``` |
2136 |
| -/// fn main() { |
2137 |
| -/// assert_eq!((3..5), std::ops::Range{ start: 3, end: 5 }); |
2138 |
| -/// assert_eq!(3+4+5, (3..6).sum()); |
2139 |
| -/// |
2140 |
| -/// let arr = [0, 1, 2, 3]; |
2141 |
| -/// assert_eq!(arr[ .. ], [0,1,2,3]); |
2142 |
| -/// assert_eq!(arr[ ..3], [0,1,2 ]); |
2143 |
| -/// assert_eq!(arr[1.. ], [ 1,2,3]); |
2144 |
| -/// assert_eq!(arr[1..3], [ 1,2 ]); // Range |
2145 |
| -/// } |
2146 |
| -/// ``` |
2147 |
| -#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186 |
2148 |
| -#[stable(feature = "rust1", since = "1.0.0")] |
2149 |
| -pub struct Range<Idx> { |
2150 |
| - /// The lower bound of the range (inclusive). |
2151 |
| - #[stable(feature = "rust1", since = "1.0.0")] |
2152 |
| - pub start: Idx, |
2153 |
| - /// The upper bound of the range (exclusive). |
2154 |
| - #[stable(feature = "rust1", since = "1.0.0")] |
2155 |
| - pub end: Idx, |
2156 |
| -} |
2157 |
| - |
2158 |
| -#[stable(feature = "rust1", since = "1.0.0")] |
2159 |
| -impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> { |
2160 |
| - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
2161 |
| - write!(fmt, "{:?}..{:?}", self.start, self.end) |
2162 |
| - } |
2163 |
| -} |
2164 |
| - |
2165 |
| -#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")] |
2166 |
| -impl<Idx: PartialOrd<Idx>> Range<Idx> { |
2167 |
| - /// # Examples |
2168 |
| - /// |
2169 |
| - /// ``` |
2170 |
| - /// #![feature(range_contains)] |
2171 |
| - /// fn main() { |
2172 |
| - /// assert!( ! (3..5).contains(2)); |
2173 |
| - /// assert!( (3..5).contains(3)); |
2174 |
| - /// assert!( (3..5).contains(4)); |
2175 |
| - /// assert!( ! (3..5).contains(5)); |
2176 |
| - /// |
2177 |
| - /// assert!( ! (3..3).contains(3)); |
2178 |
| - /// assert!( ! (3..2).contains(3)); |
2179 |
| - /// } |
2180 |
| - /// ``` |
2181 |
| - pub fn contains(&self, item: Idx) -> bool { |
2182 |
| - (self.start <= item) && (item < self.end) |
2183 |
| - } |
2184 |
| -} |
2185 |
| - |
2186 |
| -/// A range which is only bounded below: { x | start <= x }. |
2187 |
| -/// Use `start..` for its shorthand. |
2188 |
| -/// |
2189 |
| -/// See the [`contains`](#method.contains) method for its characterization. |
2190 |
| -/// |
2191 |
| -/// Note: Currently, no overflow checking is done for the iterator |
2192 |
| -/// implementation; if you use an integer range and the integer overflows, it |
2193 |
| -/// might panic in debug mode or create an endless loop in release mode. This |
2194 |
| -/// overflow behavior might change in the future. |
2195 |
| -/// |
2196 |
| -/// # Examples |
2197 |
| -/// |
2198 |
| -/// ``` |
2199 |
| -/// fn main() { |
2200 |
| -/// assert_eq!((2..), std::ops::RangeFrom{ start: 2 }); |
2201 |
| -/// assert_eq!(2+3+4, (2..).take(3).sum()); |
2202 |
| -/// |
2203 |
| -/// let arr = [0, 1, 2, 3]; |
2204 |
| -/// assert_eq!(arr[ .. ], [0,1,2,3]); |
2205 |
| -/// assert_eq!(arr[ ..3], [0,1,2 ]); |
2206 |
| -/// assert_eq!(arr[1.. ], [ 1,2,3]); // RangeFrom |
2207 |
| -/// assert_eq!(arr[1..3], [ 1,2 ]); |
2208 |
| -/// } |
2209 |
| -/// ``` |
2210 |
| -#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186 |
2211 |
| -#[stable(feature = "rust1", since = "1.0.0")] |
2212 |
| -pub struct RangeFrom<Idx> { |
2213 |
| - /// The lower bound of the range (inclusive). |
2214 |
| - #[stable(feature = "rust1", since = "1.0.0")] |
2215 |
| - pub start: Idx, |
2216 |
| -} |
2217 |
| - |
2218 |
| -#[stable(feature = "rust1", since = "1.0.0")] |
2219 |
| -impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> { |
2220 |
| - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
2221 |
| - write!(fmt, "{:?}..", self.start) |
2222 |
| - } |
2223 |
| -} |
2224 |
| - |
2225 |
| -#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")] |
2226 |
| -impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> { |
2227 |
| - /// # Examples |
2228 |
| - /// |
2229 |
| - /// ``` |
2230 |
| - /// #![feature(range_contains)] |
2231 |
| - /// fn main() { |
2232 |
| - /// assert!( ! (3..).contains(2)); |
2233 |
| - /// assert!( (3..).contains(3)); |
2234 |
| - /// assert!( (3..).contains(1_000_000_000)); |
2235 |
| - /// } |
2236 |
| - /// ``` |
2237 |
| - pub fn contains(&self, item: Idx) -> bool { |
2238 |
| - (self.start <= item) |
2239 |
| - } |
2240 |
| -} |
2241 |
| - |
2242 |
| -/// A range which is only bounded above: { x | x < end }. |
2243 |
| -/// Use `..end` (two dots) for its shorthand. |
2244 |
| -/// |
2245 |
| -/// See the [`contains`](#method.contains) method for its characterization. |
2246 |
| -/// |
2247 |
| -/// It cannot serve as an iterator because it doesn't have a starting point. |
2248 |
| -/// |
2249 |
| -/// # Examples |
2250 |
| -/// |
2251 |
| -/// The `..{integer}` syntax is a `RangeTo`: |
2252 |
| -/// |
2253 |
| -/// ``` |
2254 |
| -/// assert_eq!((..5), std::ops::RangeTo{ end: 5 }); |
2255 |
| -/// ``` |
2256 |
| -/// |
2257 |
| -/// It does not have an `IntoIterator` implementation, so you can't use it in a |
2258 |
| -/// `for` loop directly. This won't compile: |
2259 |
| -/// |
2260 |
| -/// ```ignore |
2261 |
| -/// for i in ..5 { |
2262 |
| -/// // ... |
2263 |
| -/// } |
2264 |
| -/// ``` |
2265 |
| -/// |
2266 |
| -/// When used as a slicing index, `RangeTo` produces a slice of all array |
2267 |
| -/// elements before the index indicated by `end`. |
2268 |
| -/// |
2269 |
| -/// ``` |
2270 |
| -/// let arr = [0, 1, 2, 3]; |
2271 |
| -/// assert_eq!(arr[ .. ], [0,1,2,3]); |
2272 |
| -/// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo |
2273 |
| -/// assert_eq!(arr[1.. ], [ 1,2,3]); |
2274 |
| -/// assert_eq!(arr[1..3], [ 1,2 ]); |
2275 |
| -/// ``` |
2276 |
| -#[derive(Copy, Clone, PartialEq, Eq, Hash)] |
2277 |
| -#[stable(feature = "rust1", since = "1.0.0")] |
2278 |
| -pub struct RangeTo<Idx> { |
2279 |
| - /// The upper bound of the range (exclusive). |
2280 |
| - #[stable(feature = "rust1", since = "1.0.0")] |
2281 |
| - pub end: Idx, |
2282 |
| -} |
2283 |
| - |
2284 |
| -#[stable(feature = "rust1", since = "1.0.0")] |
2285 |
| -impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> { |
2286 |
| - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
2287 |
| - write!(fmt, "..{:?}", self.end) |
2288 |
| - } |
2289 |
| -} |
2290 |
| - |
2291 |
| -#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")] |
2292 |
| -impl<Idx: PartialOrd<Idx>> RangeTo<Idx> { |
2293 |
| - /// # Examples |
2294 |
| - /// |
2295 |
| - /// ``` |
2296 |
| - /// #![feature(range_contains)] |
2297 |
| - /// fn main() { |
2298 |
| - /// assert!( (..5).contains(-1_000_000_000)); |
2299 |
| - /// assert!( (..5).contains(4)); |
2300 |
| - /// assert!( ! (..5).contains(5)); |
2301 |
| - /// } |
2302 |
| - /// ``` |
2303 |
| - pub fn contains(&self, item: Idx) -> bool { |
2304 |
| - (item < self.end) |
2305 |
| - } |
2306 |
| -} |
2307 |
| - |
2308 |
| -/// An inclusive range which is bounded at both ends: { x | start <= x <= end }. |
2309 |
| -/// Use `start...end` (three dots) for its shorthand. |
2310 |
| -/// |
2311 |
| -/// See the [`contains`](#method.contains) method for its characterization. |
2312 |
| -/// |
2313 |
| -/// # Examples |
2314 |
| -/// |
2315 |
| -/// ``` |
2316 |
| -/// #![feature(inclusive_range,inclusive_range_syntax)] |
2317 |
| -/// fn main() { |
2318 |
| -/// assert_eq!((3...5), std::ops::RangeInclusive{ start: 3, end: 5 }); |
2319 |
| -/// assert_eq!(3+4+5, (3...5).sum()); |
2320 |
| -/// |
2321 |
| -/// let arr = [0, 1, 2, 3]; |
2322 |
| -/// assert_eq!(arr[ ...2], [0,1,2 ]); |
2323 |
| -/// assert_eq!(arr[1...2], [ 1,2 ]); // RangeInclusive |
2324 |
| -/// } |
2325 |
| -/// ``` |
2326 |
| -#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186 |
2327 |
| -#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] |
2328 |
| -pub struct RangeInclusive<Idx> { |
2329 |
| - /// The lower bound of the range (inclusive). |
2330 |
| - #[unstable(feature = "inclusive_range", |
2331 |
| - reason = "recently added, follows RFC", |
2332 |
| - issue = "28237")] |
2333 |
| - pub start: Idx, |
2334 |
| - /// The upper bound of the range (inclusive). |
2335 |
| - #[unstable(feature = "inclusive_range", |
2336 |
| - reason = "recently added, follows RFC", |
2337 |
| - issue = "28237")] |
2338 |
| - pub end: Idx, |
2339 |
| -} |
2340 |
| - |
2341 |
| -#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] |
2342 |
| -impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> { |
2343 |
| - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
2344 |
| - write!(fmt, "{:?}...{:?}", self.start, self.end) |
2345 |
| - } |
2346 |
| -} |
2347 |
| - |
2348 |
| -#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")] |
2349 |
| -impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> { |
2350 |
| - /// # Examples |
2351 |
| - /// |
2352 |
| - /// ``` |
2353 |
| - /// #![feature(range_contains,inclusive_range_syntax)] |
2354 |
| - /// fn main() { |
2355 |
| - /// assert!( ! (3...5).contains(2)); |
2356 |
| - /// assert!( (3...5).contains(3)); |
2357 |
| - /// assert!( (3...5).contains(4)); |
2358 |
| - /// assert!( (3...5).contains(5)); |
2359 |
| - /// assert!( ! (3...5).contains(6)); |
2360 |
| - /// |
2361 |
| - /// assert!( (3...3).contains(3)); |
2362 |
| - /// assert!( ! (3...2).contains(3)); |
2363 |
| - /// } |
2364 |
| - /// ``` |
2365 |
| - pub fn contains(&self, item: Idx) -> bool { |
2366 |
| - self.start <= item && item <= self.end |
2367 |
| - } |
2368 |
| -} |
2369 |
| - |
2370 |
| -/// An inclusive range which is only bounded above: { x | x <= end }. |
2371 |
| -/// Use `...end` (three dots) for its shorthand. |
2372 |
| -/// |
2373 |
| -/// See the [`contains`](#method.contains) method for its characterization. |
2374 |
| -/// |
2375 |
| -/// It cannot serve as an iterator because it doesn't have a starting point. |
2376 |
| -/// |
2377 |
| -/// # Examples |
2378 |
| -/// |
2379 |
| -/// The `...{integer}` syntax is a `RangeToInclusive`: |
2380 |
| -/// |
2381 |
| -/// ``` |
2382 |
| -/// #![feature(inclusive_range,inclusive_range_syntax)] |
2383 |
| -/// assert_eq!((...5), std::ops::RangeToInclusive{ end: 5 }); |
2384 |
| -/// ``` |
2385 |
| -/// |
2386 |
| -/// It does not have an `IntoIterator` implementation, so you can't use it in a |
2387 |
| -/// `for` loop directly. This won't compile: |
2388 |
| -/// |
2389 |
| -/// ```ignore |
2390 |
| -/// for i in ...5 { |
2391 |
| -/// // ... |
2392 |
| -/// } |
2393 |
| -/// ``` |
2394 |
| -/// |
2395 |
| -/// When used as a slicing index, `RangeToInclusive` produces a slice of all |
2396 |
| -/// array elements up to and including the index indicated by `end`. |
2397 |
| -/// |
2398 |
| -/// ``` |
2399 |
| -/// #![feature(inclusive_range_syntax)] |
2400 |
| -/// let arr = [0, 1, 2, 3]; |
2401 |
| -/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive |
2402 |
| -/// assert_eq!(arr[1...2], [ 1,2 ]); |
2403 |
| -/// ``` |
2404 |
| -#[derive(Copy, Clone, PartialEq, Eq, Hash)] |
2405 |
| -#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] |
2406 |
| -pub struct RangeToInclusive<Idx> { |
2407 |
| - /// The upper bound of the range (inclusive) |
2408 |
| - #[unstable(feature = "inclusive_range", |
2409 |
| - reason = "recently added, follows RFC", |
2410 |
| - issue = "28237")] |
2411 |
| - pub end: Idx, |
2412 |
| -} |
2413 |
| - |
2414 |
| -#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] |
2415 |
| -impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> { |
2416 |
| - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
2417 |
| - write!(fmt, "...{:?}", self.end) |
2418 |
| - } |
2419 |
| -} |
2420 |
| - |
2421 |
| -#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")] |
2422 |
| -impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> { |
2423 |
| - /// # Examples |
2424 |
| - /// |
2425 |
| - /// ``` |
2426 |
| - /// #![feature(range_contains,inclusive_range_syntax)] |
2427 |
| - /// fn main() { |
2428 |
| - /// assert!( (...5).contains(-1_000_000_000)); |
2429 |
| - /// assert!( (...5).contains(5)); |
2430 |
| - /// assert!( ! (...5).contains(6)); |
2431 |
| - /// } |
2432 |
| - /// ``` |
2433 |
| - pub fn contains(&self, item: Idx) -> bool { |
2434 |
| - (item <= self.end) |
2435 |
| - } |
2436 |
| -} |
2437 |
| - |
2438 |
| -// RangeToInclusive<Idx> cannot impl From<RangeTo<Idx>> |
2439 |
| -// because underflow would be possible with (..0).into() |
2440 |
| - |
2441 | 2093 | /// The `Deref` trait is used to specify the functionality of dereferencing
|
2442 | 2094 | /// operations, like `*v`.
|
2443 | 2095 | ///
|
|
0 commit comments