core/slice/mod.rs
1//! Slice management and manipulation.
2//!
3//! For more details see [`std::slice`].
4//!
5//! [`std::slice`]: ../../std/slice/index.html
6
7#![stable(feature = "rust1", since = "1.0.0")]
8
9#[cfg(not(feature = "ferrocene_subset"))]
10use crate::clone::TrivialClone;
11use crate::cmp::Ordering::{self, Equal, Greater, Less};
12use crate::intrinsics::{exact_div, unchecked_sub};
13use crate::marker::Destruct;
14#[cfg(not(feature = "ferrocene_subset"))]
15use crate::mem::{self, MaybeUninit, SizedTypeProperties};
16use crate::num::NonZero;
17#[cfg(not(feature = "ferrocene_subset"))]
18use crate::ops::{OneSidedRange, OneSidedRangeBound, Range, RangeBounds, RangeInclusive};
19use crate::panic::const_panic;
20#[cfg(not(feature = "ferrocene_subset"))]
21use crate::simd::{self, Simd};
22use crate::ub_checks::assert_unsafe_precondition;
23#[cfg(not(feature = "ferrocene_subset"))]
24use crate::{fmt, hint, ptr, range, slice};
25
26// Ferrocene addition: imports for certified subset
27#[cfg(feature = "ferrocene_subset")]
28#[rustfmt::skip]
29use crate::{hint, mem::SizedTypeProperties, ptr};
30
31#[unstable(
32 feature = "slice_internals",
33 issue = "none",
34 reason = "exposed from core to be reused in std; use the memchr crate"
35)]
36#[doc(hidden)]
37/// Pure Rust memchr implementation, taken from rust-memchr
38pub mod memchr;
39
40#[unstable(
41 feature = "slice_internals",
42 issue = "none",
43 reason = "exposed from core to be reused in std;"
44)]
45#[doc(hidden)]
46#[cfg(not(feature = "ferrocene_subset"))]
47pub mod sort;
48
49mod ascii;
50mod cmp;
51pub(crate) mod index;
52mod iter;
53mod raw;
54mod rotate;
55mod specialize;
56
57#[stable(feature = "inherent_ascii_escape", since = "1.60.0")]
58#[cfg(not(feature = "ferrocene_subset"))]
59pub use ascii::EscapeAscii;
60#[unstable(feature = "str_internals", issue = "none")]
61#[doc(hidden)]
62pub use ascii::is_ascii_simple;
63#[stable(feature = "slice_get_slice", since = "1.28.0")]
64pub use index::SliceIndex;
65#[unstable(feature = "slice_range", issue = "76393")]
66#[cfg(not(feature = "ferrocene_subset"))]
67pub use index::{range, try_range};
68#[cfg(not(feature = "ferrocene_subset"))]
69#[stable(feature = "array_windows", since = "1.94.0")]
70pub use iter::ArrayWindows;
71#[stable(feature = "slice_group_by", since = "1.77.0")]
72#[cfg(not(feature = "ferrocene_subset"))]
73pub use iter::{ChunkBy, ChunkByMut};
74#[stable(feature = "rust1", since = "1.0.0")]
75#[cfg(not(feature = "ferrocene_subset"))]
76pub use iter::{Chunks, ChunksMut, Windows};
77#[stable(feature = "chunks_exact", since = "1.31.0")]
78pub use iter::{ChunksExact, ChunksExactMut};
79#[stable(feature = "rust1", since = "1.0.0")]
80pub use iter::{Iter, IterMut};
81#[stable(feature = "rchunks", since = "1.31.0")]
82#[cfg(not(feature = "ferrocene_subset"))]
83pub use iter::{RChunks, RChunksExact, RChunksExactMut, RChunksMut};
84#[stable(feature = "slice_rsplit", since = "1.27.0")]
85#[cfg(not(feature = "ferrocene_subset"))]
86pub use iter::{RSplit, RSplitMut};
87#[stable(feature = "rust1", since = "1.0.0")]
88#[cfg(not(feature = "ferrocene_subset"))]
89pub use iter::{RSplitN, RSplitNMut, Split, SplitMut, SplitN, SplitNMut};
90#[stable(feature = "split_inclusive", since = "1.51.0")]
91#[cfg(not(feature = "ferrocene_subset"))]
92pub use iter::{SplitInclusive, SplitInclusiveMut};
93#[stable(feature = "from_ref", since = "1.28.0")]
94pub use raw::{from_mut, from_ref};
95#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
96#[cfg(not(feature = "ferrocene_subset"))]
97pub use raw::{from_mut_ptr_range, from_ptr_range};
98#[stable(feature = "rust1", since = "1.0.0")]
99pub use raw::{from_raw_parts, from_raw_parts_mut};
100
101// Ferrocene addition: imports for certified subset
102#[stable(feature = "rust1", since = "1.0.0")]
103#[cfg(feature = "ferrocene_subset")]
104#[rustfmt::skip]
105pub use iter::{Chunks, ChunksMut, Windows};
106
107/// Calculates the direction and split point of a one-sided range.
108///
109/// This is a helper function for `split_off` and `split_off_mut` that returns
110/// the direction of the split (front or back) as well as the index at
111/// which to split. Returns `None` if the split index would overflow.
112#[inline]
113#[cfg(not(feature = "ferrocene_subset"))]
114fn split_point_of(range: impl OneSidedRange<usize>) -> Option<(Direction, usize)> {
115 use OneSidedRangeBound::{End, EndInclusive, StartInclusive};
116
117 Some(match range.bound() {
118 (StartInclusive, i) => (Direction::Back, i),
119 (End, i) => (Direction::Front, i),
120 (EndInclusive, i) => (Direction::Front, i.checked_add(1)?),
121 })
122}
123
124#[cfg(not(feature = "ferrocene_subset"))]
125enum Direction {
126 Front,
127 Back,
128}
129
130impl<T> [T] {
131 /// Returns the number of elements in the slice.
132 ///
133 /// # Examples
134 ///
135 /// ```
136 /// let a = [1, 2, 3];
137 /// assert_eq!(a.len(), 3);
138 /// ```
139 #[lang = "slice_len_fn"]
140 #[stable(feature = "rust1", since = "1.0.0")]
141 #[rustc_const_stable(feature = "const_slice_len", since = "1.39.0")]
142 #[rustc_no_implicit_autorefs]
143 #[inline]
144 #[must_use]
145 #[ferrocene::annotation(
146 "this function is guaranteed to be constant-evaluated as the size of arrays is always available at compilation"
147 )]
148 pub const fn len(&self) -> usize {
149 ptr::metadata(self)
150 }
151
152 /// Returns `true` if the slice has a length of 0.
153 ///
154 /// # Examples
155 ///
156 /// ```
157 /// let a = [1, 2, 3];
158 /// assert!(!a.is_empty());
159 ///
160 /// let b: &[i32] = &[];
161 /// assert!(b.is_empty());
162 /// ```
163 #[stable(feature = "rust1", since = "1.0.0")]
164 #[rustc_const_stable(feature = "const_slice_is_empty", since = "1.39.0")]
165 #[rustc_no_implicit_autorefs]
166 #[inline]
167 #[must_use]
168 pub const fn is_empty(&self) -> bool {
169 self.len() == 0
170 }
171
172 /// Returns the first element of the slice, or `None` if it is empty.
173 ///
174 /// # Examples
175 ///
176 /// ```
177 /// let v = [10, 40, 30];
178 /// assert_eq!(Some(&10), v.first());
179 ///
180 /// let w: &[i32] = &[];
181 /// assert_eq!(None, w.first());
182 /// ```
183 #[stable(feature = "rust1", since = "1.0.0")]
184 #[rustc_const_stable(feature = "const_slice_first_last_not_mut", since = "1.56.0")]
185 #[inline]
186 #[must_use]
187 pub const fn first(&self) -> Option<&T> {
188 if let [first, ..] = self { Some(first) } else { None }
189 }
190
191 /// Returns a mutable reference to the first element of the slice, or `None` if it is empty.
192 ///
193 /// # Examples
194 ///
195 /// ```
196 /// let x = &mut [0, 1, 2];
197 ///
198 /// if let Some(first) = x.first_mut() {
199 /// *first = 5;
200 /// }
201 /// assert_eq!(x, &[5, 1, 2]);
202 ///
203 /// let y: &mut [i32] = &mut [];
204 /// assert_eq!(None, y.first_mut());
205 /// ```
206 #[stable(feature = "rust1", since = "1.0.0")]
207 #[rustc_const_stable(feature = "const_slice_first_last", since = "1.83.0")]
208 #[inline]
209 #[must_use]
210 pub const fn first_mut(&mut self) -> Option<&mut T> {
211 if let [first, ..] = self { Some(first) } else { None }
212 }
213
214 /// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
215 ///
216 /// # Examples
217 ///
218 /// ```
219 /// let x = &[0, 1, 2];
220 ///
221 /// if let Some((first, elements)) = x.split_first() {
222 /// assert_eq!(first, &0);
223 /// assert_eq!(elements, &[1, 2]);
224 /// }
225 /// ```
226 #[stable(feature = "slice_splits", since = "1.5.0")]
227 #[rustc_const_stable(feature = "const_slice_first_last_not_mut", since = "1.56.0")]
228 #[inline]
229 #[must_use]
230 pub const fn split_first(&self) -> Option<(&T, &[T])> {
231 if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
232 }
233
234 /// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
235 ///
236 /// # Examples
237 ///
238 /// ```
239 /// let x = &mut [0, 1, 2];
240 ///
241 /// if let Some((first, elements)) = x.split_first_mut() {
242 /// *first = 3;
243 /// elements[0] = 4;
244 /// elements[1] = 5;
245 /// }
246 /// assert_eq!(x, &[3, 4, 5]);
247 /// ```
248 #[stable(feature = "slice_splits", since = "1.5.0")]
249 #[rustc_const_stable(feature = "const_slice_first_last", since = "1.83.0")]
250 #[inline]
251 #[must_use]
252 pub const fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])> {
253 if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
254 }
255
256 /// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
257 ///
258 /// # Examples
259 ///
260 /// ```
261 /// let x = &[0, 1, 2];
262 ///
263 /// if let Some((last, elements)) = x.split_last() {
264 /// assert_eq!(last, &2);
265 /// assert_eq!(elements, &[0, 1]);
266 /// }
267 /// ```
268 #[stable(feature = "slice_splits", since = "1.5.0")]
269 #[rustc_const_stable(feature = "const_slice_first_last_not_mut", since = "1.56.0")]
270 #[inline]
271 #[must_use]
272 pub const fn split_last(&self) -> Option<(&T, &[T])> {
273 if let [init @ .., last] = self { Some((last, init)) } else { None }
274 }
275
276 /// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
277 ///
278 /// # Examples
279 ///
280 /// ```
281 /// let x = &mut [0, 1, 2];
282 ///
283 /// if let Some((last, elements)) = x.split_last_mut() {
284 /// *last = 3;
285 /// elements[0] = 4;
286 /// elements[1] = 5;
287 /// }
288 /// assert_eq!(x, &[4, 5, 3]);
289 /// ```
290 #[stable(feature = "slice_splits", since = "1.5.0")]
291 #[rustc_const_stable(feature = "const_slice_first_last", since = "1.83.0")]
292 #[inline]
293 #[must_use]
294 pub const fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])> {
295 if let [init @ .., last] = self { Some((last, init)) } else { None }
296 }
297
298 /// Returns the last element of the slice, or `None` if it is empty.
299 ///
300 /// # Examples
301 ///
302 /// ```
303 /// let v = [10, 40, 30];
304 /// assert_eq!(Some(&30), v.last());
305 ///
306 /// let w: &[i32] = &[];
307 /// assert_eq!(None, w.last());
308 /// ```
309 #[stable(feature = "rust1", since = "1.0.0")]
310 #[rustc_const_stable(feature = "const_slice_first_last_not_mut", since = "1.56.0")]
311 #[inline]
312 #[must_use]
313 pub const fn last(&self) -> Option<&T> {
314 if let [.., last] = self { Some(last) } else { None }
315 }
316
317 /// Returns a mutable reference to the last item in the slice, or `None` if it is empty.
318 ///
319 /// # Examples
320 ///
321 /// ```
322 /// let x = &mut [0, 1, 2];
323 ///
324 /// if let Some(last) = x.last_mut() {
325 /// *last = 10;
326 /// }
327 /// assert_eq!(x, &[0, 1, 10]);
328 ///
329 /// let y: &mut [i32] = &mut [];
330 /// assert_eq!(None, y.last_mut());
331 /// ```
332 #[stable(feature = "rust1", since = "1.0.0")]
333 #[rustc_const_stable(feature = "const_slice_first_last", since = "1.83.0")]
334 #[inline]
335 #[must_use]
336 pub const fn last_mut(&mut self) -> Option<&mut T> {
337 if let [.., last] = self { Some(last) } else { None }
338 }
339
340 /// Returns an array reference to the first `N` items in the slice.
341 ///
342 /// If the slice is not at least `N` in length, this will return `None`.
343 ///
344 /// # Examples
345 ///
346 /// ```
347 /// let u = [10, 40, 30];
348 /// assert_eq!(Some(&[10, 40]), u.first_chunk::<2>());
349 ///
350 /// let v: &[i32] = &[10];
351 /// assert_eq!(None, v.first_chunk::<2>());
352 ///
353 /// let w: &[i32] = &[];
354 /// assert_eq!(Some(&[]), w.first_chunk::<0>());
355 /// ```
356 #[inline]
357 #[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
358 #[rustc_const_stable(feature = "slice_first_last_chunk", since = "1.77.0")]
359 pub const fn first_chunk<const N: usize>(&self) -> Option<&[T; N]> {
360 if self.len() < N {
361 None
362 } else {
363 // SAFETY: We explicitly check for the correct number of elements,
364 // and do not let the reference outlive the slice.
365 Some(unsafe { &*(self.as_ptr().cast_array()) })
366 }
367 }
368
369 /// Returns a mutable array reference to the first `N` items in the slice.
370 ///
371 /// If the slice is not at least `N` in length, this will return `None`.
372 ///
373 /// # Examples
374 ///
375 /// ```
376 /// let x = &mut [0, 1, 2];
377 ///
378 /// if let Some(first) = x.first_chunk_mut::<2>() {
379 /// first[0] = 5;
380 /// first[1] = 4;
381 /// }
382 /// assert_eq!(x, &[5, 4, 2]);
383 ///
384 /// assert_eq!(None, x.first_chunk_mut::<4>());
385 /// ```
386 #[inline]
387 #[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
388 #[rustc_const_stable(feature = "const_slice_first_last_chunk", since = "1.83.0")]
389 pub const fn first_chunk_mut<const N: usize>(&mut self) -> Option<&mut [T; N]> {
390 if self.len() < N {
391 None
392 } else {
393 // SAFETY: We explicitly check for the correct number of elements,
394 // do not let the reference outlive the slice,
395 // and require exclusive access to the entire slice to mutate the chunk.
396 Some(unsafe { &mut *(self.as_mut_ptr().cast_array()) })
397 }
398 }
399
400 /// Returns an array reference to the first `N` items in the slice and the remaining slice.
401 ///
402 /// If the slice is not at least `N` in length, this will return `None`.
403 ///
404 /// # Examples
405 ///
406 /// ```
407 /// let x = &[0, 1, 2];
408 ///
409 /// if let Some((first, elements)) = x.split_first_chunk::<2>() {
410 /// assert_eq!(first, &[0, 1]);
411 /// assert_eq!(elements, &[2]);
412 /// }
413 ///
414 /// assert_eq!(None, x.split_first_chunk::<4>());
415 /// ```
416 #[inline]
417 #[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
418 #[rustc_const_stable(feature = "slice_first_last_chunk", since = "1.77.0")]
419 #[cfg(not(feature = "ferrocene_subset"))]
420 pub const fn split_first_chunk<const N: usize>(&self) -> Option<(&[T; N], &[T])> {
421 let Some((first, tail)) = self.split_at_checked(N) else { return None };
422
423 // SAFETY: We explicitly check for the correct number of elements,
424 // and do not let the references outlive the slice.
425 Some((unsafe { &*(first.as_ptr().cast_array()) }, tail))
426 }
427
428 /// Returns a mutable array reference to the first `N` items in the slice and the remaining
429 /// slice.
430 ///
431 /// If the slice is not at least `N` in length, this will return `None`.
432 ///
433 /// # Examples
434 ///
435 /// ```
436 /// let x = &mut [0, 1, 2];
437 ///
438 /// if let Some((first, elements)) = x.split_first_chunk_mut::<2>() {
439 /// first[0] = 3;
440 /// first[1] = 4;
441 /// elements[0] = 5;
442 /// }
443 /// assert_eq!(x, &[3, 4, 5]);
444 ///
445 /// assert_eq!(None, x.split_first_chunk_mut::<4>());
446 /// ```
447 #[inline]
448 #[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
449 #[rustc_const_stable(feature = "const_slice_first_last_chunk", since = "1.83.0")]
450 #[cfg(not(feature = "ferrocene_subset"))]
451 pub const fn split_first_chunk_mut<const N: usize>(
452 &mut self,
453 ) -> Option<(&mut [T; N], &mut [T])> {
454 let Some((first, tail)) = self.split_at_mut_checked(N) else { return None };
455
456 // SAFETY: We explicitly check for the correct number of elements,
457 // do not let the reference outlive the slice,
458 // and enforce exclusive mutability of the chunk by the split.
459 Some((unsafe { &mut *(first.as_mut_ptr().cast_array()) }, tail))
460 }
461
462 /// Returns an array reference to the last `N` items in the slice and the remaining slice.
463 ///
464 /// If the slice is not at least `N` in length, this will return `None`.
465 ///
466 /// # Examples
467 ///
468 /// ```
469 /// let x = &[0, 1, 2];
470 ///
471 /// if let Some((elements, last)) = x.split_last_chunk::<2>() {
472 /// assert_eq!(elements, &[0]);
473 /// assert_eq!(last, &[1, 2]);
474 /// }
475 ///
476 /// assert_eq!(None, x.split_last_chunk::<4>());
477 /// ```
478 #[inline]
479 #[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
480 #[rustc_const_stable(feature = "slice_first_last_chunk", since = "1.77.0")]
481 #[cfg(not(feature = "ferrocene_subset"))]
482 pub const fn split_last_chunk<const N: usize>(&self) -> Option<(&[T], &[T; N])> {
483 let Some(index) = self.len().checked_sub(N) else { return None };
484 let (init, last) = self.split_at(index);
485
486 // SAFETY: We explicitly check for the correct number of elements,
487 // and do not let the references outlive the slice.
488 Some((init, unsafe { &*(last.as_ptr().cast_array()) }))
489 }
490
491 /// Returns a mutable array reference to the last `N` items in the slice and the remaining
492 /// slice.
493 ///
494 /// If the slice is not at least `N` in length, this will return `None`.
495 ///
496 /// # Examples
497 ///
498 /// ```
499 /// let x = &mut [0, 1, 2];
500 ///
501 /// if let Some((elements, last)) = x.split_last_chunk_mut::<2>() {
502 /// last[0] = 3;
503 /// last[1] = 4;
504 /// elements[0] = 5;
505 /// }
506 /// assert_eq!(x, &[5, 3, 4]);
507 ///
508 /// assert_eq!(None, x.split_last_chunk_mut::<4>());
509 /// ```
510 #[inline]
511 #[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
512 #[rustc_const_stable(feature = "const_slice_first_last_chunk", since = "1.83.0")]
513 #[cfg(not(feature = "ferrocene_subset"))]
514 pub const fn split_last_chunk_mut<const N: usize>(
515 &mut self,
516 ) -> Option<(&mut [T], &mut [T; N])> {
517 let Some(index) = self.len().checked_sub(N) else { return None };
518 let (init, last) = self.split_at_mut(index);
519
520 // SAFETY: We explicitly check for the correct number of elements,
521 // do not let the reference outlive the slice,
522 // and enforce exclusive mutability of the chunk by the split.
523 Some((init, unsafe { &mut *(last.as_mut_ptr().cast_array()) }))
524 }
525
526 /// Returns an array reference to the last `N` items in the slice.
527 ///
528 /// If the slice is not at least `N` in length, this will return `None`.
529 ///
530 /// # Examples
531 ///
532 /// ```
533 /// let u = [10, 40, 30];
534 /// assert_eq!(Some(&[40, 30]), u.last_chunk::<2>());
535 ///
536 /// let v: &[i32] = &[10];
537 /// assert_eq!(None, v.last_chunk::<2>());
538 ///
539 /// let w: &[i32] = &[];
540 /// assert_eq!(Some(&[]), w.last_chunk::<0>());
541 /// ```
542 #[inline]
543 #[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
544 #[rustc_const_stable(feature = "const_slice_last_chunk", since = "1.80.0")]
545 #[cfg(not(feature = "ferrocene_subset"))]
546 pub const fn last_chunk<const N: usize>(&self) -> Option<&[T; N]> {
547 // FIXME(const-hack): Without const traits, we need this instead of `get`.
548 let Some(index) = self.len().checked_sub(N) else { return None };
549 let (_, last) = self.split_at(index);
550
551 // SAFETY: We explicitly check for the correct number of elements,
552 // and do not let the references outlive the slice.
553 Some(unsafe { &*(last.as_ptr().cast_array()) })
554 }
555
556 /// Returns a mutable array reference to the last `N` items in the slice.
557 ///
558 /// If the slice is not at least `N` in length, this will return `None`.
559 ///
560 /// # Examples
561 ///
562 /// ```
563 /// let x = &mut [0, 1, 2];
564 ///
565 /// if let Some(last) = x.last_chunk_mut::<2>() {
566 /// last[0] = 10;
567 /// last[1] = 20;
568 /// }
569 /// assert_eq!(x, &[0, 10, 20]);
570 ///
571 /// assert_eq!(None, x.last_chunk_mut::<4>());
572 /// ```
573 #[inline]
574 #[stable(feature = "slice_first_last_chunk", since = "1.77.0")]
575 #[rustc_const_stable(feature = "const_slice_first_last_chunk", since = "1.83.0")]
576 #[cfg(not(feature = "ferrocene_subset"))]
577 pub const fn last_chunk_mut<const N: usize>(&mut self) -> Option<&mut [T; N]> {
578 // FIXME(const-hack): Without const traits, we need this instead of `get`.
579 let Some(index) = self.len().checked_sub(N) else { return None };
580 let (_, last) = self.split_at_mut(index);
581
582 // SAFETY: We explicitly check for the correct number of elements,
583 // do not let the reference outlive the slice,
584 // and require exclusive access to the entire slice to mutate the chunk.
585 Some(unsafe { &mut *(last.as_mut_ptr().cast_array()) })
586 }
587
588 /// Returns a reference to an element or subslice depending on the type of
589 /// index.
590 ///
591 /// - If given a position, returns a reference to the element at that
592 /// position or `None` if out of bounds.
593 /// - If given a range, returns the subslice corresponding to that range,
594 /// or `None` if out of bounds.
595 ///
596 /// # Examples
597 ///
598 /// ```
599 /// let v = [10, 40, 30];
600 /// assert_eq!(Some(&40), v.get(1));
601 /// assert_eq!(Some(&[10, 40][..]), v.get(0..2));
602 /// assert_eq!(None, v.get(3));
603 /// assert_eq!(None, v.get(0..4));
604 /// ```
605 #[stable(feature = "rust1", since = "1.0.0")]
606 #[rustc_no_implicit_autorefs]
607 #[inline]
608 #[must_use]
609 #[rustc_const_unstable(feature = "const_index", issue = "143775")]
610 pub const fn get<I>(&self, index: I) -> Option<&I::Output>
611 where
612 I: [const] SliceIndex<Self>,
613 {
614 index.get(self)
615 }
616
617 /// Returns a mutable reference to an element or subslice depending on the
618 /// type of index (see [`get`]) or `None` if the index is out of bounds.
619 ///
620 /// [`get`]: slice::get
621 ///
622 /// # Examples
623 ///
624 /// ```
625 /// let x = &mut [0, 1, 2];
626 ///
627 /// if let Some(elem) = x.get_mut(1) {
628 /// *elem = 42;
629 /// }
630 /// assert_eq!(x, &[0, 42, 2]);
631 /// ```
632 #[stable(feature = "rust1", since = "1.0.0")]
633 #[rustc_no_implicit_autorefs]
634 #[inline]
635 #[must_use]
636 #[rustc_const_unstable(feature = "const_index", issue = "143775")]
637 pub const fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
638 where
639 I: [const] SliceIndex<Self>,
640 {
641 index.get_mut(self)
642 }
643
644 /// Returns a reference to an element or subslice, without doing bounds
645 /// checking.
646 ///
647 /// For a safe alternative see [`get`].
648 ///
649 /// # Safety
650 ///
651 /// Calling this method with an out-of-bounds index is *[undefined behavior]*
652 /// even if the resulting reference is not used.
653 ///
654 /// You can think of this like `.get(index).unwrap_unchecked()`. It's UB
655 /// to call `.get_unchecked(len)`, even if you immediately convert to a
656 /// pointer. And it's UB to call `.get_unchecked(..len + 1)`,
657 /// `.get_unchecked(..=len)`, or similar.
658 ///
659 /// [`get`]: slice::get
660 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
661 ///
662 /// # Examples
663 ///
664 /// ```
665 /// let x = &[1, 2, 4];
666 ///
667 /// unsafe {
668 /// assert_eq!(x.get_unchecked(1), &2);
669 /// }
670 /// ```
671 #[stable(feature = "rust1", since = "1.0.0")]
672 #[rustc_no_implicit_autorefs]
673 #[inline]
674 #[must_use]
675 #[track_caller]
676 #[rustc_const_unstable(feature = "const_index", issue = "143775")]
677 pub const unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
678 where
679 I: [const] SliceIndex<Self>,
680 {
681 // SAFETY: the caller must uphold most of the safety requirements for `get_unchecked`;
682 // the slice is dereferenceable because `self` is a safe reference.
683 // The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
684 unsafe { &*index.get_unchecked(self) }
685 }
686
687 /// Returns a mutable reference to an element or subslice, without doing
688 /// bounds checking.
689 ///
690 /// For a safe alternative see [`get_mut`].
691 ///
692 /// # Safety
693 ///
694 /// Calling this method with an out-of-bounds index is *[undefined behavior]*
695 /// even if the resulting reference is not used.
696 ///
697 /// You can think of this like `.get_mut(index).unwrap_unchecked()`. It's
698 /// UB to call `.get_unchecked_mut(len)`, even if you immediately convert
699 /// to a pointer. And it's UB to call `.get_unchecked_mut(..len + 1)`,
700 /// `.get_unchecked_mut(..=len)`, or similar.
701 ///
702 /// [`get_mut`]: slice::get_mut
703 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
704 ///
705 /// # Examples
706 ///
707 /// ```
708 /// let x = &mut [1, 2, 4];
709 ///
710 /// unsafe {
711 /// let elem = x.get_unchecked_mut(1);
712 /// *elem = 13;
713 /// }
714 /// assert_eq!(x, &[1, 13, 4]);
715 /// ```
716 #[stable(feature = "rust1", since = "1.0.0")]
717 #[rustc_no_implicit_autorefs]
718 #[inline]
719 #[must_use]
720 #[track_caller]
721 #[rustc_const_unstable(feature = "const_index", issue = "143775")]
722 pub const unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
723 where
724 I: [const] SliceIndex<Self>,
725 {
726 // SAFETY: the caller must uphold the safety requirements for `get_unchecked_mut`;
727 // the slice is dereferenceable because `self` is a safe reference.
728 // The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
729 unsafe { &mut *index.get_unchecked_mut(self) }
730 }
731
732 /// Returns a raw pointer to the slice's buffer.
733 ///
734 /// The caller must ensure that the slice outlives the pointer this
735 /// function returns, or else it will end up dangling.
736 ///
737 /// The caller must also ensure that the memory the pointer (non-transitively) points to
738 /// is never written to (except inside an `UnsafeCell`) using this pointer or any pointer
739 /// derived from it. If you need to mutate the contents of the slice, use [`as_mut_ptr`].
740 ///
741 /// Modifying the container referenced by this slice may cause its buffer
742 /// to be reallocated, which would also make any pointers to it invalid.
743 ///
744 /// # Examples
745 ///
746 /// ```
747 /// let x = &[1, 2, 4];
748 /// let x_ptr = x.as_ptr();
749 ///
750 /// unsafe {
751 /// for i in 0..x.len() {
752 /// assert_eq!(x.get_unchecked(i), &*x_ptr.add(i));
753 /// }
754 /// }
755 /// ```
756 ///
757 /// [`as_mut_ptr`]: slice::as_mut_ptr
758 #[stable(feature = "rust1", since = "1.0.0")]
759 #[rustc_const_stable(feature = "const_slice_as_ptr", since = "1.32.0")]
760 #[rustc_never_returns_null_ptr]
761 #[rustc_as_ptr]
762 #[inline(always)]
763 #[must_use]
764 pub const fn as_ptr(&self) -> *const T {
765 self as *const [T] as *const T
766 }
767
768 /// Returns an unsafe mutable pointer to the slice's buffer.
769 ///
770 /// The caller must ensure that the slice outlives the pointer this
771 /// function returns, or else it will end up dangling.
772 ///
773 /// Modifying the container referenced by this slice may cause its buffer
774 /// to be reallocated, which would also make any pointers to it invalid.
775 ///
776 /// # Examples
777 ///
778 /// ```
779 /// let x = &mut [1, 2, 4];
780 /// let x_ptr = x.as_mut_ptr();
781 ///
782 /// unsafe {
783 /// for i in 0..x.len() {
784 /// *x_ptr.add(i) += 2;
785 /// }
786 /// }
787 /// assert_eq!(x, &[3, 4, 6]);
788 /// ```
789 #[stable(feature = "rust1", since = "1.0.0")]
790 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
791 #[rustc_never_returns_null_ptr]
792 #[rustc_as_ptr]
793 #[inline(always)]
794 #[must_use]
795 pub const fn as_mut_ptr(&mut self) -> *mut T {
796 self as *mut [T] as *mut T
797 }
798
799 /// Returns the two raw pointers spanning the slice.
800 ///
801 /// The returned range is half-open, which means that the end pointer
802 /// points *one past* the last element of the slice. This way, an empty
803 /// slice is represented by two equal pointers, and the difference between
804 /// the two pointers represents the size of the slice.
805 ///
806 /// See [`as_ptr`] for warnings on using these pointers. The end pointer
807 /// requires extra caution, as it does not point to a valid element in the
808 /// slice.
809 ///
810 /// This function is useful for interacting with foreign interfaces which
811 /// use two pointers to refer to a range of elements in memory, as is
812 /// common in C++.
813 ///
814 /// It can also be useful to check if a pointer to an element refers to an
815 /// element of this slice:
816 ///
817 /// ```
818 /// let a = [1, 2, 3];
819 /// let x = &a[1] as *const _;
820 /// let y = &5 as *const _;
821 ///
822 /// assert!(a.as_ptr_range().contains(&x));
823 /// assert!(!a.as_ptr_range().contains(&y));
824 /// ```
825 ///
826 /// [`as_ptr`]: slice::as_ptr
827 #[stable(feature = "slice_ptr_range", since = "1.48.0")]
828 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
829 #[inline]
830 #[must_use]
831 #[cfg(not(feature = "ferrocene_subset"))]
832 pub const fn as_ptr_range(&self) -> Range<*const T> {
833 let start = self.as_ptr();
834 // SAFETY: The `add` here is safe, because:
835 //
836 // - Both pointers are part of the same object, as pointing directly
837 // past the object also counts.
838 //
839 // - The size of the slice is never larger than `isize::MAX` bytes, as
840 // noted here:
841 // - https://github.com/rust-lang/unsafe-code-guidelines/issues/102#issuecomment-473340447
842 // - https://doc.rust-lang.org/reference/behavior-considered-undefined.html
843 // - https://doc.rust-lang.org/core/slice/fn.from_raw_parts.html#safety
844 // (This doesn't seem normative yet, but the very same assumption is
845 // made in many places, including the Index implementation of slices.)
846 //
847 // - There is no wrapping around involved, as slices do not wrap past
848 // the end of the address space.
849 //
850 // See the documentation of [`pointer::add`].
851 let end = unsafe { start.add(self.len()) };
852 start..end
853 }
854
855 /// Returns the two unsafe mutable pointers spanning the slice.
856 ///
857 /// The returned range is half-open, which means that the end pointer
858 /// points *one past* the last element of the slice. This way, an empty
859 /// slice is represented by two equal pointers, and the difference between
860 /// the two pointers represents the size of the slice.
861 ///
862 /// See [`as_mut_ptr`] for warnings on using these pointers. The end
863 /// pointer requires extra caution, as it does not point to a valid element
864 /// in the slice.
865 ///
866 /// This function is useful for interacting with foreign interfaces which
867 /// use two pointers to refer to a range of elements in memory, as is
868 /// common in C++.
869 ///
870 /// [`as_mut_ptr`]: slice::as_mut_ptr
871 #[stable(feature = "slice_ptr_range", since = "1.48.0")]
872 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
873 #[inline]
874 #[must_use]
875 #[cfg(not(feature = "ferrocene_subset"))]
876 pub const fn as_mut_ptr_range(&mut self) -> Range<*mut T> {
877 let start = self.as_mut_ptr();
878 // SAFETY: See as_ptr_range() above for why `add` here is safe.
879 let end = unsafe { start.add(self.len()) };
880 start..end
881 }
882
883 /// Gets a reference to the underlying array.
884 ///
885 /// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
886 #[stable(feature = "core_slice_as_array", since = "1.93.0")]
887 #[rustc_const_stable(feature = "core_slice_as_array", since = "1.93.0")]
888 #[inline]
889 #[must_use]
890 pub const fn as_array<const N: usize>(&self) -> Option<&[T; N]> {
891 if self.len() == N {
892 let ptr = self.as_ptr().cast_array();
893
894 // SAFETY: The underlying array of a slice can be reinterpreted as an actual array `[T; N]` if `N` is not greater than the slice's length.
895 let me = unsafe { &*ptr };
896 Some(me)
897 } else {
898 None
899 }
900 }
901
902 /// Gets a mutable reference to the slice's underlying array.
903 ///
904 /// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
905 #[stable(feature = "core_slice_as_array", since = "1.93.0")]
906 #[rustc_const_stable(feature = "core_slice_as_array", since = "1.93.0")]
907 #[inline]
908 #[must_use]
909 pub const fn as_mut_array<const N: usize>(&mut self) -> Option<&mut [T; N]> {
910 if self.len() == N {
911 let ptr = self.as_mut_ptr().cast_array();
912
913 // SAFETY: The underlying array of a slice can be reinterpreted as an actual array `[T; N]` if `N` is not greater than the slice's length.
914 let me = unsafe { &mut *ptr };
915 Some(me)
916 } else {
917 None
918 }
919 }
920
921 /// Swaps two elements in the slice.
922 ///
923 /// If `a` equals to `b`, it's guaranteed that elements won't change value.
924 ///
925 /// # Arguments
926 ///
927 /// * a - The index of the first element
928 /// * b - The index of the second element
929 ///
930 /// # Panics
931 ///
932 /// Panics if `a` or `b` are out of bounds.
933 ///
934 /// # Examples
935 ///
936 /// ```
937 /// let mut v = ["a", "b", "c", "d", "e"];
938 /// v.swap(2, 4);
939 /// assert!(v == ["a", "b", "e", "d", "c"]);
940 /// ```
941 #[stable(feature = "rust1", since = "1.0.0")]
942 #[rustc_const_stable(feature = "const_swap", since = "1.85.0")]
943 #[inline]
944 #[track_caller]
945 pub const fn swap(&mut self, a: usize, b: usize) {
946 // FIXME: use swap_unchecked here (https://github.com/rust-lang/rust/pull/88540#issuecomment-944344343)
947 // Can't take two mutable loans from one vector, so instead use raw pointers.
948 let pa = &raw mut self[a];
949 let pb = &raw mut self[b];
950 // SAFETY: `pa` and `pb` have been created from safe mutable references and refer
951 // to elements in the slice and therefore are guaranteed to be valid and aligned.
952 // Note that accessing the elements behind `a` and `b` is checked and will
953 // panic when out of bounds.
954 unsafe {
955 ptr::swap(pa, pb);
956 }
957 }
958
959 /// Swaps two elements in the slice, without doing bounds checking.
960 ///
961 /// For a safe alternative see [`swap`].
962 ///
963 /// # Arguments
964 ///
965 /// * a - The index of the first element
966 /// * b - The index of the second element
967 ///
968 /// # Safety
969 ///
970 /// Calling this method with an out-of-bounds index is *[undefined behavior]*.
971 /// The caller has to ensure that `a < self.len()` and `b < self.len()`.
972 ///
973 /// # Examples
974 ///
975 /// ```
976 /// #![feature(slice_swap_unchecked)]
977 ///
978 /// let mut v = ["a", "b", "c", "d"];
979 /// // SAFETY: we know that 1 and 3 are both indices of the slice
980 /// unsafe { v.swap_unchecked(1, 3) };
981 /// assert!(v == ["a", "d", "c", "b"]);
982 /// ```
983 ///
984 /// [`swap`]: slice::swap
985 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
986 #[unstable(feature = "slice_swap_unchecked", issue = "88539")]
987 #[track_caller]
988 #[cfg(not(feature = "ferrocene_subset"))]
989 pub const unsafe fn swap_unchecked(&mut self, a: usize, b: usize) {
990 assert_unsafe_precondition!(
991 check_library_ub,
992 "slice::swap_unchecked requires that the indices are within the slice",
993 (
994 len: usize = self.len(),
995 a: usize = a,
996 b: usize = b,
997 ) => a < len && b < len,
998 );
999
1000 let ptr = self.as_mut_ptr();
1001 // SAFETY: caller has to guarantee that `a < self.len()` and `b < self.len()`
1002 unsafe {
1003 ptr::swap(ptr.add(a), ptr.add(b));
1004 }
1005 }
1006
1007 /// Reverses the order of elements in the slice, in place.
1008 ///
1009 /// # Examples
1010 ///
1011 /// ```
1012 /// let mut v = [1, 2, 3];
1013 /// v.reverse();
1014 /// assert!(v == [3, 2, 1]);
1015 /// ```
1016 #[stable(feature = "rust1", since = "1.0.0")]
1017 #[rustc_const_stable(feature = "const_slice_reverse", since = "1.90.0")]
1018 #[inline]
1019 #[cfg(not(feature = "ferrocene_subset"))]
1020 pub const fn reverse(&mut self) {
1021 let half_len = self.len() / 2;
1022 let Range { start, end } = self.as_mut_ptr_range();
1023
1024 // These slices will skip the middle item for an odd length,
1025 // since that one doesn't need to move.
1026 let (front_half, back_half) =
1027 // SAFETY: Both are subparts of the original slice, so the memory
1028 // range is valid, and they don't overlap because they're each only
1029 // half (or less) of the original slice.
1030 unsafe {
1031 (
1032 slice::from_raw_parts_mut(start, half_len),
1033 slice::from_raw_parts_mut(end.sub(half_len), half_len),
1034 )
1035 };
1036
1037 // Introducing a function boundary here means that the two halves
1038 // get `noalias` markers, allowing better optimization as LLVM
1039 // knows that they're disjoint, unlike in the original slice.
1040 revswap(front_half, back_half, half_len);
1041
1042 #[inline]
1043 const fn revswap<T>(a: &mut [T], b: &mut [T], n: usize) {
1044 debug_assert!(a.len() == n);
1045 debug_assert!(b.len() == n);
1046
1047 // Because this function is first compiled in isolation,
1048 // this check tells LLVM that the indexing below is
1049 // in-bounds. Then after inlining -- once the actual
1050 // lengths of the slices are known -- it's removed.
1051 // FIXME(const_trait_impl) replace with let (a, b) = (&mut a[..n], &mut b[..n]);
1052 let (a, _) = a.split_at_mut(n);
1053 let (b, _) = b.split_at_mut(n);
1054
1055 let mut i = 0;
1056 while i < n {
1057 mem::swap(&mut a[i], &mut b[n - 1 - i]);
1058 i += 1;
1059 }
1060 }
1061 }
1062
1063 /// Returns an iterator over the slice.
1064 ///
1065 /// The iterator yields all items from start to end.
1066 ///
1067 /// # Examples
1068 ///
1069 /// ```
1070 /// let x = &[1, 2, 4];
1071 /// let mut iterator = x.iter();
1072 ///
1073 /// assert_eq!(iterator.next(), Some(&1));
1074 /// assert_eq!(iterator.next(), Some(&2));
1075 /// assert_eq!(iterator.next(), Some(&4));
1076 /// assert_eq!(iterator.next(), None);
1077 /// ```
1078 #[stable(feature = "rust1", since = "1.0.0")]
1079 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
1080 #[inline]
1081 #[rustc_diagnostic_item = "slice_iter"]
1082 pub const fn iter(&self) -> Iter<'_, T> {
1083 Iter::new(self)
1084 }
1085
1086 /// Returns an iterator that allows modifying each value.
1087 ///
1088 /// The iterator yields all items from start to end.
1089 ///
1090 /// # Examples
1091 ///
1092 /// ```
1093 /// let x = &mut [1, 2, 4];
1094 /// for elem in x.iter_mut() {
1095 /// *elem += 2;
1096 /// }
1097 /// assert_eq!(x, &[3, 4, 6]);
1098 /// ```
1099 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
1100 #[stable(feature = "rust1", since = "1.0.0")]
1101 #[inline]
1102 pub const fn iter_mut(&mut self) -> IterMut<'_, T> {
1103 IterMut::new(self)
1104 }
1105
1106 /// Returns an iterator over all contiguous windows of length
1107 /// `size`. The windows overlap. If the slice is shorter than
1108 /// `size`, the iterator returns no values.
1109 ///
1110 /// # Panics
1111 ///
1112 /// Panics if `size` is zero.
1113 ///
1114 /// # Examples
1115 ///
1116 /// ```
1117 /// let slice = ['l', 'o', 'r', 'e', 'm'];
1118 /// let mut iter = slice.windows(3);
1119 /// assert_eq!(iter.next().unwrap(), &['l', 'o', 'r']);
1120 /// assert_eq!(iter.next().unwrap(), &['o', 'r', 'e']);
1121 /// assert_eq!(iter.next().unwrap(), &['r', 'e', 'm']);
1122 /// assert!(iter.next().is_none());
1123 /// ```
1124 ///
1125 /// If the slice is shorter than `size`:
1126 ///
1127 /// ```
1128 /// let slice = ['f', 'o', 'o'];
1129 /// let mut iter = slice.windows(4);
1130 /// assert!(iter.next().is_none());
1131 /// ```
1132 ///
1133 /// Because the [Iterator] trait cannot represent the required lifetimes,
1134 /// there is no `windows_mut` analog to `windows`;
1135 /// `[0,1,2].windows_mut(2).collect()` would violate [the rules of references]
1136 /// (though a [LendingIterator] analog is possible). You can sometimes use
1137 /// [`Cell::as_slice_of_cells`](crate::cell::Cell::as_slice_of_cells) in
1138 /// conjunction with `windows` instead:
1139 ///
1140 /// [the rules of references]: https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#the-rules-of-references
1141 /// [LendingIterator]: https://blog.rust-lang.org/2022/10/28/gats-stabilization.html
1142 /// ```
1143 /// use std::cell::Cell;
1144 ///
1145 /// let mut array = ['R', 'u', 's', 't', ' ', '2', '0', '1', '5'];
1146 /// let slice = &mut array[..];
1147 /// let slice_of_cells: &[Cell<char>] = Cell::from_mut(slice).as_slice_of_cells();
1148 /// for w in slice_of_cells.windows(3) {
1149 /// Cell::swap(&w[0], &w[2]);
1150 /// }
1151 /// assert_eq!(array, ['s', 't', ' ', '2', '0', '1', '5', 'u', 'R']);
1152 /// ```
1153 #[stable(feature = "rust1", since = "1.0.0")]
1154 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
1155 #[inline]
1156 #[track_caller]
1157 pub const fn windows(&self, size: usize) -> Windows<'_, T> {
1158 let size = NonZero::new(size).expect("window size must be non-zero");
1159 Windows::new(self, size)
1160 }
1161
1162 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
1163 /// beginning of the slice.
1164 ///
1165 /// The chunks are slices and do not overlap. If `chunk_size` does not divide the length of the
1166 /// slice, then the last chunk will not have length `chunk_size`.
1167 ///
1168 /// See [`chunks_exact`] for a variant of this iterator that returns chunks of always exactly
1169 /// `chunk_size` elements, and [`rchunks`] for the same iterator but starting at the end of the
1170 /// slice.
1171 ///
1172 /// If your `chunk_size` is a constant, consider using [`as_chunks`] instead, which will
1173 /// give references to arrays of exactly that length, rather than slices.
1174 ///
1175 /// # Panics
1176 ///
1177 /// Panics if `chunk_size` is zero.
1178 ///
1179 /// # Examples
1180 ///
1181 /// ```
1182 /// let slice = ['l', 'o', 'r', 'e', 'm'];
1183 /// let mut iter = slice.chunks(2);
1184 /// assert_eq!(iter.next().unwrap(), &['l', 'o']);
1185 /// assert_eq!(iter.next().unwrap(), &['r', 'e']);
1186 /// assert_eq!(iter.next().unwrap(), &['m']);
1187 /// assert!(iter.next().is_none());
1188 /// ```
1189 ///
1190 /// [`chunks_exact`]: slice::chunks_exact
1191 /// [`rchunks`]: slice::rchunks
1192 /// [`as_chunks`]: slice::as_chunks
1193 #[stable(feature = "rust1", since = "1.0.0")]
1194 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
1195 #[inline]
1196 #[track_caller]
1197 pub const fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> {
1198 assert!(chunk_size != 0, "chunk size must be non-zero");
1199 Chunks::new(self, chunk_size)
1200 }
1201
1202 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
1203 /// beginning of the slice.
1204 ///
1205 /// The chunks are mutable slices, and do not overlap. If `chunk_size` does not divide the
1206 /// length of the slice, then the last chunk will not have length `chunk_size`.
1207 ///
1208 /// See [`chunks_exact_mut`] for a variant of this iterator that returns chunks of always
1209 /// exactly `chunk_size` elements, and [`rchunks_mut`] for the same iterator but starting at
1210 /// the end of the slice.
1211 ///
1212 /// If your `chunk_size` is a constant, consider using [`as_chunks_mut`] instead, which will
1213 /// give references to arrays of exactly that length, rather than slices.
1214 ///
1215 /// # Panics
1216 ///
1217 /// Panics if `chunk_size` is zero.
1218 ///
1219 /// # Examples
1220 ///
1221 /// ```
1222 /// let v = &mut [0, 0, 0, 0, 0];
1223 /// let mut count = 1;
1224 ///
1225 /// for chunk in v.chunks_mut(2) {
1226 /// for elem in chunk.iter_mut() {
1227 /// *elem += count;
1228 /// }
1229 /// count += 1;
1230 /// }
1231 /// assert_eq!(v, &[1, 1, 2, 2, 3]);
1232 /// ```
1233 ///
1234 /// [`chunks_exact_mut`]: slice::chunks_exact_mut
1235 /// [`rchunks_mut`]: slice::rchunks_mut
1236 /// [`as_chunks_mut`]: slice::as_chunks_mut
1237 #[stable(feature = "rust1", since = "1.0.0")]
1238 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
1239 #[inline]
1240 #[track_caller]
1241 pub const fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> {
1242 assert!(chunk_size != 0, "chunk size must be non-zero");
1243 ChunksMut::new(self, chunk_size)
1244 }
1245
1246 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
1247 /// beginning of the slice.
1248 ///
1249 /// The chunks are slices and do not overlap. If `chunk_size` does not divide the length of the
1250 /// slice, then the last up to `chunk_size-1` elements will be omitted and can be retrieved
1251 /// from the `remainder` function of the iterator.
1252 ///
1253 /// Due to each chunk having exactly `chunk_size` elements, the compiler can often optimize the
1254 /// resulting code better than in the case of [`chunks`].
1255 ///
1256 /// See [`chunks`] for a variant of this iterator that also returns the remainder as a smaller
1257 /// chunk, and [`rchunks_exact`] for the same iterator but starting at the end of the slice.
1258 ///
1259 /// If your `chunk_size` is a constant, consider using [`as_chunks`] instead, which will
1260 /// give references to arrays of exactly that length, rather than slices.
1261 ///
1262 /// # Panics
1263 ///
1264 /// Panics if `chunk_size` is zero.
1265 ///
1266 /// # Examples
1267 ///
1268 /// ```
1269 /// let slice = ['l', 'o', 'r', 'e', 'm'];
1270 /// let mut iter = slice.chunks_exact(2);
1271 /// assert_eq!(iter.next().unwrap(), &['l', 'o']);
1272 /// assert_eq!(iter.next().unwrap(), &['r', 'e']);
1273 /// assert!(iter.next().is_none());
1274 /// assert_eq!(iter.remainder(), &['m']);
1275 /// ```
1276 ///
1277 /// [`chunks`]: slice::chunks
1278 /// [`rchunks_exact`]: slice::rchunks_exact
1279 /// [`as_chunks`]: slice::as_chunks
1280 #[stable(feature = "chunks_exact", since = "1.31.0")]
1281 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
1282 #[inline]
1283 #[track_caller]
1284 pub const fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> {
1285 assert!(chunk_size != 0, "chunk size must be non-zero");
1286 ChunksExact::new(self, chunk_size)
1287 }
1288
1289 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
1290 /// beginning of the slice.
1291 ///
1292 /// The chunks are mutable slices, and do not overlap. If `chunk_size` does not divide the
1293 /// length of the slice, then the last up to `chunk_size-1` elements will be omitted and can be
1294 /// retrieved from the `into_remainder` function of the iterator.
1295 ///
1296 /// Due to each chunk having exactly `chunk_size` elements, the compiler can often optimize the
1297 /// resulting code better than in the case of [`chunks_mut`].
1298 ///
1299 /// See [`chunks_mut`] for a variant of this iterator that also returns the remainder as a
1300 /// smaller chunk, and [`rchunks_exact_mut`] for the same iterator but starting at the end of
1301 /// the slice.
1302 ///
1303 /// If your `chunk_size` is a constant, consider using [`as_chunks_mut`] instead, which will
1304 /// give references to arrays of exactly that length, rather than slices.
1305 ///
1306 /// # Panics
1307 ///
1308 /// Panics if `chunk_size` is zero.
1309 ///
1310 /// # Examples
1311 ///
1312 /// ```
1313 /// let v = &mut [0, 0, 0, 0, 0];
1314 /// let mut count = 1;
1315 ///
1316 /// for chunk in v.chunks_exact_mut(2) {
1317 /// for elem in chunk.iter_mut() {
1318 /// *elem += count;
1319 /// }
1320 /// count += 1;
1321 /// }
1322 /// assert_eq!(v, &[1, 1, 2, 2, 0]);
1323 /// ```
1324 ///
1325 /// [`chunks_mut`]: slice::chunks_mut
1326 /// [`rchunks_exact_mut`]: slice::rchunks_exact_mut
1327 /// [`as_chunks_mut`]: slice::as_chunks_mut
1328 #[stable(feature = "chunks_exact", since = "1.31.0")]
1329 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
1330 #[inline]
1331 #[track_caller]
1332 pub const fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> {
1333 assert!(chunk_size != 0, "chunk size must be non-zero");
1334 ChunksExactMut::new(self, chunk_size)
1335 }
1336
1337 /// Splits the slice into a slice of `N`-element arrays,
1338 /// assuming that there's no remainder.
1339 ///
1340 /// This is the inverse operation to [`as_flattened`].
1341 ///
1342 /// [`as_flattened`]: slice::as_flattened
1343 ///
1344 /// As this is `unsafe`, consider whether you could use [`as_chunks`] or
1345 /// [`as_rchunks`] instead, perhaps via something like
1346 /// `if let (chunks, []) = slice.as_chunks()` or
1347 /// `let (chunks, []) = slice.as_chunks() else { unreachable!() };`.
1348 ///
1349 /// [`as_chunks`]: slice::as_chunks
1350 /// [`as_rchunks`]: slice::as_rchunks
1351 ///
1352 /// # Safety
1353 ///
1354 /// This may only be called when
1355 /// - The slice splits exactly into `N`-element chunks (aka `self.len() % N == 0`).
1356 /// - `N != 0`.
1357 ///
1358 /// # Examples
1359 ///
1360 /// ```
1361 /// let slice: &[char] = &['l', 'o', 'r', 'e', 'm', '!'];
1362 /// let chunks: &[[char; 1]] =
1363 /// // SAFETY: 1-element chunks never have remainder
1364 /// unsafe { slice.as_chunks_unchecked() };
1365 /// assert_eq!(chunks, &[['l'], ['o'], ['r'], ['e'], ['m'], ['!']]);
1366 /// let chunks: &[[char; 3]] =
1367 /// // SAFETY: The slice length (6) is a multiple of 3
1368 /// unsafe { slice.as_chunks_unchecked() };
1369 /// assert_eq!(chunks, &[['l', 'o', 'r'], ['e', 'm', '!']]);
1370 ///
1371 /// // These would be unsound:
1372 /// // let chunks: &[[_; 5]] = slice.as_chunks_unchecked() // The slice length is not a multiple of 5
1373 /// // let chunks: &[[_; 0]] = slice.as_chunks_unchecked() // Zero-length chunks are never allowed
1374 /// ```
1375 #[stable(feature = "slice_as_chunks", since = "1.88.0")]
1376 #[rustc_const_stable(feature = "slice_as_chunks", since = "1.88.0")]
1377 #[inline]
1378 #[must_use]
1379 #[track_caller]
1380 pub const unsafe fn as_chunks_unchecked<const N: usize>(&self) -> &[[T; N]] {
1381 assert_unsafe_precondition!(
1382 check_language_ub,
1383 "slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks",
1384 (n: usize = N, len: usize = self.len()) => n != 0 && len.is_multiple_of(n),
1385 );
1386 // SAFETY: Caller must guarantee that `N` is nonzero and exactly divides the slice length
1387 let new_len = unsafe { exact_div(self.len(), N) };
1388 // SAFETY: We cast a slice of `new_len * N` elements into
1389 // a slice of `new_len` many `N` elements chunks.
1390 unsafe { from_raw_parts(self.as_ptr().cast(), new_len) }
1391 }
1392
1393 /// Splits the slice into a slice of `N`-element arrays,
1394 /// starting at the beginning of the slice,
1395 /// and a remainder slice with length strictly less than `N`.
1396 ///
1397 /// The remainder is meaningful in the division sense. Given
1398 /// `let (chunks, remainder) = slice.as_chunks()`, then:
1399 /// - `chunks.len()` equals `slice.len() / N`,
1400 /// - `remainder.len()` equals `slice.len() % N`, and
1401 /// - `slice.len()` equals `chunks.len() * N + remainder.len()`.
1402 ///
1403 /// You can flatten the chunks back into a slice-of-`T` with [`as_flattened`].
1404 ///
1405 /// [`as_flattened`]: slice::as_flattened
1406 ///
1407 /// # Panics
1408 ///
1409 /// Panics if `N` is zero.
1410 ///
1411 /// Note that this check is against a const generic parameter, not a runtime
1412 /// value, and thus a particular monomorphization will either always panic
1413 /// or it will never panic.
1414 ///
1415 /// # Examples
1416 ///
1417 /// ```
1418 /// let slice = ['l', 'o', 'r', 'e', 'm'];
1419 /// let (chunks, remainder) = slice.as_chunks();
1420 /// assert_eq!(chunks, &[['l', 'o'], ['r', 'e']]);
1421 /// assert_eq!(remainder, &['m']);
1422 /// ```
1423 ///
1424 /// If you expect the slice to be an exact multiple, you can combine
1425 /// `let`-`else` with an empty slice pattern:
1426 /// ```
1427 /// let slice = ['R', 'u', 's', 't'];
1428 /// let (chunks, []) = slice.as_chunks::<2>() else {
1429 /// panic!("slice didn't have even length")
1430 /// };
1431 /// assert_eq!(chunks, &[['R', 'u'], ['s', 't']]);
1432 /// ```
1433 #[stable(feature = "slice_as_chunks", since = "1.88.0")]
1434 #[rustc_const_stable(feature = "slice_as_chunks", since = "1.88.0")]
1435 #[inline]
1436 #[track_caller]
1437 #[must_use]
1438 pub const fn as_chunks<const N: usize>(&self) -> (&[[T; N]], &[T]) {
1439 assert!(N != 0, "chunk size must be non-zero");
1440 let len_rounded_down = self.len() / N * N;
1441 // SAFETY: The rounded-down value is always the same or smaller than the
1442 // original length, and thus must be in-bounds of the slice.
1443 let (multiple_of_n, remainder) = unsafe { self.split_at_unchecked(len_rounded_down) };
1444 // SAFETY: We already panicked for zero, and ensured by construction
1445 // that the length of the subslice is a multiple of N.
1446 let array_slice = unsafe { multiple_of_n.as_chunks_unchecked() };
1447 (array_slice, remainder)
1448 }
1449
1450 /// Splits the slice into a slice of `N`-element arrays,
1451 /// starting at the end of the slice,
1452 /// and a remainder slice with length strictly less than `N`.
1453 ///
1454 /// The remainder is meaningful in the division sense. Given
1455 /// `let (remainder, chunks) = slice.as_rchunks()`, then:
1456 /// - `remainder.len()` equals `slice.len() % N`,
1457 /// - `chunks.len()` equals `slice.len() / N`, and
1458 /// - `slice.len()` equals `chunks.len() * N + remainder.len()`.
1459 ///
1460 /// You can flatten the chunks back into a slice-of-`T` with [`as_flattened`].
1461 ///
1462 /// [`as_flattened`]: slice::as_flattened
1463 ///
1464 /// # Panics
1465 ///
1466 /// Panics if `N` is zero.
1467 ///
1468 /// Note that this check is against a const generic parameter, not a runtime
1469 /// value, and thus a particular monomorphization will either always panic
1470 /// or it will never panic.
1471 ///
1472 /// # Examples
1473 ///
1474 /// ```
1475 /// let slice = ['l', 'o', 'r', 'e', 'm'];
1476 /// let (remainder, chunks) = slice.as_rchunks();
1477 /// assert_eq!(remainder, &['l']);
1478 /// assert_eq!(chunks, &[['o', 'r'], ['e', 'm']]);
1479 /// ```
1480 #[stable(feature = "slice_as_chunks", since = "1.88.0")]
1481 #[rustc_const_stable(feature = "slice_as_chunks", since = "1.88.0")]
1482 #[inline]
1483 #[track_caller]
1484 #[must_use]
1485 #[cfg(not(feature = "ferrocene_subset"))]
1486 pub const fn as_rchunks<const N: usize>(&self) -> (&[T], &[[T; N]]) {
1487 assert!(N != 0, "chunk size must be non-zero");
1488 let len = self.len() / N;
1489 let (remainder, multiple_of_n) = self.split_at(self.len() - len * N);
1490 // SAFETY: We already panicked for zero, and ensured by construction
1491 // that the length of the subslice is a multiple of N.
1492 let array_slice = unsafe { multiple_of_n.as_chunks_unchecked() };
1493 (remainder, array_slice)
1494 }
1495
1496 /// Splits the slice into a slice of `N`-element arrays,
1497 /// assuming that there's no remainder.
1498 ///
1499 /// This is the inverse operation to [`as_flattened_mut`].
1500 ///
1501 /// [`as_flattened_mut`]: slice::as_flattened_mut
1502 ///
1503 /// As this is `unsafe`, consider whether you could use [`as_chunks_mut`] or
1504 /// [`as_rchunks_mut`] instead, perhaps via something like
1505 /// `if let (chunks, []) = slice.as_chunks_mut()` or
1506 /// `let (chunks, []) = slice.as_chunks_mut() else { unreachable!() };`.
1507 ///
1508 /// [`as_chunks_mut`]: slice::as_chunks_mut
1509 /// [`as_rchunks_mut`]: slice::as_rchunks_mut
1510 ///
1511 /// # Safety
1512 ///
1513 /// This may only be called when
1514 /// - The slice splits exactly into `N`-element chunks (aka `self.len() % N == 0`).
1515 /// - `N != 0`.
1516 ///
1517 /// # Examples
1518 ///
1519 /// ```
1520 /// let slice: &mut [char] = &mut ['l', 'o', 'r', 'e', 'm', '!'];
1521 /// let chunks: &mut [[char; 1]] =
1522 /// // SAFETY: 1-element chunks never have remainder
1523 /// unsafe { slice.as_chunks_unchecked_mut() };
1524 /// chunks[0] = ['L'];
1525 /// assert_eq!(chunks, &[['L'], ['o'], ['r'], ['e'], ['m'], ['!']]);
1526 /// let chunks: &mut [[char; 3]] =
1527 /// // SAFETY: The slice length (6) is a multiple of 3
1528 /// unsafe { slice.as_chunks_unchecked_mut() };
1529 /// chunks[1] = ['a', 'x', '?'];
1530 /// assert_eq!(slice, &['L', 'o', 'r', 'a', 'x', '?']);
1531 ///
1532 /// // These would be unsound:
1533 /// // let chunks: &[[_; 5]] = slice.as_chunks_unchecked_mut() // The slice length is not a multiple of 5
1534 /// // let chunks: &[[_; 0]] = slice.as_chunks_unchecked_mut() // Zero-length chunks are never allowed
1535 /// ```
1536 #[stable(feature = "slice_as_chunks", since = "1.88.0")]
1537 #[rustc_const_stable(feature = "slice_as_chunks", since = "1.88.0")]
1538 #[inline]
1539 #[must_use]
1540 #[track_caller]
1541 #[cfg(not(feature = "ferrocene_subset"))]
1542 pub const unsafe fn as_chunks_unchecked_mut<const N: usize>(&mut self) -> &mut [[T; N]] {
1543 assert_unsafe_precondition!(
1544 check_language_ub,
1545 "slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks",
1546 (n: usize = N, len: usize = self.len()) => n != 0 && len.is_multiple_of(n)
1547 );
1548 // SAFETY: Caller must guarantee that `N` is nonzero and exactly divides the slice length
1549 let new_len = unsafe { exact_div(self.len(), N) };
1550 // SAFETY: We cast a slice of `new_len * N` elements into
1551 // a slice of `new_len` many `N` elements chunks.
1552 unsafe { from_raw_parts_mut(self.as_mut_ptr().cast(), new_len) }
1553 }
1554
1555 /// Splits the slice into a slice of `N`-element arrays,
1556 /// starting at the beginning of the slice,
1557 /// and a remainder slice with length strictly less than `N`.
1558 ///
1559 /// The remainder is meaningful in the division sense. Given
1560 /// `let (chunks, remainder) = slice.as_chunks_mut()`, then:
1561 /// - `chunks.len()` equals `slice.len() / N`,
1562 /// - `remainder.len()` equals `slice.len() % N`, and
1563 /// - `slice.len()` equals `chunks.len() * N + remainder.len()`.
1564 ///
1565 /// You can flatten the chunks back into a slice-of-`T` with [`as_flattened_mut`].
1566 ///
1567 /// [`as_flattened_mut`]: slice::as_flattened_mut
1568 ///
1569 /// # Panics
1570 ///
1571 /// Panics if `N` is zero.
1572 ///
1573 /// Note that this check is against a const generic parameter, not a runtime
1574 /// value, and thus a particular monomorphization will either always panic
1575 /// or it will never panic.
1576 ///
1577 /// # Examples
1578 ///
1579 /// ```
1580 /// let v = &mut [0, 0, 0, 0, 0];
1581 /// let mut count = 1;
1582 ///
1583 /// let (chunks, remainder) = v.as_chunks_mut();
1584 /// remainder[0] = 9;
1585 /// for chunk in chunks {
1586 /// *chunk = [count; 2];
1587 /// count += 1;
1588 /// }
1589 /// assert_eq!(v, &[1, 1, 2, 2, 9]);
1590 /// ```
1591 #[stable(feature = "slice_as_chunks", since = "1.88.0")]
1592 #[rustc_const_stable(feature = "slice_as_chunks", since = "1.88.0")]
1593 #[inline]
1594 #[track_caller]
1595 #[must_use]
1596 #[cfg(not(feature = "ferrocene_subset"))]
1597 pub const fn as_chunks_mut<const N: usize>(&mut self) -> (&mut [[T; N]], &mut [T]) {
1598 assert!(N != 0, "chunk size must be non-zero");
1599 let len_rounded_down = self.len() / N * N;
1600 // SAFETY: The rounded-down value is always the same or smaller than the
1601 // original length, and thus must be in-bounds of the slice.
1602 let (multiple_of_n, remainder) = unsafe { self.split_at_mut_unchecked(len_rounded_down) };
1603 // SAFETY: We already panicked for zero, and ensured by construction
1604 // that the length of the subslice is a multiple of N.
1605 let array_slice = unsafe { multiple_of_n.as_chunks_unchecked_mut() };
1606 (array_slice, remainder)
1607 }
1608
1609 /// Splits the slice into a slice of `N`-element arrays,
1610 /// starting at the end of the slice,
1611 /// and a remainder slice with length strictly less than `N`.
1612 ///
1613 /// The remainder is meaningful in the division sense. Given
1614 /// `let (remainder, chunks) = slice.as_rchunks_mut()`, then:
1615 /// - `remainder.len()` equals `slice.len() % N`,
1616 /// - `chunks.len()` equals `slice.len() / N`, and
1617 /// - `slice.len()` equals `chunks.len() * N + remainder.len()`.
1618 ///
1619 /// You can flatten the chunks back into a slice-of-`T` with [`as_flattened_mut`].
1620 ///
1621 /// [`as_flattened_mut`]: slice::as_flattened_mut
1622 ///
1623 /// # Panics
1624 ///
1625 /// Panics if `N` is zero.
1626 ///
1627 /// Note that this check is against a const generic parameter, not a runtime
1628 /// value, and thus a particular monomorphization will either always panic
1629 /// or it will never panic.
1630 ///
1631 /// # Examples
1632 ///
1633 /// ```
1634 /// let v = &mut [0, 0, 0, 0, 0];
1635 /// let mut count = 1;
1636 ///
1637 /// let (remainder, chunks) = v.as_rchunks_mut();
1638 /// remainder[0] = 9;
1639 /// for chunk in chunks {
1640 /// *chunk = [count; 2];
1641 /// count += 1;
1642 /// }
1643 /// assert_eq!(v, &[9, 1, 1, 2, 2]);
1644 /// ```
1645 #[stable(feature = "slice_as_chunks", since = "1.88.0")]
1646 #[rustc_const_stable(feature = "slice_as_chunks", since = "1.88.0")]
1647 #[inline]
1648 #[track_caller]
1649 #[must_use]
1650 #[cfg(not(feature = "ferrocene_subset"))]
1651 pub const fn as_rchunks_mut<const N: usize>(&mut self) -> (&mut [T], &mut [[T; N]]) {
1652 assert!(N != 0, "chunk size must be non-zero");
1653 let len = self.len() / N;
1654 let (remainder, multiple_of_n) = self.split_at_mut(self.len() - len * N);
1655 // SAFETY: We already panicked for zero, and ensured by construction
1656 // that the length of the subslice is a multiple of N.
1657 let array_slice = unsafe { multiple_of_n.as_chunks_unchecked_mut() };
1658 (remainder, array_slice)
1659 }
1660
1661 /// Returns an iterator over overlapping windows of `N` elements of a slice,
1662 /// starting at the beginning of the slice.
1663 ///
1664 /// This is the const generic equivalent of [`windows`].
1665 ///
1666 /// If `N` is greater than the size of the slice, it will return no windows.
1667 ///
1668 /// # Panics
1669 ///
1670 /// Panics if `N` is zero.
1671 ///
1672 /// Note that this check is against a const generic parameter, not a runtime
1673 /// value, and thus a particular monomorphization will either always panic
1674 /// or it will never panic.
1675 ///
1676 /// # Examples
1677 ///
1678 /// ```
1679 /// let slice = [0, 1, 2, 3];
1680 /// let mut iter = slice.array_windows();
1681 /// assert_eq!(iter.next().unwrap(), &[0, 1]);
1682 /// assert_eq!(iter.next().unwrap(), &[1, 2]);
1683 /// assert_eq!(iter.next().unwrap(), &[2, 3]);
1684 /// assert!(iter.next().is_none());
1685 /// ```
1686 ///
1687 /// [`windows`]: slice::windows
1688 #[stable(feature = "array_windows", since = "1.94.0")]
1689 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
1690 #[inline]
1691 #[track_caller]
1692 #[cfg(not(feature = "ferrocene_subset"))]
1693 pub const fn array_windows<const N: usize>(&self) -> ArrayWindows<'_, T, N> {
1694 assert!(N != 0, "window size must be non-zero");
1695 ArrayWindows::new(self)
1696 }
1697
1698 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end
1699 /// of the slice.
1700 ///
1701 /// The chunks are slices and do not overlap. If `chunk_size` does not divide the length of the
1702 /// slice, then the last chunk will not have length `chunk_size`.
1703 ///
1704 /// See [`rchunks_exact`] for a variant of this iterator that returns chunks of always exactly
1705 /// `chunk_size` elements, and [`chunks`] for the same iterator but starting at the beginning
1706 /// of the slice.
1707 ///
1708 /// If your `chunk_size` is a constant, consider using [`as_rchunks`] instead, which will
1709 /// give references to arrays of exactly that length, rather than slices.
1710 ///
1711 /// # Panics
1712 ///
1713 /// Panics if `chunk_size` is zero.
1714 ///
1715 /// # Examples
1716 ///
1717 /// ```
1718 /// let slice = ['l', 'o', 'r', 'e', 'm'];
1719 /// let mut iter = slice.rchunks(2);
1720 /// assert_eq!(iter.next().unwrap(), &['e', 'm']);
1721 /// assert_eq!(iter.next().unwrap(), &['o', 'r']);
1722 /// assert_eq!(iter.next().unwrap(), &['l']);
1723 /// assert!(iter.next().is_none());
1724 /// ```
1725 ///
1726 /// [`rchunks_exact`]: slice::rchunks_exact
1727 /// [`chunks`]: slice::chunks
1728 /// [`as_rchunks`]: slice::as_rchunks
1729 #[stable(feature = "rchunks", since = "1.31.0")]
1730 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
1731 #[inline]
1732 #[track_caller]
1733 #[cfg(not(feature = "ferrocene_subset"))]
1734 pub const fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> {
1735 assert!(chunk_size != 0, "chunk size must be non-zero");
1736 RChunks::new(self, chunk_size)
1737 }
1738
1739 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end
1740 /// of the slice.
1741 ///
1742 /// The chunks are mutable slices, and do not overlap. If `chunk_size` does not divide the
1743 /// length of the slice, then the last chunk will not have length `chunk_size`.
1744 ///
1745 /// See [`rchunks_exact_mut`] for a variant of this iterator that returns chunks of always
1746 /// exactly `chunk_size` elements, and [`chunks_mut`] for the same iterator but starting at the
1747 /// beginning of the slice.
1748 ///
1749 /// If your `chunk_size` is a constant, consider using [`as_rchunks_mut`] instead, which will
1750 /// give references to arrays of exactly that length, rather than slices.
1751 ///
1752 /// # Panics
1753 ///
1754 /// Panics if `chunk_size` is zero.
1755 ///
1756 /// # Examples
1757 ///
1758 /// ```
1759 /// let v = &mut [0, 0, 0, 0, 0];
1760 /// let mut count = 1;
1761 ///
1762 /// for chunk in v.rchunks_mut(2) {
1763 /// for elem in chunk.iter_mut() {
1764 /// *elem += count;
1765 /// }
1766 /// count += 1;
1767 /// }
1768 /// assert_eq!(v, &[3, 2, 2, 1, 1]);
1769 /// ```
1770 ///
1771 /// [`rchunks_exact_mut`]: slice::rchunks_exact_mut
1772 /// [`chunks_mut`]: slice::chunks_mut
1773 /// [`as_rchunks_mut`]: slice::as_rchunks_mut
1774 #[stable(feature = "rchunks", since = "1.31.0")]
1775 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
1776 #[inline]
1777 #[track_caller]
1778 #[cfg(not(feature = "ferrocene_subset"))]
1779 pub const fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> {
1780 assert!(chunk_size != 0, "chunk size must be non-zero");
1781 RChunksMut::new(self, chunk_size)
1782 }
1783
1784 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
1785 /// end of the slice.
1786 ///
1787 /// The chunks are slices and do not overlap. If `chunk_size` does not divide the length of the
1788 /// slice, then the last up to `chunk_size-1` elements will be omitted and can be retrieved
1789 /// from the `remainder` function of the iterator.
1790 ///
1791 /// Due to each chunk having exactly `chunk_size` elements, the compiler can often optimize the
1792 /// resulting code better than in the case of [`rchunks`].
1793 ///
1794 /// See [`rchunks`] for a variant of this iterator that also returns the remainder as a smaller
1795 /// chunk, and [`chunks_exact`] for the same iterator but starting at the beginning of the
1796 /// slice.
1797 ///
1798 /// If your `chunk_size` is a constant, consider using [`as_rchunks`] instead, which will
1799 /// give references to arrays of exactly that length, rather than slices.
1800 ///
1801 /// # Panics
1802 ///
1803 /// Panics if `chunk_size` is zero.
1804 ///
1805 /// # Examples
1806 ///
1807 /// ```
1808 /// let slice = ['l', 'o', 'r', 'e', 'm'];
1809 /// let mut iter = slice.rchunks_exact(2);
1810 /// assert_eq!(iter.next().unwrap(), &['e', 'm']);
1811 /// assert_eq!(iter.next().unwrap(), &['o', 'r']);
1812 /// assert!(iter.next().is_none());
1813 /// assert_eq!(iter.remainder(), &['l']);
1814 /// ```
1815 ///
1816 /// [`chunks`]: slice::chunks
1817 /// [`rchunks`]: slice::rchunks
1818 /// [`chunks_exact`]: slice::chunks_exact
1819 /// [`as_rchunks`]: slice::as_rchunks
1820 #[stable(feature = "rchunks", since = "1.31.0")]
1821 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
1822 #[inline]
1823 #[track_caller]
1824 #[cfg(not(feature = "ferrocene_subset"))]
1825 pub const fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> {
1826 assert!(chunk_size != 0, "chunk size must be non-zero");
1827 RChunksExact::new(self, chunk_size)
1828 }
1829
1830 /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end
1831 /// of the slice.
1832 ///
1833 /// The chunks are mutable slices, and do not overlap. If `chunk_size` does not divide the
1834 /// length of the slice, then the last up to `chunk_size-1` elements will be omitted and can be
1835 /// retrieved from the `into_remainder` function of the iterator.
1836 ///
1837 /// Due to each chunk having exactly `chunk_size` elements, the compiler can often optimize the
1838 /// resulting code better than in the case of [`chunks_mut`].
1839 ///
1840 /// See [`rchunks_mut`] for a variant of this iterator that also returns the remainder as a
1841 /// smaller chunk, and [`chunks_exact_mut`] for the same iterator but starting at the beginning
1842 /// of the slice.
1843 ///
1844 /// If your `chunk_size` is a constant, consider using [`as_rchunks_mut`] instead, which will
1845 /// give references to arrays of exactly that length, rather than slices.
1846 ///
1847 /// # Panics
1848 ///
1849 /// Panics if `chunk_size` is zero.
1850 ///
1851 /// # Examples
1852 ///
1853 /// ```
1854 /// let v = &mut [0, 0, 0, 0, 0];
1855 /// let mut count = 1;
1856 ///
1857 /// for chunk in v.rchunks_exact_mut(2) {
1858 /// for elem in chunk.iter_mut() {
1859 /// *elem += count;
1860 /// }
1861 /// count += 1;
1862 /// }
1863 /// assert_eq!(v, &[0, 2, 2, 1, 1]);
1864 /// ```
1865 ///
1866 /// [`chunks_mut`]: slice::chunks_mut
1867 /// [`rchunks_mut`]: slice::rchunks_mut
1868 /// [`chunks_exact_mut`]: slice::chunks_exact_mut
1869 /// [`as_rchunks_mut`]: slice::as_rchunks_mut
1870 #[stable(feature = "rchunks", since = "1.31.0")]
1871 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
1872 #[inline]
1873 #[track_caller]
1874 #[cfg(not(feature = "ferrocene_subset"))]
1875 pub const fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> {
1876 assert!(chunk_size != 0, "chunk size must be non-zero");
1877 RChunksExactMut::new(self, chunk_size)
1878 }
1879
1880 /// Returns an iterator over the slice producing non-overlapping runs
1881 /// of elements using the predicate to separate them.
1882 ///
1883 /// The predicate is called for every pair of consecutive elements,
1884 /// meaning that it is called on `slice[0]` and `slice[1]`,
1885 /// followed by `slice[1]` and `slice[2]`, and so on.
1886 ///
1887 /// # Examples
1888 ///
1889 /// ```
1890 /// let slice = &[1, 1, 1, 3, 3, 2, 2, 2];
1891 ///
1892 /// let mut iter = slice.chunk_by(|a, b| a == b);
1893 ///
1894 /// assert_eq!(iter.next(), Some(&[1, 1, 1][..]));
1895 /// assert_eq!(iter.next(), Some(&[3, 3][..]));
1896 /// assert_eq!(iter.next(), Some(&[2, 2, 2][..]));
1897 /// assert_eq!(iter.next(), None);
1898 /// ```
1899 ///
1900 /// This method can be used to extract the sorted subslices:
1901 ///
1902 /// ```
1903 /// let slice = &[1, 1, 2, 3, 2, 3, 2, 3, 4];
1904 ///
1905 /// let mut iter = slice.chunk_by(|a, b| a <= b);
1906 ///
1907 /// assert_eq!(iter.next(), Some(&[1, 1, 2, 3][..]));
1908 /// assert_eq!(iter.next(), Some(&[2, 3][..]));
1909 /// assert_eq!(iter.next(), Some(&[2, 3, 4][..]));
1910 /// assert_eq!(iter.next(), None);
1911 /// ```
1912 #[stable(feature = "slice_group_by", since = "1.77.0")]
1913 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
1914 #[inline]
1915 #[cfg(not(feature = "ferrocene_subset"))]
1916 pub const fn chunk_by<F>(&self, pred: F) -> ChunkBy<'_, T, F>
1917 where
1918 F: FnMut(&T, &T) -> bool,
1919 {
1920 ChunkBy::new(self, pred)
1921 }
1922
1923 /// Returns an iterator over the slice producing non-overlapping mutable
1924 /// runs of elements using the predicate to separate them.
1925 ///
1926 /// The predicate is called for every pair of consecutive elements,
1927 /// meaning that it is called on `slice[0]` and `slice[1]`,
1928 /// followed by `slice[1]` and `slice[2]`, and so on.
1929 ///
1930 /// # Examples
1931 ///
1932 /// ```
1933 /// let slice = &mut [1, 1, 1, 3, 3, 2, 2, 2];
1934 ///
1935 /// let mut iter = slice.chunk_by_mut(|a, b| a == b);
1936 ///
1937 /// assert_eq!(iter.next(), Some(&mut [1, 1, 1][..]));
1938 /// assert_eq!(iter.next(), Some(&mut [3, 3][..]));
1939 /// assert_eq!(iter.next(), Some(&mut [2, 2, 2][..]));
1940 /// assert_eq!(iter.next(), None);
1941 /// ```
1942 ///
1943 /// This method can be used to extract the sorted subslices:
1944 ///
1945 /// ```
1946 /// let slice = &mut [1, 1, 2, 3, 2, 3, 2, 3, 4];
1947 ///
1948 /// let mut iter = slice.chunk_by_mut(|a, b| a <= b);
1949 ///
1950 /// assert_eq!(iter.next(), Some(&mut [1, 1, 2, 3][..]));
1951 /// assert_eq!(iter.next(), Some(&mut [2, 3][..]));
1952 /// assert_eq!(iter.next(), Some(&mut [2, 3, 4][..]));
1953 /// assert_eq!(iter.next(), None);
1954 /// ```
1955 #[stable(feature = "slice_group_by", since = "1.77.0")]
1956 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
1957 #[inline]
1958 #[cfg(not(feature = "ferrocene_subset"))]
1959 pub const fn chunk_by_mut<F>(&mut self, pred: F) -> ChunkByMut<'_, T, F>
1960 where
1961 F: FnMut(&T, &T) -> bool,
1962 {
1963 ChunkByMut::new(self, pred)
1964 }
1965
1966 /// Divides one slice into two at an index.
1967 ///
1968 /// The first will contain all indices from `[0, mid)` (excluding
1969 /// the index `mid` itself) and the second will contain all
1970 /// indices from `[mid, len)` (excluding the index `len` itself).
1971 ///
1972 /// # Panics
1973 ///
1974 /// Panics if `mid > len`. For a non-panicking alternative see
1975 /// [`split_at_checked`](slice::split_at_checked).
1976 ///
1977 /// # Examples
1978 ///
1979 /// ```
1980 /// let v = ['a', 'b', 'c'];
1981 ///
1982 /// {
1983 /// let (left, right) = v.split_at(0);
1984 /// assert_eq!(left, []);
1985 /// assert_eq!(right, ['a', 'b', 'c']);
1986 /// }
1987 ///
1988 /// {
1989 /// let (left, right) = v.split_at(2);
1990 /// assert_eq!(left, ['a', 'b']);
1991 /// assert_eq!(right, ['c']);
1992 /// }
1993 ///
1994 /// {
1995 /// let (left, right) = v.split_at(3);
1996 /// assert_eq!(left, ['a', 'b', 'c']);
1997 /// assert_eq!(right, []);
1998 /// }
1999 /// ```
2000 #[stable(feature = "rust1", since = "1.0.0")]
2001 #[rustc_const_stable(feature = "const_slice_split_at_not_mut", since = "1.71.0")]
2002 #[inline]
2003 #[track_caller]
2004 #[must_use]
2005 pub const fn split_at(&self, mid: usize) -> (&[T], &[T]) {
2006 match self.split_at_checked(mid) {
2007 Some(pair) => pair,
2008 None => panic!("mid > len"),
2009 }
2010 }
2011
2012 /// Divides one mutable slice into two at an index.
2013 ///
2014 /// The first will contain all indices from `[0, mid)` (excluding
2015 /// the index `mid` itself) and the second will contain all
2016 /// indices from `[mid, len)` (excluding the index `len` itself).
2017 ///
2018 /// # Panics
2019 ///
2020 /// Panics if `mid > len`. For a non-panicking alternative see
2021 /// [`split_at_mut_checked`](slice::split_at_mut_checked).
2022 ///
2023 /// # Examples
2024 ///
2025 /// ```
2026 /// let mut v = [1, 0, 3, 0, 5, 6];
2027 /// let (left, right) = v.split_at_mut(2);
2028 /// assert_eq!(left, [1, 0]);
2029 /// assert_eq!(right, [3, 0, 5, 6]);
2030 /// left[1] = 2;
2031 /// right[1] = 4;
2032 /// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
2033 /// ```
2034 #[stable(feature = "rust1", since = "1.0.0")]
2035 #[inline]
2036 #[track_caller]
2037 #[must_use]
2038 #[rustc_const_stable(feature = "const_slice_split_at_mut", since = "1.83.0")]
2039 pub const fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
2040 match self.split_at_mut_checked(mid) {
2041 Some(pair) => pair,
2042 None => panic!("mid > len"),
2043 }
2044 }
2045
2046 /// Divides one slice into two at an index, without doing bounds checking.
2047 ///
2048 /// The first will contain all indices from `[0, mid)` (excluding
2049 /// the index `mid` itself) and the second will contain all
2050 /// indices from `[mid, len)` (excluding the index `len` itself).
2051 ///
2052 /// For a safe alternative see [`split_at`].
2053 ///
2054 /// # Safety
2055 ///
2056 /// Calling this method with an out-of-bounds index is *[undefined behavior]*
2057 /// even if the resulting reference is not used. The caller has to ensure that
2058 /// `0 <= mid <= self.len()`.
2059 ///
2060 /// [`split_at`]: slice::split_at
2061 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
2062 ///
2063 /// # Examples
2064 ///
2065 /// ```
2066 /// let v = ['a', 'b', 'c'];
2067 ///
2068 /// unsafe {
2069 /// let (left, right) = v.split_at_unchecked(0);
2070 /// assert_eq!(left, []);
2071 /// assert_eq!(right, ['a', 'b', 'c']);
2072 /// }
2073 ///
2074 /// unsafe {
2075 /// let (left, right) = v.split_at_unchecked(2);
2076 /// assert_eq!(left, ['a', 'b']);
2077 /// assert_eq!(right, ['c']);
2078 /// }
2079 ///
2080 /// unsafe {
2081 /// let (left, right) = v.split_at_unchecked(3);
2082 /// assert_eq!(left, ['a', 'b', 'c']);
2083 /// assert_eq!(right, []);
2084 /// }
2085 /// ```
2086 #[stable(feature = "slice_split_at_unchecked", since = "1.79.0")]
2087 #[rustc_const_stable(feature = "const_slice_split_at_unchecked", since = "1.77.0")]
2088 #[inline]
2089 #[must_use]
2090 #[track_caller]
2091 pub const unsafe fn split_at_unchecked(&self, mid: usize) -> (&[T], &[T]) {
2092 // FIXME(const-hack): the const function `from_raw_parts` is used to make this
2093 // function const; previously the implementation used
2094 // `(self.get_unchecked(..mid), self.get_unchecked(mid..))`
2095
2096 let len = self.len();
2097 let ptr = self.as_ptr();
2098
2099 assert_unsafe_precondition!(
2100 check_library_ub,
2101 "slice::split_at_unchecked requires the index to be within the slice",
2102 (mid: usize = mid, len: usize = len) => mid <= len,
2103 );
2104
2105 // SAFETY: Caller has to check that `0 <= mid <= self.len()`
2106 unsafe { (from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), unchecked_sub(len, mid))) }
2107 }
2108
2109 /// Divides one mutable slice into two at an index, without doing bounds checking.
2110 ///
2111 /// The first will contain all indices from `[0, mid)` (excluding
2112 /// the index `mid` itself) and the second will contain all
2113 /// indices from `[mid, len)` (excluding the index `len` itself).
2114 ///
2115 /// For a safe alternative see [`split_at_mut`].
2116 ///
2117 /// # Safety
2118 ///
2119 /// Calling this method with an out-of-bounds index is *[undefined behavior]*
2120 /// even if the resulting reference is not used. The caller has to ensure that
2121 /// `0 <= mid <= self.len()`.
2122 ///
2123 /// [`split_at_mut`]: slice::split_at_mut
2124 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
2125 ///
2126 /// # Examples
2127 ///
2128 /// ```
2129 /// let mut v = [1, 0, 3, 0, 5, 6];
2130 /// // scoped to restrict the lifetime of the borrows
2131 /// unsafe {
2132 /// let (left, right) = v.split_at_mut_unchecked(2);
2133 /// assert_eq!(left, [1, 0]);
2134 /// assert_eq!(right, [3, 0, 5, 6]);
2135 /// left[1] = 2;
2136 /// right[1] = 4;
2137 /// }
2138 /// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
2139 /// ```
2140 #[stable(feature = "slice_split_at_unchecked", since = "1.79.0")]
2141 #[rustc_const_stable(feature = "const_slice_split_at_mut", since = "1.83.0")]
2142 #[inline]
2143 #[must_use]
2144 #[track_caller]
2145 pub const unsafe fn split_at_mut_unchecked(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
2146 let len = self.len();
2147 let ptr = self.as_mut_ptr();
2148
2149 assert_unsafe_precondition!(
2150 check_library_ub,
2151 "slice::split_at_mut_unchecked requires the index to be within the slice",
2152 (mid: usize = mid, len: usize = len) => mid <= len,
2153 );
2154
2155 // SAFETY: Caller has to check that `0 <= mid <= self.len()`.
2156 //
2157 // `[ptr; mid]` and `[mid; len]` are not overlapping, so returning a mutable reference
2158 // is fine.
2159 unsafe {
2160 (
2161 from_raw_parts_mut(ptr, mid),
2162 from_raw_parts_mut(ptr.add(mid), unchecked_sub(len, mid)),
2163 )
2164 }
2165 }
2166
2167 /// Divides one slice into two at an index, returning `None` if the slice is
2168 /// too short.
2169 ///
2170 /// If `mid ≤ len` returns a pair of slices where the first will contain all
2171 /// indices from `[0, mid)` (excluding the index `mid` itself) and the
2172 /// second will contain all indices from `[mid, len)` (excluding the index
2173 /// `len` itself).
2174 ///
2175 /// Otherwise, if `mid > len`, returns `None`.
2176 ///
2177 /// # Examples
2178 ///
2179 /// ```
2180 /// let v = [1, -2, 3, -4, 5, -6];
2181 ///
2182 /// {
2183 /// let (left, right) = v.split_at_checked(0).unwrap();
2184 /// assert_eq!(left, []);
2185 /// assert_eq!(right, [1, -2, 3, -4, 5, -6]);
2186 /// }
2187 ///
2188 /// {
2189 /// let (left, right) = v.split_at_checked(2).unwrap();
2190 /// assert_eq!(left, [1, -2]);
2191 /// assert_eq!(right, [3, -4, 5, -6]);
2192 /// }
2193 ///
2194 /// {
2195 /// let (left, right) = v.split_at_checked(6).unwrap();
2196 /// assert_eq!(left, [1, -2, 3, -4, 5, -6]);
2197 /// assert_eq!(right, []);
2198 /// }
2199 ///
2200 /// assert_eq!(None, v.split_at_checked(7));
2201 /// ```
2202 #[stable(feature = "split_at_checked", since = "1.80.0")]
2203 #[rustc_const_stable(feature = "split_at_checked", since = "1.80.0")]
2204 #[inline]
2205 #[must_use]
2206 pub const fn split_at_checked(&self, mid: usize) -> Option<(&[T], &[T])> {
2207 if mid <= self.len() {
2208 // SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
2209 // fulfills the requirements of `split_at_unchecked`.
2210 Some(unsafe { self.split_at_unchecked(mid) })
2211 } else {
2212 None
2213 }
2214 }
2215
2216 /// Divides one mutable slice into two at an index, returning `None` if the
2217 /// slice is too short.
2218 ///
2219 /// If `mid ≤ len` returns a pair of slices where the first will contain all
2220 /// indices from `[0, mid)` (excluding the index `mid` itself) and the
2221 /// second will contain all indices from `[mid, len)` (excluding the index
2222 /// `len` itself).
2223 ///
2224 /// Otherwise, if `mid > len`, returns `None`.
2225 ///
2226 /// # Examples
2227 ///
2228 /// ```
2229 /// let mut v = [1, 0, 3, 0, 5, 6];
2230 ///
2231 /// if let Some((left, right)) = v.split_at_mut_checked(2) {
2232 /// assert_eq!(left, [1, 0]);
2233 /// assert_eq!(right, [3, 0, 5, 6]);
2234 /// left[1] = 2;
2235 /// right[1] = 4;
2236 /// }
2237 /// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
2238 ///
2239 /// assert_eq!(None, v.split_at_mut_checked(7));
2240 /// ```
2241 #[stable(feature = "split_at_checked", since = "1.80.0")]
2242 #[rustc_const_stable(feature = "const_slice_split_at_mut", since = "1.83.0")]
2243 #[inline]
2244 #[must_use]
2245 pub const fn split_at_mut_checked(&mut self, mid: usize) -> Option<(&mut [T], &mut [T])> {
2246 if mid <= self.len() {
2247 // SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
2248 // fulfills the requirements of `split_at_unchecked`.
2249 Some(unsafe { self.split_at_mut_unchecked(mid) })
2250 } else {
2251 None
2252 }
2253 }
2254
2255 /// Returns an iterator over subslices separated by elements that match
2256 /// `pred`. The matched element is not contained in the subslices.
2257 ///
2258 /// # Examples
2259 ///
2260 /// ```
2261 /// let slice = [10, 40, 33, 20];
2262 /// let mut iter = slice.split(|num| num % 3 == 0);
2263 ///
2264 /// assert_eq!(iter.next().unwrap(), &[10, 40]);
2265 /// assert_eq!(iter.next().unwrap(), &[20]);
2266 /// assert!(iter.next().is_none());
2267 /// ```
2268 ///
2269 /// If the first element is matched, an empty slice will be the first item
2270 /// returned by the iterator. Similarly, if the last element in the slice
2271 /// is matched, an empty slice will be the last item returned by the
2272 /// iterator:
2273 ///
2274 /// ```
2275 /// let slice = [10, 40, 33];
2276 /// let mut iter = slice.split(|num| num % 3 == 0);
2277 ///
2278 /// assert_eq!(iter.next().unwrap(), &[10, 40]);
2279 /// assert_eq!(iter.next().unwrap(), &[]);
2280 /// assert!(iter.next().is_none());
2281 /// ```
2282 ///
2283 /// If two matched elements are directly adjacent, an empty slice will be
2284 /// present between them:
2285 ///
2286 /// ```
2287 /// let slice = [10, 6, 33, 20];
2288 /// let mut iter = slice.split(|num| num % 3 == 0);
2289 ///
2290 /// assert_eq!(iter.next().unwrap(), &[10]);
2291 /// assert_eq!(iter.next().unwrap(), &[]);
2292 /// assert_eq!(iter.next().unwrap(), &[20]);
2293 /// assert!(iter.next().is_none());
2294 /// ```
2295 #[stable(feature = "rust1", since = "1.0.0")]
2296 #[inline]
2297 #[cfg(not(feature = "ferrocene_subset"))]
2298 pub fn split<F>(&self, pred: F) -> Split<'_, T, F>
2299 where
2300 F: FnMut(&T) -> bool,
2301 {
2302 Split::new(self, pred)
2303 }
2304
2305 /// Returns an iterator over mutable subslices separated by elements that
2306 /// match `pred`. The matched element is not contained in the subslices.
2307 ///
2308 /// # Examples
2309 ///
2310 /// ```
2311 /// let mut v = [10, 40, 30, 20, 60, 50];
2312 ///
2313 /// for group in v.split_mut(|num| *num % 3 == 0) {
2314 /// group[0] = 1;
2315 /// }
2316 /// assert_eq!(v, [1, 40, 30, 1, 60, 1]);
2317 /// ```
2318 #[stable(feature = "rust1", since = "1.0.0")]
2319 #[inline]
2320 #[cfg(not(feature = "ferrocene_subset"))]
2321 pub fn split_mut<F>(&mut self, pred: F) -> SplitMut<'_, T, F>
2322 where
2323 F: FnMut(&T) -> bool,
2324 {
2325 SplitMut::new(self, pred)
2326 }
2327
2328 /// Returns an iterator over subslices separated by elements that match
2329 /// `pred`. The matched element is contained in the end of the previous
2330 /// subslice as a terminator.
2331 ///
2332 /// # Examples
2333 ///
2334 /// ```
2335 /// let slice = [10, 40, 33, 20];
2336 /// let mut iter = slice.split_inclusive(|num| num % 3 == 0);
2337 ///
2338 /// assert_eq!(iter.next().unwrap(), &[10, 40, 33]);
2339 /// assert_eq!(iter.next().unwrap(), &[20]);
2340 /// assert!(iter.next().is_none());
2341 /// ```
2342 ///
2343 /// If the last element of the slice is matched,
2344 /// that element will be considered the terminator of the preceding slice.
2345 /// That slice will be the last item returned by the iterator.
2346 ///
2347 /// ```
2348 /// let slice = [3, 10, 40, 33];
2349 /// let mut iter = slice.split_inclusive(|num| num % 3 == 0);
2350 ///
2351 /// assert_eq!(iter.next().unwrap(), &[3]);
2352 /// assert_eq!(iter.next().unwrap(), &[10, 40, 33]);
2353 /// assert!(iter.next().is_none());
2354 /// ```
2355 #[stable(feature = "split_inclusive", since = "1.51.0")]
2356 #[inline]
2357 #[cfg(not(feature = "ferrocene_subset"))]
2358 pub fn split_inclusive<F>(&self, pred: F) -> SplitInclusive<'_, T, F>
2359 where
2360 F: FnMut(&T) -> bool,
2361 {
2362 SplitInclusive::new(self, pred)
2363 }
2364
2365 /// Returns an iterator over mutable subslices separated by elements that
2366 /// match `pred`. The matched element is contained in the previous
2367 /// subslice as a terminator.
2368 ///
2369 /// # Examples
2370 ///
2371 /// ```
2372 /// let mut v = [10, 40, 30, 20, 60, 50];
2373 ///
2374 /// for group in v.split_inclusive_mut(|num| *num % 3 == 0) {
2375 /// let terminator_idx = group.len()-1;
2376 /// group[terminator_idx] = 1;
2377 /// }
2378 /// assert_eq!(v, [10, 40, 1, 20, 1, 1]);
2379 /// ```
2380 #[stable(feature = "split_inclusive", since = "1.51.0")]
2381 #[inline]
2382 #[cfg(not(feature = "ferrocene_subset"))]
2383 pub fn split_inclusive_mut<F>(&mut self, pred: F) -> SplitInclusiveMut<'_, T, F>
2384 where
2385 F: FnMut(&T) -> bool,
2386 {
2387 SplitInclusiveMut::new(self, pred)
2388 }
2389
2390 /// Returns an iterator over subslices separated by elements that match
2391 /// `pred`, starting at the end of the slice and working backwards.
2392 /// The matched element is not contained in the subslices.
2393 ///
2394 /// # Examples
2395 ///
2396 /// ```
2397 /// let slice = [11, 22, 33, 0, 44, 55];
2398 /// let mut iter = slice.rsplit(|num| *num == 0);
2399 ///
2400 /// assert_eq!(iter.next().unwrap(), &[44, 55]);
2401 /// assert_eq!(iter.next().unwrap(), &[11, 22, 33]);
2402 /// assert_eq!(iter.next(), None);
2403 /// ```
2404 ///
2405 /// As with `split()`, if the first or last element is matched, an empty
2406 /// slice will be the first (or last) item returned by the iterator.
2407 ///
2408 /// ```
2409 /// let v = &[0, 1, 1, 2, 3, 5, 8];
2410 /// let mut it = v.rsplit(|n| *n % 2 == 0);
2411 /// assert_eq!(it.next().unwrap(), &[]);
2412 /// assert_eq!(it.next().unwrap(), &[3, 5]);
2413 /// assert_eq!(it.next().unwrap(), &[1, 1]);
2414 /// assert_eq!(it.next().unwrap(), &[]);
2415 /// assert_eq!(it.next(), None);
2416 /// ```
2417 #[stable(feature = "slice_rsplit", since = "1.27.0")]
2418 #[inline]
2419 #[cfg(not(feature = "ferrocene_subset"))]
2420 pub fn rsplit<F>(&self, pred: F) -> RSplit<'_, T, F>
2421 where
2422 F: FnMut(&T) -> bool,
2423 {
2424 RSplit::new(self, pred)
2425 }
2426
2427 /// Returns an iterator over mutable subslices separated by elements that
2428 /// match `pred`, starting at the end of the slice and working
2429 /// backwards. The matched element is not contained in the subslices.
2430 ///
2431 /// # Examples
2432 ///
2433 /// ```
2434 /// let mut v = [100, 400, 300, 200, 600, 500];
2435 ///
2436 /// let mut count = 0;
2437 /// for group in v.rsplit_mut(|num| *num % 3 == 0) {
2438 /// count += 1;
2439 /// group[0] = count;
2440 /// }
2441 /// assert_eq!(v, [3, 400, 300, 2, 600, 1]);
2442 /// ```
2443 ///
2444 #[stable(feature = "slice_rsplit", since = "1.27.0")]
2445 #[inline]
2446 #[cfg(not(feature = "ferrocene_subset"))]
2447 pub fn rsplit_mut<F>(&mut self, pred: F) -> RSplitMut<'_, T, F>
2448 where
2449 F: FnMut(&T) -> bool,
2450 {
2451 RSplitMut::new(self, pred)
2452 }
2453
2454 /// Returns an iterator over subslices separated by elements that match
2455 /// `pred`, limited to returning at most `n` items. The matched element is
2456 /// not contained in the subslices.
2457 ///
2458 /// The last element returned, if any, will contain the remainder of the
2459 /// slice.
2460 ///
2461 /// # Examples
2462 ///
2463 /// Print the slice split once by numbers divisible by 3 (i.e., `[10, 40]`,
2464 /// `[20, 60, 50]`):
2465 ///
2466 /// ```
2467 /// let v = [10, 40, 30, 20, 60, 50];
2468 ///
2469 /// for group in v.splitn(2, |num| *num % 3 == 0) {
2470 /// println!("{group:?}");
2471 /// }
2472 /// ```
2473 #[stable(feature = "rust1", since = "1.0.0")]
2474 #[inline]
2475 #[cfg(not(feature = "ferrocene_subset"))]
2476 pub fn splitn<F>(&self, n: usize, pred: F) -> SplitN<'_, T, F>
2477 where
2478 F: FnMut(&T) -> bool,
2479 {
2480 SplitN::new(self.split(pred), n)
2481 }
2482
2483 /// Returns an iterator over mutable subslices separated by elements that match
2484 /// `pred`, limited to returning at most `n` items. The matched element is
2485 /// not contained in the subslices.
2486 ///
2487 /// The last element returned, if any, will contain the remainder of the
2488 /// slice.
2489 ///
2490 /// # Examples
2491 ///
2492 /// ```
2493 /// let mut v = [10, 40, 30, 20, 60, 50];
2494 ///
2495 /// for group in v.splitn_mut(2, |num| *num % 3 == 0) {
2496 /// group[0] = 1;
2497 /// }
2498 /// assert_eq!(v, [1, 40, 30, 1, 60, 50]);
2499 /// ```
2500 #[stable(feature = "rust1", since = "1.0.0")]
2501 #[inline]
2502 #[cfg(not(feature = "ferrocene_subset"))]
2503 pub fn splitn_mut<F>(&mut self, n: usize, pred: F) -> SplitNMut<'_, T, F>
2504 where
2505 F: FnMut(&T) -> bool,
2506 {
2507 SplitNMut::new(self.split_mut(pred), n)
2508 }
2509
2510 /// Returns an iterator over subslices separated by elements that match
2511 /// `pred` limited to returning at most `n` items. This starts at the end of
2512 /// the slice and works backwards. The matched element is not contained in
2513 /// the subslices.
2514 ///
2515 /// The last element returned, if any, will contain the remainder of the
2516 /// slice.
2517 ///
2518 /// # Examples
2519 ///
2520 /// Print the slice split once, starting from the end, by numbers divisible
2521 /// by 3 (i.e., `[50]`, `[10, 40, 30, 20]`):
2522 ///
2523 /// ```
2524 /// let v = [10, 40, 30, 20, 60, 50];
2525 ///
2526 /// for group in v.rsplitn(2, |num| *num % 3 == 0) {
2527 /// println!("{group:?}");
2528 /// }
2529 /// ```
2530 #[stable(feature = "rust1", since = "1.0.0")]
2531 #[inline]
2532 #[cfg(not(feature = "ferrocene_subset"))]
2533 pub fn rsplitn<F>(&self, n: usize, pred: F) -> RSplitN<'_, T, F>
2534 where
2535 F: FnMut(&T) -> bool,
2536 {
2537 RSplitN::new(self.rsplit(pred), n)
2538 }
2539
2540 /// Returns an iterator over subslices separated by elements that match
2541 /// `pred` limited to returning at most `n` items. This starts at the end of
2542 /// the slice and works backwards. The matched element is not contained in
2543 /// the subslices.
2544 ///
2545 /// The last element returned, if any, will contain the remainder of the
2546 /// slice.
2547 ///
2548 /// # Examples
2549 ///
2550 /// ```
2551 /// let mut s = [10, 40, 30, 20, 60, 50];
2552 ///
2553 /// for group in s.rsplitn_mut(2, |num| *num % 3 == 0) {
2554 /// group[0] = 1;
2555 /// }
2556 /// assert_eq!(s, [1, 40, 30, 20, 60, 1]);
2557 /// ```
2558 #[stable(feature = "rust1", since = "1.0.0")]
2559 #[inline]
2560 #[cfg(not(feature = "ferrocene_subset"))]
2561 pub fn rsplitn_mut<F>(&mut self, n: usize, pred: F) -> RSplitNMut<'_, T, F>
2562 where
2563 F: FnMut(&T) -> bool,
2564 {
2565 RSplitNMut::new(self.rsplit_mut(pred), n)
2566 }
2567
2568 /// Splits the slice on the first element that matches the specified
2569 /// predicate.
2570 ///
2571 /// If any matching elements are present in the slice, returns the prefix
2572 /// before the match and suffix after. The matching element itself is not
2573 /// included. If no elements match, returns `None`.
2574 ///
2575 /// # Examples
2576 ///
2577 /// ```
2578 /// #![feature(slice_split_once)]
2579 /// let s = [1, 2, 3, 2, 4];
2580 /// assert_eq!(s.split_once(|&x| x == 2), Some((
2581 /// &[1][..],
2582 /// &[3, 2, 4][..]
2583 /// )));
2584 /// assert_eq!(s.split_once(|&x| x == 0), None);
2585 /// ```
2586 #[unstable(feature = "slice_split_once", issue = "112811")]
2587 #[inline]
2588 #[cfg(not(feature = "ferrocene_subset"))]
2589 pub fn split_once<F>(&self, pred: F) -> Option<(&[T], &[T])>
2590 where
2591 F: FnMut(&T) -> bool,
2592 {
2593 let index = self.iter().position(pred)?;
2594 Some((&self[..index], &self[index + 1..]))
2595 }
2596
2597 /// Splits the slice on the last element that matches the specified
2598 /// predicate.
2599 ///
2600 /// If any matching elements are present in the slice, returns the prefix
2601 /// before the match and suffix after. The matching element itself is not
2602 /// included. If no elements match, returns `None`.
2603 ///
2604 /// # Examples
2605 ///
2606 /// ```
2607 /// #![feature(slice_split_once)]
2608 /// let s = [1, 2, 3, 2, 4];
2609 /// assert_eq!(s.rsplit_once(|&x| x == 2), Some((
2610 /// &[1, 2, 3][..],
2611 /// &[4][..]
2612 /// )));
2613 /// assert_eq!(s.rsplit_once(|&x| x == 0), None);
2614 /// ```
2615 #[unstable(feature = "slice_split_once", issue = "112811")]
2616 #[inline]
2617 #[cfg(not(feature = "ferrocene_subset"))]
2618 pub fn rsplit_once<F>(&self, pred: F) -> Option<(&[T], &[T])>
2619 where
2620 F: FnMut(&T) -> bool,
2621 {
2622 let index = self.iter().rposition(pred)?;
2623 Some((&self[..index], &self[index + 1..]))
2624 }
2625
2626 /// Returns `true` if the slice contains an element with the given value.
2627 ///
2628 /// This operation is *O*(*n*).
2629 ///
2630 /// Note that if you have a sorted slice, [`binary_search`] may be faster.
2631 ///
2632 /// [`binary_search`]: slice::binary_search
2633 ///
2634 /// # Examples
2635 ///
2636 /// ```
2637 /// let v = [10, 40, 30];
2638 /// assert!(v.contains(&30));
2639 /// assert!(!v.contains(&50));
2640 /// ```
2641 ///
2642 /// If you do not have a `&T`, but some other value that you can compare
2643 /// with one (for example, `String` implements `PartialEq<str>`), you can
2644 /// use `iter().any`:
2645 ///
2646 /// ```
2647 /// let v = [String::from("hello"), String::from("world")]; // slice of `String`
2648 /// assert!(v.iter().any(|e| e == "hello")); // search with `&str`
2649 /// assert!(!v.iter().any(|e| e == "hi"));
2650 /// ```
2651 #[stable(feature = "rust1", since = "1.0.0")]
2652 #[inline]
2653 #[must_use]
2654 #[cfg(not(feature = "ferrocene_subset"))]
2655 pub fn contains(&self, x: &T) -> bool
2656 where
2657 T: PartialEq,
2658 {
2659 cmp::SliceContains::slice_contains(x, self)
2660 }
2661
2662 /// Returns `true` if `needle` is a prefix of the slice or equal to the slice.
2663 ///
2664 /// # Examples
2665 ///
2666 /// ```
2667 /// let v = [10, 40, 30];
2668 /// assert!(v.starts_with(&[10]));
2669 /// assert!(v.starts_with(&[10, 40]));
2670 /// assert!(v.starts_with(&v));
2671 /// assert!(!v.starts_with(&[50]));
2672 /// assert!(!v.starts_with(&[10, 50]));
2673 /// ```
2674 ///
2675 /// Always returns `true` if `needle` is an empty slice:
2676 ///
2677 /// ```
2678 /// let v = &[10, 40, 30];
2679 /// assert!(v.starts_with(&[]));
2680 /// let v: &[u8] = &[];
2681 /// assert!(v.starts_with(&[]));
2682 /// ```
2683 #[stable(feature = "rust1", since = "1.0.0")]
2684 #[must_use]
2685 pub fn starts_with(&self, needle: &[T]) -> bool
2686 where
2687 T: PartialEq,
2688 {
2689 let n = needle.len();
2690 self.len() >= n && needle == &self[..n]
2691 }
2692
2693 /// Returns `true` if `needle` is a suffix of the slice or equal to the slice.
2694 ///
2695 /// # Examples
2696 ///
2697 /// ```
2698 /// let v = [10, 40, 30];
2699 /// assert!(v.ends_with(&[30]));
2700 /// assert!(v.ends_with(&[40, 30]));
2701 /// assert!(v.ends_with(&v));
2702 /// assert!(!v.ends_with(&[50]));
2703 /// assert!(!v.ends_with(&[50, 30]));
2704 /// ```
2705 ///
2706 /// Always returns `true` if `needle` is an empty slice:
2707 ///
2708 /// ```
2709 /// let v = &[10, 40, 30];
2710 /// assert!(v.ends_with(&[]));
2711 /// let v: &[u8] = &[];
2712 /// assert!(v.ends_with(&[]));
2713 /// ```
2714 #[stable(feature = "rust1", since = "1.0.0")]
2715 #[must_use]
2716 pub fn ends_with(&self, needle: &[T]) -> bool
2717 where
2718 T: PartialEq,
2719 {
2720 let (m, n) = (self.len(), needle.len());
2721 m >= n && needle == &self[m - n..]
2722 }
2723
2724 /// Returns a subslice with the prefix removed.
2725 ///
2726 /// If the slice starts with `prefix`, returns the subslice after the prefix, wrapped in `Some`.
2727 /// If `prefix` is empty, simply returns the original slice. If `prefix` is equal to the
2728 /// original slice, returns an empty slice.
2729 ///
2730 /// If the slice does not start with `prefix`, returns `None`.
2731 ///
2732 /// # Examples
2733 ///
2734 /// ```
2735 /// let v = &[10, 40, 30];
2736 /// assert_eq!(v.strip_prefix(&[10]), Some(&[40, 30][..]));
2737 /// assert_eq!(v.strip_prefix(&[10, 40]), Some(&[30][..]));
2738 /// assert_eq!(v.strip_prefix(&[10, 40, 30]), Some(&[][..]));
2739 /// assert_eq!(v.strip_prefix(&[50]), None);
2740 /// assert_eq!(v.strip_prefix(&[10, 50]), None);
2741 ///
2742 /// let prefix : &str = "he";
2743 /// assert_eq!(b"hello".strip_prefix(prefix.as_bytes()),
2744 /// Some(b"llo".as_ref()));
2745 /// ```
2746 #[must_use = "returns the subslice without modifying the original"]
2747 #[stable(feature = "slice_strip", since = "1.51.0")]
2748 #[cfg(not(feature = "ferrocene_subset"))]
2749 pub fn strip_prefix<P: SlicePattern<Item = T> + ?Sized>(&self, prefix: &P) -> Option<&[T]>
2750 where
2751 T: PartialEq,
2752 {
2753 // This function will need rewriting if and when SlicePattern becomes more sophisticated.
2754 let prefix = prefix.as_slice();
2755 let n = prefix.len();
2756 if n <= self.len() {
2757 let (head, tail) = self.split_at(n);
2758 if head == prefix {
2759 return Some(tail);
2760 }
2761 }
2762 None
2763 }
2764
2765 /// Returns a subslice with the suffix removed.
2766 ///
2767 /// If the slice ends with `suffix`, returns the subslice before the suffix, wrapped in `Some`.
2768 /// If `suffix` is empty, simply returns the original slice. If `suffix` is equal to the
2769 /// original slice, returns an empty slice.
2770 ///
2771 /// If the slice does not end with `suffix`, returns `None`.
2772 ///
2773 /// # Examples
2774 ///
2775 /// ```
2776 /// let v = &[10, 40, 30];
2777 /// assert_eq!(v.strip_suffix(&[30]), Some(&[10, 40][..]));
2778 /// assert_eq!(v.strip_suffix(&[40, 30]), Some(&[10][..]));
2779 /// assert_eq!(v.strip_suffix(&[10, 40, 30]), Some(&[][..]));
2780 /// assert_eq!(v.strip_suffix(&[50]), None);
2781 /// assert_eq!(v.strip_suffix(&[50, 30]), None);
2782 /// ```
2783 #[must_use = "returns the subslice without modifying the original"]
2784 #[stable(feature = "slice_strip", since = "1.51.0")]
2785 #[cfg(not(feature = "ferrocene_subset"))]
2786 pub fn strip_suffix<P: SlicePattern<Item = T> + ?Sized>(&self, suffix: &P) -> Option<&[T]>
2787 where
2788 T: PartialEq,
2789 {
2790 // This function will need rewriting if and when SlicePattern becomes more sophisticated.
2791 let suffix = suffix.as_slice();
2792 let (len, n) = (self.len(), suffix.len());
2793 if n <= len {
2794 let (head, tail) = self.split_at(len - n);
2795 if tail == suffix {
2796 return Some(head);
2797 }
2798 }
2799 None
2800 }
2801
2802 /// Returns a subslice with the prefix and suffix removed.
2803 ///
2804 /// If the slice starts with `prefix` and ends with `suffix`, returns the subslice after the
2805 /// prefix and before the suffix, wrapped in `Some`.
2806 ///
2807 /// If the slice does not start with `prefix` or does not end with `suffix`, returns `None`.
2808 ///
2809 /// # Examples
2810 ///
2811 /// ```
2812 /// #![feature(strip_circumfix)]
2813 ///
2814 /// let v = &[10, 50, 40, 30];
2815 /// assert_eq!(v.strip_circumfix(&[10], &[30]), Some(&[50, 40][..]));
2816 /// assert_eq!(v.strip_circumfix(&[10], &[40, 30]), Some(&[50][..]));
2817 /// assert_eq!(v.strip_circumfix(&[10, 50], &[40, 30]), Some(&[][..]));
2818 /// assert_eq!(v.strip_circumfix(&[50], &[30]), None);
2819 /// assert_eq!(v.strip_circumfix(&[10], &[40]), None);
2820 /// assert_eq!(v.strip_circumfix(&[], &[40, 30]), Some(&[10, 50][..]));
2821 /// assert_eq!(v.strip_circumfix(&[10, 50], &[]), Some(&[40, 30][..]));
2822 /// ```
2823 #[must_use = "returns the subslice without modifying the original"]
2824 #[unstable(feature = "strip_circumfix", issue = "147946")]
2825 #[cfg(not(feature = "ferrocene_subset"))]
2826 pub fn strip_circumfix<S, P>(&self, prefix: &P, suffix: &S) -> Option<&[T]>
2827 where
2828 T: PartialEq,
2829 S: SlicePattern<Item = T> + ?Sized,
2830 P: SlicePattern<Item = T> + ?Sized,
2831 {
2832 self.strip_prefix(prefix)?.strip_suffix(suffix)
2833 }
2834
2835 /// Returns a subslice with the optional prefix removed.
2836 ///
2837 /// If the slice starts with `prefix`, returns the subslice after the prefix. If `prefix`
2838 /// is empty or the slice does not start with `prefix`, simply returns the original slice.
2839 /// If `prefix` is equal to the original slice, returns an empty slice.
2840 ///
2841 /// # Examples
2842 ///
2843 /// ```
2844 /// #![feature(trim_prefix_suffix)]
2845 ///
2846 /// let v = &[10, 40, 30];
2847 ///
2848 /// // Prefix present - removes it
2849 /// assert_eq!(v.trim_prefix(&[10]), &[40, 30][..]);
2850 /// assert_eq!(v.trim_prefix(&[10, 40]), &[30][..]);
2851 /// assert_eq!(v.trim_prefix(&[10, 40, 30]), &[][..]);
2852 ///
2853 /// // Prefix absent - returns original slice
2854 /// assert_eq!(v.trim_prefix(&[50]), &[10, 40, 30][..]);
2855 /// assert_eq!(v.trim_prefix(&[10, 50]), &[10, 40, 30][..]);
2856 ///
2857 /// let prefix : &str = "he";
2858 /// assert_eq!(b"hello".trim_prefix(prefix.as_bytes()), b"llo".as_ref());
2859 /// ```
2860 #[must_use = "returns the subslice without modifying the original"]
2861 #[unstable(feature = "trim_prefix_suffix", issue = "142312")]
2862 #[cfg(not(feature = "ferrocene_subset"))]
2863 pub fn trim_prefix<P: SlicePattern<Item = T> + ?Sized>(&self, prefix: &P) -> &[T]
2864 where
2865 T: PartialEq,
2866 {
2867 // This function will need rewriting if and when SlicePattern becomes more sophisticated.
2868 let prefix = prefix.as_slice();
2869 let n = prefix.len();
2870 if n <= self.len() {
2871 let (head, tail) = self.split_at(n);
2872 if head == prefix {
2873 return tail;
2874 }
2875 }
2876 self
2877 }
2878
2879 /// Returns a subslice with the optional suffix removed.
2880 ///
2881 /// If the slice ends with `suffix`, returns the subslice before the suffix. If `suffix`
2882 /// is empty or the slice does not end with `suffix`, simply returns the original slice.
2883 /// If `suffix` is equal to the original slice, returns an empty slice.
2884 ///
2885 /// # Examples
2886 ///
2887 /// ```
2888 /// #![feature(trim_prefix_suffix)]
2889 ///
2890 /// let v = &[10, 40, 30];
2891 ///
2892 /// // Suffix present - removes it
2893 /// assert_eq!(v.trim_suffix(&[30]), &[10, 40][..]);
2894 /// assert_eq!(v.trim_suffix(&[40, 30]), &[10][..]);
2895 /// assert_eq!(v.trim_suffix(&[10, 40, 30]), &[][..]);
2896 ///
2897 /// // Suffix absent - returns original slice
2898 /// assert_eq!(v.trim_suffix(&[50]), &[10, 40, 30][..]);
2899 /// assert_eq!(v.trim_suffix(&[50, 30]), &[10, 40, 30][..]);
2900 /// ```
2901 #[must_use = "returns the subslice without modifying the original"]
2902 #[unstable(feature = "trim_prefix_suffix", issue = "142312")]
2903 #[cfg(not(feature = "ferrocene_subset"))]
2904 pub fn trim_suffix<P: SlicePattern<Item = T> + ?Sized>(&self, suffix: &P) -> &[T]
2905 where
2906 T: PartialEq,
2907 {
2908 // This function will need rewriting if and when SlicePattern becomes more sophisticated.
2909 let suffix = suffix.as_slice();
2910 let (len, n) = (self.len(), suffix.len());
2911 if n <= len {
2912 let (head, tail) = self.split_at(len - n);
2913 if tail == suffix {
2914 return head;
2915 }
2916 }
2917 self
2918 }
2919
2920 /// Binary searches this slice for a given element.
2921 /// If the slice is not sorted, the returned result is unspecified and
2922 /// meaningless.
2923 ///
2924 /// If the value is found then [`Result::Ok`] is returned, containing the
2925 /// index of the matching element. If there are multiple matches, then any
2926 /// one of the matches could be returned. The index is chosen
2927 /// deterministically, but is subject to change in future versions of Rust.
2928 /// If the value is not found then [`Result::Err`] is returned, containing
2929 /// the index where a matching element could be inserted while maintaining
2930 /// sorted order.
2931 ///
2932 /// See also [`binary_search_by`], [`binary_search_by_key`], and [`partition_point`].
2933 ///
2934 /// [`binary_search_by`]: slice::binary_search_by
2935 /// [`binary_search_by_key`]: slice::binary_search_by_key
2936 /// [`partition_point`]: slice::partition_point
2937 ///
2938 /// # Examples
2939 ///
2940 /// Looks up a series of four elements. The first is found, with a
2941 /// uniquely determined position; the second and third are not
2942 /// found; the fourth could match any position in `[1, 4]`.
2943 ///
2944 /// ```
2945 /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
2946 ///
2947 /// assert_eq!(s.binary_search(&13), Ok(9));
2948 /// assert_eq!(s.binary_search(&4), Err(7));
2949 /// assert_eq!(s.binary_search(&100), Err(13));
2950 /// let r = s.binary_search(&1);
2951 /// assert!(match r { Ok(1..=4) => true, _ => false, });
2952 /// ```
2953 ///
2954 /// If you want to find that whole *range* of matching items, rather than
2955 /// an arbitrary matching one, that can be done using [`partition_point`]:
2956 /// ```
2957 /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
2958 ///
2959 /// let low = s.partition_point(|x| x < &1);
2960 /// assert_eq!(low, 1);
2961 /// let high = s.partition_point(|x| x <= &1);
2962 /// assert_eq!(high, 5);
2963 /// let r = s.binary_search(&1);
2964 /// assert!((low..high).contains(&r.unwrap()));
2965 ///
2966 /// assert!(s[..low].iter().all(|&x| x < 1));
2967 /// assert!(s[low..high].iter().all(|&x| x == 1));
2968 /// assert!(s[high..].iter().all(|&x| x > 1));
2969 ///
2970 /// // For something not found, the "range" of equal items is empty
2971 /// assert_eq!(s.partition_point(|x| x < &11), 9);
2972 /// assert_eq!(s.partition_point(|x| x <= &11), 9);
2973 /// assert_eq!(s.binary_search(&11), Err(9));
2974 /// ```
2975 ///
2976 /// If you want to insert an item to a sorted vector, while maintaining
2977 /// sort order, consider using [`partition_point`]:
2978 ///
2979 /// ```
2980 /// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
2981 /// let num = 42;
2982 /// let idx = s.partition_point(|&x| x <= num);
2983 /// // If `num` is unique, `s.partition_point(|&x| x < num)` (with `<`) is equivalent to
2984 /// // `s.binary_search(&num).unwrap_or_else(|x| x)`, but using `<=` will allow `insert`
2985 /// // to shift less elements.
2986 /// s.insert(idx, num);
2987 /// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
2988 /// ```
2989 #[stable(feature = "rust1", since = "1.0.0")]
2990 #[cfg(not(feature = "ferrocene_subset"))]
2991 pub fn binary_search(&self, x: &T) -> Result<usize, usize>
2992 where
2993 T: Ord,
2994 {
2995 self.binary_search_by(|p| p.cmp(x))
2996 }
2997
2998 /// Binary searches this slice with a comparator function.
2999 ///
3000 /// The comparator function should return an order code that indicates
3001 /// whether its argument is `Less`, `Equal` or `Greater` the desired
3002 /// target.
3003 /// If the slice is not sorted or if the comparator function does not
3004 /// implement an order consistent with the sort order of the underlying
3005 /// slice, the returned result is unspecified and meaningless.
3006 ///
3007 /// If the value is found then [`Result::Ok`] is returned, containing the
3008 /// index of the matching element. If there are multiple matches, then any
3009 /// one of the matches could be returned. The index is chosen
3010 /// deterministically, but is subject to change in future versions of Rust.
3011 /// If the value is not found then [`Result::Err`] is returned, containing
3012 /// the index where a matching element could be inserted while maintaining
3013 /// sorted order.
3014 ///
3015 /// See also [`binary_search`], [`binary_search_by_key`], and [`partition_point`].
3016 ///
3017 /// [`binary_search`]: slice::binary_search
3018 /// [`binary_search_by_key`]: slice::binary_search_by_key
3019 /// [`partition_point`]: slice::partition_point
3020 ///
3021 /// # Examples
3022 ///
3023 /// Looks up a series of four elements. The first is found, with a
3024 /// uniquely determined position; the second and third are not
3025 /// found; the fourth could match any position in `[1, 4]`.
3026 ///
3027 /// ```
3028 /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
3029 ///
3030 /// let seek = 13;
3031 /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Ok(9));
3032 /// let seek = 4;
3033 /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(7));
3034 /// let seek = 100;
3035 /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(13));
3036 /// let seek = 1;
3037 /// let r = s.binary_search_by(|probe| probe.cmp(&seek));
3038 /// assert!(match r { Ok(1..=4) => true, _ => false, });
3039 /// ```
3040 #[stable(feature = "rust1", since = "1.0.0")]
3041 #[inline]
3042 pub fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result<usize, usize>
3043 where
3044 F: FnMut(&'a T) -> Ordering,
3045 {
3046 let mut size = self.len();
3047 if size == 0 {
3048 return Err(0);
3049 }
3050 let mut base = 0usize;
3051
3052 // This loop intentionally doesn't have an early exit if the comparison
3053 // returns Equal. We want the number of loop iterations to depend *only*
3054 // on the size of the input slice so that the CPU can reliably predict
3055 // the loop count.
3056 while size > 1 {
3057 let half = size / 2;
3058 let mid = base + half;
3059
3060 // SAFETY: the call is made safe by the following invariants:
3061 // - `mid >= 0`: by definition
3062 // - `mid < size`: `mid = size / 2 + size / 4 + size / 8 ...`
3063 let cmp = f(unsafe { self.get_unchecked(mid) });
3064
3065 // Binary search interacts poorly with branch prediction, so force
3066 // the compiler to use conditional moves if supported by the target
3067 // architecture.
3068 base = hint::select_unpredictable(cmp == Greater, base, mid);
3069
3070 // This is imprecise in the case where `size` is odd and the
3071 // comparison returns Greater: the mid element still gets included
3072 // by `size` even though it's known to be larger than the element
3073 // being searched for.
3074 //
3075 // This is fine though: we gain more performance by keeping the
3076 // loop iteration count invariant (and thus predictable) than we
3077 // lose from considering one additional element.
3078 size -= half;
3079 }
3080
3081 // SAFETY: base is always in [0, size) because base <= mid.
3082 let cmp = f(unsafe { self.get_unchecked(base) });
3083 if cmp == Equal {
3084 // SAFETY: same as the `get_unchecked` above.
3085 unsafe { hint::assert_unchecked(base < self.len()) };
3086 Ok(base)
3087 } else {
3088 let result = base + (cmp == Less) as usize;
3089 // SAFETY: same as the `get_unchecked` above.
3090 // Note that this is `<=`, unlike the assume in the `Ok` path.
3091 unsafe { hint::assert_unchecked(result <= self.len()) };
3092 Err(result)
3093 }
3094 }
3095
3096 /// Binary searches this slice with a key extraction function.
3097 ///
3098 /// Assumes that the slice is sorted by the key, for instance with
3099 /// [`sort_by_key`] using the same key extraction function.
3100 /// If the slice is not sorted by the key, the returned result is
3101 /// unspecified and meaningless.
3102 ///
3103 /// If the value is found then [`Result::Ok`] is returned, containing the
3104 /// index of the matching element. If there are multiple matches, then any
3105 /// one of the matches could be returned. The index is chosen
3106 /// deterministically, but is subject to change in future versions of Rust.
3107 /// If the value is not found then [`Result::Err`] is returned, containing
3108 /// the index where a matching element could be inserted while maintaining
3109 /// sorted order.
3110 ///
3111 /// See also [`binary_search`], [`binary_search_by`], and [`partition_point`].
3112 ///
3113 /// [`sort_by_key`]: slice::sort_by_key
3114 /// [`binary_search`]: slice::binary_search
3115 /// [`binary_search_by`]: slice::binary_search_by
3116 /// [`partition_point`]: slice::partition_point
3117 ///
3118 /// # Examples
3119 ///
3120 /// Looks up a series of four elements in a slice of pairs sorted by
3121 /// their second elements. The first is found, with a uniquely
3122 /// determined position; the second and third are not found; the
3123 /// fourth could match any position in `[1, 4]`.
3124 ///
3125 /// ```
3126 /// let s = [(0, 0), (2, 1), (4, 1), (5, 1), (3, 1),
3127 /// (1, 2), (2, 3), (4, 5), (5, 8), (3, 13),
3128 /// (1, 21), (2, 34), (4, 55)];
3129 ///
3130 /// assert_eq!(s.binary_search_by_key(&13, |&(a, b)| b), Ok(9));
3131 /// assert_eq!(s.binary_search_by_key(&4, |&(a, b)| b), Err(7));
3132 /// assert_eq!(s.binary_search_by_key(&100, |&(a, b)| b), Err(13));
3133 /// let r = s.binary_search_by_key(&1, |&(a, b)| b);
3134 /// assert!(match r { Ok(1..=4) => true, _ => false, });
3135 /// ```
3136 // Lint rustdoc::broken_intra_doc_links is allowed as `slice::sort_by_key` is
3137 // in crate `alloc`, and as such doesn't exists yet when building `core`: #74481.
3138 // This breaks links when slice is displayed in core, but changing it to use relative links
3139 // would break when the item is re-exported. So allow the core links to be broken for now.
3140 #[allow(rustdoc::broken_intra_doc_links)]
3141 #[stable(feature = "slice_binary_search_by_key", since = "1.10.0")]
3142 #[inline]
3143 pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize, usize>
3144 where
3145 F: FnMut(&'a T) -> B,
3146 B: Ord,
3147 {
3148 self.binary_search_by(|k| f(k).cmp(b))
3149 }
3150
3151 /// Sorts the slice in ascending order **without** preserving the initial order of equal elements.
3152 ///
3153 /// This sort is unstable (i.e., may reorder equal elements), in-place (i.e., does not
3154 /// allocate), and *O*(*n* \* log(*n*)) worst-case.
3155 ///
3156 /// If the implementation of [`Ord`] for `T` does not implement a [total order], the function
3157 /// may panic; even if the function exits normally, the resulting order of elements in the slice
3158 /// is unspecified. See also the note on panicking below.
3159 ///
3160 /// For example `|a, b| (a - b).cmp(a)` is a comparison function that is neither transitive nor
3161 /// reflexive nor total, `a < b < c < a` with `a = 1, b = 2, c = 3`. For more information and
3162 /// examples see the [`Ord`] documentation.
3163 ///
3164 ///
3165 /// All original elements will remain in the slice and any possible modifications via interior
3166 /// mutability are observed in the input. Same is true if the implementation of [`Ord`] for `T` panics.
3167 ///
3168 /// Sorting types that only implement [`PartialOrd`] such as [`f32`] and [`f64`] require
3169 /// additional precautions. For example, `f32::NAN != f32::NAN`, which doesn't fulfill the
3170 /// reflexivity requirement of [`Ord`]. By using an alternative comparison function with
3171 /// `slice::sort_unstable_by` such as [`f32::total_cmp`] or [`f64::total_cmp`] that defines a
3172 /// [total order] users can sort slices containing floating-point values. Alternatively, if all
3173 /// values in the slice are guaranteed to be in a subset for which [`PartialOrd::partial_cmp`]
3174 /// forms a [total order], it's possible to sort the slice with `sort_unstable_by(|a, b|
3175 /// a.partial_cmp(b).unwrap())`.
3176 ///
3177 /// # Current implementation
3178 ///
3179 /// The current implementation is based on [ipnsort] by Lukas Bergdoll and Orson Peters, which
3180 /// combines the fast average case of quicksort with the fast worst case of heapsort, achieving
3181 /// linear time on fully sorted and reversed inputs. On inputs with k distinct elements, the
3182 /// expected time to sort the data is *O*(*n* \* log(*k*)).
3183 ///
3184 /// It is typically faster than stable sorting, except in a few special cases, e.g., when the
3185 /// slice is partially sorted.
3186 ///
3187 /// # Panics
3188 ///
3189 /// May panic if the implementation of [`Ord`] for `T` does not implement a [total order], or if
3190 /// the [`Ord`] implementation panics.
3191 ///
3192 /// # Examples
3193 ///
3194 /// ```
3195 /// let mut v = [4, -5, 1, -3, 2];
3196 ///
3197 /// v.sort_unstable();
3198 /// assert_eq!(v, [-5, -3, 1, 2, 4]);
3199 /// ```
3200 ///
3201 /// [ipnsort]: https://github.com/Voultapher/sort-research-rs/tree/main/ipnsort
3202 /// [total order]: https://en.wikipedia.org/wiki/Total_order
3203 #[stable(feature = "sort_unstable", since = "1.20.0")]
3204 #[inline]
3205 #[cfg(not(feature = "ferrocene_subset"))]
3206 pub fn sort_unstable(&mut self)
3207 where
3208 T: Ord,
3209 {
3210 sort::unstable::sort(self, &mut T::lt);
3211 }
3212
3213 /// Sorts the slice in ascending order with a comparison function, **without** preserving the
3214 /// initial order of equal elements.
3215 ///
3216 /// This sort is unstable (i.e., may reorder equal elements), in-place (i.e., does not
3217 /// allocate), and *O*(*n* \* log(*n*)) worst-case.
3218 ///
3219 /// If the comparison function `compare` does not implement a [total order], the function
3220 /// may panic; even if the function exits normally, the resulting order of elements in the slice
3221 /// is unspecified. See also the note on panicking below.
3222 ///
3223 /// For example `|a, b| (a - b).cmp(a)` is a comparison function that is neither transitive nor
3224 /// reflexive nor total, `a < b < c < a` with `a = 1, b = 2, c = 3`. For more information and
3225 /// examples see the [`Ord`] documentation.
3226 ///
3227 /// All original elements will remain in the slice and any possible modifications via interior
3228 /// mutability are observed in the input. Same is true if `compare` panics.
3229 ///
3230 /// # Current implementation
3231 ///
3232 /// The current implementation is based on [ipnsort] by Lukas Bergdoll and Orson Peters, which
3233 /// combines the fast average case of quicksort with the fast worst case of heapsort, achieving
3234 /// linear time on fully sorted and reversed inputs. On inputs with k distinct elements, the
3235 /// expected time to sort the data is *O*(*n* \* log(*k*)).
3236 ///
3237 /// It is typically faster than stable sorting, except in a few special cases, e.g., when the
3238 /// slice is partially sorted.
3239 ///
3240 /// # Panics
3241 ///
3242 /// May panic if the `compare` does not implement a [total order], or if
3243 /// the `compare` itself panics.
3244 ///
3245 /// # Examples
3246 ///
3247 /// ```
3248 /// let mut v = [4, -5, 1, -3, 2];
3249 /// v.sort_unstable_by(|a, b| a.cmp(b));
3250 /// assert_eq!(v, [-5, -3, 1, 2, 4]);
3251 ///
3252 /// // reverse sorting
3253 /// v.sort_unstable_by(|a, b| b.cmp(a));
3254 /// assert_eq!(v, [4, 2, 1, -3, -5]);
3255 /// ```
3256 ///
3257 /// [ipnsort]: https://github.com/Voultapher/sort-research-rs/tree/main/ipnsort
3258 /// [total order]: https://en.wikipedia.org/wiki/Total_order
3259 #[stable(feature = "sort_unstable", since = "1.20.0")]
3260 #[inline]
3261 #[cfg(not(feature = "ferrocene_subset"))]
3262 pub fn sort_unstable_by<F>(&mut self, mut compare: F)
3263 where
3264 F: FnMut(&T, &T) -> Ordering,
3265 {
3266 sort::unstable::sort(self, &mut |a, b| compare(a, b) == Ordering::Less);
3267 }
3268
3269 /// Sorts the slice in ascending order with a key extraction function, **without** preserving
3270 /// the initial order of equal elements.
3271 ///
3272 /// This sort is unstable (i.e., may reorder equal elements), in-place (i.e., does not
3273 /// allocate), and *O*(*n* \* log(*n*)) worst-case.
3274 ///
3275 /// If the implementation of [`Ord`] for `K` does not implement a [total order], the function
3276 /// may panic; even if the function exits normally, the resulting order of elements in the slice
3277 /// is unspecified. See also the note on panicking below.
3278 ///
3279 /// For example `|a, b| (a - b).cmp(a)` is a comparison function that is neither transitive nor
3280 /// reflexive nor total, `a < b < c < a` with `a = 1, b = 2, c = 3`. For more information and
3281 /// examples see the [`Ord`] documentation.
3282 ///
3283 /// All original elements will remain in the slice and any possible modifications via interior
3284 /// mutability are observed in the input. Same is true if the implementation of [`Ord`] for `K` panics.
3285 ///
3286 /// # Current implementation
3287 ///
3288 /// The current implementation is based on [ipnsort] by Lukas Bergdoll and Orson Peters, which
3289 /// combines the fast average case of quicksort with the fast worst case of heapsort, achieving
3290 /// linear time on fully sorted and reversed inputs. On inputs with k distinct elements, the
3291 /// expected time to sort the data is *O*(*n* \* log(*k*)).
3292 ///
3293 /// It is typically faster than stable sorting, except in a few special cases, e.g., when the
3294 /// slice is partially sorted.
3295 ///
3296 /// # Panics
3297 ///
3298 /// May panic if the implementation of [`Ord`] for `K` does not implement a [total order], or if
3299 /// the [`Ord`] implementation panics.
3300 ///
3301 /// # Examples
3302 ///
3303 /// ```
3304 /// let mut v = [4i32, -5, 1, -3, 2];
3305 ///
3306 /// v.sort_unstable_by_key(|k| k.abs());
3307 /// assert_eq!(v, [1, 2, -3, 4, -5]);
3308 /// ```
3309 ///
3310 /// [ipnsort]: https://github.com/Voultapher/sort-research-rs/tree/main/ipnsort
3311 /// [total order]: https://en.wikipedia.org/wiki/Total_order
3312 #[stable(feature = "sort_unstable", since = "1.20.0")]
3313 #[inline]
3314 #[cfg(not(feature = "ferrocene_subset"))]
3315 pub fn sort_unstable_by_key<K, F>(&mut self, mut f: F)
3316 where
3317 F: FnMut(&T) -> K,
3318 K: Ord,
3319 {
3320 sort::unstable::sort(self, &mut |a, b| f(a).lt(&f(b)));
3321 }
3322
3323 /// Partially sorts the slice in ascending order **without** preserving the initial order of equal elements.
3324 ///
3325 /// Upon completion, for the specified range `start..end`, it's guaranteed that:
3326 ///
3327 /// 1. Every element in `self[..start]` is smaller than or equal to
3328 /// 2. Every element in `self[start..end]`, which is sorted, and smaller than or equal to
3329 /// 3. Every element in `self[end..]`.
3330 ///
3331 /// This partial sort is unstable, meaning it may reorder equal elements in the specified range.
3332 /// It may reorder elements outside the specified range as well, but the guarantees above still hold.
3333 ///
3334 /// This partial sort is in-place (i.e., does not allocate), and *O*(*n* + *k* \* log(*k*)) worst-case,
3335 /// where *n* is the length of the slice and *k* is the length of the specified range.
3336 ///
3337 /// See the documentation of [`sort_unstable`] for implementation notes.
3338 ///
3339 /// # Panics
3340 ///
3341 /// May panic if the implementation of [`Ord`] for `T` does not implement a total order, or if
3342 /// the [`Ord`] implementation panics, or if the specified range is out of bounds.
3343 ///
3344 /// # Examples
3345 ///
3346 /// ```
3347 /// #![feature(slice_partial_sort_unstable)]
3348 ///
3349 /// let mut v = [4, -5, 1, -3, 2];
3350 ///
3351 /// // empty range at the beginning, nothing changed
3352 /// v.partial_sort_unstable(0..0);
3353 /// assert_eq!(v, [4, -5, 1, -3, 2]);
3354 ///
3355 /// // empty range in the middle, partitioning the slice
3356 /// v.partial_sort_unstable(2..2);
3357 /// for i in 0..2 {
3358 /// assert!(v[i] <= v[2]);
3359 /// }
3360 /// for i in 3..v.len() {
3361 /// assert!(v[2] <= v[i]);
3362 /// }
3363 ///
3364 /// // single element range, same as select_nth_unstable
3365 /// v.partial_sort_unstable(2..3);
3366 /// for i in 0..2 {
3367 /// assert!(v[i] <= v[2]);
3368 /// }
3369 /// for i in 3..v.len() {
3370 /// assert!(v[2] <= v[i]);
3371 /// }
3372 ///
3373 /// // partial sort a subrange
3374 /// v.partial_sort_unstable(1..4);
3375 /// assert_eq!(&v[1..4], [-3, 1, 2]);
3376 ///
3377 /// // partial sort the whole range, same as sort_unstable
3378 /// v.partial_sort_unstable(..);
3379 /// assert_eq!(v, [-5, -3, 1, 2, 4]);
3380 /// ```
3381 ///
3382 /// [`sort_unstable`]: slice::sort_unstable
3383 #[unstable(feature = "slice_partial_sort_unstable", issue = "149046")]
3384 #[inline]
3385 #[cfg(not(feature = "ferrocene_subset"))]
3386 pub fn partial_sort_unstable<R>(&mut self, range: R)
3387 where
3388 T: Ord,
3389 R: RangeBounds<usize>,
3390 {
3391 sort::unstable::partial_sort(self, range, T::lt);
3392 }
3393
3394 /// Partially sorts the slice in ascending order with a comparison function, **without**
3395 /// preserving the initial order of equal elements.
3396 ///
3397 /// Upon completion, for the specified range `start..end`, it's guaranteed that:
3398 ///
3399 /// 1. Every element in `self[..start]` is smaller than or equal to
3400 /// 2. Every element in `self[start..end]`, which is sorted, and smaller than or equal to
3401 /// 3. Every element in `self[end..]`.
3402 ///
3403 /// This partial sort is unstable, meaning it may reorder equal elements in the specified range.
3404 /// It may reorder elements outside the specified range as well, but the guarantees above still hold.
3405 ///
3406 /// This partial sort is in-place (i.e., does not allocate), and *O*(*n* + *k* \* log(*k*)) worst-case,
3407 /// where *n* is the length of the slice and *k* is the length of the specified range.
3408 ///
3409 /// See the documentation of [`sort_unstable_by`] for implementation notes.
3410 ///
3411 /// # Panics
3412 ///
3413 /// May panic if the `compare` does not implement a total order, or if
3414 /// the `compare` itself panics, or if the specified range is out of bounds.
3415 ///
3416 /// # Examples
3417 ///
3418 /// ```
3419 /// #![feature(slice_partial_sort_unstable)]
3420 ///
3421 /// let mut v = [4, -5, 1, -3, 2];
3422 ///
3423 /// // empty range at the beginning, nothing changed
3424 /// v.partial_sort_unstable_by(0..0, |a, b| b.cmp(a));
3425 /// assert_eq!(v, [4, -5, 1, -3, 2]);
3426 ///
3427 /// // empty range in the middle, partitioning the slice
3428 /// v.partial_sort_unstable_by(2..2, |a, b| b.cmp(a));
3429 /// for i in 0..2 {
3430 /// assert!(v[i] >= v[2]);
3431 /// }
3432 /// for i in 3..v.len() {
3433 /// assert!(v[2] >= v[i]);
3434 /// }
3435 ///
3436 /// // single element range, same as select_nth_unstable
3437 /// v.partial_sort_unstable_by(2..3, |a, b| b.cmp(a));
3438 /// for i in 0..2 {
3439 /// assert!(v[i] >= v[2]);
3440 /// }
3441 /// for i in 3..v.len() {
3442 /// assert!(v[2] >= v[i]);
3443 /// }
3444 ///
3445 /// // partial sort a subrange
3446 /// v.partial_sort_unstable_by(1..4, |a, b| b.cmp(a));
3447 /// assert_eq!(&v[1..4], [2, 1, -3]);
3448 ///
3449 /// // partial sort the whole range, same as sort_unstable
3450 /// v.partial_sort_unstable_by(.., |a, b| b.cmp(a));
3451 /// assert_eq!(v, [4, 2, 1, -3, -5]);
3452 /// ```
3453 ///
3454 /// [`sort_unstable_by`]: slice::sort_unstable_by
3455 #[unstable(feature = "slice_partial_sort_unstable", issue = "149046")]
3456 #[inline]
3457 #[cfg(not(feature = "ferrocene_subset"))]
3458 pub fn partial_sort_unstable_by<F, R>(&mut self, range: R, mut compare: F)
3459 where
3460 F: FnMut(&T, &T) -> Ordering,
3461 R: RangeBounds<usize>,
3462 {
3463 sort::unstable::partial_sort(self, range, |a, b| compare(a, b) == Less);
3464 }
3465
3466 /// Partially sorts the slice in ascending order with a key extraction function, **without**
3467 /// preserving the initial order of equal elements.
3468 ///
3469 /// Upon completion, for the specified range `start..end`, it's guaranteed that:
3470 ///
3471 /// 1. Every element in `self[..start]` is smaller than or equal to
3472 /// 2. Every element in `self[start..end]`, which is sorted, and smaller than or equal to
3473 /// 3. Every element in `self[end..]`.
3474 ///
3475 /// This partial sort is unstable, meaning it may reorder equal elements in the specified range.
3476 /// It may reorder elements outside the specified range as well, but the guarantees above still hold.
3477 ///
3478 /// This partial sort is in-place (i.e., does not allocate), and *O*(*n* + *k* \* log(*k*)) worst-case,
3479 /// where *n* is the length of the slice and *k* is the length of the specified range.
3480 ///
3481 /// See the documentation of [`sort_unstable_by_key`] for implementation notes.
3482 ///
3483 /// # Panics
3484 ///
3485 /// May panic if the implementation of [`Ord`] for `K` does not implement a total order, or if
3486 /// the [`Ord`] implementation panics, or if the specified range is out of bounds.
3487 ///
3488 /// # Examples
3489 ///
3490 /// ```
3491 /// #![feature(slice_partial_sort_unstable)]
3492 ///
3493 /// let mut v = [4i32, -5, 1, -3, 2];
3494 ///
3495 /// // empty range at the beginning, nothing changed
3496 /// v.partial_sort_unstable_by_key(0..0, |k| k.abs());
3497 /// assert_eq!(v, [4, -5, 1, -3, 2]);
3498 ///
3499 /// // empty range in the middle, partitioning the slice
3500 /// v.partial_sort_unstable_by_key(2..2, |k| k.abs());
3501 /// for i in 0..2 {
3502 /// assert!(v[i].abs() <= v[2].abs());
3503 /// }
3504 /// for i in 3..v.len() {
3505 /// assert!(v[2].abs() <= v[i].abs());
3506 /// }
3507 ///
3508 /// // single element range, same as select_nth_unstable
3509 /// v.partial_sort_unstable_by_key(2..3, |k| k.abs());
3510 /// for i in 0..2 {
3511 /// assert!(v[i].abs() <= v[2].abs());
3512 /// }
3513 /// for i in 3..v.len() {
3514 /// assert!(v[2].abs() <= v[i].abs());
3515 /// }
3516 ///
3517 /// // partial sort a subrange
3518 /// v.partial_sort_unstable_by_key(1..4, |k| k.abs());
3519 /// assert_eq!(&v[1..4], [2, -3, 4]);
3520 ///
3521 /// // partial sort the whole range, same as sort_unstable
3522 /// v.partial_sort_unstable_by_key(.., |k| k.abs());
3523 /// assert_eq!(v, [1, 2, -3, 4, -5]);
3524 /// ```
3525 ///
3526 /// [`sort_unstable_by_key`]: slice::sort_unstable_by_key
3527 #[unstable(feature = "slice_partial_sort_unstable", issue = "149046")]
3528 #[inline]
3529 #[cfg(not(feature = "ferrocene_subset"))]
3530 pub fn partial_sort_unstable_by_key<K, F, R>(&mut self, range: R, mut f: F)
3531 where
3532 F: FnMut(&T) -> K,
3533 K: Ord,
3534 R: RangeBounds<usize>,
3535 {
3536 sort::unstable::partial_sort(self, range, |a, b| f(a).lt(&f(b)));
3537 }
3538
3539 /// Reorders the slice such that the element at `index` is at a sort-order position. All
3540 /// elements before `index` will be `<=` to this value, and all elements after will be `>=` to
3541 /// it.
3542 ///
3543 /// This reordering is unstable (i.e. any element that compares equal to the nth element may end
3544 /// up at that position), in-place (i.e. does not allocate), and runs in *O*(*n*) time. This
3545 /// function is also known as "kth element" in other libraries.
3546 ///
3547 /// Returns a triple that partitions the reordered slice:
3548 ///
3549 /// * The unsorted subslice before `index`, whose elements all satisfy `x <= self[index]`.
3550 ///
3551 /// * The element at `index`.
3552 ///
3553 /// * The unsorted subslice after `index`, whose elements all satisfy `x >= self[index]`.
3554 ///
3555 /// # Current implementation
3556 ///
3557 /// The current algorithm is an introselect implementation based on [ipnsort] by Lukas Bergdoll
3558 /// and Orson Peters, which is also the basis for [`sort_unstable`]. The fallback algorithm is
3559 /// Median of Medians using Tukey's Ninther for pivot selection, which guarantees linear runtime
3560 /// for all inputs.
3561 ///
3562 /// [`sort_unstable`]: slice::sort_unstable
3563 ///
3564 /// # Panics
3565 ///
3566 /// Panics when `index >= len()`, and so always panics on empty slices.
3567 ///
3568 /// May panic if the implementation of [`Ord`] for `T` does not implement a [total order].
3569 ///
3570 /// # Examples
3571 ///
3572 /// ```
3573 /// let mut v = [-5i32, 4, 2, -3, 1];
3574 ///
3575 /// // Find the items `<=` to the median, the median itself, and the items `>=` to it.
3576 /// let (lesser, median, greater) = v.select_nth_unstable(2);
3577 ///
3578 /// assert!(lesser == [-3, -5] || lesser == [-5, -3]);
3579 /// assert_eq!(median, &mut 1);
3580 /// assert!(greater == [4, 2] || greater == [2, 4]);
3581 ///
3582 /// // We are only guaranteed the slice will be one of the following, based on the way we sort
3583 /// // about the specified index.
3584 /// assert!(v == [-3, -5, 1, 2, 4] ||
3585 /// v == [-5, -3, 1, 2, 4] ||
3586 /// v == [-3, -5, 1, 4, 2] ||
3587 /// v == [-5, -3, 1, 4, 2]);
3588 /// ```
3589 ///
3590 /// [ipnsort]: https://github.com/Voultapher/sort-research-rs/tree/main/ipnsort
3591 /// [total order]: https://en.wikipedia.org/wiki/Total_order
3592 #[stable(feature = "slice_select_nth_unstable", since = "1.49.0")]
3593 #[inline]
3594 #[cfg(not(feature = "ferrocene_subset"))]
3595 pub fn select_nth_unstable(&mut self, index: usize) -> (&mut [T], &mut T, &mut [T])
3596 where
3597 T: Ord,
3598 {
3599 sort::select::partition_at_index(self, index, T::lt)
3600 }
3601
3602 /// Reorders the slice with a comparator function such that the element at `index` is at a
3603 /// sort-order position. All elements before `index` will be `<=` to this value, and all
3604 /// elements after will be `>=` to it, according to the comparator function.
3605 ///
3606 /// This reordering is unstable (i.e. any element that compares equal to the nth element may end
3607 /// up at that position), in-place (i.e. does not allocate), and runs in *O*(*n*) time. This
3608 /// function is also known as "kth element" in other libraries.
3609 ///
3610 /// Returns a triple partitioning the reordered slice:
3611 ///
3612 /// * The unsorted subslice before `index`, whose elements all satisfy
3613 /// `compare(x, self[index]).is_le()`.
3614 ///
3615 /// * The element at `index`.
3616 ///
3617 /// * The unsorted subslice after `index`, whose elements all satisfy
3618 /// `compare(x, self[index]).is_ge()`.
3619 ///
3620 /// # Current implementation
3621 ///
3622 /// The current algorithm is an introselect implementation based on [ipnsort] by Lukas Bergdoll
3623 /// and Orson Peters, which is also the basis for [`sort_unstable`]. The fallback algorithm is
3624 /// Median of Medians using Tukey's Ninther for pivot selection, which guarantees linear runtime
3625 /// for all inputs.
3626 ///
3627 /// [`sort_unstable`]: slice::sort_unstable
3628 ///
3629 /// # Panics
3630 ///
3631 /// Panics when `index >= len()`, and so always panics on empty slices.
3632 ///
3633 /// May panic if `compare` does not implement a [total order].
3634 ///
3635 /// # Examples
3636 ///
3637 /// ```
3638 /// let mut v = [-5i32, 4, 2, -3, 1];
3639 ///
3640 /// // Find the items `>=` to the median, the median itself, and the items `<=` to it, by using
3641 /// // a reversed comparator.
3642 /// let (before, median, after) = v.select_nth_unstable_by(2, |a, b| b.cmp(a));
3643 ///
3644 /// assert!(before == [4, 2] || before == [2, 4]);
3645 /// assert_eq!(median, &mut 1);
3646 /// assert!(after == [-3, -5] || after == [-5, -3]);
3647 ///
3648 /// // We are only guaranteed the slice will be one of the following, based on the way we sort
3649 /// // about the specified index.
3650 /// assert!(v == [2, 4, 1, -5, -3] ||
3651 /// v == [2, 4, 1, -3, -5] ||
3652 /// v == [4, 2, 1, -5, -3] ||
3653 /// v == [4, 2, 1, -3, -5]);
3654 /// ```
3655 ///
3656 /// [ipnsort]: https://github.com/Voultapher/sort-research-rs/tree/main/ipnsort
3657 /// [total order]: https://en.wikipedia.org/wiki/Total_order
3658 #[stable(feature = "slice_select_nth_unstable", since = "1.49.0")]
3659 #[inline]
3660 #[cfg(not(feature = "ferrocene_subset"))]
3661 pub fn select_nth_unstable_by<F>(
3662 &mut self,
3663 index: usize,
3664 mut compare: F,
3665 ) -> (&mut [T], &mut T, &mut [T])
3666 where
3667 F: FnMut(&T, &T) -> Ordering,
3668 {
3669 sort::select::partition_at_index(self, index, |a: &T, b: &T| compare(a, b) == Less)
3670 }
3671
3672 /// Reorders the slice with a key extraction function such that the element at `index` is at a
3673 /// sort-order position. All elements before `index` will have keys `<=` to the key at `index`,
3674 /// and all elements after will have keys `>=` to it.
3675 ///
3676 /// This reordering is unstable (i.e. any element that compares equal to the nth element may end
3677 /// up at that position), in-place (i.e. does not allocate), and runs in *O*(*n*) time. This
3678 /// function is also known as "kth element" in other libraries.
3679 ///
3680 /// Returns a triple partitioning the reordered slice:
3681 ///
3682 /// * The unsorted subslice before `index`, whose elements all satisfy `f(x) <= f(self[index])`.
3683 ///
3684 /// * The element at `index`.
3685 ///
3686 /// * The unsorted subslice after `index`, whose elements all satisfy `f(x) >= f(self[index])`.
3687 ///
3688 /// # Current implementation
3689 ///
3690 /// The current algorithm is an introselect implementation based on [ipnsort] by Lukas Bergdoll
3691 /// and Orson Peters, which is also the basis for [`sort_unstable`]. The fallback algorithm is
3692 /// Median of Medians using Tukey's Ninther for pivot selection, which guarantees linear runtime
3693 /// for all inputs.
3694 ///
3695 /// [`sort_unstable`]: slice::sort_unstable
3696 ///
3697 /// # Panics
3698 ///
3699 /// Panics when `index >= len()`, meaning it always panics on empty slices.
3700 ///
3701 /// May panic if `K: Ord` does not implement a total order.
3702 ///
3703 /// # Examples
3704 ///
3705 /// ```
3706 /// let mut v = [-5i32, 4, 1, -3, 2];
3707 ///
3708 /// // Find the items `<=` to the absolute median, the absolute median itself, and the items
3709 /// // `>=` to it.
3710 /// let (lesser, median, greater) = v.select_nth_unstable_by_key(2, |a| a.abs());
3711 ///
3712 /// assert!(lesser == [1, 2] || lesser == [2, 1]);
3713 /// assert_eq!(median, &mut -3);
3714 /// assert!(greater == [4, -5] || greater == [-5, 4]);
3715 ///
3716 /// // We are only guaranteed the slice will be one of the following, based on the way we sort
3717 /// // about the specified index.
3718 /// assert!(v == [1, 2, -3, 4, -5] ||
3719 /// v == [1, 2, -3, -5, 4] ||
3720 /// v == [2, 1, -3, 4, -5] ||
3721 /// v == [2, 1, -3, -5, 4]);
3722 /// ```
3723 ///
3724 /// [ipnsort]: https://github.com/Voultapher/sort-research-rs/tree/main/ipnsort
3725 /// [total order]: https://en.wikipedia.org/wiki/Total_order
3726 #[stable(feature = "slice_select_nth_unstable", since = "1.49.0")]
3727 #[inline]
3728 #[cfg(not(feature = "ferrocene_subset"))]
3729 pub fn select_nth_unstable_by_key<K, F>(
3730 &mut self,
3731 index: usize,
3732 mut f: F,
3733 ) -> (&mut [T], &mut T, &mut [T])
3734 where
3735 F: FnMut(&T) -> K,
3736 K: Ord,
3737 {
3738 sort::select::partition_at_index(self, index, |a: &T, b: &T| f(a).lt(&f(b)))
3739 }
3740
3741 /// Moves all consecutive repeated elements to the end of the slice according to the
3742 /// [`PartialEq`] trait implementation.
3743 ///
3744 /// Returns two slices. The first contains no consecutive repeated elements.
3745 /// The second contains all the duplicates in no specified order.
3746 ///
3747 /// If the slice is sorted, the first returned slice contains no duplicates.
3748 ///
3749 /// # Examples
3750 ///
3751 /// ```
3752 /// #![feature(slice_partition_dedup)]
3753 ///
3754 /// let mut slice = [1, 2, 2, 3, 3, 2, 1, 1];
3755 ///
3756 /// let (dedup, duplicates) = slice.partition_dedup();
3757 ///
3758 /// assert_eq!(dedup, [1, 2, 3, 2, 1]);
3759 /// assert_eq!(duplicates, [2, 3, 1]);
3760 /// ```
3761 #[unstable(feature = "slice_partition_dedup", issue = "54279")]
3762 #[inline]
3763 #[cfg(not(feature = "ferrocene_subset"))]
3764 pub fn partition_dedup(&mut self) -> (&mut [T], &mut [T])
3765 where
3766 T: PartialEq,
3767 {
3768 self.partition_dedup_by(|a, b| a == b)
3769 }
3770
3771 /// Moves all but the first of consecutive elements to the end of the slice satisfying
3772 /// a given equality relation.
3773 ///
3774 /// Returns two slices. The first contains no consecutive repeated elements.
3775 /// The second contains all the duplicates in no specified order.
3776 ///
3777 /// The `same_bucket` function is passed references to two elements from the slice and
3778 /// must determine if the elements compare equal. The elements are passed in opposite order
3779 /// from their order in the slice, so if `same_bucket(a, b)` returns `true`, `a` is moved
3780 /// at the end of the slice.
3781 ///
3782 /// If the slice is sorted, the first returned slice contains no duplicates.
3783 ///
3784 /// # Examples
3785 ///
3786 /// ```
3787 /// #![feature(slice_partition_dedup)]
3788 ///
3789 /// let mut slice = ["foo", "Foo", "BAZ", "Bar", "bar", "baz", "BAZ"];
3790 ///
3791 /// let (dedup, duplicates) = slice.partition_dedup_by(|a, b| a.eq_ignore_ascii_case(b));
3792 ///
3793 /// assert_eq!(dedup, ["foo", "BAZ", "Bar", "baz"]);
3794 /// assert_eq!(duplicates, ["bar", "Foo", "BAZ"]);
3795 /// ```
3796 #[unstable(feature = "slice_partition_dedup", issue = "54279")]
3797 #[inline]
3798 #[cfg(not(feature = "ferrocene_subset"))]
3799 pub fn partition_dedup_by<F>(&mut self, mut same_bucket: F) -> (&mut [T], &mut [T])
3800 where
3801 F: FnMut(&mut T, &mut T) -> bool,
3802 {
3803 // Although we have a mutable reference to `self`, we cannot make
3804 // *arbitrary* changes. The `same_bucket` calls could panic, so we
3805 // must ensure that the slice is in a valid state at all times.
3806 //
3807 // The way that we handle this is by using swaps; we iterate
3808 // over all the elements, swapping as we go so that at the end
3809 // the elements we wish to keep are in the front, and those we
3810 // wish to reject are at the back. We can then split the slice.
3811 // This operation is still `O(n)`.
3812 //
3813 // Example: We start in this state, where `r` represents "next
3814 // read" and `w` represents "next_write".
3815 //
3816 // r
3817 // +---+---+---+---+---+---+
3818 // | 0 | 1 | 1 | 2 | 3 | 3 |
3819 // +---+---+---+---+---+---+
3820 // w
3821 //
3822 // Comparing self[r] against self[w-1], this is not a duplicate, so
3823 // we swap self[r] and self[w] (no effect as r==w) and then increment both
3824 // r and w, leaving us with:
3825 //
3826 // r
3827 // +---+---+---+---+---+---+
3828 // | 0 | 1 | 1 | 2 | 3 | 3 |
3829 // +---+---+---+---+---+---+
3830 // w
3831 //
3832 // Comparing self[r] against self[w-1], this value is a duplicate,
3833 // so we increment `r` but leave everything else unchanged:
3834 //
3835 // r
3836 // +---+---+---+---+---+---+
3837 // | 0 | 1 | 1 | 2 | 3 | 3 |
3838 // +---+---+---+---+---+---+
3839 // w
3840 //
3841 // Comparing self[r] against self[w-1], this is not a duplicate,
3842 // so swap self[r] and self[w] and advance r and w:
3843 //
3844 // r
3845 // +---+---+---+---+---+---+
3846 // | 0 | 1 | 2 | 1 | 3 | 3 |
3847 // +---+---+---+---+---+---+
3848 // w
3849 //
3850 // Not a duplicate, repeat:
3851 //
3852 // r
3853 // +---+---+---+---+---+---+
3854 // | 0 | 1 | 2 | 3 | 1 | 3 |
3855 // +---+---+---+---+---+---+
3856 // w
3857 //
3858 // Duplicate, advance r. End of slice. Split at w.
3859
3860 let len = self.len();
3861 if len <= 1 {
3862 return (self, &mut []);
3863 }
3864
3865 let ptr = self.as_mut_ptr();
3866 let mut next_read: usize = 1;
3867 let mut next_write: usize = 1;
3868
3869 // SAFETY: the `while` condition guarantees `next_read` and `next_write`
3870 // are less than `len`, thus are inside `self`. `prev_ptr_write` points to
3871 // one element before `ptr_write`, but `next_write` starts at 1, so
3872 // `prev_ptr_write` is never less than 0 and is inside the slice.
3873 // This fulfils the requirements for dereferencing `ptr_read`, `prev_ptr_write`
3874 // and `ptr_write`, and for using `ptr.add(next_read)`, `ptr.add(next_write - 1)`
3875 // and `prev_ptr_write.offset(1)`.
3876 //
3877 // `next_write` is also incremented at most once per loop at most meaning
3878 // no element is skipped when it may need to be swapped.
3879 //
3880 // `ptr_read` and `prev_ptr_write` never point to the same element. This
3881 // is required for `&mut *ptr_read`, `&mut *prev_ptr_write` to be safe.
3882 // The explanation is simply that `next_read >= next_write` is always true,
3883 // thus `next_read > next_write - 1` is too.
3884 unsafe {
3885 // Avoid bounds checks by using raw pointers.
3886 while next_read < len {
3887 let ptr_read = ptr.add(next_read);
3888 let prev_ptr_write = ptr.add(next_write - 1);
3889 if !same_bucket(&mut *ptr_read, &mut *prev_ptr_write) {
3890 if next_read != next_write {
3891 let ptr_write = prev_ptr_write.add(1);
3892 mem::swap(&mut *ptr_read, &mut *ptr_write);
3893 }
3894 next_write += 1;
3895 }
3896 next_read += 1;
3897 }
3898 }
3899
3900 self.split_at_mut(next_write)
3901 }
3902
3903 /// Moves all but the first of consecutive elements to the end of the slice that resolve
3904 /// to the same key.
3905 ///
3906 /// Returns two slices. The first contains no consecutive repeated elements.
3907 /// The second contains all the duplicates in no specified order.
3908 ///
3909 /// If the slice is sorted, the first returned slice contains no duplicates.
3910 ///
3911 /// # Examples
3912 ///
3913 /// ```
3914 /// #![feature(slice_partition_dedup)]
3915 ///
3916 /// let mut slice = [10, 20, 21, 30, 30, 20, 11, 13];
3917 ///
3918 /// let (dedup, duplicates) = slice.partition_dedup_by_key(|i| *i / 10);
3919 ///
3920 /// assert_eq!(dedup, [10, 20, 30, 20, 11]);
3921 /// assert_eq!(duplicates, [21, 30, 13]);
3922 /// ```
3923 #[unstable(feature = "slice_partition_dedup", issue = "54279")]
3924 #[inline]
3925 #[cfg(not(feature = "ferrocene_subset"))]
3926 pub fn partition_dedup_by_key<K, F>(&mut self, mut key: F) -> (&mut [T], &mut [T])
3927 where
3928 F: FnMut(&mut T) -> K,
3929 K: PartialEq,
3930 {
3931 self.partition_dedup_by(|a, b| key(a) == key(b))
3932 }
3933
3934 /// Rotates the slice in-place such that the first `mid` elements of the
3935 /// slice move to the end while the last `self.len() - mid` elements move to
3936 /// the front.
3937 ///
3938 /// After calling `rotate_left`, the element previously at index `mid` will
3939 /// become the first element in the slice.
3940 ///
3941 /// # Panics
3942 ///
3943 /// This function will panic if `mid` is greater than the length of the
3944 /// slice. Note that `mid == self.len()` does _not_ panic and is a no-op
3945 /// rotation.
3946 ///
3947 /// # Complexity
3948 ///
3949 /// Takes linear (in `self.len()`) time.
3950 ///
3951 /// # Examples
3952 ///
3953 /// ```
3954 /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
3955 /// a.rotate_left(2);
3956 /// assert_eq!(a, ['c', 'd', 'e', 'f', 'a', 'b']);
3957 /// ```
3958 ///
3959 /// Rotating a subslice:
3960 ///
3961 /// ```
3962 /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
3963 /// a[1..5].rotate_left(1);
3964 /// assert_eq!(a, ['a', 'c', 'd', 'e', 'b', 'f']);
3965 /// ```
3966 #[stable(feature = "slice_rotate", since = "1.26.0")]
3967 #[rustc_const_stable(feature = "const_slice_rotate", since = "1.92.0")]
3968 pub const fn rotate_left(&mut self, mid: usize) {
3969 assert!(mid <= self.len());
3970 let k = self.len() - mid;
3971 let p = self.as_mut_ptr();
3972
3973 // SAFETY: The range `[p.add(mid) - mid, p.add(mid) + k)` is trivially
3974 // valid for reading and writing, as required by `ptr_rotate`.
3975 unsafe {
3976 rotate::ptr_rotate(mid, p.add(mid), k);
3977 }
3978 }
3979
3980 /// Rotates the slice in-place such that the first `self.len() - k`
3981 /// elements of the slice move to the end while the last `k` elements move
3982 /// to the front.
3983 ///
3984 /// After calling `rotate_right`, the element previously at index
3985 /// `self.len() - k` will become the first element in the slice.
3986 ///
3987 /// # Panics
3988 ///
3989 /// This function will panic if `k` is greater than the length of the
3990 /// slice. Note that `k == self.len()` does _not_ panic and is a no-op
3991 /// rotation.
3992 ///
3993 /// # Complexity
3994 ///
3995 /// Takes linear (in `self.len()`) time.
3996 ///
3997 /// # Examples
3998 ///
3999 /// ```
4000 /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
4001 /// a.rotate_right(2);
4002 /// assert_eq!(a, ['e', 'f', 'a', 'b', 'c', 'd']);
4003 /// ```
4004 ///
4005 /// Rotating a subslice:
4006 ///
4007 /// ```
4008 /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
4009 /// a[1..5].rotate_right(1);
4010 /// assert_eq!(a, ['a', 'e', 'b', 'c', 'd', 'f']);
4011 /// ```
4012 #[stable(feature = "slice_rotate", since = "1.26.0")]
4013 #[rustc_const_stable(feature = "const_slice_rotate", since = "1.92.0")]
4014 pub const fn rotate_right(&mut self, k: usize) {
4015 assert!(k <= self.len());
4016 let mid = self.len() - k;
4017 let p = self.as_mut_ptr();
4018
4019 // SAFETY: The range `[p.add(mid) - mid, p.add(mid) + k)` is trivially
4020 // valid for reading and writing, as required by `ptr_rotate`.
4021 unsafe {
4022 rotate::ptr_rotate(mid, p.add(mid), k);
4023 }
4024 }
4025
4026 /// Moves the elements of this slice `N` places to the left, returning the ones
4027 /// that "fall off" the front, and putting `inserted` at the end.
4028 ///
4029 /// Equivalently, you can think of concatenating `self` and `inserted` into one
4030 /// long sequence, then returning the left-most `N` items and the rest into `self`:
4031 ///
4032 /// ```text
4033 /// self (before) inserted
4034 /// vvvvvvvvvvvvvvv vvv
4035 /// [1, 2, 3, 4, 5] [9]
4036 /// ↙ ↙ ↙ ↙ ↙ ↙
4037 /// [1] [2, 3, 4, 5, 9]
4038 /// ^^^ ^^^^^^^^^^^^^^^
4039 /// returned self (after)
4040 /// ```
4041 ///
4042 /// See also [`Self::shift_right`] and compare [`Self::rotate_left`].
4043 ///
4044 /// # Examples
4045 ///
4046 /// ```
4047 /// #![feature(slice_shift)]
4048 ///
4049 /// // Same as the diagram above
4050 /// let mut a = [1, 2, 3, 4, 5];
4051 /// let inserted = [9];
4052 /// let returned = a.shift_left(inserted);
4053 /// assert_eq!(returned, [1]);
4054 /// assert_eq!(a, [2, 3, 4, 5, 9]);
4055 ///
4056 /// // You can shift multiple items at a time
4057 /// let mut a = *b"Hello world";
4058 /// assert_eq!(a.shift_left(*b" peace"), *b"Hello ");
4059 /// assert_eq!(a, *b"world peace");
4060 ///
4061 /// // The name comes from this operation's similarity to bitshifts
4062 /// let mut a: u8 = 0b10010110;
4063 /// a <<= 3;
4064 /// assert_eq!(a, 0b10110000_u8);
4065 /// let mut a: [_; 8] = [1, 0, 0, 1, 0, 1, 1, 0];
4066 /// a.shift_left([0; 3]);
4067 /// assert_eq!(a, [1, 0, 1, 1, 0, 0, 0, 0]);
4068 ///
4069 /// // Remember you can sub-slice to affect less that the whole slice.
4070 /// // For example, this is similar to `.remove(1)` + `.insert(4, 'Z')`
4071 /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
4072 /// assert_eq!(a[1..=4].shift_left(['Z']), ['b']);
4073 /// assert_eq!(a, ['a', 'c', 'd', 'e', 'Z', 'f']);
4074 ///
4075 /// // If the size matches it's equivalent to `mem::replace`
4076 /// let mut a = [1, 2, 3];
4077 /// assert_eq!(a.shift_left([7, 8, 9]), [1, 2, 3]);
4078 /// assert_eq!(a, [7, 8, 9]);
4079 ///
4080 /// // Some of the "inserted" elements end up returned if the slice is too short
4081 /// let mut a = [];
4082 /// assert_eq!(a.shift_left([1, 2, 3]), [1, 2, 3]);
4083 /// let mut a = [9];
4084 /// assert_eq!(a.shift_left([1, 2, 3]), [9, 1, 2]);
4085 /// assert_eq!(a, [3]);
4086 /// ```
4087 #[cfg(not(feature = "ferrocene_subset"))]
4088 #[unstable(feature = "slice_shift", issue = "151772")]
4089 pub const fn shift_left<const N: usize>(&mut self, inserted: [T; N]) -> [T; N] {
4090 if let Some(shift) = self.len().checked_sub(N) {
4091 // SAFETY: Having just checked that the inserted/returned arrays are
4092 // shorter than (or the same length as) the slice:
4093 // 1. The read for the items to return is in-bounds
4094 // 2. We can `memmove` the slice over to cover the items we're returning
4095 // to ensure those aren't double-dropped
4096 // 3. Then we write (in-bounds for the same reason as the read) the
4097 // inserted items atop the items of the slice that we just duplicated
4098 //
4099 // And none of this can panic, so there's no risk of intermediate unwinds.
4100 unsafe {
4101 let ptr = self.as_mut_ptr();
4102 let returned = ptr.cast_array::<N>().read();
4103 ptr.copy_from(ptr.add(N), shift);
4104 ptr.add(shift).cast_array::<N>().write(inserted);
4105 returned
4106 }
4107 } else {
4108 // SAFETY: Having checked that the slice is strictly shorter than the
4109 // inserted/returned arrays, it means we'll be copying the whole slice
4110 // into the returned array, but that's not enough on its own. We also
4111 // need to copy some of the inserted array into the returned array,
4112 // with the rest going into the slice. Because `&mut` is exclusive
4113 // and we own both `inserted` and `returned`, they're all disjoint
4114 // allocations from each other as we can use `nonoverlapping` copies.
4115 //
4116 // We avoid double-frees by `ManuallyDrop`ing the inserted items,
4117 // since we always copy them to other locations that will drop them
4118 // instead. Plus nothing in here can panic -- it's just memcpy three
4119 // times -- so there's no intermediate unwinds to worry about.
4120 unsafe {
4121 let len = self.len();
4122 let slice = self.as_mut_ptr();
4123 let inserted = mem::ManuallyDrop::new(inserted);
4124 let inserted = (&raw const inserted).cast::<T>();
4125
4126 let mut returned = MaybeUninit::<[T; N]>::uninit();
4127 let ptr = returned.as_mut_ptr().cast::<T>();
4128 ptr.copy_from_nonoverlapping(slice, len);
4129 ptr.add(len).copy_from_nonoverlapping(inserted, N - len);
4130 slice.copy_from_nonoverlapping(inserted.add(N - len), len);
4131 returned.assume_init()
4132 }
4133 }
4134 }
4135
4136 /// Moves the elements of this slice `N` places to the right, returning the ones
4137 /// that "fall off" the back, and putting `inserted` at the beginning.
4138 ///
4139 /// Equivalently, you can think of concatenating `inserted` and `self` into one
4140 /// long sequence, then returning the right-most `N` items and the rest into `self`:
4141 ///
4142 /// ```text
4143 /// inserted self (before)
4144 /// vvv vvvvvvvvvvvvvvv
4145 /// [0] [5, 6, 7, 8, 9]
4146 /// ↘ ↘ ↘ ↘ ↘ ↘
4147 /// [0, 5, 6, 7, 8] [9]
4148 /// ^^^^^^^^^^^^^^^ ^^^
4149 /// self (after) returned
4150 /// ```
4151 ///
4152 /// See also [`Self::shift_left`] and compare [`Self::rotate_right`].
4153 ///
4154 /// # Examples
4155 ///
4156 /// ```
4157 /// #![feature(slice_shift)]
4158 ///
4159 /// // Same as the diagram above
4160 /// let mut a = [5, 6, 7, 8, 9];
4161 /// let inserted = [0];
4162 /// let returned = a.shift_right(inserted);
4163 /// assert_eq!(returned, [9]);
4164 /// assert_eq!(a, [0, 5, 6, 7, 8]);
4165 ///
4166 /// // The name comes from this operation's similarity to bitshifts
4167 /// let mut a: u8 = 0b10010110;
4168 /// a >>= 3;
4169 /// assert_eq!(a, 0b00010010_u8);
4170 /// let mut a: [_; 8] = [1, 0, 0, 1, 0, 1, 1, 0];
4171 /// a.shift_right([0; 3]);
4172 /// assert_eq!(a, [0, 0, 0, 1, 0, 0, 1, 0]);
4173 ///
4174 /// // Remember you can sub-slice to affect less that the whole slice.
4175 /// // For example, this is similar to `.remove(4)` + `.insert(1, 'Z')`
4176 /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
4177 /// assert_eq!(a[1..=4].shift_right(['Z']), ['e']);
4178 /// assert_eq!(a, ['a', 'Z', 'b', 'c', 'd', 'f']);
4179 ///
4180 /// // If the size matches it's equivalent to `mem::replace`
4181 /// let mut a = [1, 2, 3];
4182 /// assert_eq!(a.shift_right([7, 8, 9]), [1, 2, 3]);
4183 /// assert_eq!(a, [7, 8, 9]);
4184 ///
4185 /// // Some of the "inserted" elements end up returned if the slice is too short
4186 /// let mut a = [];
4187 /// assert_eq!(a.shift_right([1, 2, 3]), [1, 2, 3]);
4188 /// let mut a = [9];
4189 /// assert_eq!(a.shift_right([1, 2, 3]), [2, 3, 9]);
4190 /// assert_eq!(a, [1]);
4191 /// ```
4192 #[cfg(not(feature = "ferrocene_subset"))]
4193 #[unstable(feature = "slice_shift", issue = "151772")]
4194 pub const fn shift_right<const N: usize>(&mut self, inserted: [T; N]) -> [T; N] {
4195 if let Some(shift) = self.len().checked_sub(N) {
4196 // SAFETY: Having just checked that the inserted/returned arrays are
4197 // shorter than (or the same length as) the slice:
4198 // 1. The read for the items to return is in-bounds
4199 // 2. We can `memmove` the slice over to cover the items we're returning
4200 // to ensure those aren't double-dropped
4201 // 3. Then we write (in-bounds for the same reason as the read) the
4202 // inserted items atop the items of the slice that we just duplicated
4203 //
4204 // And none of this can panic, so there's no risk of intermediate unwinds.
4205 unsafe {
4206 let ptr = self.as_mut_ptr();
4207 let returned = ptr.add(shift).cast_array::<N>().read();
4208 ptr.add(N).copy_from(ptr, shift);
4209 ptr.cast_array::<N>().write(inserted);
4210 returned
4211 }
4212 } else {
4213 // SAFETY: Having checked that the slice is strictly shorter than the
4214 // inserted/returned arrays, it means we'll be copying the whole slice
4215 // into the returned array, but that's not enough on its own. We also
4216 // need to copy some of the inserted array into the returned array,
4217 // with the rest going into the slice. Because `&mut` is exclusive
4218 // and we own both `inserted` and `returned`, they're all disjoint
4219 // allocations from each other as we can use `nonoverlapping` copies.
4220 //
4221 // We avoid double-frees by `ManuallyDrop`ing the inserted items,
4222 // since we always copy them to other locations that will drop them
4223 // instead. Plus nothing in here can panic -- it's just memcpy three
4224 // times -- so there's no intermediate unwinds to worry about.
4225 unsafe {
4226 let len = self.len();
4227 let slice = self.as_mut_ptr();
4228 let inserted = mem::ManuallyDrop::new(inserted);
4229 let inserted = (&raw const inserted).cast::<T>();
4230
4231 let mut returned = MaybeUninit::<[T; N]>::uninit();
4232 let ptr = returned.as_mut_ptr().cast::<T>();
4233 ptr.add(N - len).copy_from_nonoverlapping(slice, len);
4234 ptr.copy_from_nonoverlapping(inserted.add(len), N - len);
4235 slice.copy_from_nonoverlapping(inserted, len);
4236 returned.assume_init()
4237 }
4238 }
4239 }
4240
4241 /// Fills `self` with elements by cloning `value`.
4242 ///
4243 /// # Examples
4244 ///
4245 /// ```
4246 /// let mut buf = vec![0; 10];
4247 /// buf.fill(1);
4248 /// assert_eq!(buf, vec![1; 10]);
4249 /// ```
4250 #[doc(alias = "memset")]
4251 #[stable(feature = "slice_fill", since = "1.50.0")]
4252 pub fn fill(&mut self, value: T)
4253 where
4254 T: Clone,
4255 {
4256 specialize::SpecFill::spec_fill(self, value);
4257 }
4258
4259 /// Fills `self` with elements returned by calling a closure repeatedly.
4260 ///
4261 /// This method uses a closure to create new values. If you'd rather
4262 /// [`Clone`] a given value, use [`fill`]. If you want to use the [`Default`]
4263 /// trait to generate values, you can pass [`Default::default`] as the
4264 /// argument.
4265 ///
4266 /// [`fill`]: slice::fill
4267 ///
4268 /// # Examples
4269 ///
4270 /// ```
4271 /// let mut buf = vec![1; 10];
4272 /// buf.fill_with(Default::default);
4273 /// assert_eq!(buf, vec![0; 10]);
4274 /// ```
4275 #[stable(feature = "slice_fill_with", since = "1.51.0")]
4276 #[cfg(not(feature = "ferrocene_subset"))]
4277 pub fn fill_with<F>(&mut self, mut f: F)
4278 where
4279 F: FnMut() -> T,
4280 {
4281 for el in self {
4282 *el = f();
4283 }
4284 }
4285
4286 /// Copies the elements from `src` into `self`.
4287 ///
4288 /// The length of `src` must be the same as `self`.
4289 ///
4290 /// # Panics
4291 ///
4292 /// This function will panic if the two slices have different lengths.
4293 ///
4294 /// # Examples
4295 ///
4296 /// Cloning two elements from a slice into another:
4297 ///
4298 /// ```
4299 /// let src = [1, 2, 3, 4];
4300 /// let mut dst = [0, 0];
4301 ///
4302 /// // Because the slices have to be the same length,
4303 /// // we slice the source slice from four elements
4304 /// // to two. It will panic if we don't do this.
4305 /// dst.clone_from_slice(&src[2..]);
4306 ///
4307 /// assert_eq!(src, [1, 2, 3, 4]);
4308 /// assert_eq!(dst, [3, 4]);
4309 /// ```
4310 ///
4311 /// Rust enforces that there can only be one mutable reference with no
4312 /// immutable references to a particular piece of data in a particular
4313 /// scope. Because of this, attempting to use `clone_from_slice` on a
4314 /// single slice will result in a compile failure:
4315 ///
4316 /// ```compile_fail
4317 /// let mut slice = [1, 2, 3, 4, 5];
4318 ///
4319 /// slice[..2].clone_from_slice(&slice[3..]); // compile fail!
4320 /// ```
4321 ///
4322 /// To work around this, we can use [`split_at_mut`] to create two distinct
4323 /// sub-slices from a slice:
4324 ///
4325 /// ```
4326 /// let mut slice = [1, 2, 3, 4, 5];
4327 ///
4328 /// {
4329 /// let (left, right) = slice.split_at_mut(2);
4330 /// left.clone_from_slice(&right[1..]);
4331 /// }
4332 ///
4333 /// assert_eq!(slice, [4, 5, 3, 4, 5]);
4334 /// ```
4335 ///
4336 /// [`copy_from_slice`]: slice::copy_from_slice
4337 /// [`split_at_mut`]: slice::split_at_mut
4338 #[stable(feature = "clone_from_slice", since = "1.7.0")]
4339 #[track_caller]
4340 #[rustc_const_unstable(feature = "const_clone", issue = "142757")]
4341 pub const fn clone_from_slice(&mut self, src: &[T])
4342 where
4343 T: [const] Clone + [const] Destruct,
4344 {
4345 self.spec_clone_from(src);
4346 }
4347
4348 /// Copies all elements from `src` into `self`, using a memcpy.
4349 ///
4350 /// The length of `src` must be the same as `self`.
4351 ///
4352 /// If `T` does not implement `Copy`, use [`clone_from_slice`].
4353 ///
4354 /// # Panics
4355 ///
4356 /// This function will panic if the two slices have different lengths.
4357 ///
4358 /// # Examples
4359 ///
4360 /// Copying two elements from a slice into another:
4361 ///
4362 /// ```
4363 /// let src = [1, 2, 3, 4];
4364 /// let mut dst = [0, 0];
4365 ///
4366 /// // Because the slices have to be the same length,
4367 /// // we slice the source slice from four elements
4368 /// // to two. It will panic if we don't do this.
4369 /// dst.copy_from_slice(&src[2..]);
4370 ///
4371 /// assert_eq!(src, [1, 2, 3, 4]);
4372 /// assert_eq!(dst, [3, 4]);
4373 /// ```
4374 ///
4375 /// Rust enforces that there can only be one mutable reference with no
4376 /// immutable references to a particular piece of data in a particular
4377 /// scope. Because of this, attempting to use `copy_from_slice` on a
4378 /// single slice will result in a compile failure:
4379 ///
4380 /// ```compile_fail
4381 /// let mut slice = [1, 2, 3, 4, 5];
4382 ///
4383 /// slice[..2].copy_from_slice(&slice[3..]); // compile fail!
4384 /// ```
4385 ///
4386 /// To work around this, we can use [`split_at_mut`] to create two distinct
4387 /// sub-slices from a slice:
4388 ///
4389 /// ```
4390 /// let mut slice = [1, 2, 3, 4, 5];
4391 ///
4392 /// {
4393 /// let (left, right) = slice.split_at_mut(2);
4394 /// left.copy_from_slice(&right[1..]);
4395 /// }
4396 ///
4397 /// assert_eq!(slice, [4, 5, 3, 4, 5]);
4398 /// ```
4399 ///
4400 /// [`clone_from_slice`]: slice::clone_from_slice
4401 /// [`split_at_mut`]: slice::split_at_mut
4402 #[doc(alias = "memcpy")]
4403 #[inline]
4404 #[stable(feature = "copy_from_slice", since = "1.9.0")]
4405 #[rustc_const_stable(feature = "const_copy_from_slice", since = "1.87.0")]
4406 #[track_caller]
4407 pub const fn copy_from_slice(&mut self, src: &[T])
4408 where
4409 T: Copy,
4410 {
4411 // SAFETY: `T` implements `Copy`.
4412 unsafe { copy_from_slice_impl(self, src) }
4413 }
4414
4415 /// Copies elements from one part of the slice to another part of itself,
4416 /// using a memmove.
4417 ///
4418 /// `src` is the range within `self` to copy from. `dest` is the starting
4419 /// index of the range within `self` to copy to, which will have the same
4420 /// length as `src`. The two ranges may overlap. The ends of the two ranges
4421 /// must be less than or equal to `self.len()`.
4422 ///
4423 /// # Panics
4424 ///
4425 /// This function will panic if either range exceeds the end of the slice,
4426 /// or if the end of `src` is before the start.
4427 ///
4428 /// # Examples
4429 ///
4430 /// Copying four bytes within a slice:
4431 ///
4432 /// ```
4433 /// let mut bytes = *b"Hello, World!";
4434 ///
4435 /// bytes.copy_within(1..5, 8);
4436 ///
4437 /// assert_eq!(&bytes, b"Hello, Wello!");
4438 /// ```
4439 #[stable(feature = "copy_within", since = "1.37.0")]
4440 #[track_caller]
4441 #[cfg(not(feature = "ferrocene_subset"))]
4442 pub fn copy_within<R: RangeBounds<usize>>(&mut self, src: R, dest: usize)
4443 where
4444 T: Copy,
4445 {
4446 let Range { start: src_start, end: src_end } = slice::range(src, ..self.len());
4447 let count = src_end - src_start;
4448 assert!(dest <= self.len() - count, "dest is out of bounds");
4449 // SAFETY: the conditions for `ptr::copy` have all been checked above,
4450 // as have those for `ptr::add`.
4451 unsafe {
4452 // Derive both `src_ptr` and `dest_ptr` from the same loan
4453 let ptr = self.as_mut_ptr();
4454 let src_ptr = ptr.add(src_start);
4455 let dest_ptr = ptr.add(dest);
4456 ptr::copy(src_ptr, dest_ptr, count);
4457 }
4458 }
4459
4460 /// Swaps all elements in `self` with those in `other`.
4461 ///
4462 /// The length of `other` must be the same as `self`.
4463 ///
4464 /// # Panics
4465 ///
4466 /// This function will panic if the two slices have different lengths.
4467 ///
4468 /// # Example
4469 ///
4470 /// Swapping two elements across slices:
4471 ///
4472 /// ```
4473 /// let mut slice1 = [0, 0];
4474 /// let mut slice2 = [1, 2, 3, 4];
4475 ///
4476 /// slice1.swap_with_slice(&mut slice2[2..]);
4477 ///
4478 /// assert_eq!(slice1, [3, 4]);
4479 /// assert_eq!(slice2, [1, 2, 0, 0]);
4480 /// ```
4481 ///
4482 /// Rust enforces that there can only be one mutable reference to a
4483 /// particular piece of data in a particular scope. Because of this,
4484 /// attempting to use `swap_with_slice` on a single slice will result in
4485 /// a compile failure:
4486 ///
4487 /// ```compile_fail
4488 /// let mut slice = [1, 2, 3, 4, 5];
4489 /// slice[..2].swap_with_slice(&mut slice[3..]); // compile fail!
4490 /// ```
4491 ///
4492 /// To work around this, we can use [`split_at_mut`] to create two distinct
4493 /// mutable sub-slices from a slice:
4494 ///
4495 /// ```
4496 /// let mut slice = [1, 2, 3, 4, 5];
4497 ///
4498 /// {
4499 /// let (left, right) = slice.split_at_mut(2);
4500 /// left.swap_with_slice(&mut right[1..]);
4501 /// }
4502 ///
4503 /// assert_eq!(slice, [4, 5, 3, 1, 2]);
4504 /// ```
4505 ///
4506 /// [`split_at_mut`]: slice::split_at_mut
4507 #[stable(feature = "swap_with_slice", since = "1.27.0")]
4508 #[rustc_const_unstable(feature = "const_swap_with_slice", issue = "142204")]
4509 #[track_caller]
4510 #[cfg(not(feature = "ferrocene_subset"))]
4511 pub const fn swap_with_slice(&mut self, other: &mut [T]) {
4512 assert!(self.len() == other.len(), "destination and source slices have different lengths");
4513 // SAFETY: `self` is valid for `self.len()` elements by definition, and `src` was
4514 // checked to have the same length. The slices cannot overlap because
4515 // mutable references are exclusive.
4516 unsafe {
4517 ptr::swap_nonoverlapping(self.as_mut_ptr(), other.as_mut_ptr(), self.len());
4518 }
4519 }
4520
4521 /// Function to calculate lengths of the middle and trailing slice for `align_to{,_mut}`.
4522
4523 fn align_to_offsets<U>(&self) -> (usize, usize) {
4524 // What we gonna do about `rest` is figure out what multiple of `U`s we can put in a
4525 // lowest number of `T`s. And how many `T`s we need for each such "multiple".
4526 //
4527 // Consider for example T=u8 U=u16. Then we can put 1 U in 2 Ts. Simple. Now, consider
4528 // for example a case where size_of::<T> = 16, size_of::<U> = 24. We can put 2 Us in
4529 // place of every 3 Ts in the `rest` slice. A bit more complicated.
4530 //
4531 // Formula to calculate this is:
4532 //
4533 // Us = lcm(size_of::<T>, size_of::<U>) / size_of::<U>
4534 // Ts = lcm(size_of::<T>, size_of::<U>) / size_of::<T>
4535 //
4536 // Expanded and simplified:
4537 //
4538 // Us = size_of::<T> / gcd(size_of::<T>, size_of::<U>)
4539 // Ts = size_of::<U> / gcd(size_of::<T>, size_of::<U>)
4540 //
4541 // Luckily since all this is constant-evaluated... performance here matters not!
4542 #[ferrocene::annotation(
4543 "the only use of this function is in a const block, which means it cannot be reached during runtime"
4544 )]
4545 const fn gcd(a: usize, b: usize) -> usize {
4546 if b == 0 { a } else { gcd(b, a % b) }
4547 }
4548
4549 // Explicitly wrap the function call in a const block so it gets
4550 // constant-evaluated even in debug mode.
4551 let gcd: usize = const { gcd(size_of::<T>(), size_of::<U>()) };
4552 let ts: usize = size_of::<U>() / gcd;
4553 let us: usize = size_of::<T>() / gcd;
4554
4555 // Armed with this knowledge, we can find how many `U`s we can fit!
4556 let us_len = self.len() / ts * us;
4557 // And how many `T`s will be in the trailing slice!
4558 let ts_len = self.len() % ts;
4559 (us_len, ts_len)
4560 }
4561
4562 /// Transmutes the slice to a slice of another type, ensuring alignment of the types is
4563 /// maintained.
4564 ///
4565 /// This method splits the slice into three distinct slices: prefix, correctly aligned middle
4566 /// slice of a new type, and the suffix slice. The middle part will be as big as possible under
4567 /// the given alignment constraint and element size.
4568 ///
4569 /// This method has no purpose when either input element `T` or output element `U` are
4570 /// zero-sized and will return the original slice without splitting anything.
4571 ///
4572 /// # Safety
4573 ///
4574 /// This method is essentially a `transmute` with respect to the elements in the returned
4575 /// middle slice, so all the usual caveats pertaining to `transmute::<T, U>` also apply here.
4576 ///
4577 /// # Examples
4578 ///
4579 /// Basic usage:
4580 ///
4581 /// ```
4582 /// unsafe {
4583 /// let bytes: [u8; 7] = [1, 2, 3, 4, 5, 6, 7];
4584 /// let (prefix, shorts, suffix) = bytes.align_to::<u16>();
4585 /// // less_efficient_algorithm_for_bytes(prefix);
4586 /// // more_efficient_algorithm_for_aligned_shorts(shorts);
4587 /// // less_efficient_algorithm_for_bytes(suffix);
4588 /// }
4589 /// ```
4590 #[stable(feature = "slice_align_to", since = "1.30.0")]
4591 #[must_use]
4592 pub unsafe fn align_to<U>(&self) -> (&[T], &[U], &[T]) {
4593 // Note that most of this function will be constant-evaluated,
4594 if U::IS_ZST || T::IS_ZST {
4595 // handle ZSTs specially, which is – don't handle them at all.
4596 return (self, &[], &[]);
4597 }
4598
4599 // First, find at what point do we split between the first and 2nd slice. Easy with
4600 // ptr.align_offset.
4601 let ptr = self.as_ptr();
4602 // SAFETY: See the `align_to_mut` method for the detailed safety comment.
4603 let offset = unsafe { crate::ptr::align_offset(ptr, align_of::<U>()) };
4604 if offset > self.len() {
4605 (self, &[], &[])
4606 } else {
4607 let (left, rest) = self.split_at(offset);
4608 let (us_len, ts_len) = rest.align_to_offsets::<U>();
4609 // Inform Miri that we want to consider the "middle" pointer to be suitably aligned.
4610 #[cfg(miri)]
4611 crate::intrinsics::miri_promise_symbolic_alignment(
4612 rest.as_ptr().cast(),
4613 align_of::<U>(),
4614 );
4615 // SAFETY: now `rest` is definitely aligned, so `from_raw_parts` below is okay,
4616 // since the caller guarantees that we can transmute `T` to `U` safely.
4617 unsafe {
4618 (
4619 left,
4620 from_raw_parts(rest.as_ptr() as *const U, us_len),
4621 from_raw_parts(rest.as_ptr().add(rest.len() - ts_len), ts_len),
4622 )
4623 }
4624 }
4625 }
4626
4627 /// Transmutes the mutable slice to a mutable slice of another type, ensuring alignment of the
4628 /// types is maintained.
4629 ///
4630 /// This method splits the slice into three distinct slices: prefix, correctly aligned middle
4631 /// slice of a new type, and the suffix slice. The middle part will be as big as possible under
4632 /// the given alignment constraint and element size.
4633 ///
4634 /// This method has no purpose when either input element `T` or output element `U` are
4635 /// zero-sized and will return the original slice without splitting anything.
4636 ///
4637 /// # Safety
4638 ///
4639 /// This method is essentially a `transmute` with respect to the elements in the returned
4640 /// middle slice, so all the usual caveats pertaining to `transmute::<T, U>` also apply here.
4641 ///
4642 /// # Examples
4643 ///
4644 /// Basic usage:
4645 ///
4646 /// ```
4647 /// unsafe {
4648 /// let mut bytes: [u8; 7] = [1, 2, 3, 4, 5, 6, 7];
4649 /// let (prefix, shorts, suffix) = bytes.align_to_mut::<u16>();
4650 /// // less_efficient_algorithm_for_bytes(prefix);
4651 /// // more_efficient_algorithm_for_aligned_shorts(shorts);
4652 /// // less_efficient_algorithm_for_bytes(suffix);
4653 /// }
4654 /// ```
4655 #[stable(feature = "slice_align_to", since = "1.30.0")]
4656 #[must_use]
4657 pub unsafe fn align_to_mut<U>(&mut self) -> (&mut [T], &mut [U], &mut [T]) {
4658 // Note that most of this function will be constant-evaluated,
4659 if U::IS_ZST || T::IS_ZST {
4660 // handle ZSTs specially, which is – don't handle them at all.
4661 return (self, &mut [], &mut []);
4662 }
4663
4664 // First, find at what point do we split between the first and 2nd slice. Easy with
4665 // ptr.align_offset.
4666 let ptr = self.as_ptr();
4667 // SAFETY: Here we are ensuring we will use aligned pointers for U for the
4668 // rest of the method. This is done by passing a pointer to &[T] with an
4669 // alignment targeted for U.
4670 // `crate::ptr::align_offset` is called with a correctly aligned and
4671 // valid pointer `ptr` (it comes from a reference to `self`) and with
4672 // a size that is a power of two (since it comes from the alignment for U),
4673 // satisfying its safety constraints.
4674 let offset = unsafe { crate::ptr::align_offset(ptr, align_of::<U>()) };
4675 if offset > self.len() {
4676 (self, &mut [], &mut [])
4677 } else {
4678 let (left, rest) = self.split_at_mut(offset);
4679 let (us_len, ts_len) = rest.align_to_offsets::<U>();
4680 let rest_len = rest.len();
4681 let mut_ptr = rest.as_mut_ptr();
4682 // Inform Miri that we want to consider the "middle" pointer to be suitably aligned.
4683 #[cfg(miri)]
4684 crate::intrinsics::miri_promise_symbolic_alignment(
4685 mut_ptr.cast() as *const (),
4686 align_of::<U>(),
4687 );
4688 // We can't use `rest` again after this, that would invalidate its alias `mut_ptr`!
4689 // SAFETY: see comments for `align_to`.
4690 unsafe {
4691 (
4692 left,
4693 from_raw_parts_mut(mut_ptr as *mut U, us_len),
4694 from_raw_parts_mut(mut_ptr.add(rest_len - ts_len), ts_len),
4695 )
4696 }
4697 }
4698 }
4699
4700 /// Splits a slice into a prefix, a middle of aligned SIMD types, and a suffix.
4701 ///
4702 /// This is a safe wrapper around [`slice::align_to`], so inherits the same
4703 /// guarantees as that method.
4704 ///
4705 /// # Panics
4706 ///
4707 /// This will panic if the size of the SIMD type is different from
4708 /// `LANES` times that of the scalar.
4709 ///
4710 /// At the time of writing, the trait restrictions on `Simd<T, LANES>` keeps
4711 /// that from ever happening, as only power-of-two numbers of lanes are
4712 /// supported. It's possible that, in the future, those restrictions might
4713 /// be lifted in a way that would make it possible to see panics from this
4714 /// method for something like `LANES == 3`.
4715 ///
4716 /// # Examples
4717 ///
4718 /// ```
4719 /// #![feature(portable_simd)]
4720 /// use core::simd::prelude::*;
4721 ///
4722 /// let short = &[1, 2, 3];
4723 /// let (prefix, middle, suffix) = short.as_simd::<4>();
4724 /// assert_eq!(middle, []); // Not enough elements for anything in the middle
4725 ///
4726 /// // They might be split in any possible way between prefix and suffix
4727 /// let it = prefix.iter().chain(suffix).copied();
4728 /// assert_eq!(it.collect::<Vec<_>>(), vec![1, 2, 3]);
4729 ///
4730 /// fn basic_simd_sum(x: &[f32]) -> f32 {
4731 /// use std::ops::Add;
4732 /// let (prefix, middle, suffix) = x.as_simd();
4733 /// let sums = f32x4::from_array([
4734 /// prefix.iter().copied().sum(),
4735 /// 0.0,
4736 /// 0.0,
4737 /// suffix.iter().copied().sum(),
4738 /// ]);
4739 /// let sums = middle.iter().copied().fold(sums, f32x4::add);
4740 /// sums.reduce_sum()
4741 /// }
4742 ///
4743 /// let numbers: Vec<f32> = (1..101).map(|x| x as _).collect();
4744 /// assert_eq!(basic_simd_sum(&numbers[1..99]), 4949.0);
4745 /// ```
4746 #[unstable(feature = "portable_simd", issue = "86656")]
4747 #[must_use]
4748 #[cfg(not(feature = "ferrocene_subset"))]
4749 pub fn as_simd<const LANES: usize>(&self) -> (&[T], &[Simd<T, LANES>], &[T])
4750 where
4751 Simd<T, LANES>: AsRef<[T; LANES]>,
4752 T: simd::SimdElement,
4753 {
4754 // These are expected to always match, as vector types are laid out like
4755 // arrays per <https://llvm.org/docs/LangRef.html#vector-type>, but we
4756 // might as well double-check since it'll optimize away anyhow.
4757 assert_eq!(size_of::<Simd<T, LANES>>(), size_of::<[T; LANES]>());
4758
4759 // SAFETY: The simd types have the same layout as arrays, just with
4760 // potentially-higher alignment, so the de-facto transmutes are sound.
4761 unsafe { self.align_to() }
4762 }
4763
4764 /// Splits a mutable slice into a mutable prefix, a middle of aligned SIMD types,
4765 /// and a mutable suffix.
4766 ///
4767 /// This is a safe wrapper around [`slice::align_to_mut`], so inherits the same
4768 /// guarantees as that method.
4769 ///
4770 /// This is the mutable version of [`slice::as_simd`]; see that for examples.
4771 ///
4772 /// # Panics
4773 ///
4774 /// This will panic if the size of the SIMD type is different from
4775 /// `LANES` times that of the scalar.
4776 ///
4777 /// At the time of writing, the trait restrictions on `Simd<T, LANES>` keeps
4778 /// that from ever happening, as only power-of-two numbers of lanes are
4779 /// supported. It's possible that, in the future, those restrictions might
4780 /// be lifted in a way that would make it possible to see panics from this
4781 /// method for something like `LANES == 3`.
4782 #[unstable(feature = "portable_simd", issue = "86656")]
4783 #[must_use]
4784 #[cfg(not(feature = "ferrocene_subset"))]
4785 pub fn as_simd_mut<const LANES: usize>(&mut self) -> (&mut [T], &mut [Simd<T, LANES>], &mut [T])
4786 where
4787 Simd<T, LANES>: AsMut<[T; LANES]>,
4788 T: simd::SimdElement,
4789 {
4790 // These are expected to always match, as vector types are laid out like
4791 // arrays per <https://llvm.org/docs/LangRef.html#vector-type>, but we
4792 // might as well double-check since it'll optimize away anyhow.
4793 assert_eq!(size_of::<Simd<T, LANES>>(), size_of::<[T; LANES]>());
4794
4795 // SAFETY: The simd types have the same layout as arrays, just with
4796 // potentially-higher alignment, so the de-facto transmutes are sound.
4797 unsafe { self.align_to_mut() }
4798 }
4799
4800 /// Checks if the elements of this slice are sorted.
4801 ///
4802 /// That is, for each element `a` and its following element `b`, `a <= b` must hold. If the
4803 /// slice yields exactly zero or one element, `true` is returned.
4804 ///
4805 /// Note that if `Self::Item` is only `PartialOrd`, but not `Ord`, the above definition
4806 /// implies that this function returns `false` if any two consecutive items are not
4807 /// comparable.
4808 ///
4809 /// # Examples
4810 ///
4811 /// ```
4812 /// let empty: [i32; 0] = [];
4813 ///
4814 /// assert!([1, 2, 2, 9].is_sorted());
4815 /// assert!(![1, 3, 2, 4].is_sorted());
4816 /// assert!([0].is_sorted());
4817 /// assert!(empty.is_sorted());
4818 /// assert!(![0.0, 1.0, f32::NAN].is_sorted());
4819 /// ```
4820 #[inline]
4821 #[stable(feature = "is_sorted", since = "1.82.0")]
4822 #[must_use]
4823 #[cfg(not(feature = "ferrocene_subset"))]
4824 pub fn is_sorted(&self) -> bool
4825 where
4826 T: PartialOrd,
4827 {
4828 // This odd number works the best. 32 + 1 extra due to overlapping chunk boundaries.
4829 const CHUNK_SIZE: usize = 33;
4830 if self.len() < CHUNK_SIZE {
4831 return self.windows(2).all(|w| w[0] <= w[1]);
4832 }
4833 let mut i = 0;
4834 // Check in chunks for autovectorization.
4835 while i < self.len() - CHUNK_SIZE {
4836 let chunk = &self[i..i + CHUNK_SIZE];
4837 if !chunk.windows(2).fold(true, |acc, w| acc & (w[0] <= w[1])) {
4838 return false;
4839 }
4840 // We need to ensure that chunk boundaries are also sorted.
4841 // Overlap the next chunk with the last element of our last chunk.
4842 i += CHUNK_SIZE - 1;
4843 }
4844 self[i..].windows(2).all(|w| w[0] <= w[1])
4845 }
4846
4847 /// Checks if the elements of this slice are sorted using the given comparator function.
4848 ///
4849 /// Instead of using `PartialOrd::partial_cmp`, this function uses the given `compare`
4850 /// function to determine whether two elements are to be considered in sorted order.
4851 ///
4852 /// # Examples
4853 ///
4854 /// ```
4855 /// assert!([1, 2, 2, 9].is_sorted_by(|a, b| a <= b));
4856 /// assert!(![1, 2, 2, 9].is_sorted_by(|a, b| a < b));
4857 ///
4858 /// assert!([0].is_sorted_by(|a, b| true));
4859 /// assert!([0].is_sorted_by(|a, b| false));
4860 ///
4861 /// let empty: [i32; 0] = [];
4862 /// assert!(empty.is_sorted_by(|a, b| false));
4863 /// assert!(empty.is_sorted_by(|a, b| true));
4864 /// ```
4865 #[stable(feature = "is_sorted", since = "1.82.0")]
4866 #[must_use]
4867 #[cfg(not(feature = "ferrocene_subset"))]
4868 pub fn is_sorted_by<'a, F>(&'a self, mut compare: F) -> bool
4869 where
4870 F: FnMut(&'a T, &'a T) -> bool,
4871 {
4872 self.array_windows().all(|[a, b]| compare(a, b))
4873 }
4874
4875 /// Checks if the elements of this slice are sorted using the given key extraction function.
4876 ///
4877 /// Instead of comparing the slice's elements directly, this function compares the keys of the
4878 /// elements, as determined by `f`. Apart from that, it's equivalent to [`is_sorted`]; see its
4879 /// documentation for more information.
4880 ///
4881 /// [`is_sorted`]: slice::is_sorted
4882 ///
4883 /// # Examples
4884 ///
4885 /// ```
4886 /// assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));
4887 /// assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
4888 /// ```
4889 #[inline]
4890 #[stable(feature = "is_sorted", since = "1.82.0")]
4891 #[must_use]
4892 #[cfg(not(feature = "ferrocene_subset"))]
4893 pub fn is_sorted_by_key<'a, F, K>(&'a self, f: F) -> bool
4894 where
4895 F: FnMut(&'a T) -> K,
4896 K: PartialOrd,
4897 {
4898 self.iter().is_sorted_by_key(f)
4899 }
4900
4901 /// Returns the index of the partition point according to the given predicate
4902 /// (the index of the first element of the second partition).
4903 ///
4904 /// The slice is assumed to be partitioned according to the given predicate.
4905 /// This means that all elements for which the predicate returns true are at the start of the slice
4906 /// and all elements for which the predicate returns false are at the end.
4907 /// For example, `[7, 15, 3, 5, 4, 12, 6]` is partitioned under the predicate `x % 2 != 0`
4908 /// (all odd numbers are at the start, all even at the end).
4909 ///
4910 /// If this slice is not partitioned, the returned result is unspecified and meaningless,
4911 /// as this method performs a kind of binary search.
4912 ///
4913 /// See also [`binary_search`], [`binary_search_by`], and [`binary_search_by_key`].
4914 ///
4915 /// [`binary_search`]: slice::binary_search
4916 /// [`binary_search_by`]: slice::binary_search_by
4917 /// [`binary_search_by_key`]: slice::binary_search_by_key
4918 ///
4919 /// # Examples
4920 ///
4921 /// ```
4922 /// let v = [1, 2, 3, 3, 5, 6, 7];
4923 /// let i = v.partition_point(|&x| x < 5);
4924 ///
4925 /// assert_eq!(i, 4);
4926 /// assert!(v[..i].iter().all(|&x| x < 5));
4927 /// assert!(v[i..].iter().all(|&x| !(x < 5)));
4928 /// ```
4929 ///
4930 /// If all elements of the slice match the predicate, including if the slice
4931 /// is empty, then the length of the slice will be returned:
4932 ///
4933 /// ```
4934 /// let a = [2, 4, 8];
4935 /// assert_eq!(a.partition_point(|x| x < &100), a.len());
4936 /// let a: [i32; 0] = [];
4937 /// assert_eq!(a.partition_point(|x| x < &100), 0);
4938 /// ```
4939 ///
4940 /// If you want to insert an item to a sorted vector, while maintaining
4941 /// sort order:
4942 ///
4943 /// ```
4944 /// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
4945 /// let num = 42;
4946 /// let idx = s.partition_point(|&x| x <= num);
4947 /// s.insert(idx, num);
4948 /// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
4949 /// ```
4950 #[stable(feature = "partition_point", since = "1.52.0")]
4951 #[must_use]
4952 #[cfg(not(feature = "ferrocene_subset"))]
4953 pub fn partition_point<P>(&self, mut pred: P) -> usize
4954 where
4955 P: FnMut(&T) -> bool,
4956 {
4957 self.binary_search_by(|x| if pred(x) { Less } else { Greater }).unwrap_or_else(|i| i)
4958 }
4959
4960 /// Removes the subslice corresponding to the given range
4961 /// and returns a reference to it.
4962 ///
4963 /// Returns `None` and does not modify the slice if the given
4964 /// range is out of bounds.
4965 ///
4966 /// Note that this method only accepts one-sided ranges such as
4967 /// `2..` or `..6`, but not `2..6`.
4968 ///
4969 /// # Examples
4970 ///
4971 /// Splitting off the first three elements of a slice:
4972 ///
4973 /// ```
4974 /// let mut slice: &[_] = &['a', 'b', 'c', 'd'];
4975 /// let mut first_three = slice.split_off(..3).unwrap();
4976 ///
4977 /// assert_eq!(slice, &['d']);
4978 /// assert_eq!(first_three, &['a', 'b', 'c']);
4979 /// ```
4980 ///
4981 /// Splitting off a slice starting with the third element:
4982 ///
4983 /// ```
4984 /// let mut slice: &[_] = &['a', 'b', 'c', 'd'];
4985 /// let mut tail = slice.split_off(2..).unwrap();
4986 ///
4987 /// assert_eq!(slice, &['a', 'b']);
4988 /// assert_eq!(tail, &['c', 'd']);
4989 /// ```
4990 ///
4991 /// Getting `None` when `range` is out of bounds:
4992 ///
4993 /// ```
4994 /// let mut slice: &[_] = &['a', 'b', 'c', 'd'];
4995 ///
4996 /// assert_eq!(None, slice.split_off(5..));
4997 /// assert_eq!(None, slice.split_off(..5));
4998 /// assert_eq!(None, slice.split_off(..=4));
4999 /// let expected: &[char] = &['a', 'b', 'c', 'd'];
5000 /// assert_eq!(Some(expected), slice.split_off(..4));
5001 /// ```
5002 #[inline]
5003 #[must_use = "method does not modify the slice if the range is out of bounds"]
5004 #[stable(feature = "slice_take", since = "1.87.0")]
5005 #[cfg(not(feature = "ferrocene_subset"))]
5006 pub fn split_off<'a, R: OneSidedRange<usize>>(
5007 self: &mut &'a Self,
5008 range: R,
5009 ) -> Option<&'a Self> {
5010 let (direction, split_index) = split_point_of(range)?;
5011 if split_index > self.len() {
5012 return None;
5013 }
5014 let (front, back) = self.split_at(split_index);
5015 match direction {
5016 Direction::Front => {
5017 *self = back;
5018 Some(front)
5019 }
5020 Direction::Back => {
5021 *self = front;
5022 Some(back)
5023 }
5024 }
5025 }
5026
5027 /// Removes the subslice corresponding to the given range
5028 /// and returns a mutable reference to it.
5029 ///
5030 /// Returns `None` and does not modify the slice if the given
5031 /// range is out of bounds.
5032 ///
5033 /// Note that this method only accepts one-sided ranges such as
5034 /// `2..` or `..6`, but not `2..6`.
5035 ///
5036 /// # Examples
5037 ///
5038 /// Splitting off the first three elements of a slice:
5039 ///
5040 /// ```
5041 /// let mut slice: &mut [_] = &mut ['a', 'b', 'c', 'd'];
5042 /// let mut first_three = slice.split_off_mut(..3).unwrap();
5043 ///
5044 /// assert_eq!(slice, &mut ['d']);
5045 /// assert_eq!(first_three, &mut ['a', 'b', 'c']);
5046 /// ```
5047 ///
5048 /// Splitting off a slice starting with the third element:
5049 ///
5050 /// ```
5051 /// let mut slice: &mut [_] = &mut ['a', 'b', 'c', 'd'];
5052 /// let mut tail = slice.split_off_mut(2..).unwrap();
5053 ///
5054 /// assert_eq!(slice, &mut ['a', 'b']);
5055 /// assert_eq!(tail, &mut ['c', 'd']);
5056 /// ```
5057 ///
5058 /// Getting `None` when `range` is out of bounds:
5059 ///
5060 /// ```
5061 /// let mut slice: &mut [_] = &mut ['a', 'b', 'c', 'd'];
5062 ///
5063 /// assert_eq!(None, slice.split_off_mut(5..));
5064 /// assert_eq!(None, slice.split_off_mut(..5));
5065 /// assert_eq!(None, slice.split_off_mut(..=4));
5066 /// let expected: &mut [_] = &mut ['a', 'b', 'c', 'd'];
5067 /// assert_eq!(Some(expected), slice.split_off_mut(..4));
5068 /// ```
5069 #[inline]
5070 #[must_use = "method does not modify the slice if the range is out of bounds"]
5071 #[stable(feature = "slice_take", since = "1.87.0")]
5072 #[cfg(not(feature = "ferrocene_subset"))]
5073 pub fn split_off_mut<'a, R: OneSidedRange<usize>>(
5074 self: &mut &'a mut Self,
5075 range: R,
5076 ) -> Option<&'a mut Self> {
5077 let (direction, split_index) = split_point_of(range)?;
5078 if split_index > self.len() {
5079 return None;
5080 }
5081 let (front, back) = mem::take(self).split_at_mut(split_index);
5082 match direction {
5083 Direction::Front => {
5084 *self = back;
5085 Some(front)
5086 }
5087 Direction::Back => {
5088 *self = front;
5089 Some(back)
5090 }
5091 }
5092 }
5093
5094 /// Removes the first element of the slice and returns a reference
5095 /// to it.
5096 ///
5097 /// Returns `None` if the slice is empty.
5098 ///
5099 /// # Examples
5100 ///
5101 /// ```
5102 /// let mut slice: &[_] = &['a', 'b', 'c'];
5103 /// let first = slice.split_off_first().unwrap();
5104 ///
5105 /// assert_eq!(slice, &['b', 'c']);
5106 /// assert_eq!(first, &'a');
5107 /// ```
5108 #[inline]
5109 #[stable(feature = "slice_take", since = "1.87.0")]
5110 #[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
5111 #[cfg(not(feature = "ferrocene_subset"))]
5112 pub const fn split_off_first<'a>(self: &mut &'a Self) -> Option<&'a T> {
5113 // FIXME(const-hack): Use `?` when available in const instead of `let-else`.
5114 let Some((first, rem)) = self.split_first() else { return None };
5115 *self = rem;
5116 Some(first)
5117 }
5118
5119 /// Removes the first element of the slice and returns a mutable
5120 /// reference to it.
5121 ///
5122 /// Returns `None` if the slice is empty.
5123 ///
5124 /// # Examples
5125 ///
5126 /// ```
5127 /// let mut slice: &mut [_] = &mut ['a', 'b', 'c'];
5128 /// let first = slice.split_off_first_mut().unwrap();
5129 /// *first = 'd';
5130 ///
5131 /// assert_eq!(slice, &['b', 'c']);
5132 /// assert_eq!(first, &'d');
5133 /// ```
5134 #[inline]
5135 #[stable(feature = "slice_take", since = "1.87.0")]
5136 #[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
5137 #[cfg(not(feature = "ferrocene_subset"))]
5138 pub const fn split_off_first_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
5139 // FIXME(const-hack): Use `mem::take` and `?` when available in const.
5140 // Original: `mem::take(self).split_first_mut()?`
5141 let Some((first, rem)) = mem::replace(self, &mut []).split_first_mut() else { return None };
5142 *self = rem;
5143 Some(first)
5144 }
5145
5146 /// Removes the last element of the slice and returns a reference
5147 /// to it.
5148 ///
5149 /// Returns `None` if the slice is empty.
5150 ///
5151 /// # Examples
5152 ///
5153 /// ```
5154 /// let mut slice: &[_] = &['a', 'b', 'c'];
5155 /// let last = slice.split_off_last().unwrap();
5156 ///
5157 /// assert_eq!(slice, &['a', 'b']);
5158 /// assert_eq!(last, &'c');
5159 /// ```
5160 #[inline]
5161 #[stable(feature = "slice_take", since = "1.87.0")]
5162 #[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
5163 #[cfg(not(feature = "ferrocene_subset"))]
5164 pub const fn split_off_last<'a>(self: &mut &'a Self) -> Option<&'a T> {
5165 // FIXME(const-hack): Use `?` when available in const instead of `let-else`.
5166 let Some((last, rem)) = self.split_last() else { return None };
5167 *self = rem;
5168 Some(last)
5169 }
5170
5171 /// Removes the last element of the slice and returns a mutable
5172 /// reference to it.
5173 ///
5174 /// Returns `None` if the slice is empty.
5175 ///
5176 /// # Examples
5177 ///
5178 /// ```
5179 /// let mut slice: &mut [_] = &mut ['a', 'b', 'c'];
5180 /// let last = slice.split_off_last_mut().unwrap();
5181 /// *last = 'd';
5182 ///
5183 /// assert_eq!(slice, &['a', 'b']);
5184 /// assert_eq!(last, &'d');
5185 /// ```
5186 #[inline]
5187 #[stable(feature = "slice_take", since = "1.87.0")]
5188 #[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
5189 #[cfg(not(feature = "ferrocene_subset"))]
5190 pub const fn split_off_last_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
5191 // FIXME(const-hack): Use `mem::take` and `?` when available in const.
5192 // Original: `mem::take(self).split_last_mut()?`
5193 let Some((last, rem)) = mem::replace(self, &mut []).split_last_mut() else { return None };
5194 *self = rem;
5195 Some(last)
5196 }
5197
5198 /// Returns mutable references to many indices at once, without doing any checks.
5199 ///
5200 /// An index can be either a `usize`, a [`Range`] or a [`RangeInclusive`]. Note
5201 /// that this method takes an array, so all indices must be of the same type.
5202 /// If passed an array of `usize`s this method gives back an array of mutable references
5203 /// to single elements, while if passed an array of ranges it gives back an array of
5204 /// mutable references to slices.
5205 ///
5206 /// For a safe alternative see [`get_disjoint_mut`].
5207 ///
5208 /// # Safety
5209 ///
5210 /// Calling this method with overlapping or out-of-bounds indices is *[undefined behavior]*
5211 /// even if the resulting references are not used.
5212 ///
5213 /// # Examples
5214 ///
5215 /// ```
5216 /// let x = &mut [1, 2, 4];
5217 ///
5218 /// unsafe {
5219 /// let [a, b] = x.get_disjoint_unchecked_mut([0, 2]);
5220 /// *a *= 10;
5221 /// *b *= 100;
5222 /// }
5223 /// assert_eq!(x, &[10, 2, 400]);
5224 ///
5225 /// unsafe {
5226 /// let [a, b] = x.get_disjoint_unchecked_mut([0..1, 1..3]);
5227 /// a[0] = 8;
5228 /// b[0] = 88;
5229 /// b[1] = 888;
5230 /// }
5231 /// assert_eq!(x, &[8, 88, 888]);
5232 ///
5233 /// unsafe {
5234 /// let [a, b] = x.get_disjoint_unchecked_mut([1..=2, 0..=0]);
5235 /// a[0] = 11;
5236 /// a[1] = 111;
5237 /// b[0] = 1;
5238 /// }
5239 /// assert_eq!(x, &[1, 11, 111]);
5240 /// ```
5241 ///
5242 /// [`get_disjoint_mut`]: slice::get_disjoint_mut
5243 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
5244 #[stable(feature = "get_many_mut", since = "1.86.0")]
5245 #[inline]
5246 #[track_caller]
5247 #[cfg(not(feature = "ferrocene_subset"))]
5248 pub unsafe fn get_disjoint_unchecked_mut<I, const N: usize>(
5249 &mut self,
5250 indices: [I; N],
5251 ) -> [&mut I::Output; N]
5252 where
5253 I: GetDisjointMutIndex + SliceIndex<Self>,
5254 {
5255 // NB: This implementation is written as it is because any variation of
5256 // `indices.map(|i| self.get_unchecked_mut(i))` would make miri unhappy,
5257 // or generate worse code otherwise. This is also why we need to go
5258 // through a raw pointer here.
5259 let slice: *mut [T] = self;
5260 let mut arr: MaybeUninit<[&mut I::Output; N]> = MaybeUninit::uninit();
5261 let arr_ptr = arr.as_mut_ptr();
5262
5263 // SAFETY: We expect `indices` to contain disjunct values that are
5264 // in bounds of `self`.
5265 unsafe {
5266 for i in 0..N {
5267 let idx = indices.get_unchecked(i).clone();
5268 arr_ptr.cast::<&mut I::Output>().add(i).write(&mut *slice.get_unchecked_mut(idx));
5269 }
5270 arr.assume_init()
5271 }
5272 }
5273
5274 /// Returns mutable references to many indices at once.
5275 ///
5276 /// An index can be either a `usize`, a [`Range`] or a [`RangeInclusive`]. Note
5277 /// that this method takes an array, so all indices must be of the same type.
5278 /// If passed an array of `usize`s this method gives back an array of mutable references
5279 /// to single elements, while if passed an array of ranges it gives back an array of
5280 /// mutable references to slices.
5281 ///
5282 /// Returns an error if any index is out-of-bounds, or if there are overlapping indices.
5283 /// An empty range is not considered to overlap if it is located at the beginning or at
5284 /// the end of another range, but is considered to overlap if it is located in the middle.
5285 ///
5286 /// This method does a O(n^2) check to check that there are no overlapping indices, so be careful
5287 /// when passing many indices.
5288 ///
5289 /// # Examples
5290 ///
5291 /// ```
5292 /// let v = &mut [1, 2, 3];
5293 /// if let Ok([a, b]) = v.get_disjoint_mut([0, 2]) {
5294 /// *a = 413;
5295 /// *b = 612;
5296 /// }
5297 /// assert_eq!(v, &[413, 2, 612]);
5298 ///
5299 /// if let Ok([a, b]) = v.get_disjoint_mut([0..1, 1..3]) {
5300 /// a[0] = 8;
5301 /// b[0] = 88;
5302 /// b[1] = 888;
5303 /// }
5304 /// assert_eq!(v, &[8, 88, 888]);
5305 ///
5306 /// if let Ok([a, b]) = v.get_disjoint_mut([1..=2, 0..=0]) {
5307 /// a[0] = 11;
5308 /// a[1] = 111;
5309 /// b[0] = 1;
5310 /// }
5311 /// assert_eq!(v, &[1, 11, 111]);
5312 /// ```
5313 #[stable(feature = "get_many_mut", since = "1.86.0")]
5314 #[inline]
5315 #[cfg(not(feature = "ferrocene_subset"))]
5316 pub fn get_disjoint_mut<I, const N: usize>(
5317 &mut self,
5318 indices: [I; N],
5319 ) -> Result<[&mut I::Output; N], GetDisjointMutError>
5320 where
5321 I: GetDisjointMutIndex + SliceIndex<Self>,
5322 {
5323 get_disjoint_check_valid(&indices, self.len())?;
5324 // SAFETY: The `get_disjoint_check_valid()` call checked that all indices
5325 // are disjunct and in bounds.
5326 unsafe { Ok(self.get_disjoint_unchecked_mut(indices)) }
5327 }
5328
5329 /// Returns the index that an element reference points to.
5330 ///
5331 /// Returns `None` if `element` does not point to the start of an element within the slice.
5332 ///
5333 /// This method is useful for extending slice iterators like [`slice::split`].
5334 ///
5335 /// Note that this uses pointer arithmetic and **does not compare elements**.
5336 /// To find the index of an element via comparison, use
5337 /// [`.iter().position()`](crate::iter::Iterator::position) instead.
5338 ///
5339 /// # Panics
5340 /// Panics if `T` is zero-sized.
5341 ///
5342 /// # Examples
5343 /// Basic usage:
5344 /// ```
5345 /// let nums: &[u32] = &[1, 7, 1, 1];
5346 /// let num = &nums[2];
5347 ///
5348 /// assert_eq!(num, &1);
5349 /// assert_eq!(nums.element_offset(num), Some(2));
5350 /// ```
5351 /// Returning `None` with an unaligned element:
5352 /// ```
5353 /// let arr: &[[u32; 2]] = &[[0, 1], [2, 3]];
5354 /// let flat_arr: &[u32] = arr.as_flattened();
5355 ///
5356 /// let ok_elm: &[u32; 2] = flat_arr[0..2].try_into().unwrap();
5357 /// let weird_elm: &[u32; 2] = flat_arr[1..3].try_into().unwrap();
5358 ///
5359 /// assert_eq!(ok_elm, &[0, 1]);
5360 /// assert_eq!(weird_elm, &[1, 2]);
5361 ///
5362 /// assert_eq!(arr.element_offset(ok_elm), Some(0)); // Points to element 0
5363 /// assert_eq!(arr.element_offset(weird_elm), None); // Points between element 0 and 1
5364 /// ```
5365 #[must_use]
5366 #[cfg(not(feature = "ferrocene_subset"))]
5367 #[stable(feature = "element_offset", since = "1.94.0")]
5368 pub fn element_offset(&self, element: &T) -> Option<usize> {
5369 if T::IS_ZST {
5370 panic!("elements are zero-sized");
5371 }
5372
5373 let self_start = self.as_ptr().addr();
5374 let elem_start = ptr::from_ref(element).addr();
5375
5376 let byte_offset = elem_start.wrapping_sub(self_start);
5377
5378 if !byte_offset.is_multiple_of(size_of::<T>()) {
5379 return None;
5380 }
5381
5382 let offset = byte_offset / size_of::<T>();
5383
5384 if offset < self.len() { Some(offset) } else { None }
5385 }
5386
5387 /// Returns the range of indices that a subslice points to.
5388 ///
5389 /// Returns `None` if `subslice` does not point within the slice or if it is not aligned with the
5390 /// elements in the slice.
5391 ///
5392 /// This method **does not compare elements**. Instead, this method finds the location in the slice that
5393 /// `subslice` was obtained from. To find the index of a subslice via comparison, instead use
5394 /// [`.windows()`](slice::windows)[`.position()`](crate::iter::Iterator::position).
5395 ///
5396 /// This method is useful for extending slice iterators like [`slice::split`].
5397 ///
5398 /// Note that this may return a false positive (either `Some(0..0)` or `Some(self.len()..self.len())`)
5399 /// if `subslice` has a length of zero and points to the beginning or end of another, separate, slice.
5400 ///
5401 /// # Panics
5402 /// Panics if `T` is zero-sized.
5403 ///
5404 /// # Examples
5405 /// Basic usage:
5406 /// ```
5407 /// #![feature(substr_range)]
5408 ///
5409 /// let nums = &[0, 5, 10, 0, 0, 5];
5410 ///
5411 /// let mut iter = nums
5412 /// .split(|t| *t == 0)
5413 /// .map(|n| nums.subslice_range(n).unwrap());
5414 ///
5415 /// assert_eq!(iter.next(), Some(0..0));
5416 /// assert_eq!(iter.next(), Some(1..3));
5417 /// assert_eq!(iter.next(), Some(4..4));
5418 /// assert_eq!(iter.next(), Some(5..6));
5419 /// ```
5420 #[must_use]
5421 #[unstable(feature = "substr_range", issue = "126769")]
5422 #[cfg(not(feature = "ferrocene_subset"))]
5423 pub fn subslice_range(&self, subslice: &[T]) -> Option<Range<usize>> {
5424 if T::IS_ZST {
5425 panic!("elements are zero-sized");
5426 }
5427
5428 let self_start = self.as_ptr().addr();
5429 let subslice_start = subslice.as_ptr().addr();
5430
5431 let byte_start = subslice_start.wrapping_sub(self_start);
5432
5433 if !byte_start.is_multiple_of(size_of::<T>()) {
5434 return None;
5435 }
5436
5437 let start = byte_start / size_of::<T>();
5438 let end = start.wrapping_add(subslice.len());
5439
5440 if start <= self.len() && end <= self.len() { Some(start..end) } else { None }
5441 }
5442
5443 /// Returns the same slice `&[T]`.
5444 ///
5445 /// This method is redundant when used directly on `&[T]`, but
5446 /// it helps dereferencing other "container" types to slices,
5447 /// for example `Box<[T]>` or `Arc<[T]>`.
5448 #[cfg(not(feature = "ferrocene_subset"))]
5449 #[inline]
5450 #[unstable(feature = "str_as_str", issue = "130366")]
5451 pub const fn as_slice(&self) -> &[T] {
5452 self
5453 }
5454
5455 /// Returns the same slice `&mut [T]`.
5456 ///
5457 /// This method is redundant when used directly on `&mut [T]`, but
5458 /// it helps dereferencing other "container" types to slices,
5459 /// for example `Box<[T]>` or `MutexGuard<[T]>`.
5460 #[cfg(not(feature = "ferrocene_subset"))]
5461 #[inline]
5462 #[unstable(feature = "str_as_str", issue = "130366")]
5463 pub const fn as_mut_slice(&mut self) -> &mut [T] {
5464 self
5465 }
5466}
5467
5468#[cfg(not(feature = "ferrocene_subset"))]
5469impl<T> [MaybeUninit<T>] {
5470 /// Transmutes the mutable uninitialized slice to a mutable uninitialized slice of
5471 /// another type, ensuring alignment of the types is maintained.
5472 ///
5473 /// This is a safe wrapper around [`slice::align_to_mut`], so inherits the same
5474 /// guarantees as that method.
5475 ///
5476 /// # Examples
5477 ///
5478 /// ```
5479 /// #![feature(align_to_uninit_mut)]
5480 /// use std::mem::MaybeUninit;
5481 ///
5482 /// pub struct BumpAllocator<'scope> {
5483 /// memory: &'scope mut [MaybeUninit<u8>],
5484 /// }
5485 ///
5486 /// impl<'scope> BumpAllocator<'scope> {
5487 /// pub fn new(memory: &'scope mut [MaybeUninit<u8>]) -> Self {
5488 /// Self { memory }
5489 /// }
5490 /// pub fn try_alloc_uninit<T>(&mut self) -> Option<&'scope mut MaybeUninit<T>> {
5491 /// let first_end = self.memory.as_ptr().align_offset(align_of::<T>()) + size_of::<T>();
5492 /// let prefix = self.memory.split_off_mut(..first_end)?;
5493 /// Some(&mut prefix.align_to_uninit_mut::<T>().1[0])
5494 /// }
5495 /// pub fn try_alloc_u32(&mut self, value: u32) -> Option<&'scope mut u32> {
5496 /// let uninit = self.try_alloc_uninit()?;
5497 /// Some(uninit.write(value))
5498 /// }
5499 /// }
5500 ///
5501 /// let mut memory = [MaybeUninit::<u8>::uninit(); 10];
5502 /// let mut allocator = BumpAllocator::new(&mut memory);
5503 /// let v = allocator.try_alloc_u32(42);
5504 /// assert_eq!(v, Some(&mut 42));
5505 /// ```
5506 #[unstable(feature = "align_to_uninit_mut", issue = "139062")]
5507 #[inline]
5508 #[must_use]
5509 pub fn align_to_uninit_mut<U>(&mut self) -> (&mut Self, &mut [MaybeUninit<U>], &mut Self) {
5510 // SAFETY: `MaybeUninit` is transparent. Correct size and alignment are guaranteed by
5511 // `align_to_mut` itself. Therefore the only thing that we have to ensure for a safe
5512 // `transmute` is that the values are valid for the types involved. But for `MaybeUninit`
5513 // any values are valid, so this operation is safe.
5514 unsafe { self.align_to_mut() }
5515 }
5516}
5517
5518#[cfg(not(feature = "ferrocene_subset"))]
5519impl<T, const N: usize> [[T; N]] {
5520 /// Takes a `&[[T; N]]`, and flattens it to a `&[T]`.
5521 ///
5522 /// For the opposite operation, see [`as_chunks`] and [`as_rchunks`].
5523 ///
5524 /// [`as_chunks`]: slice::as_chunks
5525 /// [`as_rchunks`]: slice::as_rchunks
5526 ///
5527 /// # Panics
5528 ///
5529 /// This panics if the length of the resulting slice would overflow a `usize`.
5530 ///
5531 /// This is only possible when flattening a slice of arrays of zero-sized
5532 /// types, and thus tends to be irrelevant in practice. If
5533 /// `size_of::<T>() > 0`, this will never panic.
5534 ///
5535 /// # Examples
5536 ///
5537 /// ```
5538 /// assert_eq!([[1, 2, 3], [4, 5, 6]].as_flattened(), &[1, 2, 3, 4, 5, 6]);
5539 ///
5540 /// assert_eq!(
5541 /// [[1, 2, 3], [4, 5, 6]].as_flattened(),
5542 /// [[1, 2], [3, 4], [5, 6]].as_flattened(),
5543 /// );
5544 ///
5545 /// let slice_of_empty_arrays: &[[i32; 0]] = &[[], [], [], [], []];
5546 /// assert!(slice_of_empty_arrays.as_flattened().is_empty());
5547 ///
5548 /// let empty_slice_of_arrays: &[[u32; 10]] = &[];
5549 /// assert!(empty_slice_of_arrays.as_flattened().is_empty());
5550 /// ```
5551 #[stable(feature = "slice_flatten", since = "1.80.0")]
5552 #[rustc_const_stable(feature = "const_slice_flatten", since = "1.87.0")]
5553 pub const fn as_flattened(&self) -> &[T] {
5554 let len = if T::IS_ZST {
5555 self.len().checked_mul(N).expect("slice len overflow")
5556 } else {
5557 // SAFETY: `self.len() * N` cannot overflow because `self` is
5558 // already in the address space.
5559 unsafe { self.len().unchecked_mul(N) }
5560 };
5561 // SAFETY: `[T]` is layout-identical to `[T; N]`
5562 unsafe { from_raw_parts(self.as_ptr().cast(), len) }
5563 }
5564
5565 /// Takes a `&mut [[T; N]]`, and flattens it to a `&mut [T]`.
5566 ///
5567 /// For the opposite operation, see [`as_chunks_mut`] and [`as_rchunks_mut`].
5568 ///
5569 /// [`as_chunks_mut`]: slice::as_chunks_mut
5570 /// [`as_rchunks_mut`]: slice::as_rchunks_mut
5571 ///
5572 /// # Panics
5573 ///
5574 /// This panics if the length of the resulting slice would overflow a `usize`.
5575 ///
5576 /// This is only possible when flattening a slice of arrays of zero-sized
5577 /// types, and thus tends to be irrelevant in practice. If
5578 /// `size_of::<T>() > 0`, this will never panic.
5579 ///
5580 /// # Examples
5581 ///
5582 /// ```
5583 /// fn add_5_to_all(slice: &mut [i32]) {
5584 /// for i in slice {
5585 /// *i += 5;
5586 /// }
5587 /// }
5588 ///
5589 /// let mut array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
5590 /// add_5_to_all(array.as_flattened_mut());
5591 /// assert_eq!(array, [[6, 7, 8], [9, 10, 11], [12, 13, 14]]);
5592 /// ```
5593 #[stable(feature = "slice_flatten", since = "1.80.0")]
5594 #[rustc_const_stable(feature = "const_slice_flatten", since = "1.87.0")]
5595 pub const fn as_flattened_mut(&mut self) -> &mut [T] {
5596 let len = if T::IS_ZST {
5597 self.len().checked_mul(N).expect("slice len overflow")
5598 } else {
5599 // SAFETY: `self.len() * N` cannot overflow because `self` is
5600 // already in the address space.
5601 unsafe { self.len().unchecked_mul(N) }
5602 };
5603 // SAFETY: `[T]` is layout-identical to `[T; N]`
5604 unsafe { from_raw_parts_mut(self.as_mut_ptr().cast(), len) }
5605 }
5606}
5607
5608#[cfg(not(feature = "ferrocene_subset"))]
5609impl [f32] {
5610 /// Sorts the slice of floats.
5611 ///
5612 /// This sort is in-place (i.e. does not allocate), *O*(*n* \* log(*n*)) worst-case, and uses
5613 /// the ordering defined by [`f32::total_cmp`].
5614 ///
5615 /// # Current implementation
5616 ///
5617 /// This uses the same sorting algorithm as [`sort_unstable_by`](slice::sort_unstable_by).
5618 ///
5619 /// # Examples
5620 ///
5621 /// ```
5622 /// #![feature(sort_floats)]
5623 /// let mut v = [2.6, -5e-8, f32::NAN, 8.29, f32::INFINITY, -1.0, 0.0, -f32::INFINITY, -0.0];
5624 ///
5625 /// v.sort_floats();
5626 /// let sorted = [-f32::INFINITY, -1.0, -5e-8, -0.0, 0.0, 2.6, 8.29, f32::INFINITY, f32::NAN];
5627 /// assert_eq!(&v[..8], &sorted[..8]);
5628 /// assert!(v[8].is_nan());
5629 /// ```
5630 #[unstable(feature = "sort_floats", issue = "93396")]
5631 #[inline]
5632 pub fn sort_floats(&mut self) {
5633 self.sort_unstable_by(f32::total_cmp);
5634 }
5635}
5636
5637#[cfg(not(feature = "ferrocene_subset"))]
5638impl [f64] {
5639 /// Sorts the slice of floats.
5640 ///
5641 /// This sort is in-place (i.e. does not allocate), *O*(*n* \* log(*n*)) worst-case, and uses
5642 /// the ordering defined by [`f64::total_cmp`].
5643 ///
5644 /// # Current implementation
5645 ///
5646 /// This uses the same sorting algorithm as [`sort_unstable_by`](slice::sort_unstable_by).
5647 ///
5648 /// # Examples
5649 ///
5650 /// ```
5651 /// #![feature(sort_floats)]
5652 /// let mut v = [2.6, -5e-8, f64::NAN, 8.29, f64::INFINITY, -1.0, 0.0, -f64::INFINITY, -0.0];
5653 ///
5654 /// v.sort_floats();
5655 /// let sorted = [-f64::INFINITY, -1.0, -5e-8, -0.0, 0.0, 2.6, 8.29, f64::INFINITY, f64::NAN];
5656 /// assert_eq!(&v[..8], &sorted[..8]);
5657 /// assert!(v[8].is_nan());
5658 /// ```
5659 #[unstable(feature = "sort_floats", issue = "93396")]
5660 #[inline]
5661 pub fn sort_floats(&mut self) {
5662 self.sort_unstable_by(f64::total_cmp);
5663 }
5664}
5665
5666/// Copies `src` to `dest`.
5667///
5668/// # Safety
5669/// `T` must implement one of `Copy` or `TrivialClone`.
5670#[track_caller]
5671const unsafe fn copy_from_slice_impl<T: Clone>(dest: &mut [T], src: &[T]) {
5672 // The panic code path was put into a cold function to not bloat the
5673 // call site.
5674 #[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
5675 #[cfg_attr(panic = "immediate-abort", inline)]
5676 #[track_caller]
5677 const fn len_mismatch_fail(dst_len: usize, src_len: usize) -> ! {
5678 const_panic!(
5679 "copy_from_slice: source slice length does not match destination slice length",
5680 "copy_from_slice: source slice length ({src_len}) does not match destination slice length ({dst_len})",
5681 src_len: usize,
5682 dst_len: usize,
5683 )
5684 }
5685
5686 if dest.len() != src.len() {
5687 len_mismatch_fail(dest.len(), src.len());
5688 }
5689
5690 // SAFETY: `self` is valid for `self.len()` elements by definition, and `src` was
5691 // checked to have the same length. The slices cannot overlap because
5692 // mutable references are exclusive.
5693 unsafe {
5694 ptr::copy_nonoverlapping(src.as_ptr(), dest.as_mut_ptr(), dest.len());
5695 }
5696}
5697
5698#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
5699const trait CloneFromSpec<T> {
5700 fn spec_clone_from(&mut self, src: &[T])
5701 where
5702 T: [const] Destruct;
5703}
5704
5705#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
5706impl<T> const CloneFromSpec<T> for [T]
5707where
5708 T: [const] Clone + [const] Destruct,
5709{
5710 #[track_caller]
5711 default fn spec_clone_from(&mut self, src: &[T]) {
5712 assert!(self.len() == src.len(), "destination and source slices have different lengths");
5713 // NOTE: We need to explicitly slice them to the same length
5714 // to make it easier for the optimizer to elide bounds checking.
5715 // But since it can't be relied on we also have an explicit specialization for T: Copy.
5716 let len = self.len();
5717 let src = &src[..len];
5718 // FIXME(const_hack): make this a `for idx in 0..self.len()` loop.
5719 let mut idx = 0;
5720 while idx < self.len() {
5721 self[idx].clone_from(&src[idx]);
5722 idx += 1;
5723 }
5724 }
5725}
5726
5727#[cfg(not(feature = "ferrocene_subset"))]
5728#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
5729impl<T> const CloneFromSpec<T> for [T]
5730where
5731 T: [const] TrivialClone + [const] Destruct,
5732{
5733 #[track_caller]
5734 fn spec_clone_from(&mut self, src: &[T]) {
5735 // SAFETY: `T` implements `TrivialClone`.
5736 unsafe {
5737 copy_from_slice_impl(self, src);
5738 }
5739 }
5740}
5741
5742#[stable(feature = "rust1", since = "1.0.0")]
5743#[rustc_const_unstable(feature = "const_default", issue = "143894")]
5744#[cfg(not(feature = "ferrocene_subset"))]
5745impl<T> const Default for &[T] {
5746 /// Creates an empty slice.
5747 fn default() -> Self {
5748 &[]
5749 }
5750}
5751
5752#[stable(feature = "mut_slice_default", since = "1.5.0")]
5753#[rustc_const_unstable(feature = "const_default", issue = "143894")]
5754#[cfg(not(feature = "ferrocene_subset"))]
5755impl<T> const Default for &mut [T] {
5756 /// Creates a mutable empty slice.
5757 fn default() -> Self {
5758 &mut []
5759 }
5760}
5761
5762#[unstable(feature = "slice_pattern", reason = "stopgap trait for slice patterns", issue = "56345")]
5763/// Patterns in slices - currently, only used by `strip_prefix` and `strip_suffix`. At a future
5764/// point, we hope to generalise `core::str::Pattern` (which at the time of writing is limited to
5765/// `str`) to slices, and then this trait will be replaced or abolished.
5766#[cfg(not(feature = "ferrocene_subset"))]
5767pub trait SlicePattern {
5768 /// The element type of the slice being matched on.
5769 type Item;
5770
5771 /// Currently, the consumers of `SlicePattern` need a slice.
5772 fn as_slice(&self) -> &[Self::Item];
5773}
5774
5775#[stable(feature = "slice_strip", since = "1.51.0")]
5776#[cfg(not(feature = "ferrocene_subset"))]
5777impl<T> SlicePattern for [T] {
5778 type Item = T;
5779
5780 #[inline]
5781 fn as_slice(&self) -> &[Self::Item] {
5782 self
5783 }
5784}
5785
5786#[stable(feature = "slice_strip", since = "1.51.0")]
5787#[cfg(not(feature = "ferrocene_subset"))]
5788impl<T, const N: usize> SlicePattern for [T; N] {
5789 type Item = T;
5790
5791 #[inline]
5792 fn as_slice(&self) -> &[Self::Item] {
5793 self
5794 }
5795}
5796
5797/// This checks every index against each other, and against `len`.
5798///
5799/// This will do `binomial(N + 1, 2) = N * (N + 1) / 2 = 0, 1, 3, 6, 10, ..`
5800/// comparison operations.
5801#[inline]
5802#[cfg(not(feature = "ferrocene_subset"))]
5803fn get_disjoint_check_valid<I: GetDisjointMutIndex, const N: usize>(
5804 indices: &[I; N],
5805 len: usize,
5806) -> Result<(), GetDisjointMutError> {
5807 // NB: The optimizer should inline the loops into a sequence
5808 // of instructions without additional branching.
5809 for (i, idx) in indices.iter().enumerate() {
5810 if !idx.is_in_bounds(len) {
5811 return Err(GetDisjointMutError::IndexOutOfBounds);
5812 }
5813 for idx2 in &indices[..i] {
5814 if idx.is_overlapping(idx2) {
5815 return Err(GetDisjointMutError::OverlappingIndices);
5816 }
5817 }
5818 }
5819 Ok(())
5820}
5821
5822/// The error type returned by [`get_disjoint_mut`][`slice::get_disjoint_mut`].
5823///
5824/// It indicates one of two possible errors:
5825/// - An index is out-of-bounds.
5826/// - The same index appeared multiple times in the array
5827/// (or different but overlapping indices when ranges are provided).
5828///
5829/// # Examples
5830///
5831/// ```
5832/// use std::slice::GetDisjointMutError;
5833///
5834/// let v = &mut [1, 2, 3];
5835/// assert_eq!(v.get_disjoint_mut([0, 999]), Err(GetDisjointMutError::IndexOutOfBounds));
5836/// assert_eq!(v.get_disjoint_mut([1, 1]), Err(GetDisjointMutError::OverlappingIndices));
5837/// ```
5838#[stable(feature = "get_many_mut", since = "1.86.0")]
5839#[derive(Debug, Clone, PartialEq, Eq)]
5840#[cfg(not(feature = "ferrocene_subset"))]
5841pub enum GetDisjointMutError {
5842 /// An index provided was out-of-bounds for the slice.
5843 IndexOutOfBounds,
5844 /// Two indices provided were overlapping.
5845 OverlappingIndices,
5846}
5847
5848#[stable(feature = "get_many_mut", since = "1.86.0")]
5849#[cfg(not(feature = "ferrocene_subset"))]
5850impl fmt::Display for GetDisjointMutError {
5851 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5852 let msg = match self {
5853 GetDisjointMutError::IndexOutOfBounds => "an index is out of bounds",
5854 GetDisjointMutError::OverlappingIndices => "there were overlapping indices",
5855 };
5856 fmt::Display::fmt(msg, f)
5857 }
5858}
5859
5860#[cfg(not(feature = "ferrocene_subset"))]
5861mod private_get_disjoint_mut_index {
5862 use super::{Range, RangeInclusive, range};
5863
5864 #[unstable(feature = "get_disjoint_mut_helpers", issue = "none")]
5865 pub trait Sealed {}
5866
5867 #[unstable(feature = "get_disjoint_mut_helpers", issue = "none")]
5868 impl Sealed for usize {}
5869 #[unstable(feature = "get_disjoint_mut_helpers", issue = "none")]
5870 impl Sealed for Range<usize> {}
5871 #[unstable(feature = "get_disjoint_mut_helpers", issue = "none")]
5872 impl Sealed for RangeInclusive<usize> {}
5873 #[unstable(feature = "get_disjoint_mut_helpers", issue = "none")]
5874 impl Sealed for range::Range<usize> {}
5875 #[unstable(feature = "get_disjoint_mut_helpers", issue = "none")]
5876 impl Sealed for range::RangeInclusive<usize> {}
5877}
5878
5879/// A helper trait for `<[T]>::get_disjoint_mut()`.
5880///
5881/// # Safety
5882///
5883/// If `is_in_bounds()` returns `true` and `is_overlapping()` returns `false`,
5884/// it must be safe to index the slice with the indices.
5885#[unstable(feature = "get_disjoint_mut_helpers", issue = "none")]
5886#[cfg(not(feature = "ferrocene_subset"))]
5887pub unsafe trait GetDisjointMutIndex:
5888 Clone + private_get_disjoint_mut_index::Sealed
5889{
5890 /// Returns `true` if `self` is in bounds for `len` slice elements.
5891 #[unstable(feature = "get_disjoint_mut_helpers", issue = "none")]
5892 fn is_in_bounds(&self, len: usize) -> bool;
5893
5894 /// Returns `true` if `self` overlaps with `other`.
5895 ///
5896 /// Note that we don't consider zero-length ranges to overlap at the beginning or the end,
5897 /// but do consider them to overlap in the middle.
5898 #[unstable(feature = "get_disjoint_mut_helpers", issue = "none")]
5899 fn is_overlapping(&self, other: &Self) -> bool;
5900}
5901
5902#[unstable(feature = "get_disjoint_mut_helpers", issue = "none")]
5903// SAFETY: We implement `is_in_bounds()` and `is_overlapping()` correctly.
5904#[cfg(not(feature = "ferrocene_subset"))]
5905unsafe impl GetDisjointMutIndex for usize {
5906 #[inline]
5907 fn is_in_bounds(&self, len: usize) -> bool {
5908 *self < len
5909 }
5910
5911 #[inline]
5912 fn is_overlapping(&self, other: &Self) -> bool {
5913 *self == *other
5914 }
5915}
5916
5917#[unstable(feature = "get_disjoint_mut_helpers", issue = "none")]
5918// SAFETY: We implement `is_in_bounds()` and `is_overlapping()` correctly.
5919#[cfg(not(feature = "ferrocene_subset"))]
5920unsafe impl GetDisjointMutIndex for Range<usize> {
5921 #[inline]
5922 fn is_in_bounds(&self, len: usize) -> bool {
5923 (self.start <= self.end) & (self.end <= len)
5924 }
5925
5926 #[inline]
5927 fn is_overlapping(&self, other: &Self) -> bool {
5928 (self.start < other.end) & (other.start < self.end)
5929 }
5930}
5931
5932#[unstable(feature = "get_disjoint_mut_helpers", issue = "none")]
5933// SAFETY: We implement `is_in_bounds()` and `is_overlapping()` correctly.
5934#[cfg(not(feature = "ferrocene_subset"))]
5935unsafe impl GetDisjointMutIndex for RangeInclusive<usize> {
5936 #[inline]
5937 fn is_in_bounds(&self, len: usize) -> bool {
5938 (self.start <= self.end) & (self.end < len)
5939 }
5940
5941 #[inline]
5942 fn is_overlapping(&self, other: &Self) -> bool {
5943 (self.start <= other.end) & (other.start <= self.end)
5944 }
5945}
5946
5947#[unstable(feature = "get_disjoint_mut_helpers", issue = "none")]
5948// SAFETY: We implement `is_in_bounds()` and `is_overlapping()` correctly.
5949#[cfg(not(feature = "ferrocene_subset"))]
5950unsafe impl GetDisjointMutIndex for range::Range<usize> {
5951 #[inline]
5952 fn is_in_bounds(&self, len: usize) -> bool {
5953 Range::from(*self).is_in_bounds(len)
5954 }
5955
5956 #[inline]
5957 fn is_overlapping(&self, other: &Self) -> bool {
5958 Range::from(*self).is_overlapping(&Range::from(*other))
5959 }
5960}
5961
5962#[unstable(feature = "get_disjoint_mut_helpers", issue = "none")]
5963// SAFETY: We implement `is_in_bounds()` and `is_overlapping()` correctly.
5964#[cfg(not(feature = "ferrocene_subset"))]
5965unsafe impl GetDisjointMutIndex for range::RangeInclusive<usize> {
5966 #[inline]
5967 fn is_in_bounds(&self, len: usize) -> bool {
5968 RangeInclusive::from(*self).is_in_bounds(len)
5969 }
5970
5971 #[inline]
5972 fn is_overlapping(&self, other: &Self) -> bool {
5973 RangeInclusive::from(*self).is_overlapping(&RangeInclusive::from(*other))
5974 }
5975}