Skip to content

Commit 69289c9

Browse files
Add doc example for Iter and IterMut
1 parent b12b4e4 commit 69289c9

File tree

1 file changed

+162
-4
lines changed

1 file changed

+162
-4
lines changed

src/libcore/slice.rs

+162-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use cmp::{Ordering, PartialEq, PartialOrd, Eq, Ord};
3838
use cmp::Ordering::{Less, Equal, Greater};
3939
use cmp;
4040
use default::Default;
41+
use fmt;
4142
use intrinsics::assume;
4243
use iter::*;
4344
use ops::{FnMut, self, Index};
@@ -632,8 +633,7 @@ impl<T> ops::Index<ops::RangeToInclusive<usize>> for [T] {
632633

633634
#[inline]
634635
fn index(&self, index: ops::RangeToInclusive<usize>) -> &[T] {
635-
// SNAP 4d3eebf change this to `0...index.end`
636-
self.index(ops::RangeInclusive::NonEmpty { start: 0, end: index.end })
636+
self.index(0...index.end)
637637
}
638638
}
639639

@@ -723,8 +723,7 @@ impl<T> ops::IndexMut<ops::RangeInclusive<usize>> for [T] {
723723
impl<T> ops::IndexMut<ops::RangeToInclusive<usize>> for [T] {
724724
#[inline]
725725
fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut [T] {
726-
// SNAP 4d3eebf change this to `0...index.end`
727-
self.index_mut(ops::RangeInclusive::NonEmpty { start: 0, end: index.end })
726+
self.index_mut(0...index.end)
728727
}
729728
}
730729

@@ -872,13 +871,36 @@ macro_rules! make_mut_slice {
872871
}
873872

874873
/// Immutable slice iterator
874+
///
875+
/// # Examples
876+
///
877+
/// Basic usage:
878+
///
879+
/// ```
880+
/// // First, we declare a type which has `iter` method to get the `Iter` struct (&[usize here]):
881+
/// let slice = &[1, 2, 3];
882+
///
883+
/// // Then, we iterate over it:
884+
/// for element in slice.iter() {
885+
/// println!("{}", element);
886+
/// }
887+
/// ```
875888
#[stable(feature = "rust1", since = "1.0.0")]
876889
pub struct Iter<'a, T: 'a> {
877890
ptr: *const T,
878891
end: *const T,
879892
_marker: marker::PhantomData<&'a T>,
880893
}
881894

895+
#[stable(feature = "core_impl_debug", since = "1.9.0")]
896+
impl<'a, T: 'a + fmt::Debug> fmt::Debug for Iter<'a, T> {
897+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
898+
f.debug_tuple("Iter")
899+
.field(&self.as_slice())
900+
.finish()
901+
}
902+
}
903+
882904
#[stable(feature = "rust1", since = "1.0.0")]
883905
unsafe impl<'a, T: Sync> Sync for Iter<'a, T> {}
884906
#[stable(feature = "rust1", since = "1.0.0")]
@@ -889,6 +911,26 @@ impl<'a, T> Iter<'a, T> {
889911
///
890912
/// This has the same lifetime as the original slice, and so the
891913
/// iterator can continue to be used while this exists.
914+
///
915+
/// # Examples
916+
///
917+
/// Basic usage:
918+
///
919+
/// ```
920+
/// // First, we declare a type which has the `iter` method to get the `Iter`
921+
/// // struct (&[usize here]):
922+
/// let slice = &[1, 2, 3];
923+
///
924+
/// // Then, we get the iterator:
925+
/// let mut iter = slice.iter();
926+
/// // So if we print what `as_slice` method returns here, we have "[1, 2, 3]":
927+
/// println!("{:?}", iter.as_slice());
928+
///
929+
/// // Next, we move to the second element of the slice:
930+
/// iter.next();
931+
/// // Now `as_slice` returns "[2, 3]":
932+
/// println!("{:?}", iter.as_slice());
933+
/// ```
892934
#[stable(feature = "iter_to_slice", since = "1.4.0")]
893935
pub fn as_slice(&self) -> &'a [T] {
894936
make_slice!(self.ptr, self.end)
@@ -920,13 +962,40 @@ impl<'a, T> Clone for Iter<'a, T> {
920962
}
921963

922964
/// Mutable slice iterator.
965+
///
966+
/// # Examples
967+
///
968+
/// Basic usage:
969+
///
970+
/// ```
971+
/// // First, we declare a type which has `iter_mut` method to get the `IterMut`
972+
/// // struct (&[usize here]):
973+
/// let mut slice = &mut [1, 2, 3];
974+
///
975+
/// // Then, we iterate over it and increment each element value:
976+
/// for element in slice.iter_mut() {
977+
/// *element += 1;
978+
/// }
979+
///
980+
/// // We now have "[2, 3, 4]":
981+
/// println!("{:?}", slice);
982+
/// ```
923983
#[stable(feature = "rust1", since = "1.0.0")]
924984
pub struct IterMut<'a, T: 'a> {
925985
ptr: *mut T,
926986
end: *mut T,
927987
_marker: marker::PhantomData<&'a mut T>,
928988
}
929989

990+
#[stable(feature = "core_impl_debug", since = "1.9.0")]
991+
impl<'a, T: 'a + fmt::Debug> fmt::Debug for IterMut<'a, T> {
992+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
993+
f.debug_tuple("IterMut")
994+
.field(&make_slice!(self.ptr, self.end))
995+
.finish()
996+
}
997+
}
998+
930999
#[stable(feature = "rust1", since = "1.0.0")]
9311000
unsafe impl<'a, T: Sync> Sync for IterMut<'a, T> {}
9321001
#[stable(feature = "rust1", since = "1.0.0")]
@@ -939,6 +1008,35 @@ impl<'a, T> IterMut<'a, T> {
9391008
/// to consume the iterator. Consider using the `Slice` and
9401009
/// `SliceMut` implementations for obtaining slices with more
9411010
/// restricted lifetimes that do not consume the iterator.
1011+
///
1012+
/// # Examples
1013+
///
1014+
/// Basic usage:
1015+
///
1016+
/// ```
1017+
/// // First, we declare a type which has `iter_mut` method to get the `IterMut`
1018+
/// // struct (&[usize here]):
1019+
/// let mut slice = &mut [1, 2, 3];
1020+
///
1021+
/// {
1022+
/// // Then, we get the iterator:
1023+
/// let mut iter = slice.iter_mut();
1024+
/// // We move to next element:
1025+
/// iter.next();
1026+
/// // So if we print what `into_slice` method returns here, we have "[2, 3]":
1027+
/// println!("{:?}", iter.into_slice());
1028+
/// }
1029+
///
1030+
/// // Now let's modify a value of the slice:
1031+
/// {
1032+
/// // First we get back the iterator:
1033+
/// let mut iter = slice.iter_mut();
1034+
/// // We change the value of the first element of the slice returned by the `next` method:
1035+
/// *iter.next().unwrap() += 1;
1036+
/// }
1037+
/// // Now slice is "[2, 2, 3]":
1038+
/// println!("{:?}", slice);
1039+
/// ```
9421040
#[stable(feature = "iter_to_slice", since = "1.4.0")]
9431041
pub fn into_slice(self) -> &'a mut [T] {
9441042
make_mut_slice!(self.ptr, self.end)
@@ -982,6 +1080,16 @@ pub struct Split<'a, T:'a, P> where P: FnMut(&T) -> bool {
9821080
finished: bool
9831081
}
9841082

1083+
#[stable(feature = "core_impl_debug", since = "1.9.0")]
1084+
impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for Split<'a, T, P> where P: FnMut(&T) -> bool {
1085+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1086+
f.debug_struct("Split")
1087+
.field("v", &self.v)
1088+
.field("finished", &self.finished)
1089+
.finish()
1090+
}
1091+
}
1092+
9851093
// FIXME(#19839) Remove in favor of `#[derive(Clone)]`
9861094
#[stable(feature = "rust1", since = "1.0.0")]
9871095
impl<'a, T, P> Clone for Split<'a, T, P> where P: Clone + FnMut(&T) -> bool {
@@ -1055,6 +1163,16 @@ pub struct SplitMut<'a, T:'a, P> where P: FnMut(&T) -> bool {
10551163
finished: bool
10561164
}
10571165

1166+
#[stable(feature = "core_impl_debug", since = "1.9.0")]
1167+
impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for SplitMut<'a, T, P> where P: FnMut(&T) -> bool {
1168+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1169+
f.debug_struct("SplitMut")
1170+
.field("v", &self.v)
1171+
.field("finished", &self.finished)
1172+
.finish()
1173+
}
1174+
}
1175+
10581176
impl<'a, T, P> SplitIter for SplitMut<'a, T, P> where P: FnMut(&T) -> bool {
10591177
#[inline]
10601178
fn finish(&mut self) -> Option<&'a mut [T]> {
@@ -1129,6 +1247,7 @@ impl<'a, T, P> DoubleEndedIterator for SplitMut<'a, T, P> where
11291247
/// An private iterator over subslices separated by elements that
11301248
/// match a predicate function, splitting at most a fixed number of
11311249
/// times.
1250+
#[derive(Debug)]
11321251
struct GenericSplitN<I> {
11331252
iter: I,
11341253
count: usize,
@@ -1164,6 +1283,15 @@ pub struct SplitN<'a, T: 'a, P> where P: FnMut(&T) -> bool {
11641283
inner: GenericSplitN<Split<'a, T, P>>
11651284
}
11661285

1286+
#[stable(feature = "core_impl_debug", since = "1.9.0")]
1287+
impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for SplitN<'a, T, P> where P: FnMut(&T) -> bool {
1288+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1289+
f.debug_struct("SplitN")
1290+
.field("inner", &self.inner)
1291+
.finish()
1292+
}
1293+
}
1294+
11671295
/// An iterator over subslices separated by elements that match a
11681296
/// predicate function, limited to a given number of splits, starting
11691297
/// from the end of the slice.
@@ -1172,13 +1300,31 @@ pub struct RSplitN<'a, T: 'a, P> where P: FnMut(&T) -> bool {
11721300
inner: GenericSplitN<Split<'a, T, P>>
11731301
}
11741302

1303+
#[stable(feature = "core_impl_debug", since = "1.9.0")]
1304+
impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for RSplitN<'a, T, P> where P: FnMut(&T) -> bool {
1305+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1306+
f.debug_struct("RSplitN")
1307+
.field("inner", &self.inner)
1308+
.finish()
1309+
}
1310+
}
1311+
11751312
/// An iterator over subslices separated by elements that match a predicate
11761313
/// function, limited to a given number of splits.
11771314
#[stable(feature = "rust1", since = "1.0.0")]
11781315
pub struct SplitNMut<'a, T: 'a, P> where P: FnMut(&T) -> bool {
11791316
inner: GenericSplitN<SplitMut<'a, T, P>>
11801317
}
11811318

1319+
#[stable(feature = "core_impl_debug", since = "1.9.0")]
1320+
impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for SplitNMut<'a, T, P> where P: FnMut(&T) -> bool {
1321+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1322+
f.debug_struct("SplitNMut")
1323+
.field("inner", &self.inner)
1324+
.finish()
1325+
}
1326+
}
1327+
11821328
/// An iterator over subslices separated by elements that match a
11831329
/// predicate function, limited to a given number of splits, starting
11841330
/// from the end of the slice.
@@ -1187,6 +1333,15 @@ pub struct RSplitNMut<'a, T: 'a, P> where P: FnMut(&T) -> bool {
11871333
inner: GenericSplitN<SplitMut<'a, T, P>>
11881334
}
11891335

1336+
#[stable(feature = "core_impl_debug", since = "1.9.0")]
1337+
impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for RSplitNMut<'a, T, P> where P: FnMut(&T) -> bool {
1338+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1339+
f.debug_struct("RSplitNMut")
1340+
.field("inner", &self.inner)
1341+
.finish()
1342+
}
1343+
}
1344+
11901345
macro_rules! forward_iterator {
11911346
($name:ident: $elem:ident, $iter_of:ty) => {
11921347
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1214,6 +1369,7 @@ forward_iterator! { SplitNMut: T, &'a mut [T] }
12141369
forward_iterator! { RSplitNMut: T, &'a mut [T] }
12151370

12161371
/// An iterator over overlapping subslices of length `size`.
1372+
#[derive(Debug)]
12171373
#[stable(feature = "rust1", since = "1.0.0")]
12181374
pub struct Windows<'a, T:'a> {
12191375
v: &'a [T],
@@ -1307,6 +1463,7 @@ impl<'a, T> ExactSizeIterator for Windows<'a, T> {}
13071463
///
13081464
/// When the slice len is not evenly divided by the chunk size, the last slice
13091465
/// of the iteration will be the remainder.
1466+
#[derive(Debug)]
13101467
#[stable(feature = "rust1", since = "1.0.0")]
13111468
pub struct Chunks<'a, T:'a> {
13121469
v: &'a [T],
@@ -1407,6 +1564,7 @@ impl<'a, T> ExactSizeIterator for Chunks<'a, T> {}
14071564
/// An iterator over a slice in (non-overlapping) mutable chunks (`size`
14081565
/// elements at a time). When the slice len is not evenly divided by the chunk
14091566
/// size, the last slice of the iteration will be the remainder.
1567+
#[derive(Debug)]
14101568
#[stable(feature = "rust1", since = "1.0.0")]
14111569
pub struct ChunksMut<'a, T:'a> {
14121570
v: &'a mut [T],

0 commit comments

Comments
 (0)