@@ -729,7 +729,8 @@ impl<T> [T] {
729
729
/// Returns an iterator over `chunk_size` elements of the slice at a
730
730
/// time. The chunks are slices and do not overlap. If `chunk_size` does
731
731
/// not divide the length of the slice, then the last up to `chunk_size-1`
732
- /// elements will be omitted.
732
+ /// elements will be omitted and can be retrieved from the `remainder`
733
+ /// function of the iterator.
733
734
///
734
735
/// Due to each chunk having exactly `chunk_size` elements, the compiler
735
736
/// can often optimize the resulting code better than in the case of
@@ -758,14 +759,15 @@ impl<T> [T] {
758
759
assert ! ( chunk_size != 0 ) ;
759
760
let rem = self . len ( ) % chunk_size;
760
761
let len = self . len ( ) - rem;
761
- ExactChunks { v : & self [ ..len] , chunk_size : chunk_size}
762
+ let ( fst, snd) = self . split_at ( len) ;
763
+ ExactChunks { v : fst, rem : snd, chunk_size : chunk_size}
762
764
}
763
765
764
766
/// Returns an iterator over `chunk_size` elements of the slice at a time.
765
767
/// The chunks are mutable slices, and do not overlap. If `chunk_size` does
766
768
/// not divide the length of the slice, then the last up to `chunk_size-1`
767
- /// elements will be omitted.
768
- ///
769
+ /// elements will be omitted and can be retrieved from the `into_remainder`
770
+ /// function of the iterator.
769
771
///
770
772
/// Due to each chunk having exactly `chunk_size` elements, the compiler
771
773
/// can often optimize the resulting code better than in the case of
@@ -799,7 +801,8 @@ impl<T> [T] {
799
801
assert ! ( chunk_size != 0 ) ;
800
802
let rem = self . len ( ) % chunk_size;
801
803
let len = self . len ( ) - rem;
802
- ExactChunksMut { v : & mut self [ ..len] , chunk_size : chunk_size}
804
+ let ( fst, snd) = self . split_at_mut ( len) ;
805
+ ExactChunksMut { v : fst, rem : snd, chunk_size : chunk_size}
803
806
}
804
807
805
808
/// Divides one slice into two at an index.
@@ -3654,25 +3657,39 @@ unsafe impl<'a, T> TrustedRandomAccess for ChunksMut<'a, T> {
3654
3657
/// time).
3655
3658
///
3656
3659
/// When the slice len is not evenly divided by the chunk size, the last
3657
- /// up to `chunk_size-1` elements will be omitted.
3660
+ /// up to `chunk_size-1` elements will be omitted but can be retrieved from
3661
+ /// the [`remainder`] function from the iterator.
3658
3662
///
3659
3663
/// This struct is created by the [`exact_chunks`] method on [slices].
3660
3664
///
3661
3665
/// [`exact_chunks`]: ../../std/primitive.slice.html#method.exact_chunks
3666
+ /// [`remainder`]: ../../std/slice/struct.ExactChunks.html#method.remainder
3662
3667
/// [slices]: ../../std/primitive.slice.html
3663
3668
#[ derive( Debug ) ]
3664
3669
#[ unstable( feature = "exact_chunks" , issue = "47115" ) ]
3665
3670
pub struct ExactChunks < ' a , T : ' a > {
3666
3671
v : & ' a [ T ] ,
3672
+ rem : & ' a [ T ] ,
3667
3673
chunk_size : usize
3668
3674
}
3669
3675
3676
+ #[ unstable( feature = "exact_chunks" , issue = "47115" ) ]
3677
+ impl < ' a , T > ExactChunks < ' a , T > {
3678
+ /// Return the remainder of the original slice that is not going to be
3679
+ /// returned by the iterator. The returned slice has at most `chunk_size-1`
3680
+ /// elements.
3681
+ pub fn remainder ( & self ) -> & ' a [ T ] {
3682
+ self . rem
3683
+ }
3684
+ }
3685
+
3670
3686
// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
3671
3687
#[ unstable( feature = "exact_chunks" , issue = "47115" ) ]
3672
3688
impl < ' a , T > Clone for ExactChunks < ' a , T > {
3673
3689
fn clone ( & self ) -> ExactChunks < ' a , T > {
3674
3690
ExactChunks {
3675
3691
v : self . v ,
3692
+ rem : self . rem ,
3676
3693
chunk_size : self . chunk_size ,
3677
3694
}
3678
3695
}
@@ -3760,20 +3777,35 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunks<'a, T> {
3760
3777
}
3761
3778
3762
3779
/// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
3763
- /// elements at a time). When the slice len is not evenly divided by the chunk
3764
- /// size, the last up to `chunk_size-1` elements will be omitted.
3780
+ /// elements at a time).
3781
+ ///
3782
+ /// When the slice len is not evenly divided by the chunk size, the last up to
3783
+ /// `chunk_size-1` elements will be omitted but can be retrieved from the
3784
+ /// [`into_remainder`] function from the iterator.
3765
3785
///
3766
3786
/// This struct is created by the [`exact_chunks_mut`] method on [slices].
3767
3787
///
3768
3788
/// [`exact_chunks_mut`]: ../../std/primitive.slice.html#method.exact_chunks_mut
3789
+ /// [`into_remainder`]: ../../std/slice/struct.ExactChunksMut.html#method.into_remainder
3769
3790
/// [slices]: ../../std/primitive.slice.html
3770
3791
#[ derive( Debug ) ]
3771
3792
#[ unstable( feature = "exact_chunks" , issue = "47115" ) ]
3772
3793
pub struct ExactChunksMut < ' a , T : ' a > {
3773
3794
v : & ' a mut [ T ] ,
3795
+ rem : & ' a mut [ T ] ,
3774
3796
chunk_size : usize
3775
3797
}
3776
3798
3799
+ #[ unstable( feature = "exact_chunks" , issue = "47115" ) ]
3800
+ impl < ' a , T > ExactChunksMut < ' a , T > {
3801
+ /// Return the remainder of the original slice that is not going to be
3802
+ /// returned by the iterator. The returned slice has at most `chunk_size-1`
3803
+ /// elements.
3804
+ pub fn into_remainder ( self ) -> & ' a mut [ T ] {
3805
+ self . rem
3806
+ }
3807
+ }
3808
+
3777
3809
#[ unstable( feature = "exact_chunks" , issue = "47115" ) ]
3778
3810
impl < ' a , T > Iterator for ExactChunksMut < ' a , T > {
3779
3811
type Item = & ' a mut [ T ] ;
0 commit comments