1 // ignore-tidy-filelength
3 //! Slice management and manipulation.
5 //! For more details see [`std::slice`].
7 //! [`std::slice`]: ../../std/slice/index.html
9 #![stable(feature = "rust1", since = "1.0.0")]
11 // How this module is organized.
13 // The library infrastructure for slices is fairly messy. There's
14 // a lot of stuff defined here. Let's keep it clean.
16 // The layout of this file is thus:
18 // * Inherent methods. This is where most of the slice API resides.
19 // * Implementations of a few common traits with important slice ops.
20 // * Definitions of a bunch of iterators.
22 // * The `raw` and `bytes` submodules.
23 // * Boilerplate trait implementations.
25 use crate::cmp::Ordering::{self, Less, Equal, Greater};
28 use crate::intrinsics::assume;
31 use crate::ops::{FnMut, Try, self};
32 use crate::option::Option;
33 use crate::option::Option::{None, Some};
34 use crate::result::Result;
35 use crate::result::Result::{Ok, Err};
38 use crate::marker::{Copy, Send, Sync, Sized, self};
40 #[unstable(feature = "slice_internals", issue = "0",
41 reason = "exposed from core to be reused in std; use the memchr crate")]
42 /// Pure rust memchr implementation, taken from rust-memchr
49 union Repr<'a, T: 'a> {
51 rust_mut: &'a mut [T],
68 /// Returns the number of elements in the slice.
73 /// let a = [1, 2, 3];
74 /// assert_eq!(a.len(), 3);
76 #[stable(feature = "rust1", since = "1.0.0")]
78 #[rustc_const_unstable(feature = "const_slice_len")]
79 pub const fn len(&self) -> usize {
81 Repr { rust: self }.raw.len
85 /// Returns `true` if the slice has a length of 0.
90 /// let a = [1, 2, 3];
91 /// assert!(!a.is_empty());
93 #[stable(feature = "rust1", since = "1.0.0")]
95 #[rustc_const_unstable(feature = "const_slice_len")]
96 pub const fn is_empty(&self) -> bool {
100 /// Returns the first element of the slice, or `None` if it is empty.
105 /// let v = [10, 40, 30];
106 /// assert_eq!(Some(&10), v.first());
108 /// let w: &[i32] = &[];
109 /// assert_eq!(None, w.first());
111 #[stable(feature = "rust1", since = "1.0.0")]
113 pub fn first(&self) -> Option<&T> {
117 /// Returns a mutable pointer to the first element of the slice, or `None` if it is empty.
122 /// let x = &mut [0, 1, 2];
124 /// if let Some(first) = x.first_mut() {
127 /// assert_eq!(x, &[5, 1, 2]);
129 #[stable(feature = "rust1", since = "1.0.0")]
131 pub fn first_mut(&mut self) -> Option<&mut T> {
135 /// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
140 /// let x = &[0, 1, 2];
142 /// if let Some((first, elements)) = x.split_first() {
143 /// assert_eq!(first, &0);
144 /// assert_eq!(elements, &[1, 2]);
147 #[stable(feature = "slice_splits", since = "1.5.0")]
149 pub fn split_first(&self) -> Option<(&T, &[T])> {
150 if self.is_empty() { None } else { Some((&self[0], &self[1..])) }
153 /// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
158 /// let x = &mut [0, 1, 2];
160 /// if let Some((first, elements)) = x.split_first_mut() {
165 /// assert_eq!(x, &[3, 4, 5]);
167 #[stable(feature = "slice_splits", since = "1.5.0")]
169 pub fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])> {
170 if self.is_empty() { None } else {
171 let split = self.split_at_mut(1);
172 Some((&mut split.0[0], split.1))
176 /// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
181 /// let x = &[0, 1, 2];
183 /// if let Some((last, elements)) = x.split_last() {
184 /// assert_eq!(last, &2);
185 /// assert_eq!(elements, &[0, 1]);
188 #[stable(feature = "slice_splits", since = "1.5.0")]
190 pub fn split_last(&self) -> Option<(&T, &[T])> {
191 let len = self.len();
192 if len == 0 { None } else { Some((&self[len - 1], &self[..(len - 1)])) }
195 /// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
200 /// let x = &mut [0, 1, 2];
202 /// if let Some((last, elements)) = x.split_last_mut() {
207 /// assert_eq!(x, &[4, 5, 3]);
209 #[stable(feature = "slice_splits", since = "1.5.0")]
211 pub fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])> {
212 let len = self.len();
213 if len == 0 { None } else {
214 let split = self.split_at_mut(len - 1);
215 Some((&mut split.1[0], split.0))
220 /// Returns the last element of the slice, or `None` if it is empty.
225 /// let v = [10, 40, 30];
226 /// assert_eq!(Some(&30), v.last());
228 /// let w: &[i32] = &[];
229 /// assert_eq!(None, w.last());
231 #[stable(feature = "rust1", since = "1.0.0")]
233 pub fn last(&self) -> Option<&T> {
234 let last_idx = self.len().checked_sub(1)?;
238 /// Returns a mutable pointer to the last item in the slice.
243 /// let x = &mut [0, 1, 2];
245 /// if let Some(last) = x.last_mut() {
248 /// assert_eq!(x, &[0, 1, 10]);
250 #[stable(feature = "rust1", since = "1.0.0")]
252 pub fn last_mut(&mut self) -> Option<&mut T> {
253 let last_idx = self.len().checked_sub(1)?;
254 self.get_mut(last_idx)
257 /// Returns a reference to an element or subslice depending on the type of
260 /// - If given a position, returns a reference to the element at that
261 /// position or `None` if out of bounds.
262 /// - If given a range, returns the subslice corresponding to that range,
263 /// or `None` if out of bounds.
268 /// let v = [10, 40, 30];
269 /// assert_eq!(Some(&40), v.get(1));
270 /// assert_eq!(Some(&[10, 40][..]), v.get(0..2));
271 /// assert_eq!(None, v.get(3));
272 /// assert_eq!(None, v.get(0..4));
274 #[stable(feature = "rust1", since = "1.0.0")]
276 pub fn get<I>(&self, index: I) -> Option<&I::Output>
277 where I: SliceIndex<Self>
282 /// Returns a mutable reference to an element or subslice depending on the
283 /// type of index (see [`get`]) or `None` if the index is out of bounds.
285 /// [`get`]: #method.get
290 /// let x = &mut [0, 1, 2];
292 /// if let Some(elem) = x.get_mut(1) {
295 /// assert_eq!(x, &[0, 42, 2]);
297 #[stable(feature = "rust1", since = "1.0.0")]
299 pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
300 where I: SliceIndex<Self>
305 /// Returns a reference to an element or subslice, without doing bounds
308 /// This is generally not recommended, use with caution! For a safe
309 /// alternative see [`get`].
311 /// [`get`]: #method.get
316 /// let x = &[1, 2, 4];
319 /// assert_eq!(x.get_unchecked(1), &2);
322 #[stable(feature = "rust1", since = "1.0.0")]
324 pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
325 where I: SliceIndex<Self>
327 index.get_unchecked(self)
330 /// Returns a mutable reference to an element or subslice, without doing
333 /// This is generally not recommended, use with caution! For a safe
334 /// alternative see [`get_mut`].
336 /// [`get_mut`]: #method.get_mut
341 /// let x = &mut [1, 2, 4];
344 /// let elem = x.get_unchecked_mut(1);
347 /// assert_eq!(x, &[1, 13, 4]);
349 #[stable(feature = "rust1", since = "1.0.0")]
351 pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
352 where I: SliceIndex<Self>
354 index.get_unchecked_mut(self)
357 /// Returns a raw pointer to the slice's buffer.
359 /// The caller must ensure that the slice outlives the pointer this
360 /// function returns, or else it will end up pointing to garbage.
362 /// The caller must also ensure that the memory the pointer (non-transitively) points to
363 /// is never written to (except inside an `UnsafeCell`) using this pointer or any pointer
364 /// derived from it. If you need to mutate the contents of the slice, use [`as_mut_ptr`].
366 /// Modifying the container referenced by this slice may cause its buffer
367 /// to be reallocated, which would also make any pointers to it invalid.
372 /// let x = &[1, 2, 4];
373 /// let x_ptr = x.as_ptr();
376 /// for i in 0..x.len() {
377 /// assert_eq!(x.get_unchecked(i), &*x_ptr.add(i));
382 /// [`as_mut_ptr`]: #method.as_mut_ptr
383 #[stable(feature = "rust1", since = "1.0.0")]
385 pub const fn as_ptr(&self) -> *const T {
386 self as *const [T] as *const T
389 /// Returns an unsafe mutable pointer to the slice's buffer.
391 /// The caller must ensure that the slice outlives the pointer this
392 /// function returns, or else it will end up pointing to garbage.
394 /// Modifying the container referenced by this slice may cause its buffer
395 /// to be reallocated, which would also make any pointers to it invalid.
400 /// let x = &mut [1, 2, 4];
401 /// let x_ptr = x.as_mut_ptr();
404 /// for i in 0..x.len() {
405 /// *x_ptr.add(i) += 2;
408 /// assert_eq!(x, &[3, 4, 6]);
410 #[stable(feature = "rust1", since = "1.0.0")]
412 pub fn as_mut_ptr(&mut self) -> *mut T {
413 self as *mut [T] as *mut T
416 /// Swaps two elements in the slice.
420 /// * a - The index of the first element
421 /// * b - The index of the second element
425 /// Panics if `a` or `b` are out of bounds.
430 /// let mut v = ["a", "b", "c", "d"];
432 /// assert!(v == ["a", "d", "c", "b"]);
434 #[stable(feature = "rust1", since = "1.0.0")]
436 pub fn swap(&mut self, a: usize, b: usize) {
438 // Can't take two mutable loans from one vector, so instead just cast
439 // them to their raw pointers to do the swap
440 let pa: *mut T = &mut self[a];
441 let pb: *mut T = &mut self[b];
446 /// Reverses the order of elements in the slice, in place.
451 /// let mut v = [1, 2, 3];
453 /// assert!(v == [3, 2, 1]);
455 #[stable(feature = "rust1", since = "1.0.0")]
457 pub fn reverse(&mut self) {
458 let mut i: usize = 0;
461 // For very small types, all the individual reads in the normal
462 // path perform poorly. We can do better, given efficient unaligned
463 // load/store, by loading a larger chunk and reversing a register.
465 // Ideally LLVM would do this for us, as it knows better than we do
466 // whether unaligned reads are efficient (since that changes between
467 // different ARM versions, for example) and what the best chunk size
468 // would be. Unfortunately, as of LLVM 4.0 (2017-05) it only unrolls
469 // the loop, so we need to do this ourselves. (Hypothesis: reverse
470 // is troublesome because the sides can be aligned differently --
471 // will be, when the length is odd -- so there's no way of emitting
472 // pre- and postludes to use fully-aligned SIMD in the middle.)
475 cfg!(any(target_arch = "x86", target_arch = "x86_64"));
477 if fast_unaligned && mem::size_of::<T>() == 1 {
478 // Use the llvm.bswap intrinsic to reverse u8s in a usize
479 let chunk = mem::size_of::<usize>();
480 while i + chunk - 1 < ln / 2 {
482 let pa: *mut T = self.get_unchecked_mut(i);
483 let pb: *mut T = self.get_unchecked_mut(ln - i - chunk);
484 let va = ptr::read_unaligned(pa as *mut usize);
485 let vb = ptr::read_unaligned(pb as *mut usize);
486 ptr::write_unaligned(pa as *mut usize, vb.swap_bytes());
487 ptr::write_unaligned(pb as *mut usize, va.swap_bytes());
493 if fast_unaligned && mem::size_of::<T>() == 2 {
494 // Use rotate-by-16 to reverse u16s in a u32
495 let chunk = mem::size_of::<u32>() / 2;
496 while i + chunk - 1 < ln / 2 {
498 let pa: *mut T = self.get_unchecked_mut(i);
499 let pb: *mut T = self.get_unchecked_mut(ln - i - chunk);
500 let va = ptr::read_unaligned(pa as *mut u32);
501 let vb = ptr::read_unaligned(pb as *mut u32);
502 ptr::write_unaligned(pa as *mut u32, vb.rotate_left(16));
503 ptr::write_unaligned(pb as *mut u32, va.rotate_left(16));
510 // Unsafe swap to avoid the bounds check in safe swap.
512 let pa: *mut T = self.get_unchecked_mut(i);
513 let pb: *mut T = self.get_unchecked_mut(ln - i - 1);
520 /// Returns an iterator over the slice.
525 /// let x = &[1, 2, 4];
526 /// let mut iterator = x.iter();
528 /// assert_eq!(iterator.next(), Some(&1));
529 /// assert_eq!(iterator.next(), Some(&2));
530 /// assert_eq!(iterator.next(), Some(&4));
531 /// assert_eq!(iterator.next(), None);
533 #[stable(feature = "rust1", since = "1.0.0")]
535 pub fn iter(&self) -> Iter<'_, T> {
537 let ptr = self.as_ptr();
538 assume(!ptr.is_null());
540 let end = if mem::size_of::<T>() == 0 {
541 (ptr as *const u8).wrapping_add(self.len()) as *const T
549 _marker: marker::PhantomData
554 /// Returns an iterator that allows modifying each value.
559 /// let x = &mut [1, 2, 4];
560 /// for elem in x.iter_mut() {
563 /// assert_eq!(x, &[3, 4, 6]);
565 #[stable(feature = "rust1", since = "1.0.0")]
567 pub fn iter_mut(&mut self) -> IterMut<'_, T> {
569 let ptr = self.as_mut_ptr();
570 assume(!ptr.is_null());
572 let end = if mem::size_of::<T>() == 0 {
573 (ptr as *mut u8).wrapping_add(self.len()) as *mut T
581 _marker: marker::PhantomData
586 /// Returns an iterator over all contiguous windows of length
587 /// `size`. The windows overlap. If the slice is shorter than
588 /// `size`, the iterator returns no values.
592 /// Panics if `size` is 0.
597 /// let slice = ['r', 'u', 's', 't'];
598 /// let mut iter = slice.windows(2);
599 /// assert_eq!(iter.next().unwrap(), &['r', 'u']);
600 /// assert_eq!(iter.next().unwrap(), &['u', 's']);
601 /// assert_eq!(iter.next().unwrap(), &['s', 't']);
602 /// assert!(iter.next().is_none());
605 /// If the slice is shorter than `size`:
608 /// let slice = ['f', 'o', 'o'];
609 /// let mut iter = slice.windows(4);
610 /// assert!(iter.next().is_none());
612 #[stable(feature = "rust1", since = "1.0.0")]
614 pub fn windows(&self, size: usize) -> Windows<'_, T> {
616 Windows { v: self, size }
619 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
620 /// beginning of the slice.
622 /// The chunks are slices and do not overlap. If `chunk_size` does not divide the length of the
623 /// slice, then the last chunk will not have length `chunk_size`.
625 /// See [`chunks_exact`] for a variant of this iterator that returns chunks of always exactly
626 /// `chunk_size` elements, and [`rchunks`] for the same iterator but starting at the end of the
627 /// slice of the slice.
631 /// Panics if `chunk_size` is 0.
636 /// let slice = ['l', 'o', 'r', 'e', 'm'];
637 /// let mut iter = slice.chunks(2);
638 /// assert_eq!(iter.next().unwrap(), &['l', 'o']);
639 /// assert_eq!(iter.next().unwrap(), &['r', 'e']);
640 /// assert_eq!(iter.next().unwrap(), &['m']);
641 /// assert!(iter.next().is_none());
644 /// [`chunks_exact`]: #method.chunks_exact
645 /// [`rchunks`]: #method.rchunks
646 #[stable(feature = "rust1", since = "1.0.0")]
648 pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> {
649 assert!(chunk_size != 0);
650 Chunks { v: self, chunk_size }
653 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
654 /// beginning of the slice.
656 /// The chunks are mutable slices, and do not overlap. If `chunk_size` does not divide the
657 /// length of the slice, then the last chunk will not have length `chunk_size`.
659 /// See [`chunks_exact_mut`] for a variant of this iterator that returns chunks of always
660 /// exactly `chunk_size` elements, and [`rchunks_mut`] for the same iterator but starting at
661 /// the end of the slice of the slice.
665 /// Panics if `chunk_size` is 0.
670 /// let v = &mut [0, 0, 0, 0, 0];
671 /// let mut count = 1;
673 /// for chunk in v.chunks_mut(2) {
674 /// for elem in chunk.iter_mut() {
679 /// assert_eq!(v, &[1, 1, 2, 2, 3]);
682 /// [`chunks_exact_mut`]: #method.chunks_exact_mut
683 /// [`rchunks_mut`]: #method.rchunks_mut
684 #[stable(feature = "rust1", since = "1.0.0")]
686 pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> {
687 assert!(chunk_size != 0);
688 ChunksMut { v: self, chunk_size }
691 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
692 /// beginning of the slice.
694 /// The chunks are slices and do not overlap. If `chunk_size` does not divide the length of the
695 /// slice, then the last up to `chunk_size-1` elements will be omitted and can be retrieved
696 /// from the `remainder` function of the iterator.
698 /// Due to each chunk having exactly `chunk_size` elements, the compiler can often optimize the
699 /// resulting code better than in the case of [`chunks`].
701 /// See [`chunks`] for a variant of this iterator that also returns the remainder as a smaller
702 /// chunk, and [`rchunks_exact`] for the same iterator but starting at the end of the slice.
706 /// Panics if `chunk_size` is 0.
711 /// let slice = ['l', 'o', 'r', 'e', 'm'];
712 /// let mut iter = slice.chunks_exact(2);
713 /// assert_eq!(iter.next().unwrap(), &['l', 'o']);
714 /// assert_eq!(iter.next().unwrap(), &['r', 'e']);
715 /// assert!(iter.next().is_none());
716 /// assert_eq!(iter.remainder(), &['m']);
719 /// [`chunks`]: #method.chunks
720 /// [`rchunks_exact`]: #method.rchunks_exact
721 #[stable(feature = "chunks_exact", since = "1.31.0")]
723 pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> {
724 assert!(chunk_size != 0);
725 let rem = self.len() % chunk_size;
726 let len = self.len() - rem;
727 let (fst, snd) = self.split_at(len);
728 ChunksExact { v: fst, rem: snd, chunk_size }
731 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
732 /// beginning of the slice.
734 /// The chunks are mutable slices, and do not overlap. If `chunk_size` does not divide the
735 /// length of the slice, then the last up to `chunk_size-1` elements will be omitted and can be
736 /// retrieved from the `into_remainder` function of the iterator.
738 /// Due to each chunk having exactly `chunk_size` elements, the compiler can often optimize the
739 /// resulting code better than in the case of [`chunks_mut`].
741 /// See [`chunks_mut`] for a variant of this iterator that also returns the remainder as a
742 /// smaller chunk, and [`rchunks_exact_mut`] for the same iterator but starting at the end of
743 /// the slice of the slice.
747 /// Panics if `chunk_size` is 0.
752 /// let v = &mut [0, 0, 0, 0, 0];
753 /// let mut count = 1;
755 /// for chunk in v.chunks_exact_mut(2) {
756 /// for elem in chunk.iter_mut() {
761 /// assert_eq!(v, &[1, 1, 2, 2, 0]);
764 /// [`chunks_mut`]: #method.chunks_mut
765 /// [`rchunks_exact_mut`]: #method.rchunks_exact_mut
766 #[stable(feature = "chunks_exact", since = "1.31.0")]
768 pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> {
769 assert!(chunk_size != 0);
770 let rem = self.len() % chunk_size;
771 let len = self.len() - rem;
772 let (fst, snd) = self.split_at_mut(len);
773 ChunksExactMut { v: fst, rem: snd, chunk_size }
776 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end
779 /// The chunks are slices and do not overlap. If `chunk_size` does not divide the length of the
780 /// slice, then the last chunk will not have length `chunk_size`.
782 /// See [`rchunks_exact`] for a variant of this iterator that returns chunks of always exactly
783 /// `chunk_size` elements, and [`chunks`] for the same iterator but starting at the beginning
788 /// Panics if `chunk_size` is 0.
793 /// let slice = ['l', 'o', 'r', 'e', 'm'];
794 /// let mut iter = slice.rchunks(2);
795 /// assert_eq!(iter.next().unwrap(), &['e', 'm']);
796 /// assert_eq!(iter.next().unwrap(), &['o', 'r']);
797 /// assert_eq!(iter.next().unwrap(), &['l']);
798 /// assert!(iter.next().is_none());
801 /// [`rchunks_exact`]: #method.rchunks_exact
802 /// [`chunks`]: #method.chunks
803 #[stable(feature = "rchunks", since = "1.31.0")]
805 pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> {
806 assert!(chunk_size != 0);
807 RChunks { v: self, chunk_size }
810 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end
813 /// The chunks are mutable slices, and do not overlap. If `chunk_size` does not divide the
814 /// length of the slice, then the last chunk will not have length `chunk_size`.
816 /// See [`rchunks_exact_mut`] for a variant of this iterator that returns chunks of always
817 /// exactly `chunk_size` elements, and [`chunks_mut`] for the same iterator but starting at the
818 /// beginning of the slice.
822 /// Panics if `chunk_size` is 0.
827 /// let v = &mut [0, 0, 0, 0, 0];
828 /// let mut count = 1;
830 /// for chunk in v.rchunks_mut(2) {
831 /// for elem in chunk.iter_mut() {
836 /// assert_eq!(v, &[3, 2, 2, 1, 1]);
839 /// [`rchunks_exact_mut`]: #method.rchunks_exact_mut
840 /// [`chunks_mut`]: #method.chunks_mut
841 #[stable(feature = "rchunks", since = "1.31.0")]
843 pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> {
844 assert!(chunk_size != 0);
845 RChunksMut { v: self, chunk_size }
848 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
849 /// end of the slice.
851 /// The chunks are slices and do not overlap. If `chunk_size` does not divide the length of the
852 /// slice, then the last up to `chunk_size-1` elements will be omitted and can be retrieved
853 /// from the `remainder` function of the iterator.
855 /// Due to each chunk having exactly `chunk_size` elements, the compiler can often optimize the
856 /// resulting code better than in the case of [`chunks`].
858 /// See [`rchunks`] for a variant of this iterator that also returns the remainder as a smaller
859 /// chunk, and [`chunks_exact`] for the same iterator but starting at the beginning of the
864 /// Panics if `chunk_size` is 0.
869 /// let slice = ['l', 'o', 'r', 'e', 'm'];
870 /// let mut iter = slice.rchunks_exact(2);
871 /// assert_eq!(iter.next().unwrap(), &['e', 'm']);
872 /// assert_eq!(iter.next().unwrap(), &['o', 'r']);
873 /// assert!(iter.next().is_none());
874 /// assert_eq!(iter.remainder(), &['l']);
877 /// [`chunks`]: #method.chunks
878 /// [`rchunks`]: #method.rchunks
879 /// [`chunks_exact`]: #method.chunks_exact
880 #[stable(feature = "rchunks", since = "1.31.0")]
882 pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> {
883 assert!(chunk_size != 0);
884 let rem = self.len() % chunk_size;
885 let (fst, snd) = self.split_at(rem);
886 RChunksExact { v: snd, rem: fst, chunk_size }
889 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end
892 /// The chunks are mutable slices, and do not overlap. If `chunk_size` does not divide the
893 /// length of the slice, then the last up to `chunk_size-1` elements will be omitted and can be
894 /// retrieved from the `into_remainder` function of the iterator.
896 /// Due to each chunk having exactly `chunk_size` elements, the compiler can often optimize the
897 /// resulting code better than in the case of [`chunks_mut`].
899 /// See [`rchunks_mut`] for a variant of this iterator that also returns the remainder as a
900 /// smaller chunk, and [`chunks_exact_mut`] for the same iterator but starting at the beginning
905 /// Panics if `chunk_size` is 0.
910 /// let v = &mut [0, 0, 0, 0, 0];
911 /// let mut count = 1;
913 /// for chunk in v.rchunks_exact_mut(2) {
914 /// for elem in chunk.iter_mut() {
919 /// assert_eq!(v, &[0, 2, 2, 1, 1]);
922 /// [`chunks_mut`]: #method.chunks_mut
923 /// [`rchunks_mut`]: #method.rchunks_mut
924 /// [`chunks_exact_mut`]: #method.chunks_exact_mut
925 #[stable(feature = "rchunks", since = "1.31.0")]
927 pub fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> {
928 assert!(chunk_size != 0);
929 let rem = self.len() % chunk_size;
930 let (fst, snd) = self.split_at_mut(rem);
931 RChunksExactMut { v: snd, rem: fst, chunk_size }
934 /// Divides one slice into two at an index.
936 /// The first will contain all indices from `[0, mid)` (excluding
937 /// the index `mid` itself) and the second will contain all
938 /// indices from `[mid, len)` (excluding the index `len` itself).
942 /// Panics if `mid > len`.
947 /// let v = [1, 2, 3, 4, 5, 6];
950 /// let (left, right) = v.split_at(0);
951 /// assert!(left == []);
952 /// assert!(right == [1, 2, 3, 4, 5, 6]);
956 /// let (left, right) = v.split_at(2);
957 /// assert!(left == [1, 2]);
958 /// assert!(right == [3, 4, 5, 6]);
962 /// let (left, right) = v.split_at(6);
963 /// assert!(left == [1, 2, 3, 4, 5, 6]);
964 /// assert!(right == []);
967 #[stable(feature = "rust1", since = "1.0.0")]
969 pub fn split_at(&self, mid: usize) -> (&[T], &[T]) {
970 (&self[..mid], &self[mid..])
973 /// Divides one mutable slice into two at an index.
975 /// The first will contain all indices from `[0, mid)` (excluding
976 /// the index `mid` itself) and the second will contain all
977 /// indices from `[mid, len)` (excluding the index `len` itself).
981 /// Panics if `mid > len`.
986 /// let mut v = [1, 0, 3, 0, 5, 6];
987 /// // scoped to restrict the lifetime of the borrows
989 /// let (left, right) = v.split_at_mut(2);
990 /// assert!(left == [1, 0]);
991 /// assert!(right == [3, 0, 5, 6]);
995 /// assert!(v == [1, 2, 3, 4, 5, 6]);
997 #[stable(feature = "rust1", since = "1.0.0")]
999 pub fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
1000 let len = self.len();
1001 let ptr = self.as_mut_ptr();
1004 assert!(mid <= len);
1006 (from_raw_parts_mut(ptr, mid),
1007 from_raw_parts_mut(ptr.add(mid), len - mid))
1011 /// Returns an iterator over subslices separated by elements that match
1012 /// `pred`. The matched element is not contained in the subslices.
1017 /// let slice = [10, 40, 33, 20];
1018 /// let mut iter = slice.split(|num| num % 3 == 0);
1020 /// assert_eq!(iter.next().unwrap(), &[10, 40]);
1021 /// assert_eq!(iter.next().unwrap(), &[20]);
1022 /// assert!(iter.next().is_none());
1025 /// If the first element is matched, an empty slice will be the first item
1026 /// returned by the iterator. Similarly, if the last element in the slice
1027 /// is matched, an empty slice will be the last item returned by the
1031 /// let slice = [10, 40, 33];
1032 /// let mut iter = slice.split(|num| num % 3 == 0);
1034 /// assert_eq!(iter.next().unwrap(), &[10, 40]);
1035 /// assert_eq!(iter.next().unwrap(), &[]);
1036 /// assert!(iter.next().is_none());
1039 /// If two matched elements are directly adjacent, an empty slice will be
1040 /// present between them:
1043 /// let slice = [10, 6, 33, 20];
1044 /// let mut iter = slice.split(|num| num % 3 == 0);
1046 /// assert_eq!(iter.next().unwrap(), &[10]);
1047 /// assert_eq!(iter.next().unwrap(), &[]);
1048 /// assert_eq!(iter.next().unwrap(), &[20]);
1049 /// assert!(iter.next().is_none());
1051 #[stable(feature = "rust1", since = "1.0.0")]
1053 pub fn split<F>(&self, pred: F) -> Split<'_, T, F>
1054 where F: FnMut(&T) -> bool
1063 /// Returns an iterator over mutable subslices separated by elements that
1064 /// match `pred`. The matched element is not contained in the subslices.
1069 /// let mut v = [10, 40, 30, 20, 60, 50];
1071 /// for group in v.split_mut(|num| *num % 3 == 0) {
1074 /// assert_eq!(v, [1, 40, 30, 1, 60, 1]);
1076 #[stable(feature = "rust1", since = "1.0.0")]
1078 pub fn split_mut<F>(&mut self, pred: F) -> SplitMut<'_, T, F>
1079 where F: FnMut(&T) -> bool
1081 SplitMut { v: self, pred, finished: false }
1084 /// Returns an iterator over subslices separated by elements that match
1085 /// `pred`, starting at the end of the slice and working backwards.
1086 /// The matched element is not contained in the subslices.
1091 /// let slice = [11, 22, 33, 0, 44, 55];
1092 /// let mut iter = slice.rsplit(|num| *num == 0);
1094 /// assert_eq!(iter.next().unwrap(), &[44, 55]);
1095 /// assert_eq!(iter.next().unwrap(), &[11, 22, 33]);
1096 /// assert_eq!(iter.next(), None);
1099 /// As with `split()`, if the first or last element is matched, an empty
1100 /// slice will be the first (or last) item returned by the iterator.
1103 /// let v = &[0, 1, 1, 2, 3, 5, 8];
1104 /// let mut it = v.rsplit(|n| *n % 2 == 0);
1105 /// assert_eq!(it.next().unwrap(), &[]);
1106 /// assert_eq!(it.next().unwrap(), &[3, 5]);
1107 /// assert_eq!(it.next().unwrap(), &[1, 1]);
1108 /// assert_eq!(it.next().unwrap(), &[]);
1109 /// assert_eq!(it.next(), None);
1111 #[stable(feature = "slice_rsplit", since = "1.27.0")]
1113 pub fn rsplit<F>(&self, pred: F) -> RSplit<'_, T, F>
1114 where F: FnMut(&T) -> bool
1116 RSplit { inner: self.split(pred) }
1119 /// Returns an iterator over mutable subslices separated by elements that
1120 /// match `pred`, starting at the end of the slice and working
1121 /// backwards. The matched element is not contained in the subslices.
1126 /// let mut v = [100, 400, 300, 200, 600, 500];
1128 /// let mut count = 0;
1129 /// for group in v.rsplit_mut(|num| *num % 3 == 0) {
1131 /// group[0] = count;
1133 /// assert_eq!(v, [3, 400, 300, 2, 600, 1]);
1136 #[stable(feature = "slice_rsplit", since = "1.27.0")]
1138 pub fn rsplit_mut<F>(&mut self, pred: F) -> RSplitMut<'_, T, F>
1139 where F: FnMut(&T) -> bool
1141 RSplitMut { inner: self.split_mut(pred) }
1144 /// Returns an iterator over subslices separated by elements that match
1145 /// `pred`, limited to returning at most `n` items. The matched element is
1146 /// not contained in the subslices.
1148 /// The last element returned, if any, will contain the remainder of the
1153 /// Print the slice split once by numbers divisible by 3 (i.e., `[10, 40]`,
1154 /// `[20, 60, 50]`):
1157 /// let v = [10, 40, 30, 20, 60, 50];
1159 /// for group in v.splitn(2, |num| *num % 3 == 0) {
1160 /// println!("{:?}", group);
1163 #[stable(feature = "rust1", since = "1.0.0")]
1165 pub fn splitn<F>(&self, n: usize, pred: F) -> SplitN<'_, T, F>
1166 where F: FnMut(&T) -> bool
1169 inner: GenericSplitN {
1170 iter: self.split(pred),
1176 /// Returns an iterator over subslices separated by elements that match
1177 /// `pred`, limited to returning at most `n` items. The matched element is
1178 /// not contained in the subslices.
1180 /// The last element returned, if any, will contain the remainder of the
1186 /// let mut v = [10, 40, 30, 20, 60, 50];
1188 /// for group in v.splitn_mut(2, |num| *num % 3 == 0) {
1191 /// assert_eq!(v, [1, 40, 30, 1, 60, 50]);
1193 #[stable(feature = "rust1", since = "1.0.0")]
1195 pub fn splitn_mut<F>(&mut self, n: usize, pred: F) -> SplitNMut<'_, T, F>
1196 where F: FnMut(&T) -> bool
1199 inner: GenericSplitN {
1200 iter: self.split_mut(pred),
1206 /// Returns an iterator over subslices separated by elements that match
1207 /// `pred` limited to returning at most `n` items. This starts at the end of
1208 /// the slice and works backwards. The matched element is not contained in
1211 /// The last element returned, if any, will contain the remainder of the
1216 /// Print the slice split once, starting from the end, by numbers divisible
1217 /// by 3 (i.e., `[50]`, `[10, 40, 30, 20]`):
1220 /// let v = [10, 40, 30, 20, 60, 50];
1222 /// for group in v.rsplitn(2, |num| *num % 3 == 0) {
1223 /// println!("{:?}", group);
1226 #[stable(feature = "rust1", since = "1.0.0")]
1228 pub fn rsplitn<F>(&self, n: usize, pred: F) -> RSplitN<'_, T, F>
1229 where F: FnMut(&T) -> bool
1232 inner: GenericSplitN {
1233 iter: self.rsplit(pred),
1239 /// Returns an iterator over subslices separated by elements that match
1240 /// `pred` limited to returning at most `n` items. This starts at the end of
1241 /// the slice and works backwards. The matched element is not contained in
1244 /// The last element returned, if any, will contain the remainder of the
1250 /// let mut s = [10, 40, 30, 20, 60, 50];
1252 /// for group in s.rsplitn_mut(2, |num| *num % 3 == 0) {
1255 /// assert_eq!(s, [1, 40, 30, 20, 60, 1]);
1257 #[stable(feature = "rust1", since = "1.0.0")]
1259 pub fn rsplitn_mut<F>(&mut self, n: usize, pred: F) -> RSplitNMut<'_, T, F>
1260 where F: FnMut(&T) -> bool
1263 inner: GenericSplitN {
1264 iter: self.rsplit_mut(pred),
1270 /// Returns `true` if the slice contains an element with the given value.
1275 /// let v = [10, 40, 30];
1276 /// assert!(v.contains(&30));
1277 /// assert!(!v.contains(&50));
1279 #[stable(feature = "rust1", since = "1.0.0")]
1280 pub fn contains(&self, x: &T) -> bool
1283 x.slice_contains(self)
1286 /// Returns `true` if `needle` is a prefix of the slice.
1291 /// let v = [10, 40, 30];
1292 /// assert!(v.starts_with(&[10]));
1293 /// assert!(v.starts_with(&[10, 40]));
1294 /// assert!(!v.starts_with(&[50]));
1295 /// assert!(!v.starts_with(&[10, 50]));
1298 /// Always returns `true` if `needle` is an empty slice:
1301 /// let v = &[10, 40, 30];
1302 /// assert!(v.starts_with(&[]));
1303 /// let v: &[u8] = &[];
1304 /// assert!(v.starts_with(&[]));
1306 #[stable(feature = "rust1", since = "1.0.0")]
1307 pub fn starts_with(&self, needle: &[T]) -> bool
1310 let n = needle.len();
1311 self.len() >= n && needle == &self[..n]
1314 /// Returns `true` if `needle` is a suffix of the slice.
1319 /// let v = [10, 40, 30];
1320 /// assert!(v.ends_with(&[30]));
1321 /// assert!(v.ends_with(&[40, 30]));
1322 /// assert!(!v.ends_with(&[50]));
1323 /// assert!(!v.ends_with(&[50, 30]));
1326 /// Always returns `true` if `needle` is an empty slice:
1329 /// let v = &[10, 40, 30];
1330 /// assert!(v.ends_with(&[]));
1331 /// let v: &[u8] = &[];
1332 /// assert!(v.ends_with(&[]));
1334 #[stable(feature = "rust1", since = "1.0.0")]
1335 pub fn ends_with(&self, needle: &[T]) -> bool
1338 let (m, n) = (self.len(), needle.len());
1339 m >= n && needle == &self[m-n..]
1342 /// Binary searches this sorted slice for a given element.
1344 /// If the value is found then [`Result::Ok`] is returned, containing the
1345 /// index of the matching element. If there are multiple matches, then any
1346 /// one of the matches could be returned. If the value is not found then
1347 /// [`Result::Err`] is returned, containing the index where a matching
1348 /// element could be inserted while maintaining sorted order.
1352 /// Looks up a series of four elements. The first is found, with a
1353 /// uniquely determined position; the second and third are not
1354 /// found; the fourth could match any position in `[1, 4]`.
1357 /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
1359 /// assert_eq!(s.binary_search(&13), Ok(9));
1360 /// assert_eq!(s.binary_search(&4), Err(7));
1361 /// assert_eq!(s.binary_search(&100), Err(13));
1362 /// let r = s.binary_search(&1);
1363 /// assert!(match r { Ok(1..=4) => true, _ => false, });
1365 #[stable(feature = "rust1", since = "1.0.0")]
1366 pub fn binary_search(&self, x: &T) -> Result<usize, usize>
1369 self.binary_search_by(|p| p.cmp(x))
1372 /// Binary searches this sorted slice with a comparator function.
1374 /// The comparator function should implement an order consistent
1375 /// with the sort order of the underlying slice, returning an
1376 /// order code that indicates whether its argument is `Less`,
1377 /// `Equal` or `Greater` the desired target.
1379 /// If the value is found then [`Result::Ok`] is returned, containing the
1380 /// index of the matching element. If there are multiple matches, then any
1381 /// one of the matches could be returned. If the value is not found then
1382 /// [`Result::Err`] is returned, containing the index where a matching
1383 /// element could be inserted while maintaining sorted order.
1387 /// Looks up a series of four elements. The first is found, with a
1388 /// uniquely determined position; the second and third are not
1389 /// found; the fourth could match any position in `[1, 4]`.
1392 /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
1395 /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Ok(9));
1397 /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(7));
1399 /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(13));
1401 /// let r = s.binary_search_by(|probe| probe.cmp(&seek));
1402 /// assert!(match r { Ok(1..=4) => true, _ => false, });
1404 #[stable(feature = "rust1", since = "1.0.0")]
1406 pub fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result<usize, usize>
1407 where F: FnMut(&'a T) -> Ordering
1410 let mut size = s.len();
1414 let mut base = 0usize;
1416 let half = size / 2;
1417 let mid = base + half;
1418 // mid is always in [0, size), that means mid is >= 0 and < size.
1419 // mid >= 0: by definition
1420 // mid < size: mid = size / 2 + size / 4 + size / 8 ...
1421 let cmp = f(unsafe { s.get_unchecked(mid) });
1422 base = if cmp == Greater { base } else { mid };
1425 // base is always in [0, size) because base <= mid.
1426 let cmp = f(unsafe { s.get_unchecked(base) });
1427 if cmp == Equal { Ok(base) } else { Err(base + (cmp == Less) as usize) }
1431 /// Binary searches this sorted slice with a key extraction function.
1433 /// Assumes that the slice is sorted by the key, for instance with
1434 /// [`sort_by_key`] using the same key extraction function.
1436 /// If the value is found then [`Result::Ok`] is returned, containing the
1437 /// index of the matching element. If there are multiple matches, then any
1438 /// one of the matches could be returned. If the value is not found then
1439 /// [`Result::Err`] is returned, containing the index where a matching
1440 /// element could be inserted while maintaining sorted order.
1442 /// [`sort_by_key`]: #method.sort_by_key
1446 /// Looks up a series of four elements in a slice of pairs sorted by
1447 /// their second elements. The first is found, with a uniquely
1448 /// determined position; the second and third are not found; the
1449 /// fourth could match any position in `[1, 4]`.
1452 /// let s = [(0, 0), (2, 1), (4, 1), (5, 1), (3, 1),
1453 /// (1, 2), (2, 3), (4, 5), (5, 8), (3, 13),
1454 /// (1, 21), (2, 34), (4, 55)];
1456 /// assert_eq!(s.binary_search_by_key(&13, |&(a,b)| b), Ok(9));
1457 /// assert_eq!(s.binary_search_by_key(&4, |&(a,b)| b), Err(7));
1458 /// assert_eq!(s.binary_search_by_key(&100, |&(a,b)| b), Err(13));
1459 /// let r = s.binary_search_by_key(&1, |&(a,b)| b);
1460 /// assert!(match r { Ok(1..=4) => true, _ => false, });
1462 #[stable(feature = "slice_binary_search_by_key", since = "1.10.0")]
1464 pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize, usize>
1465 where F: FnMut(&'a T) -> B,
1468 self.binary_search_by(|k| f(k).cmp(b))
1471 /// Sorts the slice, but may not preserve the order of equal elements.
1473 /// This sort is unstable (i.e., may reorder equal elements), in-place
1474 /// (i.e., does not allocate), and `O(n log n)` worst-case.
1476 /// # Current implementation
1478 /// The current algorithm is based on [pattern-defeating quicksort][pdqsort] by Orson Peters,
1479 /// which combines the fast average case of randomized quicksort with the fast worst case of
1480 /// heapsort, while achieving linear time on slices with certain patterns. It uses some
1481 /// randomization to avoid degenerate cases, but with a fixed seed to always provide
1482 /// deterministic behavior.
1484 /// It is typically faster than stable sorting, except in a few special cases, e.g., when the
1485 /// slice consists of several concatenated sorted sequences.
1490 /// let mut v = [-5, 4, 1, -3, 2];
1492 /// v.sort_unstable();
1493 /// assert!(v == [-5, -3, 1, 2, 4]);
1496 /// [pdqsort]: https://github.com/orlp/pdqsort
1497 #[stable(feature = "sort_unstable", since = "1.20.0")]
1499 pub fn sort_unstable(&mut self)
1502 sort::quicksort(self, |a, b| a.lt(b));
1505 /// Sorts the slice with a comparator function, but may not preserve the order of equal
1508 /// This sort is unstable (i.e., may reorder equal elements), in-place
1509 /// (i.e., does not allocate), and `O(n log n)` worst-case.
1511 /// The comparator function must define a total ordering for the elements in the slice. If
1512 /// the ordering is not total, the order of the elements is unspecified. An order is a
1513 /// total order if it is (for all a, b and c):
1515 /// * total and antisymmetric: exactly one of a < b, a == b or a > b is true; and
1516 /// * transitive, a < b and b < c implies a < c. The same must hold for both == and >.
1518 /// For example, while [`f64`] doesn't implement [`Ord`] because `NaN != NaN`, we can use
1519 /// `partial_cmp` as our sort function when we know the slice doesn't contain a `NaN`.
1522 /// let mut floats = [5f64, 4.0, 1.0, 3.0, 2.0];
1523 /// floats.sort_by(|a, b| a.partial_cmp(b).unwrap());
1524 /// assert_eq!(floats, [1.0, 2.0, 3.0, 4.0, 5.0]);
1527 /// # Current implementation
1529 /// The current algorithm is based on [pattern-defeating quicksort][pdqsort] by Orson Peters,
1530 /// which combines the fast average case of randomized quicksort with the fast worst case of
1531 /// heapsort, while achieving linear time on slices with certain patterns. It uses some
1532 /// randomization to avoid degenerate cases, but with a fixed seed to always provide
1533 /// deterministic behavior.
1535 /// It is typically faster than stable sorting, except in a few special cases, e.g., when the
1536 /// slice consists of several concatenated sorted sequences.
1541 /// let mut v = [5, 4, 1, 3, 2];
1542 /// v.sort_unstable_by(|a, b| a.cmp(b));
1543 /// assert!(v == [1, 2, 3, 4, 5]);
1545 /// // reverse sorting
1546 /// v.sort_unstable_by(|a, b| b.cmp(a));
1547 /// assert!(v == [5, 4, 3, 2, 1]);
1550 /// [pdqsort]: https://github.com/orlp/pdqsort
1551 #[stable(feature = "sort_unstable", since = "1.20.0")]
1553 pub fn sort_unstable_by<F>(&mut self, mut compare: F)
1554 where F: FnMut(&T, &T) -> Ordering
1556 sort::quicksort(self, |a, b| compare(a, b) == Ordering::Less);
1559 /// Sorts the slice with a key extraction function, but may not preserve the order of equal
1562 /// This sort is unstable (i.e., may reorder equal elements), in-place
1563 /// (i.e., does not allocate), and `O(m n log(m n))` worst-case, where the key function is
1566 /// # Current implementation
1568 /// The current algorithm is based on [pattern-defeating quicksort][pdqsort] by Orson Peters,
1569 /// which combines the fast average case of randomized quicksort with the fast worst case of
1570 /// heapsort, while achieving linear time on slices with certain patterns. It uses some
1571 /// randomization to avoid degenerate cases, but with a fixed seed to always provide
1572 /// deterministic behavior.
1574 /// Due to its key calling strategy, [`sort_unstable_by_key`](#method.sort_unstable_by_key)
1575 /// is likely to be slower than [`sort_by_cached_key`](#method.sort_by_cached_key) in
1576 /// cases where the key function is expensive.
1581 /// let mut v = [-5i32, 4, 1, -3, 2];
1583 /// v.sort_unstable_by_key(|k| k.abs());
1584 /// assert!(v == [1, 2, -3, 4, -5]);
1587 /// [pdqsort]: https://github.com/orlp/pdqsort
1588 #[stable(feature = "sort_unstable", since = "1.20.0")]
1590 pub fn sort_unstable_by_key<K, F>(&mut self, mut f: F)
1591 where F: FnMut(&T) -> K, K: Ord
1593 sort::quicksort(self, |a, b| f(a).lt(&f(b)));
1596 /// Reorder the slice such that the element at `index` is at its final sorted position.
1598 /// This reordering has the additional property that any value at position `i < index` will be
1599 /// less than or equal to any value at a position `j > index`. Additionally, this reordering is
1600 /// unstable (i.e. any number of equal elements may end up at position `index`), in-place
1601 /// (i.e. does not allocate), and `O(n)` worst-case. This function is also/ known as "kth
1602 /// element" in other libraries. It returns a triplet of the following values: all elements less
1603 /// than the one at the given index, the value at the given index, and all elements greater than
1604 /// the one at the given index.
1606 /// # Current implementation
1608 /// The current algorithm is based on the quickselect portion of the same quicksort algorithm
1609 /// used for [`sort_unstable`].
1611 /// [`sort_unstable`]: #method.sort_unstable
1615 /// Panics when `index >= len()`, meaning it always panics on empty slices.
1620 /// #![feature(slice_partition_at_index)]
1622 /// let mut v = [-5i32, 4, 1, -3, 2];
1624 /// // Find the median
1625 /// v.partition_at_index(2);
1627 /// // We are only guaranteed the slice will be one of the following, based on the way we sort
1628 /// // about the specified index.
1629 /// assert!(v == [-3, -5, 1, 2, 4] ||
1630 /// v == [-5, -3, 1, 2, 4] ||
1631 /// v == [-3, -5, 1, 4, 2] ||
1632 /// v == [-5, -3, 1, 4, 2]);
1634 #[unstable(feature = "slice_partition_at_index", issue = "55300")]
1636 pub fn partition_at_index(&mut self, index: usize) -> (&mut [T], &mut T, &mut [T])
1639 let mut f = |a: &T, b: &T| a.lt(b);
1640 sort::partition_at_index(self, index, &mut f)
1643 /// Reorder the slice with a comparator function such that the element at `index` is at its
1644 /// final sorted position.
1646 /// This reordering has the additional property that any value at position `i < index` will be
1647 /// less than or equal to any value at a position `j > index` using the comparator function.
1648 /// Additionally, this reordering is unstable (i.e. any number of equal elements may end up at
1649 /// position `index`), in-place (i.e. does not allocate), and `O(n)` worst-case. This function
1650 /// is also known as "kth element" in other libraries. It returns a triplet of the following
1651 /// values: all elements less than the one at the given index, the value at the given index,
1652 /// and all elements greater than the one at the given index, using the provided comparator
1655 /// # Current implementation
1657 /// The current algorithm is based on the quickselect portion of the same quicksort algorithm
1658 /// used for [`sort_unstable`].
1660 /// [`sort_unstable`]: #method.sort_unstable
1664 /// Panics when `index >= len()`, meaning it always panics on empty slices.
1669 /// #![feature(slice_partition_at_index)]
1671 /// let mut v = [-5i32, 4, 1, -3, 2];
1673 /// // Find the median as if the slice were sorted in descending order.
1674 /// v.partition_at_index_by(2, |a, b| b.cmp(a));
1676 /// // We are only guaranteed the slice will be one of the following, based on the way we sort
1677 /// // about the specified index.
1678 /// assert!(v == [2, 4, 1, -5, -3] ||
1679 /// v == [2, 4, 1, -3, -5] ||
1680 /// v == [4, 2, 1, -5, -3] ||
1681 /// v == [4, 2, 1, -3, -5]);
1683 #[unstable(feature = "slice_partition_at_index", issue = "55300")]
1685 pub fn partition_at_index_by<F>(&mut self, index: usize, mut compare: F)
1686 -> (&mut [T], &mut T, &mut [T])
1687 where F: FnMut(&T, &T) -> Ordering
1689 let mut f = |a: &T, b: &T| compare(a, b) == Less;
1690 sort::partition_at_index(self, index, &mut f)
1693 /// Reorder the slice with a key extraction function such that the element at `index` is at its
1694 /// final sorted position.
1696 /// This reordering has the additional property that any value at position `i < index` will be
1697 /// less than or equal to any value at a position `j > index` using the key extraction function.
1698 /// Additionally, this reordering is unstable (i.e. any number of equal elements may end up at
1699 /// position `index`), in-place (i.e. does not allocate), and `O(n)` worst-case. This function
1700 /// is also known as "kth element" in other libraries. It returns a triplet of the following
1701 /// values: all elements less than the one at the given index, the value at the given index, and
1702 /// all elements greater than the one at the given index, using the provided key extraction
1705 /// # Current implementation
1707 /// The current algorithm is based on the quickselect portion of the same quicksort algorithm
1708 /// used for [`sort_unstable`].
1710 /// [`sort_unstable`]: #method.sort_unstable
1714 /// Panics when `index >= len()`, meaning it always panics on empty slices.
1719 /// #![feature(slice_partition_at_index)]
1721 /// let mut v = [-5i32, 4, 1, -3, 2];
1723 /// // Return the median as if the array were sorted according to absolute value.
1724 /// v.partition_at_index_by_key(2, |a| a.abs());
1726 /// // We are only guaranteed the slice will be one of the following, based on the way we sort
1727 /// // about the specified index.
1728 /// assert!(v == [1, 2, -3, 4, -5] ||
1729 /// v == [1, 2, -3, -5, 4] ||
1730 /// v == [2, 1, -3, 4, -5] ||
1731 /// v == [2, 1, -3, -5, 4]);
1733 #[unstable(feature = "slice_partition_at_index", issue = "55300")]
1735 pub fn partition_at_index_by_key<K, F>(&mut self, index: usize, mut f: F)
1736 -> (&mut [T], &mut T, &mut [T])
1737 where F: FnMut(&T) -> K, K: Ord
1739 let mut g = |a: &T, b: &T| f(a).lt(&f(b));
1740 sort::partition_at_index(self, index, &mut g)
1743 /// Moves all consecutive repeated elements to the end of the slice according to the
1744 /// [`PartialEq`] trait implementation.
1746 /// Returns two slices. The first contains no consecutive repeated elements.
1747 /// The second contains all the duplicates in no specified order.
1749 /// If the slice is sorted, the first returned slice contains no duplicates.
1754 /// #![feature(slice_partition_dedup)]
1756 /// let mut slice = [1, 2, 2, 3, 3, 2, 1, 1];
1758 /// let (dedup, duplicates) = slice.partition_dedup();
1760 /// assert_eq!(dedup, [1, 2, 3, 2, 1]);
1761 /// assert_eq!(duplicates, [2, 3, 1]);
1763 #[unstable(feature = "slice_partition_dedup", issue = "54279")]
1765 pub fn partition_dedup(&mut self) -> (&mut [T], &mut [T])
1768 self.partition_dedup_by(|a, b| a == b)
1771 /// Moves all but the first of consecutive elements to the end of the slice satisfying
1772 /// a given equality relation.
1774 /// Returns two slices. The first contains no consecutive repeated elements.
1775 /// The second contains all the duplicates in no specified order.
1777 /// The `same_bucket` function is passed references to two elements from the slice and
1778 /// must determine if the elements compare equal. The elements are passed in opposite order
1779 /// from their order in the slice, so if `same_bucket(a, b)` returns `true`, `a` is moved
1780 /// at the end of the slice.
1782 /// If the slice is sorted, the first returned slice contains no duplicates.
1787 /// #![feature(slice_partition_dedup)]
1789 /// let mut slice = ["foo", "Foo", "BAZ", "Bar", "bar", "baz", "BAZ"];
1791 /// let (dedup, duplicates) = slice.partition_dedup_by(|a, b| a.eq_ignore_ascii_case(b));
1793 /// assert_eq!(dedup, ["foo", "BAZ", "Bar", "baz"]);
1794 /// assert_eq!(duplicates, ["bar", "Foo", "BAZ"]);
1796 #[unstable(feature = "slice_partition_dedup", issue = "54279")]
1798 pub fn partition_dedup_by<F>(&mut self, mut same_bucket: F) -> (&mut [T], &mut [T])
1799 where F: FnMut(&mut T, &mut T) -> bool
1801 // Although we have a mutable reference to `self`, we cannot make
1802 // *arbitrary* changes. The `same_bucket` calls could panic, so we
1803 // must ensure that the slice is in a valid state at all times.
1805 // The way that we handle this is by using swaps; we iterate
1806 // over all the elements, swapping as we go so that at the end
1807 // the elements we wish to keep are in the front, and those we
1808 // wish to reject are at the back. We can then split the slice.
1809 // This operation is still O(n).
1811 // Example: We start in this state, where `r` represents "next
1812 // read" and `w` represents "next_write`.
1815 // +---+---+---+---+---+---+
1816 // | 0 | 1 | 1 | 2 | 3 | 3 |
1817 // +---+---+---+---+---+---+
1820 // Comparing self[r] against self[w-1], this is not a duplicate, so
1821 // we swap self[r] and self[w] (no effect as r==w) and then increment both
1822 // r and w, leaving us with:
1825 // +---+---+---+---+---+---+
1826 // | 0 | 1 | 1 | 2 | 3 | 3 |
1827 // +---+---+---+---+---+---+
1830 // Comparing self[r] against self[w-1], this value is a duplicate,
1831 // so we increment `r` but leave everything else unchanged:
1834 // +---+---+---+---+---+---+
1835 // | 0 | 1 | 1 | 2 | 3 | 3 |
1836 // +---+---+---+---+---+---+
1839 // Comparing self[r] against self[w-1], this is not a duplicate,
1840 // so swap self[r] and self[w] and advance r and w:
1843 // +---+---+---+---+---+---+
1844 // | 0 | 1 | 2 | 1 | 3 | 3 |
1845 // +---+---+---+---+---+---+
1848 // Not a duplicate, repeat:
1851 // +---+---+---+---+---+---+
1852 // | 0 | 1 | 2 | 3 | 1 | 3 |
1853 // +---+---+---+---+---+---+
1856 // Duplicate, advance r. End of slice. Split at w.
1858 let len = self.len();
1860 return (self, &mut [])
1863 let ptr = self.as_mut_ptr();
1864 let mut next_read: usize = 1;
1865 let mut next_write: usize = 1;
1868 // Avoid bounds checks by using raw pointers.
1869 while next_read < len {
1870 let ptr_read = ptr.add(next_read);
1871 let prev_ptr_write = ptr.add(next_write - 1);
1872 if !same_bucket(&mut *ptr_read, &mut *prev_ptr_write) {
1873 if next_read != next_write {
1874 let ptr_write = prev_ptr_write.offset(1);
1875 mem::swap(&mut *ptr_read, &mut *ptr_write);
1883 self.split_at_mut(next_write)
1886 /// Moves all but the first of consecutive elements to the end of the slice that resolve
1887 /// to the same key.
1889 /// Returns two slices. The first contains no consecutive repeated elements.
1890 /// The second contains all the duplicates in no specified order.
1892 /// If the slice is sorted, the first returned slice contains no duplicates.
1897 /// #![feature(slice_partition_dedup)]
1899 /// let mut slice = [10, 20, 21, 30, 30, 20, 11, 13];
1901 /// let (dedup, duplicates) = slice.partition_dedup_by_key(|i| *i / 10);
1903 /// assert_eq!(dedup, [10, 20, 30, 20, 11]);
1904 /// assert_eq!(duplicates, [21, 30, 13]);
1906 #[unstable(feature = "slice_partition_dedup", issue = "54279")]
1908 pub fn partition_dedup_by_key<K, F>(&mut self, mut key: F) -> (&mut [T], &mut [T])
1909 where F: FnMut(&mut T) -> K,
1912 self.partition_dedup_by(|a, b| key(a) == key(b))
1915 /// Rotates the slice in-place such that the first `mid` elements of the
1916 /// slice move to the end while the last `self.len() - mid` elements move to
1917 /// the front. After calling `rotate_left`, the element previously at index
1918 /// `mid` will become the first element in the slice.
1922 /// This function will panic if `mid` is greater than the length of the
1923 /// slice. Note that `mid == self.len()` does _not_ panic and is a no-op
1928 /// Takes linear (in `self.len()`) time.
1933 /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
1934 /// a.rotate_left(2);
1935 /// assert_eq!(a, ['c', 'd', 'e', 'f', 'a', 'b']);
1938 /// Rotating a subslice:
1941 /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
1942 /// a[1..5].rotate_left(1);
1943 /// assert_eq!(a, ['a', 'c', 'd', 'e', 'b', 'f']);
1945 #[stable(feature = "slice_rotate", since = "1.26.0")]
1946 pub fn rotate_left(&mut self, mid: usize) {
1947 assert!(mid <= self.len());
1948 let k = self.len() - mid;
1951 let p = self.as_mut_ptr();
1952 rotate::ptr_rotate(mid, p.add(mid), k);
1956 /// Rotates the slice in-place such that the first `self.len() - k`
1957 /// elements of the slice move to the end while the last `k` elements move
1958 /// to the front. After calling `rotate_right`, the element previously at
1959 /// index `self.len() - k` will become the first element in the slice.
1963 /// This function will panic if `k` is greater than the length of the
1964 /// slice. Note that `k == self.len()` does _not_ panic and is a no-op
1969 /// Takes linear (in `self.len()`) time.
1974 /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
1975 /// a.rotate_right(2);
1976 /// assert_eq!(a, ['e', 'f', 'a', 'b', 'c', 'd']);
1979 /// Rotate a subslice:
1982 /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
1983 /// a[1..5].rotate_right(1);
1984 /// assert_eq!(a, ['a', 'e', 'b', 'c', 'd', 'f']);
1986 #[stable(feature = "slice_rotate", since = "1.26.0")]
1987 pub fn rotate_right(&mut self, k: usize) {
1988 assert!(k <= self.len());
1989 let mid = self.len() - k;
1992 let p = self.as_mut_ptr();
1993 rotate::ptr_rotate(mid, p.add(mid), k);
1997 /// Copies the elements from `src` into `self`.
1999 /// The length of `src` must be the same as `self`.
2001 /// If `src` implements `Copy`, it can be more performant to use
2002 /// [`copy_from_slice`].
2006 /// This function will panic if the two slices have different lengths.
2010 /// Cloning two elements from a slice into another:
2013 /// let src = [1, 2, 3, 4];
2014 /// let mut dst = [0, 0];
2016 /// // Because the slices have to be the same length,
2017 /// // we slice the source slice from four elements
2018 /// // to two. It will panic if we don't do this.
2019 /// dst.clone_from_slice(&src[2..]);
2021 /// assert_eq!(src, [1, 2, 3, 4]);
2022 /// assert_eq!(dst, [3, 4]);
2025 /// Rust enforces that there can only be one mutable reference with no
2026 /// immutable references to a particular piece of data in a particular
2027 /// scope. Because of this, attempting to use `clone_from_slice` on a
2028 /// single slice will result in a compile failure:
2031 /// let mut slice = [1, 2, 3, 4, 5];
2033 /// slice[..2].clone_from_slice(&slice[3..]); // compile fail!
2036 /// To work around this, we can use [`split_at_mut`] to create two distinct
2037 /// sub-slices from a slice:
2040 /// let mut slice = [1, 2, 3, 4, 5];
2043 /// let (left, right) = slice.split_at_mut(2);
2044 /// left.clone_from_slice(&right[1..]);
2047 /// assert_eq!(slice, [4, 5, 3, 4, 5]);
2050 /// [`copy_from_slice`]: #method.copy_from_slice
2051 /// [`split_at_mut`]: #method.split_at_mut
2052 #[stable(feature = "clone_from_slice", since = "1.7.0")]
2053 pub fn clone_from_slice(&mut self, src: &[T]) where T: Clone {
2054 assert!(self.len() == src.len(),
2055 "destination and source slices have different lengths");
2056 // NOTE: We need to explicitly slice them to the same length
2057 // for bounds checking to be elided, and the optimizer will
2058 // generate memcpy for simple cases (for example T = u8).
2059 let len = self.len();
2060 let src = &src[..len];
2062 self[i].clone_from(&src[i]);
2067 /// Copies all elements from `src` into `self`, using a memcpy.
2069 /// The length of `src` must be the same as `self`.
2071 /// If `src` does not implement `Copy`, use [`clone_from_slice`].
2075 /// This function will panic if the two slices have different lengths.
2079 /// Copying two elements from a slice into another:
2082 /// let src = [1, 2, 3, 4];
2083 /// let mut dst = [0, 0];
2085 /// // Because the slices have to be the same length,
2086 /// // we slice the source slice from four elements
2087 /// // to two. It will panic if we don't do this.
2088 /// dst.copy_from_slice(&src[2..]);
2090 /// assert_eq!(src, [1, 2, 3, 4]);
2091 /// assert_eq!(dst, [3, 4]);
2094 /// Rust enforces that there can only be one mutable reference with no
2095 /// immutable references to a particular piece of data in a particular
2096 /// scope. Because of this, attempting to use `copy_from_slice` on a
2097 /// single slice will result in a compile failure:
2100 /// let mut slice = [1, 2, 3, 4, 5];
2102 /// slice[..2].copy_from_slice(&slice[3..]); // compile fail!
2105 /// To work around this, we can use [`split_at_mut`] to create two distinct
2106 /// sub-slices from a slice:
2109 /// let mut slice = [1, 2, 3, 4, 5];
2112 /// let (left, right) = slice.split_at_mut(2);
2113 /// left.copy_from_slice(&right[1..]);
2116 /// assert_eq!(slice, [4, 5, 3, 4, 5]);
2119 /// [`clone_from_slice`]: #method.clone_from_slice
2120 /// [`split_at_mut`]: #method.split_at_mut
2121 #[stable(feature = "copy_from_slice", since = "1.9.0")]
2122 pub fn copy_from_slice(&mut self, src: &[T]) where T: Copy {
2123 assert_eq!(self.len(), src.len(),
2124 "destination and source slices have different lengths");
2126 ptr::copy_nonoverlapping(
2127 src.as_ptr(), self.as_mut_ptr(), self.len());
2131 /// Copies elements from one part of the slice to another part of itself,
2132 /// using a memmove.
2134 /// `src` is the range within `self` to copy from. `dest` is the starting
2135 /// index of the range within `self` to copy to, which will have the same
2136 /// length as `src`. The two ranges may overlap. The ends of the two ranges
2137 /// must be less than or equal to `self.len()`.
2141 /// This function will panic if either range exceeds the end of the slice,
2142 /// or if the end of `src` is before the start.
2146 /// Copying four bytes within a slice:
2149 /// let mut bytes = *b"Hello, World!";
2151 /// bytes.copy_within(1..5, 8);
2153 /// assert_eq!(&bytes, b"Hello, Wello!");
2155 #[stable(feature = "copy_within", since = "1.37.0")]
2156 pub fn copy_within<R: ops::RangeBounds<usize>>(&mut self, src: R, dest: usize)
2160 let src_start = match src.start_bound() {
2161 ops::Bound::Included(&n) => n,
2162 ops::Bound::Excluded(&n) => n
2164 .unwrap_or_else(|| slice_index_overflow_fail()),
2165 ops::Bound::Unbounded => 0,
2167 let src_end = match src.end_bound() {
2168 ops::Bound::Included(&n) => n
2170 .unwrap_or_else(|| slice_index_overflow_fail()),
2171 ops::Bound::Excluded(&n) => n,
2172 ops::Bound::Unbounded => self.len(),
2174 assert!(src_start <= src_end, "src end is before src start");
2175 assert!(src_end <= self.len(), "src is out of bounds");
2176 let count = src_end - src_start;
2177 assert!(dest <= self.len() - count, "dest is out of bounds");
2180 self.get_unchecked(src_start),
2181 self.get_unchecked_mut(dest),
2187 /// Swaps all elements in `self` with those in `other`.
2189 /// The length of `other` must be the same as `self`.
2193 /// This function will panic if the two slices have different lengths.
2197 /// Swapping two elements across slices:
2200 /// let mut slice1 = [0, 0];
2201 /// let mut slice2 = [1, 2, 3, 4];
2203 /// slice1.swap_with_slice(&mut slice2[2..]);
2205 /// assert_eq!(slice1, [3, 4]);
2206 /// assert_eq!(slice2, [1, 2, 0, 0]);
2209 /// Rust enforces that there can only be one mutable reference to a
2210 /// particular piece of data in a particular scope. Because of this,
2211 /// attempting to use `swap_with_slice` on a single slice will result in
2212 /// a compile failure:
2215 /// let mut slice = [1, 2, 3, 4, 5];
2216 /// slice[..2].swap_with_slice(&mut slice[3..]); // compile fail!
2219 /// To work around this, we can use [`split_at_mut`] to create two distinct
2220 /// mutable sub-slices from a slice:
2223 /// let mut slice = [1, 2, 3, 4, 5];
2226 /// let (left, right) = slice.split_at_mut(2);
2227 /// left.swap_with_slice(&mut right[1..]);
2230 /// assert_eq!(slice, [4, 5, 3, 1, 2]);
2233 /// [`split_at_mut`]: #method.split_at_mut
2234 #[stable(feature = "swap_with_slice", since = "1.27.0")]
2235 pub fn swap_with_slice(&mut self, other: &mut [T]) {
2236 assert!(self.len() == other.len(),
2237 "destination and source slices have different lengths");
2239 ptr::swap_nonoverlapping(
2240 self.as_mut_ptr(), other.as_mut_ptr(), self.len());
2244 /// Function to calculate lengths of the middle and trailing slice for `align_to{,_mut}`.
2245 fn align_to_offsets<U>(&self) -> (usize, usize) {
2246 // What we gonna do about `rest` is figure out what multiple of `U`s we can put in a
2247 // lowest number of `T`s. And how many `T`s we need for each such "multiple".
2249 // Consider for example T=u8 U=u16. Then we can put 1 U in 2 Ts. Simple. Now, consider
2250 // for example a case where size_of::<T> = 16, size_of::<U> = 24. We can put 2 Us in
2251 // place of every 3 Ts in the `rest` slice. A bit more complicated.
2253 // Formula to calculate this is:
2255 // Us = lcm(size_of::<T>, size_of::<U>) / size_of::<U>
2256 // Ts = lcm(size_of::<T>, size_of::<U>) / size_of::<T>
2258 // Expanded and simplified:
2260 // Us = size_of::<T> / gcd(size_of::<T>, size_of::<U>)
2261 // Ts = size_of::<U> / gcd(size_of::<T>, size_of::<U>)
2263 // Luckily since all this is constant-evaluated... performance here matters not!
2265 fn gcd(a: usize, b: usize) -> usize {
2266 use crate::intrinsics;
2267 // iterative stein’s algorithm
2268 // We should still make this `const fn` (and revert to recursive algorithm if we do)
2269 // because relying on llvm to consteval all this is… well, it makes me uncomfortable.
2270 let (ctz_a, mut ctz_b) = unsafe {
2271 if a == 0 { return b; }
2272 if b == 0 { return a; }
2273 (intrinsics::cttz_nonzero(a), intrinsics::cttz_nonzero(b))
2275 let k = ctz_a.min(ctz_b);
2276 let mut a = a >> ctz_a;
2279 // remove all factors of 2 from b
2282 mem::swap(&mut a, &mut b);
2289 ctz_b = intrinsics::cttz_nonzero(b);
2294 let gcd: usize = gcd(mem::size_of::<T>(), mem::size_of::<U>());
2295 let ts: usize = mem::size_of::<U>() / gcd;
2296 let us: usize = mem::size_of::<T>() / gcd;
2298 // Armed with this knowledge, we can find how many `U`s we can fit!
2299 let us_len = self.len() / ts * us;
2300 // And how many `T`s will be in the trailing slice!
2301 let ts_len = self.len() % ts;
2305 /// Transmute the slice to a slice of another type, ensuring alignment of the types is
2308 /// This method splits the slice into three distinct slices: prefix, correctly aligned middle
2309 /// slice of a new type, and the suffix slice. The method does a best effort to make the
2310 /// middle slice the greatest length possible for a given type and input slice, but only
2311 /// your algorithm's performance should depend on that, not its correctness.
2313 /// This method has no purpose when either input element `T` or output element `U` are
2314 /// zero-sized and will return the original slice without splitting anything.
2318 /// This method is essentially a `transmute` with respect to the elements in the returned
2319 /// middle slice, so all the usual caveats pertaining to `transmute::<T, U>` also apply here.
2327 /// let bytes: [u8; 7] = [1, 2, 3, 4, 5, 6, 7];
2328 /// let (prefix, shorts, suffix) = bytes.align_to::<u16>();
2329 /// // less_efficient_algorithm_for_bytes(prefix);
2330 /// // more_efficient_algorithm_for_aligned_shorts(shorts);
2331 /// // less_efficient_algorithm_for_bytes(suffix);
2334 #[stable(feature = "slice_align_to", since = "1.30.0")]
2335 pub unsafe fn align_to<U>(&self) -> (&[T], &[U], &[T]) {
2336 // Note that most of this function will be constant-evaluated,
2337 if mem::size_of::<U>() == 0 || mem::size_of::<T>() == 0 {
2338 // handle ZSTs specially, which is – don't handle them at all.
2339 return (self, &[], &[]);
2342 // First, find at what point do we split between the first and 2nd slice. Easy with
2343 // ptr.align_offset.
2344 let ptr = self.as_ptr();
2345 let offset = crate::ptr::align_offset(ptr, mem::align_of::<U>());
2346 if offset > self.len() {
2349 let (left, rest) = self.split_at(offset);
2350 // now `rest` is definitely aligned, so `from_raw_parts_mut` below is okay
2351 let (us_len, ts_len) = rest.align_to_offsets::<U>();
2353 from_raw_parts(rest.as_ptr() as *const U, us_len),
2354 from_raw_parts(rest.as_ptr().add(rest.len() - ts_len), ts_len))
2358 /// Transmute the slice to a slice of another type, ensuring alignment of the types is
2361 /// This method splits the slice into three distinct slices: prefix, correctly aligned middle
2362 /// slice of a new type, and the suffix slice. The method does a best effort to make the
2363 /// middle slice the greatest length possible for a given type and input slice, but only
2364 /// your algorithm's performance should depend on that, not its correctness.
2366 /// This method has no purpose when either input element `T` or output element `U` are
2367 /// zero-sized and will return the original slice without splitting anything.
2371 /// This method is essentially a `transmute` with respect to the elements in the returned
2372 /// middle slice, so all the usual caveats pertaining to `transmute::<T, U>` also apply here.
2380 /// let mut bytes: [u8; 7] = [1, 2, 3, 4, 5, 6, 7];
2381 /// let (prefix, shorts, suffix) = bytes.align_to_mut::<u16>();
2382 /// // less_efficient_algorithm_for_bytes(prefix);
2383 /// // more_efficient_algorithm_for_aligned_shorts(shorts);
2384 /// // less_efficient_algorithm_for_bytes(suffix);
2387 #[stable(feature = "slice_align_to", since = "1.30.0")]
2388 pub unsafe fn align_to_mut<U>(&mut self) -> (&mut [T], &mut [U], &mut [T]) {
2389 // Note that most of this function will be constant-evaluated,
2390 if mem::size_of::<U>() == 0 || mem::size_of::<T>() == 0 {
2391 // handle ZSTs specially, which is – don't handle them at all.
2392 return (self, &mut [], &mut []);
2395 // First, find at what point do we split between the first and 2nd slice. Easy with
2396 // ptr.align_offset.
2397 let ptr = self.as_ptr();
2398 let offset = crate::ptr::align_offset(ptr, mem::align_of::<U>());
2399 if offset > self.len() {
2400 (self, &mut [], &mut [])
2402 let (left, rest) = self.split_at_mut(offset);
2403 // now `rest` is definitely aligned, so `from_raw_parts_mut` below is okay
2404 let (us_len, ts_len) = rest.align_to_offsets::<U>();
2405 let mut_ptr = rest.as_mut_ptr();
2407 from_raw_parts_mut(mut_ptr as *mut U, us_len),
2408 from_raw_parts_mut(mut_ptr.add(rest.len() - ts_len), ts_len))
2412 /// Checks if the elements of this slice are sorted.
2414 /// That is, for each element `a` and its following element `b`, `a <= b` must hold. If the
2415 /// slice yields exactly zero or one element, `true` is returned.
2417 /// Note that if `Self::Item` is only `PartialOrd`, but not `Ord`, the above definition
2418 /// implies that this function returns `false` if any two consecutive items are not
2424 /// #![feature(is_sorted)]
2425 /// let empty: [i32; 0] = [];
2427 /// assert!([1, 2, 2, 9].is_sorted());
2428 /// assert!(![1, 3, 2, 4].is_sorted());
2429 /// assert!([0].is_sorted());
2430 /// assert!(empty.is_sorted());
2431 /// assert!(![0.0, 1.0, std::f32::NAN].is_sorted());
2434 #[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
2435 pub fn is_sorted(&self) -> bool
2439 self.is_sorted_by(|a, b| a.partial_cmp(b))
2442 /// Checks if the elements of this slice are sorted using the given comparator function.
2444 /// Instead of using `PartialOrd::partial_cmp`, this function uses the given `compare`
2445 /// function to determine the ordering of two elements. Apart from that, it's equivalent to
2446 /// [`is_sorted`]; see its documentation for more information.
2448 /// [`is_sorted`]: #method.is_sorted
2449 #[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
2450 pub fn is_sorted_by<F>(&self, mut compare: F) -> bool
2452 F: FnMut(&T, &T) -> Option<Ordering>
2454 self.iter().is_sorted_by(|a, b| compare(*a, *b))
2457 /// Checks if the elements of this slice are sorted using the given key extraction function.
2459 /// Instead of comparing the slice's elements directly, this function compares the keys of the
2460 /// elements, as determined by `f`. Apart from that, it's equivalent to [`is_sorted`]; see its
2461 /// documentation for more information.
2463 /// [`is_sorted`]: #method.is_sorted
2468 /// #![feature(is_sorted)]
2470 /// assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));
2471 /// assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
2474 #[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
2475 pub fn is_sorted_by_key<F, K>(&self, mut f: F) -> bool
2480 self.is_sorted_by(|a, b| f(a).partial_cmp(&f(b)))
2484 #[lang = "slice_u8"]
2487 /// Checks if all bytes in this slice are within the ASCII range.
2488 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2490 pub fn is_ascii(&self) -> bool {
2491 self.iter().all(|b| b.is_ascii())
2494 /// Checks that two slices are an ASCII case-insensitive match.
2496 /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
2497 /// but without allocating and copying temporaries.
2498 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2500 pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool {
2501 self.len() == other.len() &&
2502 self.iter().zip(other).all(|(a, b)| {
2503 a.eq_ignore_ascii_case(b)
2507 /// Converts this slice to its ASCII upper case equivalent in-place.
2509 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
2510 /// but non-ASCII letters are unchanged.
2512 /// To return a new uppercased value without modifying the existing one, use
2513 /// [`to_ascii_uppercase`].
2515 /// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
2516 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2518 pub fn make_ascii_uppercase(&mut self) {
2520 byte.make_ascii_uppercase();
2524 /// Converts this slice to its ASCII lower case equivalent in-place.
2526 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
2527 /// but non-ASCII letters are unchanged.
2529 /// To return a new lowercased value without modifying the existing one, use
2530 /// [`to_ascii_lowercase`].
2532 /// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
2533 #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2535 pub fn make_ascii_lowercase(&mut self) {
2537 byte.make_ascii_lowercase();
2543 #[stable(feature = "rust1", since = "1.0.0")]
2544 impl<T, I> ops::Index<I> for [T]
2545 where I: SliceIndex<[T]>
2547 type Output = I::Output;
2550 fn index(&self, index: I) -> &I::Output {
2555 #[stable(feature = "rust1", since = "1.0.0")]
2556 impl<T, I> ops::IndexMut<I> for [T]
2557 where I: SliceIndex<[T]>
2560 fn index_mut(&mut self, index: I) -> &mut I::Output {
2561 index.index_mut(self)
2567 fn slice_index_len_fail(index: usize, len: usize) -> ! {
2568 panic!("index {} out of range for slice of length {}", index, len);
2573 fn slice_index_order_fail(index: usize, end: usize) -> ! {
2574 panic!("slice index starts at {} but ends at {}", index, end);
2579 fn slice_index_overflow_fail() -> ! {
2580 panic!("attempted to index slice up to maximum usize");
2583 mod private_slice_index {
2585 #[stable(feature = "slice_get_slice", since = "1.28.0")]
2588 #[stable(feature = "slice_get_slice", since = "1.28.0")]
2589 impl Sealed for usize {}
2590 #[stable(feature = "slice_get_slice", since = "1.28.0")]
2591 impl Sealed for ops::Range<usize> {}
2592 #[stable(feature = "slice_get_slice", since = "1.28.0")]
2593 impl Sealed for ops::RangeTo<usize> {}
2594 #[stable(feature = "slice_get_slice", since = "1.28.0")]
2595 impl Sealed for ops::RangeFrom<usize> {}
2596 #[stable(feature = "slice_get_slice", since = "1.28.0")]
2597 impl Sealed for ops::RangeFull {}
2598 #[stable(feature = "slice_get_slice", since = "1.28.0")]
2599 impl Sealed for ops::RangeInclusive<usize> {}
2600 #[stable(feature = "slice_get_slice", since = "1.28.0")]
2601 impl Sealed for ops::RangeToInclusive<usize> {}
2604 /// A helper trait used for indexing operations.
2605 #[stable(feature = "slice_get_slice", since = "1.28.0")]
2606 #[rustc_on_unimplemented(
2609 label = "string indices are ranges of `usize`",
2612 all(any(T = "str", T = "&str", T = "std::string::String"), _Self="{integer}"),
2613 note="you can use `.chars().nth()` or `.bytes().nth()`
2614 see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
2616 message = "the type `{T}` cannot be indexed by `{Self}`",
2617 label = "slice indices are of type `usize` or ranges of `usize`",
2619 pub trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
2620 /// The output type returned by methods.
2621 #[stable(feature = "slice_get_slice", since = "1.28.0")]
2622 type Output: ?Sized;
2624 /// Returns a shared reference to the output at this location, if in
2626 #[unstable(feature = "slice_index_methods", issue = "0")]
2627 fn get(self, slice: &T) -> Option<&Self::Output>;
2629 /// Returns a mutable reference to the output at this location, if in
2631 #[unstable(feature = "slice_index_methods", issue = "0")]
2632 fn get_mut(self, slice: &mut T) -> Option<&mut Self::Output>;
2634 /// Returns a shared reference to the output at this location, without
2635 /// performing any bounds checking.
2636 #[unstable(feature = "slice_index_methods", issue = "0")]
2637 unsafe fn get_unchecked(self, slice: &T) -> &Self::Output;
2639 /// Returns a mutable reference to the output at this location, without
2640 /// performing any bounds checking.
2641 #[unstable(feature = "slice_index_methods", issue = "0")]
2642 unsafe fn get_unchecked_mut(self, slice: &mut T) -> &mut Self::Output;
2644 /// Returns a shared reference to the output at this location, panicking
2645 /// if out of bounds.
2646 #[unstable(feature = "slice_index_methods", issue = "0")]
2647 fn index(self, slice: &T) -> &Self::Output;
2649 /// Returns a mutable reference to the output at this location, panicking
2650 /// if out of bounds.
2651 #[unstable(feature = "slice_index_methods", issue = "0")]
2652 fn index_mut(self, slice: &mut T) -> &mut Self::Output;
2655 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
2656 impl<T> SliceIndex<[T]> for usize {
2660 fn get(self, slice: &[T]) -> Option<&T> {
2661 if self < slice.len() {
2663 Some(self.get_unchecked(slice))
2671 fn get_mut(self, slice: &mut [T]) -> Option<&mut T> {
2672 if self < slice.len() {
2674 Some(self.get_unchecked_mut(slice))
2682 unsafe fn get_unchecked(self, slice: &[T]) -> &T {
2683 &*slice.as_ptr().add(self)
2687 unsafe fn get_unchecked_mut(self, slice: &mut [T]) -> &mut T {
2688 &mut *slice.as_mut_ptr().add(self)
2692 fn index(self, slice: &[T]) -> &T {
2693 // N.B., use intrinsic indexing
2698 fn index_mut(self, slice: &mut [T]) -> &mut T {
2699 // N.B., use intrinsic indexing
2704 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
2705 impl<T> SliceIndex<[T]> for ops::Range<usize> {
2709 fn get(self, slice: &[T]) -> Option<&[T]> {
2710 if self.start > self.end || self.end > slice.len() {
2714 Some(self.get_unchecked(slice))
2720 fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
2721 if self.start > self.end || self.end > slice.len() {
2725 Some(self.get_unchecked_mut(slice))
2731 unsafe fn get_unchecked(self, slice: &[T]) -> &[T] {
2732 from_raw_parts(slice.as_ptr().add(self.start), self.end - self.start)
2736 unsafe fn get_unchecked_mut(self, slice: &mut [T]) -> &mut [T] {
2737 from_raw_parts_mut(slice.as_mut_ptr().add(self.start), self.end - self.start)
2741 fn index(self, slice: &[T]) -> &[T] {
2742 if self.start > self.end {
2743 slice_index_order_fail(self.start, self.end);
2744 } else if self.end > slice.len() {
2745 slice_index_len_fail(self.end, slice.len());
2748 self.get_unchecked(slice)
2753 fn index_mut(self, slice: &mut [T]) -> &mut [T] {
2754 if self.start > self.end {
2755 slice_index_order_fail(self.start, self.end);
2756 } else if self.end > slice.len() {
2757 slice_index_len_fail(self.end, slice.len());
2760 self.get_unchecked_mut(slice)
2765 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
2766 impl<T> SliceIndex<[T]> for ops::RangeTo<usize> {
2770 fn get(self, slice: &[T]) -> Option<&[T]> {
2771 (0..self.end).get(slice)
2775 fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
2776 (0..self.end).get_mut(slice)
2780 unsafe fn get_unchecked(self, slice: &[T]) -> &[T] {
2781 (0..self.end).get_unchecked(slice)
2785 unsafe fn get_unchecked_mut(self, slice: &mut [T]) -> &mut [T] {
2786 (0..self.end).get_unchecked_mut(slice)
2790 fn index(self, slice: &[T]) -> &[T] {
2791 (0..self.end).index(slice)
2795 fn index_mut(self, slice: &mut [T]) -> &mut [T] {
2796 (0..self.end).index_mut(slice)
2800 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
2801 impl<T> SliceIndex<[T]> for ops::RangeFrom<usize> {
2805 fn get(self, slice: &[T]) -> Option<&[T]> {
2806 (self.start..slice.len()).get(slice)
2810 fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
2811 (self.start..slice.len()).get_mut(slice)
2815 unsafe fn get_unchecked(self, slice: &[T]) -> &[T] {
2816 (self.start..slice.len()).get_unchecked(slice)
2820 unsafe fn get_unchecked_mut(self, slice: &mut [T]) -> &mut [T] {
2821 (self.start..slice.len()).get_unchecked_mut(slice)
2825 fn index(self, slice: &[T]) -> &[T] {
2826 (self.start..slice.len()).index(slice)
2830 fn index_mut(self, slice: &mut [T]) -> &mut [T] {
2831 (self.start..slice.len()).index_mut(slice)
2835 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
2836 impl<T> SliceIndex<[T]> for ops::RangeFull {
2840 fn get(self, slice: &[T]) -> Option<&[T]> {
2845 fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
2850 unsafe fn get_unchecked(self, slice: &[T]) -> &[T] {
2855 unsafe fn get_unchecked_mut(self, slice: &mut [T]) -> &mut [T] {
2860 fn index(self, slice: &[T]) -> &[T] {
2865 fn index_mut(self, slice: &mut [T]) -> &mut [T] {
2871 #[stable(feature = "inclusive_range", since = "1.26.0")]
2872 impl<T> SliceIndex<[T]> for ops::RangeInclusive<usize> {
2876 fn get(self, slice: &[T]) -> Option<&[T]> {
2877 if *self.end() == usize::max_value() { None }
2878 else { (*self.start()..self.end() + 1).get(slice) }
2882 fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
2883 if *self.end() == usize::max_value() { None }
2884 else { (*self.start()..self.end() + 1).get_mut(slice) }
2888 unsafe fn get_unchecked(self, slice: &[T]) -> &[T] {
2889 (*self.start()..self.end() + 1).get_unchecked(slice)
2893 unsafe fn get_unchecked_mut(self, slice: &mut [T]) -> &mut [T] {
2894 (*self.start()..self.end() + 1).get_unchecked_mut(slice)
2898 fn index(self, slice: &[T]) -> &[T] {
2899 if *self.end() == usize::max_value() { slice_index_overflow_fail(); }
2900 (*self.start()..self.end() + 1).index(slice)
2904 fn index_mut(self, slice: &mut [T]) -> &mut [T] {
2905 if *self.end() == usize::max_value() { slice_index_overflow_fail(); }
2906 (*self.start()..self.end() + 1).index_mut(slice)
2910 #[stable(feature = "inclusive_range", since = "1.26.0")]
2911 impl<T> SliceIndex<[T]> for ops::RangeToInclusive<usize> {
2915 fn get(self, slice: &[T]) -> Option<&[T]> {
2916 (0..=self.end).get(slice)
2920 fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
2921 (0..=self.end).get_mut(slice)
2925 unsafe fn get_unchecked(self, slice: &[T]) -> &[T] {
2926 (0..=self.end).get_unchecked(slice)
2930 unsafe fn get_unchecked_mut(self, slice: &mut [T]) -> &mut [T] {
2931 (0..=self.end).get_unchecked_mut(slice)
2935 fn index(self, slice: &[T]) -> &[T] {
2936 (0..=self.end).index(slice)
2940 fn index_mut(self, slice: &mut [T]) -> &mut [T] {
2941 (0..=self.end).index_mut(slice)
2945 ////////////////////////////////////////////////////////////////////////////////
2947 ////////////////////////////////////////////////////////////////////////////////
2949 #[stable(feature = "rust1", since = "1.0.0")]
2950 impl<T> Default for &[T] {
2951 /// Creates an empty slice.
2952 fn default() -> Self { &[] }
2955 #[stable(feature = "mut_slice_default", since = "1.5.0")]
2956 impl<T> Default for &mut [T] {
2957 /// Creates a mutable empty slice.
2958 fn default() -> Self { &mut [] }
2965 #[stable(feature = "rust1", since = "1.0.0")]
2966 impl<'a, T> IntoIterator for &'a [T] {
2968 type IntoIter = Iter<'a, T>;
2970 fn into_iter(self) -> Iter<'a, T> {
2975 #[stable(feature = "rust1", since = "1.0.0")]
2976 impl<'a, T> IntoIterator for &'a mut [T] {
2977 type Item = &'a mut T;
2978 type IntoIter = IterMut<'a, T>;
2980 fn into_iter(self) -> IterMut<'a, T> {
2985 // Macro helper functions
2987 fn size_from_ptr<T>(_: *const T) -> usize {
2991 // Inlining is_empty and len makes a huge performance difference
2992 macro_rules! is_empty {
2993 // The way we encode the length of a ZST iterator, this works both for ZST
2995 ($self: ident) => {$self.ptr == $self.end}
2997 // To get rid of some bounds checks (see `position`), we compute the length in a somewhat
2998 // unexpected way. (Tested by `codegen/slice-position-bounds-check`.)
3000 ($self: ident) => {{
3001 let start = $self.ptr;
3002 let diff = ($self.end as usize).wrapping_sub(start as usize);
3003 let size = size_from_ptr(start);
3007 // Using division instead of `offset_from` helps LLVM remove bounds checks
3013 // The shared definition of the `Iter` and `IterMut` iterators
3014 macro_rules! iterator {
3016 struct $name:ident -> $ptr:ty,
3022 impl<'a, T> $name<'a, T> {
3023 // Helper function for creating a slice from the iterator.
3025 fn make_slice(&self) -> &'a [T] {
3026 unsafe { from_raw_parts(self.ptr, len!(self)) }
3029 // Helper function for moving the start of the iterator forwards by `offset` elements,
3030 // returning the old start.
3031 // Unsafe because the offset must be in-bounds or one-past-the-end.
3033 unsafe fn post_inc_start(&mut self, offset: isize) -> * $raw_mut T {
3034 if mem::size_of::<T>() == 0 {
3035 // This is *reducing* the length. `ptr` never changes with ZST.
3036 self.end = (self.end as * $raw_mut u8).wrapping_offset(-offset) as * $raw_mut T;
3040 self.ptr = self.ptr.offset(offset);
3045 // Helper function for moving the end of the iterator backwards by `offset` elements,
3046 // returning the new end.
3047 // Unsafe because the offset must be in-bounds or one-past-the-end.
3049 unsafe fn pre_dec_end(&mut self, offset: isize) -> * $raw_mut T {
3050 if mem::size_of::<T>() == 0 {
3051 self.end = (self.end as * $raw_mut u8).wrapping_offset(-offset) as * $raw_mut T;
3054 self.end = self.end.offset(-offset);
3060 #[stable(feature = "rust1", since = "1.0.0")]
3061 impl<T> ExactSizeIterator for $name<'_, T> {
3063 fn len(&self) -> usize {
3068 fn is_empty(&self) -> bool {
3073 #[stable(feature = "rust1", since = "1.0.0")]
3074 impl<'a, T> Iterator for $name<'a, T> {
3078 fn next(&mut self) -> Option<$elem> {
3079 // could be implemented with slices, but this avoids bounds checks
3081 assume(!self.ptr.is_null());
3082 if mem::size_of::<T>() != 0 {
3083 assume(!self.end.is_null());
3085 if is_empty!(self) {
3088 Some(& $( $mut_ )* *self.post_inc_start(1))
3094 fn size_hint(&self) -> (usize, Option<usize>) {
3095 let exact = len!(self);
3096 (exact, Some(exact))
3100 fn count(self) -> usize {
3105 fn nth(&mut self, n: usize) -> Option<$elem> {
3106 if n >= len!(self) {
3107 // This iterator is now empty.
3108 if mem::size_of::<T>() == 0 {
3109 // We have to do it this way as `ptr` may never be 0, but `end`
3110 // could be (due to wrapping).
3111 self.end = self.ptr;
3113 self.ptr = self.end;
3117 // We are in bounds. `offset` does the right thing even for ZSTs.
3119 let elem = Some(& $( $mut_ )* *self.ptr.add(n));
3120 self.post_inc_start((n as isize).wrapping_add(1));
3126 fn last(mut self) -> Option<$elem> {
3131 fn try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R where
3132 Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
3134 // manual unrolling is needed when there are conditional exits from the loop
3135 let mut accum = init;
3137 while len!(self) >= 4 {
3138 accum = f(accum, & $( $mut_ )* *self.post_inc_start(1))?;
3139 accum = f(accum, & $( $mut_ )* *self.post_inc_start(1))?;
3140 accum = f(accum, & $( $mut_ )* *self.post_inc_start(1))?;
3141 accum = f(accum, & $( $mut_ )* *self.post_inc_start(1))?;
3143 while !is_empty!(self) {
3144 accum = f(accum, & $( $mut_ )* *self.post_inc_start(1))?;
3151 fn fold<Acc, Fold>(mut self, init: Acc, mut f: Fold) -> Acc
3152 where Fold: FnMut(Acc, Self::Item) -> Acc,
3154 // Let LLVM unroll this, rather than using the default
3155 // impl that would force the manual unrolling above
3156 let mut accum = init;
3157 while let Some(x) = self.next() {
3158 accum = f(accum, x);
3164 #[rustc_inherit_overflow_checks]
3165 fn position<P>(&mut self, mut predicate: P) -> Option<usize> where
3167 P: FnMut(Self::Item) -> bool,
3169 // The addition might panic on overflow.
3171 self.try_fold(0, move |i, x| {
3172 if predicate(x) { Err(i) }
3176 unsafe { assume(i < n) };
3182 fn rposition<P>(&mut self, mut predicate: P) -> Option<usize> where
3183 P: FnMut(Self::Item) -> bool,
3184 Self: Sized + ExactSizeIterator + DoubleEndedIterator
3186 // No need for an overflow check here, because `ExactSizeIterator`
3188 self.try_rfold(n, move |i, x| {
3190 if predicate(x) { Err(i) }
3194 unsafe { assume(i < n) };
3202 #[stable(feature = "rust1", since = "1.0.0")]
3203 impl<'a, T> DoubleEndedIterator for $name<'a, T> {
3205 fn next_back(&mut self) -> Option<$elem> {
3206 // could be implemented with slices, but this avoids bounds checks
3208 assume(!self.ptr.is_null());
3209 if mem::size_of::<T>() != 0 {
3210 assume(!self.end.is_null());
3212 if is_empty!(self) {
3215 Some(& $( $mut_ )* *self.pre_dec_end(1))
3221 fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R where
3222 Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
3224 // manual unrolling is needed when there are conditional exits from the loop
3225 let mut accum = init;
3227 while len!(self) >= 4 {
3228 accum = f(accum, & $( $mut_ )* *self.pre_dec_end(1))?;
3229 accum = f(accum, & $( $mut_ )* *self.pre_dec_end(1))?;
3230 accum = f(accum, & $( $mut_ )* *self.pre_dec_end(1))?;
3231 accum = f(accum, & $( $mut_ )* *self.pre_dec_end(1))?;
3233 // inlining is_empty everywhere makes a huge performance difference
3234 while !is_empty!(self) {
3235 accum = f(accum, & $( $mut_ )* *self.pre_dec_end(1))?;
3242 fn rfold<Acc, Fold>(mut self, init: Acc, mut f: Fold) -> Acc
3243 where Fold: FnMut(Acc, Self::Item) -> Acc,
3245 // Let LLVM unroll this, rather than using the default
3246 // impl that would force the manual unrolling above
3247 let mut accum = init;
3248 while let Some(x) = self.next_back() {
3249 accum = f(accum, x);
3255 #[stable(feature = "fused", since = "1.26.0")]
3256 impl<T> FusedIterator for $name<'_, T> {}
3258 #[unstable(feature = "trusted_len", issue = "37572")]
3259 unsafe impl<T> TrustedLen for $name<'_, T> {}
3263 /// Immutable slice iterator
3265 /// This struct is created by the [`iter`] method on [slices].
3272 /// // First, we declare a type which has `iter` method to get the `Iter` struct (&[usize here]):
3273 /// let slice = &[1, 2, 3];
3275 /// // Then, we iterate over it:
3276 /// for element in slice.iter() {
3277 /// println!("{}", element);
3281 /// [`iter`]: ../../std/primitive.slice.html#method.iter
3282 /// [slices]: ../../std/primitive.slice.html
3283 #[stable(feature = "rust1", since = "1.0.0")]
3284 pub struct Iter<'a, T: 'a> {
3286 end: *const T, // If T is a ZST, this is actually ptr+len. This encoding is picked so that
3287 // ptr == end is a quick test for the Iterator being empty, that works
3288 // for both ZST and non-ZST.
3289 _marker: marker::PhantomData<&'a T>,
3292 #[stable(feature = "core_impl_debug", since = "1.9.0")]
3293 impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
3294 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3295 f.debug_tuple("Iter")
3296 .field(&self.as_slice())
3301 #[stable(feature = "rust1", since = "1.0.0")]
3302 unsafe impl<T: Sync> Sync for Iter<'_, T> {}
3303 #[stable(feature = "rust1", since = "1.0.0")]
3304 unsafe impl<T: Sync> Send for Iter<'_, T> {}
3306 impl<'a, T> Iter<'a, T> {
3307 /// Views the underlying data as a subslice of the original data.
3309 /// This has the same lifetime as the original slice, and so the
3310 /// iterator can continue to be used while this exists.
3317 /// // First, we declare a type which has the `iter` method to get the `Iter`
3318 /// // struct (&[usize here]):
3319 /// let slice = &[1, 2, 3];
3321 /// // Then, we get the iterator:
3322 /// let mut iter = slice.iter();
3323 /// // So if we print what `as_slice` method returns here, we have "[1, 2, 3]":
3324 /// println!("{:?}", iter.as_slice());
3326 /// // Next, we move to the second element of the slice:
3328 /// // Now `as_slice` returns "[2, 3]":
3329 /// println!("{:?}", iter.as_slice());
3331 #[stable(feature = "iter_to_slice", since = "1.4.0")]
3332 pub fn as_slice(&self) -> &'a [T] {
3337 iterator!{struct Iter -> *const T, &'a T, const, {/* no mut */}, {
3338 fn is_sorted_by<F>(self, mut compare: F) -> bool
3341 F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>,
3343 self.as_slice().windows(2).all(|w| {
3344 compare(&&w[0], &&w[1]).map(|o| o != Ordering::Greater).unwrap_or(false)
3349 #[stable(feature = "rust1", since = "1.0.0")]
3350 impl<T> Clone for Iter<'_, T> {
3351 fn clone(&self) -> Self { Iter { ptr: self.ptr, end: self.end, _marker: self._marker } }
3354 #[stable(feature = "slice_iter_as_ref", since = "1.13.0")]
3355 impl<T> AsRef<[T]> for Iter<'_, T> {
3356 fn as_ref(&self) -> &[T] {
3361 /// Mutable slice iterator.
3363 /// This struct is created by the [`iter_mut`] method on [slices].
3370 /// // First, we declare a type which has `iter_mut` method to get the `IterMut`
3371 /// // struct (&[usize here]):
3372 /// let mut slice = &mut [1, 2, 3];
3374 /// // Then, we iterate over it and increment each element value:
3375 /// for element in slice.iter_mut() {
3379 /// // We now have "[2, 3, 4]":
3380 /// println!("{:?}", slice);
3383 /// [`iter_mut`]: ../../std/primitive.slice.html#method.iter_mut
3384 /// [slices]: ../../std/primitive.slice.html
3385 #[stable(feature = "rust1", since = "1.0.0")]
3386 pub struct IterMut<'a, T: 'a> {
3388 end: *mut T, // If T is a ZST, this is actually ptr+len. This encoding is picked so that
3389 // ptr == end is a quick test for the Iterator being empty, that works
3390 // for both ZST and non-ZST.
3391 _marker: marker::PhantomData<&'a mut T>,
3394 #[stable(feature = "core_impl_debug", since = "1.9.0")]
3395 impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
3396 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3397 f.debug_tuple("IterMut")
3398 .field(&self.make_slice())
3403 #[stable(feature = "rust1", since = "1.0.0")]
3404 unsafe impl<T: Sync> Sync for IterMut<'_, T> {}
3405 #[stable(feature = "rust1", since = "1.0.0")]
3406 unsafe impl<T: Send> Send for IterMut<'_, T> {}
3408 impl<'a, T> IterMut<'a, T> {
3409 /// Views the underlying data as a subslice of the original data.
3411 /// To avoid creating `&mut` references that alias, this is forced
3412 /// to consume the iterator.
3419 /// // First, we declare a type which has `iter_mut` method to get the `IterMut`
3420 /// // struct (&[usize here]):
3421 /// let mut slice = &mut [1, 2, 3];
3424 /// // Then, we get the iterator:
3425 /// let mut iter = slice.iter_mut();
3426 /// // We move to next element:
3428 /// // So if we print what `into_slice` method returns here, we have "[2, 3]":
3429 /// println!("{:?}", iter.into_slice());
3432 /// // Now let's modify a value of the slice:
3434 /// // First we get back the iterator:
3435 /// let mut iter = slice.iter_mut();
3436 /// // We change the value of the first element of the slice returned by the `next` method:
3437 /// *iter.next().unwrap() += 1;
3439 /// // Now slice is "[2, 2, 3]":
3440 /// println!("{:?}", slice);
3442 #[stable(feature = "iter_to_slice", since = "1.4.0")]
3443 pub fn into_slice(self) -> &'a mut [T] {
3444 unsafe { from_raw_parts_mut(self.ptr, len!(self)) }
3447 /// Views the underlying data as a subslice of the original data.
3449 /// To avoid creating `&mut [T]` references that alias, the returned slice
3450 /// borrows its lifetime from the iterator the method is applied on.
3457 /// # #![feature(slice_iter_mut_as_slice)]
3458 /// let mut slice: &mut [usize] = &mut [1, 2, 3];
3460 /// // First, we get the iterator:
3461 /// let mut iter = slice.iter_mut();
3462 /// // So if we check what the `as_slice` method returns here, we have "[1, 2, 3]":
3463 /// assert_eq!(iter.as_slice(), &[1, 2, 3]);
3465 /// // Next, we move to the second element of the slice:
3467 /// // Now `as_slice` returns "[2, 3]":
3468 /// assert_eq!(iter.as_slice(), &[2, 3]);
3470 #[unstable(feature = "slice_iter_mut_as_slice", reason = "recently added", issue = "58957")]
3471 pub fn as_slice(&self) -> &[T] {
3476 iterator!{struct IterMut -> *mut T, &'a mut T, mut, {mut}, {}}
3478 /// An internal abstraction over the splitting iterators, so that
3479 /// splitn, splitn_mut etc can be implemented once.
3481 trait SplitIter: DoubleEndedIterator {
3482 /// Marks the underlying iterator as complete, extracting the remaining
3483 /// portion of the slice.
3484 fn finish(&mut self) -> Option<Self::Item>;
3487 /// An iterator over subslices separated by elements that match a predicate
3490 /// This struct is created by the [`split`] method on [slices].
3492 /// [`split`]: ../../std/primitive.slice.html#method.split
3493 /// [slices]: ../../std/primitive.slice.html
3494 #[stable(feature = "rust1", since = "1.0.0")]
3495 pub struct Split<'a, T:'a, P> where P: FnMut(&T) -> bool {
3501 #[stable(feature = "core_impl_debug", since = "1.9.0")]
3502 impl<T: fmt::Debug, P> fmt::Debug for Split<'_, T, P> where P: FnMut(&T) -> bool {
3503 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3504 f.debug_struct("Split")
3505 .field("v", &self.v)
3506 .field("finished", &self.finished)
3511 // FIXME(#26925) Remove in favor of `#[derive(Clone)]`
3512 #[stable(feature = "rust1", since = "1.0.0")]
3513 impl<T, P> Clone for Split<'_, T, P> where P: Clone + FnMut(&T) -> bool {
3514 fn clone(&self) -> Self {
3517 pred: self.pred.clone(),
3518 finished: self.finished,
3523 #[stable(feature = "rust1", since = "1.0.0")]
3524 impl<'a, T, P> Iterator for Split<'a, T, P> where P: FnMut(&T) -> bool {
3525 type Item = &'a [T];
3528 fn next(&mut self) -> Option<&'a [T]> {
3529 if self.finished { return None; }
3531 match self.v.iter().position(|x| (self.pred)(x)) {
3532 None => self.finish(),
3534 let ret = Some(&self.v[..idx]);
3535 self.v = &self.v[idx + 1..];
3542 fn size_hint(&self) -> (usize, Option<usize>) {
3546 (1, Some(self.v.len() + 1))
3551 #[stable(feature = "rust1", since = "1.0.0")]
3552 impl<'a, T, P> DoubleEndedIterator for Split<'a, T, P> where P: FnMut(&T) -> bool {
3554 fn next_back(&mut self) -> Option<&'a [T]> {
3555 if self.finished { return None; }
3557 match self.v.iter().rposition(|x| (self.pred)(x)) {
3558 None => self.finish(),
3560 let ret = Some(&self.v[idx + 1..]);
3561 self.v = &self.v[..idx];
3568 impl<'a, T, P> SplitIter for Split<'a, T, P> where P: FnMut(&T) -> bool {
3570 fn finish(&mut self) -> Option<&'a [T]> {
3571 if self.finished { None } else { self.finished = true; Some(self.v) }
3575 #[stable(feature = "fused", since = "1.26.0")]
3576 impl<T, P> FusedIterator for Split<'_, T, P> where P: FnMut(&T) -> bool {}
3578 /// An iterator over the subslices of the vector which are separated
3579 /// by elements that match `pred`.
3581 /// This struct is created by the [`split_mut`] method on [slices].
3583 /// [`split_mut`]: ../../std/primitive.slice.html#method.split_mut
3584 /// [slices]: ../../std/primitive.slice.html
3585 #[stable(feature = "rust1", since = "1.0.0")]
3586 pub struct SplitMut<'a, T:'a, P> where P: FnMut(&T) -> bool {
3592 #[stable(feature = "core_impl_debug", since = "1.9.0")]
3593 impl<T: fmt::Debug, P> fmt::Debug for SplitMut<'_, T, P> where P: FnMut(&T) -> bool {
3594 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3595 f.debug_struct("SplitMut")
3596 .field("v", &self.v)
3597 .field("finished", &self.finished)
3602 impl<'a, T, P> SplitIter for SplitMut<'a, T, P> where P: FnMut(&T) -> bool {
3604 fn finish(&mut self) -> Option<&'a mut [T]> {
3608 self.finished = true;
3609 Some(mem::replace(&mut self.v, &mut []))
3614 #[stable(feature = "rust1", since = "1.0.0")]
3615 impl<'a, T, P> Iterator for SplitMut<'a, T, P> where P: FnMut(&T) -> bool {
3616 type Item = &'a mut [T];
3619 fn next(&mut self) -> Option<&'a mut [T]> {
3620 if self.finished { return None; }
3622 let idx_opt = { // work around borrowck limitations
3623 let pred = &mut self.pred;
3624 self.v.iter().position(|x| (*pred)(x))
3627 None => self.finish(),
3629 let tmp = mem::replace(&mut self.v, &mut []);
3630 let (head, tail) = tmp.split_at_mut(idx);
3631 self.v = &mut tail[1..];
3638 fn size_hint(&self) -> (usize, Option<usize>) {
3642 // if the predicate doesn't match anything, we yield one slice
3643 // if it matches every element, we yield len+1 empty slices.
3644 (1, Some(self.v.len() + 1))
3649 #[stable(feature = "rust1", since = "1.0.0")]
3650 impl<'a, T, P> DoubleEndedIterator for SplitMut<'a, T, P> where
3651 P: FnMut(&T) -> bool,
3654 fn next_back(&mut self) -> Option<&'a mut [T]> {
3655 if self.finished { return None; }
3657 let idx_opt = { // work around borrowck limitations
3658 let pred = &mut self.pred;
3659 self.v.iter().rposition(|x| (*pred)(x))
3662 None => self.finish(),
3664 let tmp = mem::replace(&mut self.v, &mut []);
3665 let (head, tail) = tmp.split_at_mut(idx);
3667 Some(&mut tail[1..])
3673 #[stable(feature = "fused", since = "1.26.0")]
3674 impl<T, P> FusedIterator for SplitMut<'_, T, P> where P: FnMut(&T) -> bool {}
3676 /// An iterator over subslices separated by elements that match a predicate
3677 /// function, starting from the end of the slice.
3679 /// This struct is created by the [`rsplit`] method on [slices].
3681 /// [`rsplit`]: ../../std/primitive.slice.html#method.rsplit
3682 /// [slices]: ../../std/primitive.slice.html
3683 #[stable(feature = "slice_rsplit", since = "1.27.0")]
3684 #[derive(Clone)] // Is this correct, or does it incorrectly require `T: Clone`?
3685 pub struct RSplit<'a, T:'a, P> where P: FnMut(&T) -> bool {
3686 inner: Split<'a, T, P>
3689 #[stable(feature = "slice_rsplit", since = "1.27.0")]
3690 impl<T: fmt::Debug, P> fmt::Debug for RSplit<'_, T, P> where P: FnMut(&T) -> bool {
3691 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3692 f.debug_struct("RSplit")
3693 .field("v", &self.inner.v)
3694 .field("finished", &self.inner.finished)
3699 #[stable(feature = "slice_rsplit", since = "1.27.0")]
3700 impl<'a, T, P> Iterator for RSplit<'a, T, P> where P: FnMut(&T) -> bool {
3701 type Item = &'a [T];
3704 fn next(&mut self) -> Option<&'a [T]> {
3705 self.inner.next_back()
3709 fn size_hint(&self) -> (usize, Option<usize>) {
3710 self.inner.size_hint()
3714 #[stable(feature = "slice_rsplit", since = "1.27.0")]
3715 impl<'a, T, P> DoubleEndedIterator for RSplit<'a, T, P> where P: FnMut(&T) -> bool {
3717 fn next_back(&mut self) -> Option<&'a [T]> {
3722 #[stable(feature = "slice_rsplit", since = "1.27.0")]
3723 impl<'a, T, P> SplitIter for RSplit<'a, T, P> where P: FnMut(&T) -> bool {
3725 fn finish(&mut self) -> Option<&'a [T]> {
3730 #[stable(feature = "slice_rsplit", since = "1.27.0")]
3731 impl<T, P> FusedIterator for RSplit<'_, T, P> where P: FnMut(&T) -> bool {}
3733 /// An iterator over the subslices of the vector which are separated
3734 /// by elements that match `pred`, starting from the end of the slice.
3736 /// This struct is created by the [`rsplit_mut`] method on [slices].
3738 /// [`rsplit_mut`]: ../../std/primitive.slice.html#method.rsplit_mut
3739 /// [slices]: ../../std/primitive.slice.html
3740 #[stable(feature = "slice_rsplit", since = "1.27.0")]
3741 pub struct RSplitMut<'a, T:'a, P> where P: FnMut(&T) -> bool {
3742 inner: SplitMut<'a, T, P>
3745 #[stable(feature = "slice_rsplit", since = "1.27.0")]
3746 impl<T: fmt::Debug, P> fmt::Debug for RSplitMut<'_, T, P> where P: FnMut(&T) -> bool {
3747 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3748 f.debug_struct("RSplitMut")
3749 .field("v", &self.inner.v)
3750 .field("finished", &self.inner.finished)
3755 #[stable(feature = "slice_rsplit", since = "1.27.0")]
3756 impl<'a, T, P> SplitIter for RSplitMut<'a, T, P> where P: FnMut(&T) -> bool {
3758 fn finish(&mut self) -> Option<&'a mut [T]> {
3763 #[stable(feature = "slice_rsplit", since = "1.27.0")]
3764 impl<'a, T, P> Iterator for RSplitMut<'a, T, P> where P: FnMut(&T) -> bool {
3765 type Item = &'a mut [T];
3768 fn next(&mut self) -> Option<&'a mut [T]> {
3769 self.inner.next_back()
3773 fn size_hint(&self) -> (usize, Option<usize>) {
3774 self.inner.size_hint()
3778 #[stable(feature = "slice_rsplit", since = "1.27.0")]
3779 impl<'a, T, P> DoubleEndedIterator for RSplitMut<'a, T, P> where
3780 P: FnMut(&T) -> bool,
3783 fn next_back(&mut self) -> Option<&'a mut [T]> {
3788 #[stable(feature = "slice_rsplit", since = "1.27.0")]
3789 impl<T, P> FusedIterator for RSplitMut<'_, T, P> where P: FnMut(&T) -> bool {}
3791 /// An private iterator over subslices separated by elements that
3792 /// match a predicate function, splitting at most a fixed number of
3795 struct GenericSplitN<I> {
3800 impl<T, I: SplitIter<Item=T>> Iterator for GenericSplitN<I> {
3804 fn next(&mut self) -> Option<T> {
3807 1 => { self.count -= 1; self.iter.finish() }
3808 _ => { self.count -= 1; self.iter.next() }
3813 fn size_hint(&self) -> (usize, Option<usize>) {
3814 let (lower, upper_opt) = self.iter.size_hint();
3815 (lower, upper_opt.map(|upper| cmp::min(self.count, upper)))
3819 /// An iterator over subslices separated by elements that match a predicate
3820 /// function, limited to a given number of splits.
3822 /// This struct is created by the [`splitn`] method on [slices].
3824 /// [`splitn`]: ../../std/primitive.slice.html#method.splitn
3825 /// [slices]: ../../std/primitive.slice.html
3826 #[stable(feature = "rust1", since = "1.0.0")]
3827 pub struct SplitN<'a, T: 'a, P> where P: FnMut(&T) -> bool {
3828 inner: GenericSplitN<Split<'a, T, P>>
3831 #[stable(feature = "core_impl_debug", since = "1.9.0")]
3832 impl<T: fmt::Debug, P> fmt::Debug for SplitN<'_, T, P> where P: FnMut(&T) -> bool {
3833 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3834 f.debug_struct("SplitN")
3835 .field("inner", &self.inner)
3840 /// An iterator over subslices separated by elements that match a
3841 /// predicate function, limited to a given number of splits, starting
3842 /// from the end of the slice.
3844 /// This struct is created by the [`rsplitn`] method on [slices].
3846 /// [`rsplitn`]: ../../std/primitive.slice.html#method.rsplitn
3847 /// [slices]: ../../std/primitive.slice.html
3848 #[stable(feature = "rust1", since = "1.0.0")]
3849 pub struct RSplitN<'a, T: 'a, P> where P: FnMut(&T) -> bool {
3850 inner: GenericSplitN<RSplit<'a, T, P>>
3853 #[stable(feature = "core_impl_debug", since = "1.9.0")]
3854 impl<T: fmt::Debug, P> fmt::Debug for RSplitN<'_, T, P> where P: FnMut(&T) -> bool {
3855 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3856 f.debug_struct("RSplitN")
3857 .field("inner", &self.inner)
3862 /// An iterator over subslices separated by elements that match a predicate
3863 /// function, limited to a given number of splits.
3865 /// This struct is created by the [`splitn_mut`] method on [slices].
3867 /// [`splitn_mut`]: ../../std/primitive.slice.html#method.splitn_mut
3868 /// [slices]: ../../std/primitive.slice.html
3869 #[stable(feature = "rust1", since = "1.0.0")]
3870 pub struct SplitNMut<'a, T: 'a, P> where P: FnMut(&T) -> bool {
3871 inner: GenericSplitN<SplitMut<'a, T, P>>
3874 #[stable(feature = "core_impl_debug", since = "1.9.0")]
3875 impl<T: fmt::Debug, P> fmt::Debug for SplitNMut<'_, T, P> where P: FnMut(&T) -> bool {
3876 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3877 f.debug_struct("SplitNMut")
3878 .field("inner", &self.inner)
3883 /// An iterator over subslices separated by elements that match a
3884 /// predicate function, limited to a given number of splits, starting
3885 /// from the end of the slice.
3887 /// This struct is created by the [`rsplitn_mut`] method on [slices].
3889 /// [`rsplitn_mut`]: ../../std/primitive.slice.html#method.rsplitn_mut
3890 /// [slices]: ../../std/primitive.slice.html
3891 #[stable(feature = "rust1", since = "1.0.0")]
3892 pub struct RSplitNMut<'a, T: 'a, P> where P: FnMut(&T) -> bool {
3893 inner: GenericSplitN<RSplitMut<'a, T, P>>
3896 #[stable(feature = "core_impl_debug", since = "1.9.0")]
3897 impl<T: fmt::Debug, P> fmt::Debug for RSplitNMut<'_, T, P> where P: FnMut(&T) -> bool {
3898 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3899 f.debug_struct("RSplitNMut")
3900 .field("inner", &self.inner)
3905 macro_rules! forward_iterator {
3906 ($name:ident: $elem:ident, $iter_of:ty) => {
3907 #[stable(feature = "rust1", since = "1.0.0")]
3908 impl<'a, $elem, P> Iterator for $name<'a, $elem, P> where
3909 P: FnMut(&T) -> bool
3911 type Item = $iter_of;
3914 fn next(&mut self) -> Option<$iter_of> {
3919 fn size_hint(&self) -> (usize, Option<usize>) {
3920 self.inner.size_hint()
3924 #[stable(feature = "fused", since = "1.26.0")]
3925 impl<'a, $elem, P> FusedIterator for $name<'a, $elem, P>
3926 where P: FnMut(&T) -> bool {}
3930 forward_iterator! { SplitN: T, &'a [T] }
3931 forward_iterator! { RSplitN: T, &'a [T] }
3932 forward_iterator! { SplitNMut: T, &'a mut [T] }
3933 forward_iterator! { RSplitNMut: T, &'a mut [T] }
3935 /// An iterator over overlapping subslices of length `size`.
3937 /// This struct is created by the [`windows`] method on [slices].
3939 /// [`windows`]: ../../std/primitive.slice.html#method.windows
3940 /// [slices]: ../../std/primitive.slice.html
3942 #[stable(feature = "rust1", since = "1.0.0")]
3943 pub struct Windows<'a, T:'a> {
3948 // FIXME(#26925) Remove in favor of `#[derive(Clone)]`
3949 #[stable(feature = "rust1", since = "1.0.0")]
3950 impl<T> Clone for Windows<'_, T> {
3951 fn clone(&self) -> Self {
3959 #[stable(feature = "rust1", since = "1.0.0")]
3960 impl<'a, T> Iterator for Windows<'a, T> {
3961 type Item = &'a [T];
3964 fn next(&mut self) -> Option<&'a [T]> {
3965 if self.size > self.v.len() {
3968 let ret = Some(&self.v[..self.size]);
3969 self.v = &self.v[1..];
3975 fn size_hint(&self) -> (usize, Option<usize>) {
3976 if self.size > self.v.len() {
3979 let size = self.v.len() - self.size + 1;
3985 fn count(self) -> usize {
3990 fn nth(&mut self, n: usize) -> Option<Self::Item> {
3991 let (end, overflow) = self.size.overflowing_add(n);
3992 if end > self.v.len() || overflow {
3996 let nth = &self.v[n..end];
3997 self.v = &self.v[n+1..];
4003 fn last(self) -> Option<Self::Item> {
4004 if self.size > self.v.len() {
4007 let start = self.v.len() - self.size;
4008 Some(&self.v[start..])
4013 #[stable(feature = "rust1", since = "1.0.0")]
4014 impl<'a, T> DoubleEndedIterator for Windows<'a, T> {
4016 fn next_back(&mut self) -> Option<&'a [T]> {
4017 if self.size > self.v.len() {
4020 let ret = Some(&self.v[self.v.len()-self.size..]);
4021 self.v = &self.v[..self.v.len()-1];
4027 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
4028 let (end, overflow) = self.v.len().overflowing_sub(n);
4029 if end < self.size || overflow {
4033 let ret = &self.v[end-self.size..end];
4034 self.v = &self.v[..end-1];
4040 #[stable(feature = "rust1", since = "1.0.0")]
4041 impl<T> ExactSizeIterator for Windows<'_, T> {}
4043 #[unstable(feature = "trusted_len", issue = "37572")]
4044 unsafe impl<T> TrustedLen for Windows<'_, T> {}
4046 #[stable(feature = "fused", since = "1.26.0")]
4047 impl<T> FusedIterator for Windows<'_, T> {}
4050 unsafe impl<'a, T> TrustedRandomAccess for Windows<'a, T> {
4051 unsafe fn get_unchecked(&mut self, i: usize) -> &'a [T] {
4052 from_raw_parts(self.v.as_ptr().add(i), self.size)
4054 fn may_have_side_effect() -> bool { false }
4057 /// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
4058 /// time), starting at the beginning of the slice.
4060 /// When the slice len is not evenly divided by the chunk size, the last slice
4061 /// of the iteration will be the remainder.
4063 /// This struct is created by the [`chunks`] method on [slices].
4065 /// [`chunks`]: ../../std/primitive.slice.html#method.chunks
4066 /// [slices]: ../../std/primitive.slice.html
4068 #[stable(feature = "rust1", since = "1.0.0")]
4069 pub struct Chunks<'a, T:'a> {
4074 // FIXME(#26925) Remove in favor of `#[derive(Clone)]`
4075 #[stable(feature = "rust1", since = "1.0.0")]
4076 impl<T> Clone for Chunks<'_, T> {
4077 fn clone(&self) -> Self {
4080 chunk_size: self.chunk_size,
4085 #[stable(feature = "rust1", since = "1.0.0")]
4086 impl<'a, T> Iterator for Chunks<'a, T> {
4087 type Item = &'a [T];
4090 fn next(&mut self) -> Option<&'a [T]> {
4091 if self.v.is_empty() {
4094 let chunksz = cmp::min(self.v.len(), self.chunk_size);
4095 let (fst, snd) = self.v.split_at(chunksz);
4102 fn size_hint(&self) -> (usize, Option<usize>) {
4103 if self.v.is_empty() {
4106 let n = self.v.len() / self.chunk_size;
4107 let rem = self.v.len() % self.chunk_size;
4108 let n = if rem > 0 { n+1 } else { n };
4114 fn count(self) -> usize {
4119 fn nth(&mut self, n: usize) -> Option<Self::Item> {
4120 let (start, overflow) = n.overflowing_mul(self.chunk_size);
4121 if start >= self.v.len() || overflow {
4125 let end = match start.checked_add(self.chunk_size) {
4126 Some(sum) => cmp::min(self.v.len(), sum),
4127 None => self.v.len(),
4129 let nth = &self.v[start..end];
4130 self.v = &self.v[end..];
4136 fn last(self) -> Option<Self::Item> {
4137 if self.v.is_empty() {
4140 let start = (self.v.len() - 1) / self.chunk_size * self.chunk_size;
4141 Some(&self.v[start..])
4146 #[stable(feature = "rust1", since = "1.0.0")]
4147 impl<'a, T> DoubleEndedIterator for Chunks<'a, T> {
4149 fn next_back(&mut self) -> Option<&'a [T]> {
4150 if self.v.is_empty() {
4153 let remainder = self.v.len() % self.chunk_size;
4154 let chunksz = if remainder != 0 { remainder } else { self.chunk_size };
4155 let (fst, snd) = self.v.split_at(self.v.len() - chunksz);
4162 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
4163 let len = self.len();
4168 let start = (len - 1 - n) * self.chunk_size;
4169 let end = match start.checked_add(self.chunk_size) {
4170 Some(res) => cmp::min(res, self.v.len()),
4171 None => self.v.len(),
4173 let nth_back = &self.v[start..end];
4174 self.v = &self.v[..start];
4180 #[stable(feature = "rust1", since = "1.0.0")]
4181 impl<T> ExactSizeIterator for Chunks<'_, T> {}
4183 #[unstable(feature = "trusted_len", issue = "37572")]
4184 unsafe impl<T> TrustedLen for Chunks<'_, T> {}
4186 #[stable(feature = "fused", since = "1.26.0")]
4187 impl<T> FusedIterator for Chunks<'_, T> {}
4190 unsafe impl<'a, T> TrustedRandomAccess for Chunks<'a, T> {
4191 unsafe fn get_unchecked(&mut self, i: usize) -> &'a [T] {
4192 let start = i * self.chunk_size;
4193 let end = match start.checked_add(self.chunk_size) {
4194 None => self.v.len(),
4195 Some(end) => cmp::min(end, self.v.len()),
4197 from_raw_parts(self.v.as_ptr().add(start), end - start)
4199 fn may_have_side_effect() -> bool { false }
4202 /// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
4203 /// elements at a time), starting at the beginning of the slice.
4205 /// When the slice len is not evenly divided by the chunk size, the last slice
4206 /// of the iteration will be the remainder.
4208 /// This struct is created by the [`chunks_mut`] method on [slices].
4210 /// [`chunks_mut`]: ../../std/primitive.slice.html#method.chunks_mut
4211 /// [slices]: ../../std/primitive.slice.html
4213 #[stable(feature = "rust1", since = "1.0.0")]
4214 pub struct ChunksMut<'a, T:'a> {
4219 #[stable(feature = "rust1", since = "1.0.0")]
4220 impl<'a, T> Iterator for ChunksMut<'a, T> {
4221 type Item = &'a mut [T];
4224 fn next(&mut self) -> Option<&'a mut [T]> {
4225 if self.v.is_empty() {
4228 let sz = cmp::min(self.v.len(), self.chunk_size);
4229 let tmp = mem::replace(&mut self.v, &mut []);
4230 let (head, tail) = tmp.split_at_mut(sz);
4237 fn size_hint(&self) -> (usize, Option<usize>) {
4238 if self.v.is_empty() {
4241 let n = self.v.len() / self.chunk_size;
4242 let rem = self.v.len() % self.chunk_size;
4243 let n = if rem > 0 { n + 1 } else { n };
4249 fn count(self) -> usize {
4254 fn nth(&mut self, n: usize) -> Option<&'a mut [T]> {
4255 let (start, overflow) = n.overflowing_mul(self.chunk_size);
4256 if start >= self.v.len() || overflow {
4260 let end = match start.checked_add(self.chunk_size) {
4261 Some(sum) => cmp::min(self.v.len(), sum),
4262 None => self.v.len(),
4264 let tmp = mem::replace(&mut self.v, &mut []);
4265 let (head, tail) = tmp.split_at_mut(end);
4266 let (_, nth) = head.split_at_mut(start);
4273 fn last(self) -> Option<Self::Item> {
4274 if self.v.is_empty() {
4277 let start = (self.v.len() - 1) / self.chunk_size * self.chunk_size;
4278 Some(&mut self.v[start..])
4283 #[stable(feature = "rust1", since = "1.0.0")]
4284 impl<'a, T> DoubleEndedIterator for ChunksMut<'a, T> {
4286 fn next_back(&mut self) -> Option<&'a mut [T]> {
4287 if self.v.is_empty() {
4290 let remainder = self.v.len() % self.chunk_size;
4291 let sz = if remainder != 0 { remainder } else { self.chunk_size };
4292 let tmp = mem::replace(&mut self.v, &mut []);
4293 let tmp_len = tmp.len();
4294 let (head, tail) = tmp.split_at_mut(tmp_len - sz);
4301 #[stable(feature = "rust1", since = "1.0.0")]
4302 impl<T> ExactSizeIterator for ChunksMut<'_, T> {}
4304 #[unstable(feature = "trusted_len", issue = "37572")]
4305 unsafe impl<T> TrustedLen for ChunksMut<'_, T> {}
4307 #[stable(feature = "fused", since = "1.26.0")]
4308 impl<T> FusedIterator for ChunksMut<'_, T> {}
4311 unsafe impl<'a, T> TrustedRandomAccess for ChunksMut<'a, T> {
4312 unsafe fn get_unchecked(&mut self, i: usize) -> &'a mut [T] {
4313 let start = i * self.chunk_size;
4314 let end = match start.checked_add(self.chunk_size) {
4315 None => self.v.len(),
4316 Some(end) => cmp::min(end, self.v.len()),
4318 from_raw_parts_mut(self.v.as_mut_ptr().add(start), end - start)
4320 fn may_have_side_effect() -> bool { false }
4323 /// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
4324 /// time), starting at the beginning of the slice.
4326 /// When the slice len is not evenly divided by the chunk size, the last
4327 /// up to `chunk_size-1` elements will be omitted but can be retrieved from
4328 /// the [`remainder`] function from the iterator.
4330 /// This struct is created by the [`chunks_exact`] method on [slices].
4332 /// [`chunks_exact`]: ../../std/primitive.slice.html#method.chunks_exact
4333 /// [`remainder`]: ../../std/slice/struct.ChunksExact.html#method.remainder
4334 /// [slices]: ../../std/primitive.slice.html
4336 #[stable(feature = "chunks_exact", since = "1.31.0")]
4337 pub struct ChunksExact<'a, T:'a> {
4343 impl<'a, T> ChunksExact<'a, T> {
4344 /// Returns the remainder of the original slice that is not going to be
4345 /// returned by the iterator. The returned slice has at most `chunk_size-1`
4347 #[stable(feature = "chunks_exact", since = "1.31.0")]
4348 pub fn remainder(&self) -> &'a [T] {
4353 // FIXME(#26925) Remove in favor of `#[derive(Clone)]`
4354 #[stable(feature = "chunks_exact", since = "1.31.0")]
4355 impl<T> Clone for ChunksExact<'_, T> {
4356 fn clone(&self) -> Self {
4360 chunk_size: self.chunk_size,
4365 #[stable(feature = "chunks_exact", since = "1.31.0")]
4366 impl<'a, T> Iterator for ChunksExact<'a, T> {
4367 type Item = &'a [T];
4370 fn next(&mut self) -> Option<&'a [T]> {
4371 if self.v.len() < self.chunk_size {
4374 let (fst, snd) = self.v.split_at(self.chunk_size);
4381 fn size_hint(&self) -> (usize, Option<usize>) {
4382 let n = self.v.len() / self.chunk_size;
4387 fn count(self) -> usize {
4392 fn nth(&mut self, n: usize) -> Option<Self::Item> {
4393 let (start, overflow) = n.overflowing_mul(self.chunk_size);
4394 if start >= self.v.len() || overflow {
4398 let (_, snd) = self.v.split_at(start);
4405 fn last(mut self) -> Option<Self::Item> {
4410 #[stable(feature = "chunks_exact", since = "1.31.0")]
4411 impl<'a, T> DoubleEndedIterator for ChunksExact<'a, T> {
4413 fn next_back(&mut self) -> Option<&'a [T]> {
4414 if self.v.len() < self.chunk_size {
4417 let (fst, snd) = self.v.split_at(self.v.len() - self.chunk_size);
4424 #[stable(feature = "chunks_exact", since = "1.31.0")]
4425 impl<T> ExactSizeIterator for ChunksExact<'_, T> {
4426 fn is_empty(&self) -> bool {
4431 #[unstable(feature = "trusted_len", issue = "37572")]
4432 unsafe impl<T> TrustedLen for ChunksExact<'_, T> {}
4434 #[stable(feature = "chunks_exact", since = "1.31.0")]
4435 impl<T> FusedIterator for ChunksExact<'_, T> {}
4438 #[stable(feature = "chunks_exact", since = "1.31.0")]
4439 unsafe impl<'a, T> TrustedRandomAccess for ChunksExact<'a, T> {
4440 unsafe fn get_unchecked(&mut self, i: usize) -> &'a [T] {
4441 let start = i * self.chunk_size;
4442 from_raw_parts(self.v.as_ptr().add(start), self.chunk_size)
4444 fn may_have_side_effect() -> bool { false }
4447 /// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
4448 /// elements at a time), starting at the beginning of the slice.
4450 /// When the slice len is not evenly divided by the chunk size, the last up to
4451 /// `chunk_size-1` elements will be omitted but can be retrieved from the
4452 /// [`into_remainder`] function from the iterator.
4454 /// This struct is created by the [`chunks_exact_mut`] method on [slices].
4456 /// [`chunks_exact_mut`]: ../../std/primitive.slice.html#method.chunks_exact_mut
4457 /// [`into_remainder`]: ../../std/slice/struct.ChunksExactMut.html#method.into_remainder
4458 /// [slices]: ../../std/primitive.slice.html
4460 #[stable(feature = "chunks_exact", since = "1.31.0")]
4461 pub struct ChunksExactMut<'a, T:'a> {
4467 impl<'a, T> ChunksExactMut<'a, T> {
4468 /// Returns the remainder of the original slice that is not going to be
4469 /// returned by the iterator. The returned slice has at most `chunk_size-1`
4471 #[stable(feature = "chunks_exact", since = "1.31.0")]
4472 pub fn into_remainder(self) -> &'a mut [T] {
4477 #[stable(feature = "chunks_exact", since = "1.31.0")]
4478 impl<'a, T> Iterator for ChunksExactMut<'a, T> {
4479 type Item = &'a mut [T];
4482 fn next(&mut self) -> Option<&'a mut [T]> {
4483 if self.v.len() < self.chunk_size {
4486 let tmp = mem::replace(&mut self.v, &mut []);
4487 let (head, tail) = tmp.split_at_mut(self.chunk_size);
4494 fn size_hint(&self) -> (usize, Option<usize>) {
4495 let n = self.v.len() / self.chunk_size;
4500 fn count(self) -> usize {
4505 fn nth(&mut self, n: usize) -> Option<&'a mut [T]> {
4506 let (start, overflow) = n.overflowing_mul(self.chunk_size);
4507 if start >= self.v.len() || overflow {
4511 let tmp = mem::replace(&mut self.v, &mut []);
4512 let (_, snd) = tmp.split_at_mut(start);
4519 fn last(mut self) -> Option<Self::Item> {
4524 #[stable(feature = "chunks_exact", since = "1.31.0")]
4525 impl<'a, T> DoubleEndedIterator for ChunksExactMut<'a, T> {
4527 fn next_back(&mut self) -> Option<&'a mut [T]> {
4528 if self.v.len() < self.chunk_size {
4531 let tmp = mem::replace(&mut self.v, &mut []);
4532 let tmp_len = tmp.len();
4533 let (head, tail) = tmp.split_at_mut(tmp_len - self.chunk_size);
4540 #[stable(feature = "chunks_exact", since = "1.31.0")]
4541 impl<T> ExactSizeIterator for ChunksExactMut<'_, T> {
4542 fn is_empty(&self) -> bool {
4547 #[unstable(feature = "trusted_len", issue = "37572")]
4548 unsafe impl<T> TrustedLen for ChunksExactMut<'_, T> {}
4550 #[stable(feature = "chunks_exact", since = "1.31.0")]
4551 impl<T> FusedIterator for ChunksExactMut<'_, T> {}
4554 #[stable(feature = "chunks_exact", since = "1.31.0")]
4555 unsafe impl<'a, T> TrustedRandomAccess for ChunksExactMut<'a, T> {
4556 unsafe fn get_unchecked(&mut self, i: usize) -> &'a mut [T] {
4557 let start = i * self.chunk_size;
4558 from_raw_parts_mut(self.v.as_mut_ptr().add(start), self.chunk_size)
4560 fn may_have_side_effect() -> bool { false }
4563 /// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
4564 /// time), starting at the end of the slice.
4566 /// When the slice len is not evenly divided by the chunk size, the last slice
4567 /// of the iteration will be the remainder.
4569 /// This struct is created by the [`rchunks`] method on [slices].
4571 /// [`rchunks`]: ../../std/primitive.slice.html#method.rchunks
4572 /// [slices]: ../../std/primitive.slice.html
4574 #[stable(feature = "rchunks", since = "1.31.0")]
4575 pub struct RChunks<'a, T:'a> {
4580 // FIXME(#26925) Remove in favor of `#[derive(Clone)]`
4581 #[stable(feature = "rchunks", since = "1.31.0")]
4582 impl<T> Clone for RChunks<'_, T> {
4583 fn clone(&self) -> Self {
4586 chunk_size: self.chunk_size,
4591 #[stable(feature = "rchunks", since = "1.31.0")]
4592 impl<'a, T> Iterator for RChunks<'a, T> {
4593 type Item = &'a [T];
4596 fn next(&mut self) -> Option<&'a [T]> {
4597 if self.v.is_empty() {
4600 let chunksz = cmp::min(self.v.len(), self.chunk_size);
4601 let (fst, snd) = self.v.split_at(self.v.len() - chunksz);
4608 fn size_hint(&self) -> (usize, Option<usize>) {
4609 if self.v.is_empty() {
4612 let n = self.v.len() / self.chunk_size;
4613 let rem = self.v.len() % self.chunk_size;
4614 let n = if rem > 0 { n+1 } else { n };
4620 fn count(self) -> usize {
4625 fn nth(&mut self, n: usize) -> Option<Self::Item> {
4626 let (end, overflow) = n.overflowing_mul(self.chunk_size);
4627 if end >= self.v.len() || overflow {
4631 // Can't underflow because of the check above
4632 let end = self.v.len() - end;
4633 let start = match end.checked_sub(self.chunk_size) {
4637 let nth = &self.v[start..end];
4638 self.v = &self.v[0..start];
4644 fn last(self) -> Option<Self::Item> {
4645 if self.v.is_empty() {
4648 let rem = self.v.len() % self.chunk_size;
4649 let end = if rem == 0 { self.chunk_size } else { rem };
4650 Some(&self.v[0..end])
4655 #[stable(feature = "rchunks", since = "1.31.0")]
4656 impl<'a, T> DoubleEndedIterator for RChunks<'a, T> {
4658 fn next_back(&mut self) -> Option<&'a [T]> {
4659 if self.v.is_empty() {
4662 let remainder = self.v.len() % self.chunk_size;
4663 let chunksz = if remainder != 0 { remainder } else { self.chunk_size };
4664 let (fst, snd) = self.v.split_at(chunksz);
4671 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
4672 let len = self.len();
4677 // can't underflow because `n < len`
4678 let offset_from_end = (len - 1 - n) * self.chunk_size;
4679 let end = self.v.len() - offset_from_end;
4680 let start = end.saturating_sub(self.chunk_size);
4681 let nth_back = &self.v[start..end];
4682 self.v = &self.v[end..];
4688 #[stable(feature = "rchunks", since = "1.31.0")]
4689 impl<T> ExactSizeIterator for RChunks<'_, T> {}
4691 #[unstable(feature = "trusted_len", issue = "37572")]
4692 unsafe impl<T> TrustedLen for RChunks<'_, T> {}
4694 #[stable(feature = "rchunks", since = "1.31.0")]
4695 impl<T> FusedIterator for RChunks<'_, T> {}
4698 #[stable(feature = "rchunks", since = "1.31.0")]
4699 unsafe impl<'a, T> TrustedRandomAccess for RChunks<'a, T> {
4700 unsafe fn get_unchecked(&mut self, i: usize) -> &'a [T] {
4701 let end = self.v.len() - i * self.chunk_size;
4702 let start = match end.checked_sub(self.chunk_size) {
4704 Some(start) => start,
4706 from_raw_parts(self.v.as_ptr().add(start), end - start)
4708 fn may_have_side_effect() -> bool { false }
4711 /// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
4712 /// elements at a time), starting at the end of the slice.
4714 /// When the slice len is not evenly divided by the chunk size, the last slice
4715 /// of the iteration will be the remainder.
4717 /// This struct is created by the [`rchunks_mut`] method on [slices].
4719 /// [`rchunks_mut`]: ../../std/primitive.slice.html#method.rchunks_mut
4720 /// [slices]: ../../std/primitive.slice.html
4722 #[stable(feature = "rchunks", since = "1.31.0")]
4723 pub struct RChunksMut<'a, T:'a> {
4728 #[stable(feature = "rchunks", since = "1.31.0")]
4729 impl<'a, T> Iterator for RChunksMut<'a, T> {
4730 type Item = &'a mut [T];
4733 fn next(&mut self) -> Option<&'a mut [T]> {
4734 if self.v.is_empty() {
4737 let sz = cmp::min(self.v.len(), self.chunk_size);
4738 let tmp = mem::replace(&mut self.v, &mut []);
4739 let tmp_len = tmp.len();
4740 let (head, tail) = tmp.split_at_mut(tmp_len - sz);
4747 fn size_hint(&self) -> (usize, Option<usize>) {
4748 if self.v.is_empty() {
4751 let n = self.v.len() / self.chunk_size;
4752 let rem = self.v.len() % self.chunk_size;
4753 let n = if rem > 0 { n + 1 } else { n };
4759 fn count(self) -> usize {
4764 fn nth(&mut self, n: usize) -> Option<&'a mut [T]> {
4765 let (end, overflow) = n.overflowing_mul(self.chunk_size);
4766 if end >= self.v.len() || overflow {
4770 // Can't underflow because of the check above
4771 let end = self.v.len() - end;
4772 let start = match end.checked_sub(self.chunk_size) {
4776 let tmp = mem::replace(&mut self.v, &mut []);
4777 let (head, tail) = tmp.split_at_mut(start);
4778 let (nth, _) = tail.split_at_mut(end - start);
4785 fn last(self) -> Option<Self::Item> {
4786 if self.v.is_empty() {
4789 let rem = self.v.len() % self.chunk_size;
4790 let end = if rem == 0 { self.chunk_size } else { rem };
4791 Some(&mut self.v[0..end])
4796 #[stable(feature = "rchunks", since = "1.31.0")]
4797 impl<'a, T> DoubleEndedIterator for RChunksMut<'a, T> {
4799 fn next_back(&mut self) -> Option<&'a mut [T]> {
4800 if self.v.is_empty() {
4803 let remainder = self.v.len() % self.chunk_size;
4804 let sz = if remainder != 0 { remainder } else { self.chunk_size };
4805 let tmp = mem::replace(&mut self.v, &mut []);
4806 let (head, tail) = tmp.split_at_mut(sz);
4813 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
4814 let len = self.len();
4819 // can't underflow because `n < len`
4820 let offset_from_end = (len - 1 - n) * self.chunk_size;
4821 let end = self.v.len() - offset_from_end;
4822 let start = end.saturating_sub(self.chunk_size);
4823 let (tmp, tail) = mem::replace(&mut self.v, &mut []).split_at_mut(end);
4824 let (_, nth_back) = tmp.split_at_mut(start);
4831 #[stable(feature = "rchunks", since = "1.31.0")]
4832 impl<T> ExactSizeIterator for RChunksMut<'_, T> {}
4834 #[unstable(feature = "trusted_len", issue = "37572")]
4835 unsafe impl<T> TrustedLen for RChunksMut<'_, T> {}
4837 #[stable(feature = "rchunks", since = "1.31.0")]
4838 impl<T> FusedIterator for RChunksMut<'_, T> {}
4841 #[stable(feature = "rchunks", since = "1.31.0")]
4842 unsafe impl<'a, T> TrustedRandomAccess for RChunksMut<'a, T> {
4843 unsafe fn get_unchecked(&mut self, i: usize) -> &'a mut [T] {
4844 let end = self.v.len() - i * self.chunk_size;
4845 let start = match end.checked_sub(self.chunk_size) {
4847 Some(start) => start,
4849 from_raw_parts_mut(self.v.as_mut_ptr().add(start), end - start)
4851 fn may_have_side_effect() -> bool { false }
4854 /// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
4855 /// time), starting at the end of the slice.
4857 /// When the slice len is not evenly divided by the chunk size, the last
4858 /// up to `chunk_size-1` elements will be omitted but can be retrieved from
4859 /// the [`remainder`] function from the iterator.
4861 /// This struct is created by the [`rchunks_exact`] method on [slices].
4863 /// [`rchunks_exact`]: ../../std/primitive.slice.html#method.rchunks_exact
4864 /// [`remainder`]: ../../std/slice/struct.ChunksExact.html#method.remainder
4865 /// [slices]: ../../std/primitive.slice.html
4867 #[stable(feature = "rchunks", since = "1.31.0")]
4868 pub struct RChunksExact<'a, T:'a> {
4874 impl<'a, T> RChunksExact<'a, T> {
4875 /// Returns the remainder of the original slice that is not going to be
4876 /// returned by the iterator. The returned slice has at most `chunk_size-1`
4878 #[stable(feature = "rchunks", since = "1.31.0")]
4879 pub fn remainder(&self) -> &'a [T] {
4884 // FIXME(#26925) Remove in favor of `#[derive(Clone)]`
4885 #[stable(feature = "rchunks", since = "1.31.0")]
4886 impl<'a, T> Clone for RChunksExact<'a, T> {
4887 fn clone(&self) -> RChunksExact<'a, T> {
4891 chunk_size: self.chunk_size,
4896 #[stable(feature = "rchunks", since = "1.31.0")]
4897 impl<'a, T> Iterator for RChunksExact<'a, T> {
4898 type Item = &'a [T];
4901 fn next(&mut self) -> Option<&'a [T]> {
4902 if self.v.len() < self.chunk_size {
4905 let (fst, snd) = self.v.split_at(self.v.len() - self.chunk_size);
4912 fn size_hint(&self) -> (usize, Option<usize>) {
4913 let n = self.v.len() / self.chunk_size;
4918 fn count(self) -> usize {
4923 fn nth(&mut self, n: usize) -> Option<Self::Item> {
4924 let (end, overflow) = n.overflowing_mul(self.chunk_size);
4925 if end >= self.v.len() || overflow {
4929 let (fst, _) = self.v.split_at(self.v.len() - end);
4936 fn last(mut self) -> Option<Self::Item> {
4941 #[stable(feature = "rchunks", since = "1.31.0")]
4942 impl<'a, T> DoubleEndedIterator for RChunksExact<'a, T> {
4944 fn next_back(&mut self) -> Option<&'a [T]> {
4945 if self.v.len() < self.chunk_size {
4948 let (fst, snd) = self.v.split_at(self.chunk_size);
4955 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
4956 let len = self.len();
4961 // now that we know that `n` corresponds to a chunk,
4962 // none of these operations can underflow/overflow
4963 let offset = (len - n) * self.chunk_size;
4964 let start = self.v.len() - offset;
4965 let end = start + self.chunk_size;
4966 let nth_back = &self.v[start..end];
4967 self.v = &self.v[end..];
4973 #[stable(feature = "rchunks", since = "1.31.0")]
4974 impl<'a, T> ExactSizeIterator for RChunksExact<'a, T> {
4975 fn is_empty(&self) -> bool {
4980 #[unstable(feature = "trusted_len", issue = "37572")]
4981 unsafe impl<T> TrustedLen for RChunksExact<'_, T> {}
4983 #[stable(feature = "rchunks", since = "1.31.0")]
4984 impl<T> FusedIterator for RChunksExact<'_, T> {}
4987 #[stable(feature = "rchunks", since = "1.31.0")]
4988 unsafe impl<'a, T> TrustedRandomAccess for RChunksExact<'a, T> {
4989 unsafe fn get_unchecked(&mut self, i: usize) -> &'a [T] {
4990 let end = self.v.len() - i * self.chunk_size;
4991 let start = end - self.chunk_size;
4992 from_raw_parts(self.v.as_ptr().add(start), self.chunk_size)
4994 fn may_have_side_effect() -> bool { false }
4997 /// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
4998 /// elements at a time), starting at the end of the slice.
5000 /// When the slice len is not evenly divided by the chunk size, the last up to
5001 /// `chunk_size-1` elements will be omitted but can be retrieved from the
5002 /// [`into_remainder`] function from the iterator.
5004 /// This struct is created by the [`rchunks_exact_mut`] method on [slices].
5006 /// [`rchunks_exact_mut`]: ../../std/primitive.slice.html#method.rchunks_exact_mut
5007 /// [`into_remainder`]: ../../std/slice/struct.ChunksExactMut.html#method.into_remainder
5008 /// [slices]: ../../std/primitive.slice.html
5010 #[stable(feature = "rchunks", since = "1.31.0")]
5011 pub struct RChunksExactMut<'a, T:'a> {
5017 impl<'a, T> RChunksExactMut<'a, T> {
5018 /// Returns the remainder of the original slice that is not going to be
5019 /// returned by the iterator. The returned slice has at most `chunk_size-1`
5021 #[stable(feature = "rchunks", since = "1.31.0")]
5022 pub fn into_remainder(self) -> &'a mut [T] {
5027 #[stable(feature = "rchunks", since = "1.31.0")]
5028 impl<'a, T> Iterator for RChunksExactMut<'a, T> {
5029 type Item = &'a mut [T];
5032 fn next(&mut self) -> Option<&'a mut [T]> {
5033 if self.v.len() < self.chunk_size {
5036 let tmp = mem::replace(&mut self.v, &mut []);
5037 let tmp_len = tmp.len();
5038 let (head, tail) = tmp.split_at_mut(tmp_len - self.chunk_size);
5045 fn size_hint(&self) -> (usize, Option<usize>) {
5046 let n = self.v.len() / self.chunk_size;
5051 fn count(self) -> usize {
5056 fn nth(&mut self, n: usize) -> Option<&'a mut [T]> {
5057 let (end, overflow) = n.overflowing_mul(self.chunk_size);
5058 if end >= self.v.len() || overflow {
5062 let tmp = mem::replace(&mut self.v, &mut []);
5063 let tmp_len = tmp.len();
5064 let (fst, _) = tmp.split_at_mut(tmp_len - end);
5071 fn last(mut self) -> Option<Self::Item> {
5076 #[stable(feature = "rchunks", since = "1.31.0")]
5077 impl<'a, T> DoubleEndedIterator for RChunksExactMut<'a, T> {
5079 fn next_back(&mut self) -> Option<&'a mut [T]> {
5080 if self.v.len() < self.chunk_size {
5083 let tmp = mem::replace(&mut self.v, &mut []);
5084 let (head, tail) = tmp.split_at_mut(self.chunk_size);
5091 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
5092 let len = self.len();
5097 // now that we know that `n` corresponds to a chunk,
5098 // none of these operations can underflow/overflow
5099 let offset = (len - n) * self.chunk_size;
5100 let start = self.v.len() - offset;
5101 let end = start + self.chunk_size;
5102 let (tmp, tail) = mem::replace(&mut self.v, &mut []).split_at_mut(end);
5103 let (_, nth_back) = tmp.split_at_mut(start);
5110 #[stable(feature = "rchunks", since = "1.31.0")]
5111 impl<T> ExactSizeIterator for RChunksExactMut<'_, T> {
5112 fn is_empty(&self) -> bool {
5117 #[unstable(feature = "trusted_len", issue = "37572")]
5118 unsafe impl<T> TrustedLen for RChunksExactMut<'_, T> {}
5120 #[stable(feature = "rchunks", since = "1.31.0")]
5121 impl<T> FusedIterator for RChunksExactMut<'_, T> {}
5124 #[stable(feature = "rchunks", since = "1.31.0")]
5125 unsafe impl<'a, T> TrustedRandomAccess for RChunksExactMut<'a, T> {
5126 unsafe fn get_unchecked(&mut self, i: usize) -> &'a mut [T] {
5127 let end = self.v.len() - i * self.chunk_size;
5128 let start = end - self.chunk_size;
5129 from_raw_parts_mut(self.v.as_mut_ptr().add(start), self.chunk_size)
5131 fn may_have_side_effect() -> bool { false }
5138 /// Forms a slice from a pointer and a length.
5140 /// The `len` argument is the number of **elements**, not the number of bytes.
5144 /// This function is unsafe as there is no guarantee that the given pointer is
5145 /// valid for `len` elements, nor whether the lifetime inferred is a suitable
5146 /// lifetime for the returned slice.
5148 /// `data` must be non-null and aligned, even for zero-length slices. One
5149 /// reason for this is that enum layout optimizations may rely on references
5150 /// (including slices of any length) being aligned and non-null to distinguish
5151 /// them from other data. You can obtain a pointer that is usable as `data`
5152 /// for zero-length slices using [`NonNull::dangling()`].
5154 /// The total size of the slice must be no larger than `isize::MAX` **bytes**
5155 /// in memory. See the safety documentation of [`pointer::offset`].
5159 /// The lifetime for the returned slice is inferred from its usage. To
5160 /// prevent accidental misuse, it's suggested to tie the lifetime to whichever
5161 /// source lifetime is safe in the context, such as by providing a helper
5162 /// function taking the lifetime of a host value for the slice, or by explicit
5170 /// // manifest a slice for a single element
5172 /// let ptr = &x as *const _;
5173 /// let slice = unsafe { slice::from_raw_parts(ptr, 1) };
5174 /// assert_eq!(slice[0], 42);
5177 /// [`NonNull::dangling()`]: ../../std/ptr/struct.NonNull.html#method.dangling
5178 /// [`pointer::offset`]: ../../std/primitive.pointer.html#method.offset
5180 #[stable(feature = "rust1", since = "1.0.0")]
5181 pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
5182 debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
5183 debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
5184 "attempt to create slice covering half the address space");
5185 Repr { raw: FatPtr { data, len } }.rust
5188 /// Performs the same functionality as [`from_raw_parts`], except that a
5189 /// mutable slice is returned.
5191 /// This function is unsafe for the same reasons as [`from_raw_parts`], as well
5192 /// as not being able to provide a non-aliasing guarantee of the returned
5193 /// mutable slice. `data` must be non-null and aligned even for zero-length
5194 /// slices as with [`from_raw_parts`]. The total size of the slice must be no
5195 /// larger than `isize::MAX` **bytes** in memory.
5197 /// See the documentation of [`from_raw_parts`] for more details.
5199 /// [`from_raw_parts`]: ../../std/slice/fn.from_raw_parts.html
5201 #[stable(feature = "rust1", since = "1.0.0")]
5202 pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {
5203 debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
5204 debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
5205 "attempt to create slice covering half the address space");
5206 Repr { raw: FatPtr { data, len } }.rust_mut
5209 /// Converts a reference to T into a slice of length 1 (without copying).
5210 #[stable(feature = "from_ref", since = "1.28.0")]
5211 pub fn from_ref<T>(s: &T) -> &[T] {
5213 from_raw_parts(s, 1)
5217 /// Converts a reference to T into a slice of length 1 (without copying).
5218 #[stable(feature = "from_ref", since = "1.28.0")]
5219 pub fn from_mut<T>(s: &mut T) -> &mut [T] {
5221 from_raw_parts_mut(s, 1)
5225 // This function is public only because there is no other way to unit test heapsort.
5226 #[unstable(feature = "sort_internals", reason = "internal to sort module", issue = "0")]
5228 pub fn heapsort<T, F>(v: &mut [T], mut is_less: F)
5229 where F: FnMut(&T, &T) -> bool
5231 sort::heapsort(v, &mut is_less);
5235 // Comparison traits
5239 /// Calls implementation provided memcmp.
5241 /// Interprets the data as u8.
5243 /// Returns 0 for equal, < 0 for less than and > 0 for greater
5245 // FIXME(#32610): Return type should be c_int
5246 fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32;
5249 #[stable(feature = "rust1", since = "1.0.0")]
5250 impl<A, B> PartialEq<[B]> for [A] where A: PartialEq<B> {
5251 fn eq(&self, other: &[B]) -> bool {
5252 SlicePartialEq::equal(self, other)
5255 fn ne(&self, other: &[B]) -> bool {
5256 SlicePartialEq::not_equal(self, other)
5260 #[stable(feature = "rust1", since = "1.0.0")]
5261 impl<T: Eq> Eq for [T] {}
5263 /// Implements comparison of vectors lexicographically.
5264 #[stable(feature = "rust1", since = "1.0.0")]
5265 impl<T: Ord> Ord for [T] {
5266 fn cmp(&self, other: &[T]) -> Ordering {
5267 SliceOrd::compare(self, other)
5271 /// Implements comparison of vectors lexicographically.
5272 #[stable(feature = "rust1", since = "1.0.0")]
5273 impl<T: PartialOrd> PartialOrd for [T] {
5274 fn partial_cmp(&self, other: &[T]) -> Option<Ordering> {
5275 SlicePartialOrd::partial_compare(self, other)
5280 // intermediate trait for specialization of slice's PartialEq
5281 trait SlicePartialEq<B> {
5282 fn equal(&self, other: &[B]) -> bool;
5284 fn not_equal(&self, other: &[B]) -> bool { !self.equal(other) }
5287 // Generic slice equality
5288 impl<A, B> SlicePartialEq<B> for [A]
5289 where A: PartialEq<B>
5291 default fn equal(&self, other: &[B]) -> bool {
5292 if self.len() != other.len() {
5296 for i in 0..self.len() {
5297 if !self[i].eq(&other[i]) {
5306 // Use memcmp for bytewise equality when the types allow
5307 impl<A> SlicePartialEq<A> for [A]
5308 where A: PartialEq<A> + BytewiseEquality
5310 fn equal(&self, other: &[A]) -> bool {
5311 if self.len() != other.len() {
5314 if self.as_ptr() == other.as_ptr() {
5318 let size = mem::size_of_val(self);
5319 memcmp(self.as_ptr() as *const u8,
5320 other.as_ptr() as *const u8, size) == 0
5326 // intermediate trait for specialization of slice's PartialOrd
5327 trait SlicePartialOrd<B> {
5328 fn partial_compare(&self, other: &[B]) -> Option<Ordering>;
5331 impl<A> SlicePartialOrd<A> for [A]
5334 default fn partial_compare(&self, other: &[A]) -> Option<Ordering> {
5335 let l = cmp::min(self.len(), other.len());
5337 // Slice to the loop iteration range to enable bound check
5338 // elimination in the compiler
5339 let lhs = &self[..l];
5340 let rhs = &other[..l];
5343 match lhs[i].partial_cmp(&rhs[i]) {
5344 Some(Ordering::Equal) => (),
5345 non_eq => return non_eq,
5349 self.len().partial_cmp(&other.len())
5353 impl<A> SlicePartialOrd<A> for [A]
5356 default fn partial_compare(&self, other: &[A]) -> Option<Ordering> {
5357 Some(SliceOrd::compare(self, other))
5362 // intermediate trait for specialization of slice's Ord
5364 fn compare(&self, other: &[B]) -> Ordering;
5367 impl<A> SliceOrd<A> for [A]
5370 default fn compare(&self, other: &[A]) -> Ordering {
5371 let l = cmp::min(self.len(), other.len());
5373 // Slice to the loop iteration range to enable bound check
5374 // elimination in the compiler
5375 let lhs = &self[..l];
5376 let rhs = &other[..l];
5379 match lhs[i].cmp(&rhs[i]) {
5380 Ordering::Equal => (),
5381 non_eq => return non_eq,
5385 self.len().cmp(&other.len())
5389 // memcmp compares a sequence of unsigned bytes lexicographically.
5390 // this matches the order we want for [u8], but no others (not even [i8]).
5391 impl SliceOrd<u8> for [u8] {
5393 fn compare(&self, other: &[u8]) -> Ordering {
5394 let order = unsafe {
5395 memcmp(self.as_ptr(), other.as_ptr(),
5396 cmp::min(self.len(), other.len()))
5399 self.len().cmp(&other.len())
5400 } else if order < 0 {
5409 /// Trait implemented for types that can be compared for equality using
5410 /// their bytewise representation
5411 trait BytewiseEquality { }
5413 macro_rules! impl_marker_for {
5414 ($traitname:ident, $($ty:ty)*) => {
5416 impl $traitname for $ty { }
5421 impl_marker_for!(BytewiseEquality,
5422 u8 i8 u16 i16 u32 i32 u64 i64 usize isize char bool);
5425 unsafe impl<'a, T> TrustedRandomAccess for Iter<'a, T> {
5426 unsafe fn get_unchecked(&mut self, i: usize) -> &'a T {
5429 fn may_have_side_effect() -> bool { false }
5433 unsafe impl<'a, T> TrustedRandomAccess for IterMut<'a, T> {
5434 unsafe fn get_unchecked(&mut self, i: usize) -> &'a mut T {
5435 &mut *self.ptr.add(i)
5437 fn may_have_side_effect() -> bool { false }
5440 trait SliceContains: Sized {
5441 fn slice_contains(&self, x: &[Self]) -> bool;
5444 impl<T> SliceContains for T where T: PartialEq {
5445 default fn slice_contains(&self, x: &[Self]) -> bool {
5446 x.iter().any(|y| *y == *self)
5450 impl SliceContains for u8 {
5451 fn slice_contains(&self, x: &[Self]) -> bool {
5452 memchr::memchr(*self, x).is_some()
5456 impl SliceContains for i8 {
5457 fn slice_contains(&self, x: &[Self]) -> bool {
5458 let byte = *self as u8;
5459 let bytes: &[u8] = unsafe { from_raw_parts(x.as_ptr() as *const u8, x.len()) };
5460 memchr::memchr(byte, bytes).is_some()