Skip to content

Commit b6fd64f

Browse files
committed
add .iter() shorthand for .clone().into_iter()
1 parent 4a4e57d commit b6fd64f

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

library/core/src/range.rs

+69
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
9292
}
9393
}
9494

95+
impl<Idx: Step> Range<Idx> {
96+
/// Create an iterator over the elements within this range.
97+
///
98+
/// Shorthand for `.clone().into_iter()`
99+
///
100+
/// # Examples
101+
///
102+
/// ```
103+
/// #![feature(new_range_api)]
104+
/// use core::range::Range;
105+
///
106+
/// let mut i = Range::from(3..9).iter().map(|n| n*n);
107+
/// assert_eq!(i.next(), Some(9));
108+
/// assert_eq!(i.next(), Some(16));
109+
/// assert_eq!(i.next(), Some(25));
110+
/// ```
111+
#[unstable(feature = "new_range_api", issue = "125687")]
112+
#[inline]
113+
pub fn iter(&self) -> IterRange<Idx> {
114+
self.clone().into_iter()
115+
}
116+
}
117+
95118
impl<Idx: PartialOrd<Idx>> Range<Idx> {
96119
/// Returns `true` if `item` is contained in the range.
97120
///
@@ -289,6 +312,29 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
289312
}
290313
}
291314

315+
impl<Idx: Step> RangeInclusive<Idx> {
316+
/// Create an iterator over the elements within this range.
317+
///
318+
/// Shorthand for `.clone().into_iter()`
319+
///
320+
/// # Examples
321+
///
322+
/// ```
323+
/// #![feature(new_range_api)]
324+
/// use core::range::RangeInclusive;
325+
///
326+
/// let mut i = RangeInclusive::from(3..=8).iter().map(|n| n*n);
327+
/// assert_eq!(i.next(), Some(9));
328+
/// assert_eq!(i.next(), Some(16));
329+
/// assert_eq!(i.next(), Some(25));
330+
/// ```
331+
#[unstable(feature = "new_range_api", issue = "125687")]
332+
#[inline]
333+
pub fn iter(&self) -> IterRangeInclusive<Idx> {
334+
self.clone().into_iter()
335+
}
336+
}
337+
292338
impl RangeInclusive<usize> {
293339
/// Converts to an exclusive `Range` for `SliceIndex` implementations.
294340
/// The caller is responsible for dealing with `end == usize::MAX`.
@@ -382,6 +428,29 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
382428
}
383429
}
384430

431+
impl<Idx: Step> RangeFrom<Idx> {
432+
/// Create an iterator over the elements within this range.
433+
///
434+
/// Shorthand for `.clone().into_iter()`
435+
///
436+
/// # Examples
437+
///
438+
/// ```
439+
/// #![feature(new_range_api)]
440+
/// use core::range::RangeFrom;
441+
///
442+
/// let mut i = RangeFrom::from(3..).iter().map(|n| n*n);
443+
/// assert_eq!(i.next(), Some(9));
444+
/// assert_eq!(i.next(), Some(16));
445+
/// assert_eq!(i.next(), Some(25));
446+
/// ```
447+
#[unstable(feature = "new_range_api", issue = "125687")]
448+
#[inline]
449+
pub fn iter(&self) -> IterRangeFrom<Idx> {
450+
self.clone().into_iter()
451+
}
452+
}
453+
385454
impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
386455
/// Returns `true` if `item` is contained in the range.
387456
///

0 commit comments

Comments
 (0)