Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5327,6 +5327,32 @@ impl<T> [T] {
/// assert_eq!(iter.next(), Some(Range { start: 4, end: 4 }));
/// assert_eq!(iter.next(), Some(Range { start: 5, end: 6 }));
/// ```
/// Returning `None` with a slice not aligned to the elements of the base array:
/// ```
/// use core::range::Range;
/// use core::slice::from_ref;
///
/// let arr: &[[u8; 4]] = &[[0, 1, 2, 3], [4, 5, 6, 7]];
/// let flattened: &[u8] = arr.as_flattened();
///
/// // This sub-slice is aligned with the first element of `arr`:
/// // &[[0, 1, 2, 3], [4, 5, 6, 7]]
/// // &[[0, 1, 2, 3]]
/// let aligned_elem: &[u8; 4] = flattened[0..4].try_into().unwrap();
/// let aligned_slice: &[[u8; 4]] = from_ref(aligned_elem);
///
/// // `aligned_slice` is `&arr[0..1]`
/// assert_eq!(arr.subslice_range(aligned_slice), Some(Range { start: 0, end: 1 }));
///
/// // This sub-slice is **not** aligned to the elements of `arr`, despite pointing inside it:
/// // &[[0, 1, 2, 3], [4, 5, 6, 7]]
/// // &[[1, 2, 3, 4]]
/// let misaligned_elem: &[u8; 4] = flattened[1..5].try_into().unwrap();
/// let misaligned_slice: &[[u8; 4]] = from_ref(misaligned_elem);
///
/// // `misaligned_slice` has no corresponding slice of `arr`
/// assert_eq!(arr.subslice_range(misaligned_slice), None);
/// ```
Comment thread
asquared31415 marked this conversation as resolved.
Resolved
#[must_use]
#[stable(feature = "substr_range", since = "1.98.0")]
pub fn subslice_range(&self, subslice: &[T]) -> Option<core::range::Range<usize>> {
Expand Down
Loading