@@ -871,6 +871,20 @@ macro_rules! make_mut_slice {
871871}
872872
873873/// 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+ /// ```
874888#[ stable( feature = "rust1" , since = "1.0.0" ) ]
875889pub struct Iter < ' a , T : ' a > {
876890 ptr : * const T ,
@@ -897,6 +911,26 @@ impl<'a, T> Iter<'a, T> {
897911 ///
898912 /// This has the same lifetime as the original slice, and so the
899913 /// 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+ /// ```
900934 #[ stable( feature = "iter_to_slice" , since = "1.4.0" ) ]
901935 pub fn as_slice ( & self ) -> & ' a [ T ] {
902936 make_slice ! ( self . ptr, self . end)
@@ -928,6 +962,24 @@ impl<'a, T> Clone for Iter<'a, T> {
928962}
929963
930964/// 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+ /// ```
931983#[ stable( feature = "rust1" , since = "1.0.0" ) ]
932984pub struct IterMut < ' a , T : ' a > {
933985 ptr : * mut T ,
@@ -956,6 +1008,35 @@ impl<'a, T> IterMut<'a, T> {
9561008 /// to consume the iterator. Consider using the `Slice` and
9571009 /// `SliceMut` implementations for obtaining slices with more
9581010 /// 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+ /// ```
9591040 #[ stable( feature = "iter_to_slice" , since = "1.4.0" ) ]
9601041 pub fn into_slice ( self ) -> & ' a mut [ T ] {
9611042 make_mut_slice ! ( self . ptr, self . end)
0 commit comments