### starts_with Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html?search=u32+-%3E+bool Checks if the SmallVec starts with a given slice as a prefix. ```APIDOC ## pub fn starts_with(&self, needle: &[T]) -> bool ### Description Returns `true` if `needle` is a prefix of the slice or equal to the slice. ### Parameters - `needle`: The slice to check as a prefix. ### Returns - `bool`: `true` if `needle` is a prefix, `false` otherwise. ### Examples ```rust let v = [10, 40, 30]; assert!(v.starts_with(&[10])); assert!(v.starts_with(&[10, 40])); assert!(v.starts_with(&v)); assert!(!v.starts_with(&[50])); assert!(!v.starts_with(&[10, 50])); ``` Always returns `true` if `needle` is an empty slice: ```rust let v = &[10, 40, 30]; assert!(v.starts_with(&[])); let v: &[u8] = &[]; assert!(v.starts_with(&[])); ``` ``` -------------------------------- ### Select Nth Unstable By Example Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Illustrates `select_nth_unstable_by` using a custom comparator to partition the slice. This example uses a reversed comparator to find elements greater than or equal to the median first. ```rust let mut v = [-5i32, 4, 2, -3, 1]; // Find the items `>=` to the median, the median itself, and the items `<=` to it, by using // a reversed comparator. let (before, median, after) = v.select_nth_unstable_by(2, |a, b| b.cmp(a)); assert!(before == [4, 2] || before == [2, 4]); assert_eq!(median, &mut 1); assert!(after == [-3, -5] || after == [-5, -3]); // We are only guaranteed the slice will be one of the following, based on the way we sort // about the specified index. assert!(v == [2, 4, 1, -5, -3] || v == [2, 4, 1, -3, -5] || v == [4, 2, 1, -5, -3] || v == [4, 2, 1, -3, -5]); ``` -------------------------------- ### SmallVec::rsplit Example Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html?search=u32+-%3E+bool Demonstrates splitting a slice from the end using `rsplit` with a predicate. ```rust let slice = [11, 22, 33, 0, 44, 55]; let mut iter = slice.rsplit(|num| *num == 0); assert_eq!(iter.next().unwrap(), &[44, 55]); assert_eq!(iter.next().unwrap(), &[11, 22, 33]); assert_eq!(iter.next(), None); ``` -------------------------------- ### starts_with Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Checks if the slice starts with the given `needle` slice. Returns `true` if `needle` is a prefix or equal to the slice. ```APIDOC ## pub fn starts_with(&self, needle: &[T]) -> bool ### Description Returns `true` if `needle` is a prefix of the slice or equal to the slice. ### Parameters * `needle`: The slice to check as a prefix. ### Returns `true` if `needle` is a prefix of the slice, `false` otherwise. ### Examples ```rust let v = [10, 40, 30]; assert!(v.starts_with(&[10])); assert!(v.starts_with(&[10, 40])); assert!(v.starts_with(&v)); assert!(!v.starts_with(&[50])); assert!(!v.starts_with(&[10, 50])); ``` Always returns `true` if `needle` is an empty slice: ```rust let v = &[10, 40, 30]; assert!(v.starts_with(&[])); let v: &[u8] = &[]; assert!(v.starts_with(&[])); ``` ``` -------------------------------- ### IntoIter::step_by Source: https://docs.rs/smallvec/latest/smallvec/struct.IntoIter.html?search=std%3A%3Avec Creates an iterator starting at the same point, but stepping by the given amount at each iteration. ```APIDOC ## Iterator::step_by for IntoIter ### Description Creates an iterator starting at the same point, but stepping by the given amount at each iteration. ### Method `step_by(self, step: usize) -> StepBy` ``` -------------------------------- ### IntoIter::step_by Source: https://docs.rs/smallvec/latest/smallvec/struct.IntoIter.html?search= Creates an iterator starting at the same point, but stepping by the given amount at each iteration. ```APIDOC ## IntoIter::step_by ### Description Creates an iterator starting at the same point, but stepping by the given amount at each iteration. ### Method `step_by(self, step: usize) -> StepBy where Self: Sized` ``` -------------------------------- ### Select Nth Unstable Example Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Demonstrates how `select_nth_unstable` partitions a slice around a chosen index, placing elements less than or equal to the pivot before it, and greater than or equal to it after. ```rust let mut v = [-5i32, 4, 2, -3, 1]; // Find the items `<=` to the median, the median itself, and the items `>=` to it. let (lesser, median, greater) = v.select_nth_unstable(2); assert!(lesser == [-3, -5] || lesser == [-5, -3]); assert_eq!(median, &mut 1); assert!(greater == [4, 2] || greater == [2, 4]); // We are only guaranteed the slice will be one of the following, based on the way we sort // about the specified index. assert!(v == [-3, -5, 1, 2, 4] || v == [-5, -3, 1, 2, 4] || v == [-3, -5, 1, 4, 2] || v == [-5, -3, 1, 4, 2]); ``` -------------------------------- ### Check if SmallVec starts with a prefix Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html?search= Use `starts_with` to determine if a slice begins with a given sequence. It returns `true` for an empty prefix slice. ```rust let v = [10, 40, 30]; assert!(v.starts_with(&[10])); assert!(v.starts_with(&[10, 40])); assert!(v.starts_with(&v)); assert!(!v.starts_with(&[50])); assert!(!v.starts_with(&[10, 50])); ``` ```rust let v = &[10, 40, 30]; assert!(v.starts_with(&[])); let v: &[u8] = &[]; assert!(v.starts_with(&[])); ``` -------------------------------- ### Check if SmallVec starts with a prefix Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Use `starts_with` to determine if a slice begins with a given sequence. It returns `true` if the `needle` is a prefix or equal to the slice. ```rust let v = [10, 40, 30]; assert!(v.starts_with(&[10])); assert!(v.starts_with(&[10, 40])); assert!(v.starts_with(&v)); assert!(!v.starts_with(&[50])); assert!(!v.starts_with(&[10, 50])); ``` -------------------------------- ### Mutably iterate over slice in reverse chunks Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Use `rchunks_mut` to get an iterator over mutable slices of a specified size, starting from the end. This allows in-place modification of elements within each chunk. ```rust let v = &mut [0, 0, 0, 0, 0]; let mut count = 1; for chunk in v.rchunks_mut(2) { for elem in chunk.iter_mut() { *elem += count; } count += 1; } assert_eq!(v, &[3, 2, 2, 1, 1]); ``` -------------------------------- ### Initialize and extend SmallVec Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Demonstrates initializing an empty SmallVec and extending it. Shows how it behaves when the capacity is not exceeded versus when it is, causing a spill to the heap. ```rust use smallvec::SmallVec; let mut v = SmallVec::<[u8; 4]>::new(); // initialize an empty vector // The vector can hold up to 4 items without spilling onto the heap. v.extend(0..4); assert_eq!(v.len(), 4); assert!(!v.spilled()); // Pushing another element will force the buffer to spill: v.push(4); assert_eq!(v.len(), 5); assert!(v.spilled()); ``` -------------------------------- ### Example of drain_filter on SmallVec Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Demonstrates splitting a SmallVec into even and odd numbers using drain_filter, reusing the original allocation. The filter closure mutates elements. ```rust let mut numbers: SmallVec<[i32; 16]> = SmallVec::from_slice(&[1i32, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14, 15]); let evens = numbers.drain_filter(|x| *x % 2 == 0).collect::>(); let odds = numbers; assert_eq!(evens, SmallVec::<[i32; 16]>::from_slice(&[2i32, 4, 6, 8, 14])); assert_eq!(odds, SmallVec::<[i32; 16]>::from_slice(&[1i32, 3, 5, 9, 11, 13, 15])); ``` -------------------------------- ### SIMD Slice Splitting Example Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Demonstrates splitting a slice into mutable prefix, middle SIMD, and suffix parts using `as_simd_mut`. This is useful for SIMD operations where data needs to be aligned. ```rust #![feature(portable_simd)] use core::simd::prelude::*; let short = &[1, 2, 3]; let (prefix, middle, suffix) = short.as_simd::<4>(); assert_eq!(middle, []); // Not enough elements for anything in the middle // They might be split in any possible way between prefix and suffix let it = prefix.iter().chain(suffix).copied(); assert_eq!(it.collect::>(), vec![1, 2, 3]); fn basic_simd_sum(x: &[f32]) -> f32 { use std::ops::Add; let (prefix, middle, suffix) = x.as_simd(); let sums = f32x4::from_array([ prefix.iter().copied().sum(), 0.0, 0.0, suffix.iter().copied().sum(), ]); let sums = middle.iter().copied().fold(sums, f32x4::add); sums.reduce_sum() } let numbers: Vec = (1..101).map(|x| x as _).collect(); assert_eq!(basic_simd_sum(&numbers[1..99]), 4949.0); ``` -------------------------------- ### Iterate over slice in reverse chunks Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Use `rchunks` to get an iterator over mutable slices of a specified size, starting from the end. The last chunk may be smaller if the slice length is not divisible by `chunk_size`. ```rust let slice = ['l', 'o', 'r', 'e', 'm']; let mut iter = slice.rchunks(2); assert_eq!(iter.next().unwrap(), &['e', 'm']); assert_eq!(iter.next().unwrap(), &['o', 'r']); assert_eq!(iter.next().unwrap(), &['l']); assert!(iter.next().is_none()); ``` -------------------------------- ### Iterate over slice in exact reverse chunks Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Use `rchunks_exact` to get an iterator over mutable slices of a specified size, starting from the end. This method omits any trailing elements that do not form a full chunk, which can be accessed via `remainder()`. ```rust let slice = ['l', 'o', 'r', 'e', 'm']; let mut iter = slice.rchunks_exact(2); assert_eq!(iter.next().unwrap(), &['e', 'm']); assert_eq!(iter.next().unwrap(), &['o', 'r']); assert!(iter.next().is_none()); assert_eq!(iter.remainder(), &['l']); ``` -------------------------------- ### take Source: https://docs.rs/smallvec/latest/smallvec/struct.DrainFilter.html Creates an iterator that yields the first `n` elements. ```APIDOC ## fn take(self, n: usize) -> Take ### Description Creates an iterator that yields the first `n` elements, or fewer if the underlying iterator ends sooner. ### Method `take` ### Parameters #### Path Parameters - `n` (usize): The maximum number of elements to yield. ### Returns A `Take` iterator that yields at most `n` elements. ``` -------------------------------- ### Get Element Index via Pointer Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Use `element_offset` to find the index of an element reference within a slice using pointer arithmetic. This method does not compare elements and returns `None` if the element reference is not aligned with the start of an element. ```rust let nums: &[u32] = &[1, 7, 1, 1]; let num = &nums[2]; assert_eq!(num, &1); assert_eq!(nums.element_offset(num), Some(2)); ``` ```rust let arr: &[[u32; 2]] = &[[0, 1], [2, 3]]; let flat_arr: &[u32] = arr.as_flattened(); let ok_elm: &[u32; 2] = flat_arr[0..2].try_into().unwrap(); let weird_elm: &[u32; 2] = flat_arr[1..3].try_into().unwrap(); assert_eq!(ok_elm, &[0, 1]); assert_eq!(weird_elm, &[1, 2]); assert_eq!(arr.element_offset(ok_elm), Some(0)); // Points to element 0 assert_eq!(arr.element_offset(weird_elm), None); // Points between element 0 and 1 ``` -------------------------------- ### Handle empty prefix with `starts_with` Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html The `starts_with` method always returns `true` if the `needle` is an empty slice, regardless of the slice's content. ```rust let v = &[10, 40, 30]; assert!(v.starts_with(&[])); let v: &[u8] = &[]; assert!(v.starts_with(&[])); ``` -------------------------------- ### Mutably iterate over slice in exact reverse chunks Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Use `rchunks_exact_mut` to get an iterator over mutable slices of a specified size, starting from the end. This method omits any trailing elements that do not form a full chunk, which can be accessed via `into_remainder()`. ```rust let v = &mut [0, 0, 0, 0, 0]; let mut count = 1; for chunk in v.rchunks_exact_mut(2) { for elem in chunk.iter_mut() { *elem += count; } count += 1; } assert_eq!(v, &[0, 2, 2, 1, 1]); ``` -------------------------------- ### by_ref Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Creates a "by reference" adapter for this instance of Write. ```APIDOC ## fn by_ref(&mut self) -> &mut Self ### Description Creates a "by reference" adapter for this instance of `Write`. ### Signature `fn by_ref(&mut self) -> &mut Self` ``` -------------------------------- ### Get Last Element Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Use `last` to get an immutable reference to the last element. Returns `None` if the slice is empty. ```rust let v = [10, 40, 30]; assert_eq!(Some(&30), v.last()); let w: &[i32] = &[]; assert_eq!(None, w.last()); ``` -------------------------------- ### try_from Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Performs the conversion. ```APIDOC ## fn try_from(value: U) -> Result>::Error> ### Description Performs the conversion. ### Signature `fn try_from(value: U) -> Result>::Error>` ``` -------------------------------- ### Demonstrate shift_right on slices Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html?search= Illustrates the `shift_right` operation on slices, showing how elements are shifted and new elements are inserted. Also demonstrates its similarity to bit shifts and its behavior with sub-slices and when the slice is too short. ```rust #![feature(slice_shift)] // Same as the diagram above let mut a = [5, 6, 7, 8, 9]; let inserted = [0]; let returned = a.shift_right(inserted); assert_eq!(returned, [9]); assert_eq!(a, [0, 5, 6, 7, 8]); // The name comes from this operation's similarity to bitshifts let mut a: u8 = 0b10010110; a >>= 3; assert_eq!(a, 0b00010010_u8); let mut a: [_; 8] = [1, 0, 0, 1, 0, 1, 1, 0]; a.shift_right([0; 3]); assert_eq!(a, [0, 0, 0, 1, 0, 0, 1, 0]); // Remember you can sub-slice to affect less that the whole slice. // For example, this is similar to `.remove(4)` + `.insert(1, 'Z')` let mut a = ['a', 'b', 'c', 'd', 'e', 'f']; assert_eq!(a[1..=4].shift_right(['Z']), ['e']); assert_eq!(a, ['a', 'Z', 'b', 'c', 'd', 'f']); // If the size matches it's equivalent to `mem::replace` let mut a = [1, 2, 3]; assert_eq!(a.shift_right([7, 8, 9]), [1, 2, 3]); assert_eq!(a, [7, 8, 9]); // Some of the "inserted" elements end up returned if the slice is too short let mut a = []; assert_eq!(a.shift_right([1, 2, 3]), [1, 2, 3]); let mut a = [9]; assert_eq!(a.shift_right([1, 2, 3]), [2, 3, 9]); assert_eq!(a, [1]); ``` -------------------------------- ### Get element or subslice by index Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html The `get` method safely retrieves a reference to an element or subslice. It returns `None` if the index is out of bounds. ```rust let v = [10, 40, 30]; assert_eq!(Some(&40), v.get(1)); assert_eq!(Some(&[10, 40][..]), v.get(0..2)); assert_eq!(None, v.get(3)); assert_eq!(None, v.get(0..4)); ``` -------------------------------- ### Get Last Mutable Element Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Use `last_mut` to get a mutable reference to the last element. Returns `None` if the slice is empty. ```rust let x = &mut [0, 1, 2]; if let Some(last) = x.last_mut() { *last = 10; } assert_eq!(x, &[0, 1, 10]); let y: &mut [i32] = &mut []; assert_eq!(None, y.last_mut()); ``` -------------------------------- ### Get mutable reference to the last chunk of a specific size Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Use `last_chunk_mut` to get a mutable slice of the last N elements. Returns `None` if the vector is shorter than N. ```rust let x = &mut [0, 1, 2]; if let Some(last) = x.last_chunk_mut::<2>() { last[0] = 10; last[1] = 20; } assert_eq!(x, &[0, 10, 20]); assert_eq!(None, x.last_chunk_mut::<4>()); ``` -------------------------------- ### into Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Calls U::from(self). ```APIDOC ## fn into(self) -> U ### Description Calls `U::from(self)`. ### Signature `fn into(self) -> U` ``` -------------------------------- ### Get Last Chunk Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Use `last_chunk` with a const generic `N` to get an immutable reference to the last `N` elements as an array. Returns `None` if the slice is shorter than `N`. ```rust let u = [10, 40, 30]; assert_eq!(Some(&[40, 30]), u.last_chunk::<2>()); let v: &[i32] = &[10]; assert_eq!(None, v.last_chunk::<2>()); let w: &[i32] = &[]; assert_eq!(Some(&[]), w.last_chunk::<0>()); ``` -------------------------------- ### Partial Sort Unstable By Key Examples Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Demonstrates various use cases of `partial_sort_unstable_by_key` including empty ranges, single element ranges, subrange sorting, and full range sorting. ```rust #![feature(slice_partial_sort_unstable)] let mut v = [4i32, -5, 1, -3, 2]; // empty range at the beginning, nothing changed v.partial_sort_unstable_by_key(0..0, |k| k.abs()); assert_eq!(v, [4, -5, 1, -3, 2]); // empty range in the middle, partitioning the slice v.partial_sort_unstable_by_key(2..2, |k| k.abs()); for i in 0..2 { assert!(v[i].abs() <= v[2].abs()); } for i in 3..v.len() { assert!(v[2].abs() <= v[i].abs()); } // single element range, same as select_nth_unstable v.partial_sort_unstable_by_key(2..3, |k| k.abs()); for i in 0..2 { assert!(v[i].abs() <= v[2].abs()); } for i in 3..v.len() { assert!(v[2].abs() <= v[i].abs()); } // partial sort a subrange v.partial_sort_unstable_by_key(1..4, |k| k.abs()); assert_eq!(&v[1..4], [2, -3, 4]); // partial sort the whole range, same as sort_unstable v.partial_sort_unstable_by_key(.., |k| k.abs()); assert_eq!(v, [1, 2, -3, 4, -5]); ``` -------------------------------- ### Demonstrating slice shift_right operation Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html?search=std%3A%3Avec Illustrates the `shift_right` operation on slices, showing how elements are shifted and new elements are inserted. It also includes an example of bitwise right shift for unsigned integers. ```rust #![feature(slice_shift)] // Same as the diagram above let mut a = [5, 6, 7, 8, 9]; let inserted = [0]; let returned = a.shift_right(inserted); assert_eq!(returned, [9]); assert_eq!(a, [0, 5, 6, 7, 8]); // The name comes from this operation's similarity to bitshifts let mut a: u8 = 0b10010110; a >>= 3; assert_eq!(a, 0b00010010_u8); let mut a: [_; 8] = [1, 0, 0, 1, 0, 1, 1, 0]; a.shift_right([0; 3]); assert_eq!(a, [0, 0, 0, 1, 0, 0, 1, 0]); // Remember you can sub-slice to affect less that the whole slice. // For example, this is similar to `.remove(4)` + `.insert(1, 'Z')` let mut a = ['a', 'b', 'c', 'd', 'e', 'f']; assert_eq!(a[1..=4].shift_right(['Z']), ['e']); assert_eq!(a, ['a', 'Z', 'b', 'c', 'd', 'f']); // If the size matches it's equivalent to `mem::replace` let mut a = [1, 2, 3]; assert_eq!(a.shift_right([7, 8, 9]), [1, 2, 3]); assert_eq!(a, [7, 8, 9]); // Some of the "inserted" elements end up returned if the slice is too short let mut a = []; assert_eq!(a.shift_right([1, 2, 3]), [1, 2, 3]); let mut a = [9]; assert_eq!(a.shift_right([1, 2, 3]), [2, 3, 9]); assert_eq!(a, [1]); ``` -------------------------------- ### Get First Chunk Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Use `first_chunk` with a const generic `N` to get an immutable reference to the first `N` elements as an array. Returns `None` if the slice is shorter than `N`. ```rust let u = [10, 40, 30]; assert_eq!(Some(&[10, 40]), u.first_chunk::<2>()); let v: &[i32] = &[10]; assert_eq!(None, v.first_chunk::<2>()); let w: &[i32] = &[]; assert_eq!(Some(&[]), w.first_chunk::<0>()); ``` -------------------------------- ### SmallVec::new Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Constructs an empty SmallVec. ```APIDOC ## SmallVec::new ### Description Construct an empty vector. ### Method `SmallVec::new()` ### Parameters None ### Returns An empty `SmallVec`. ``` -------------------------------- ### Get First Mutable Chunk Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Use `first_chunk_mut` with a const generic `N` to get a mutable reference to the first `N` elements as a mutable array. Returns `None` if the slice is shorter than `N`. ```rust let x = &mut [0, 1, 2]; if let Some(first) = x.first_chunk_mut::<2>() { first[0] = 5; first[1] = 4; } assert_eq!(x, &[5, 4, 2]); assert_eq!(None, x.first_chunk_mut::<4>()); ``` -------------------------------- ### Create SmallVec from a list of elements Source: https://docs.rs/smallvec/latest/smallvec/macro.smallvec.html Use this form to initialize a SmallVec with a sequence of elements. The elements are cloned into the vector. ```rust let v: SmallVec<[_; 128]> = smallvec![1, 2, 3]; assert_eq!(v[0], 1); assert_eq!(v[1], 2); assert_eq!(v[2], 3); ``` -------------------------------- ### type_id Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Gets the TypeId of self. ```APIDOC ## fn type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Signature `fn type_id(&self) -> TypeId` ``` -------------------------------- ### try_into Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Performs the conversion. ```APIDOC ## fn try_into(self) -> Result>::Error> ### Description Performs the conversion. ### Signature `fn try_into(self) -> Result>::Error>` ``` -------------------------------- ### Get slice length Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Returns the number of elements in a slice. ```rust let a = [1, 2, 3]; assert_eq!(a.len(), 3); ``` -------------------------------- ### From Implementation Source: https://docs.rs/smallvec/latest/smallvec/struct.DrainFilter.html Implementation of the From trait. ```APIDOC ### impl From for T #### fn from(t: T) -> T Returns the argument unchanged. ``` -------------------------------- ### iter Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Returns an iterator over the slice, yielding items from start to end. ```APIDOC ## pub fn iter(&self) -> Iter<'_, T> ### Description Returns an iterator over the slice. The iterator yields all items from start to end. ### Examples ``` let x = &[1, 2, 4]; let mut iterator = x.iter(); assert_eq!(iterator.next(), Some(&1)); assert_eq!(iterator.next(), Some(&2)); assert_eq!(iterator.next(), Some(&4)); assert_eq!(iterator.next(), None); ``` ``` -------------------------------- ### from_raw_parts Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Creates a `SmallVec` from raw components (pointer, length, capacity). This is an unsafe operation with strict requirements. ```APIDOC ## pub unsafe fn from_raw_parts( ptr: *mut A::Item, length: usize, capacity: usize, ) -> SmallVec ### Description Creates a `SmallVec` directly from the raw components of another `SmallVec`. ### Safety This is highly unsafe, due to the number of invariants that aren’t checked: * `ptr` needs to have been previously allocated via `SmallVec` for its spilled storage (at least, it’s highly likely to be incorrect if it wasn’t). * `ptr`’s `A::Item` type needs to be the same size and alignment that it was allocated with * `length` needs to be less than or equal to `capacity`. * `capacity` needs to be the capacity that the pointer was allocated with. Violating these may cause problems like corrupting the allocator’s internal data structures. Additionally, `capacity` must be greater than the amount of inline storage `A` has; that is, the new `SmallVec` must need to spill over into heap allocated storage. This condition is asserted against. The ownership of `ptr` is effectively transferred to the `SmallVec` which may then deallocate, reallocate or change the contents of memory pointed to by the pointer at will. Ensure that nothing else uses the pointer after calling this function. ### Parameters - `ptr`: A raw pointer to the start of the allocated memory. - `length`: The number of elements currently in the `SmallVec`. - `capacity`: The total capacity of the allocated memory. ``` -------------------------------- ### strip_prefix Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html?search= Returns a subslice with the prefix removed if the slice starts with the given prefix. ```APIDOC ## pub fn strip_prefix

(&self, prefix: &P) -> Option<&[T]> ### Description Returns a subslice with the prefix removed. If the slice starts with `prefix`, returns the subslice after the prefix, wrapped in `Some`. If `prefix` is empty, simply returns the original slice. If `prefix` is equal to the original slice, returns an empty slice. If the slice does not start with `prefix`, returns `None`. ### Parameters - `prefix`: The pattern to strip from the beginning of the slice. ### Returns - `Option<&[T]>`: `Some` containing the subslice after the prefix if found, otherwise `None`. ### Examples ```rust let v = &[10, 40, 30]; assert_eq!(v.strip_prefix(&[10]), Some(&[40, 30][..])); assert_eq!(v.strip_prefix(&[10, 40]), Some(&[30][..])); assert_eq!(v.strip_prefix(&[10, 40, 30]), Some(&[][..])); assert_eq!(v.strip_prefix(&[50]), None); assert_eq!(v.strip_prefix(&[10, 50]), None); let prefix : &str = "he"; assert_eq!(b"hello".strip_prefix(prefix.as_bytes()), Some(b"llo".as_ref())); ``` ``` -------------------------------- ### take Source: https://docs.rs/smallvec/latest/smallvec/struct.Drain.html Creates an iterator that yields the first `n` elements, or fewer if the underlying iterator ends sooner. ```APIDOC ## fn take(self, n: usize) -> Take ### Description Yields at most `n` elements from the iterator. ### Parameters * `n`: The maximum number of elements to yield. ### Returns A new iterator of type `Take`. ``` -------------------------------- ### Get current length of SmallVec Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Returns the number of elements currently stored in the SmallVec. ```rust assert_eq!(v.len(), 5); ``` -------------------------------- ### smallvec! Macro Source: https://docs.rs/smallvec/latest/smallvec/macro.smallvec.html Creates a SmallVec containing the provided arguments. It supports two main forms: initializing with a list of elements or initializing with a repeated element and a count. ```APIDOC ## smallvec! Macro ### Description Creates a `SmallVec` containing the arguments. `smallvec!` allows `SmallVec`s to be defined with the same syntax as array expressions. ### Syntax - `smallvec![elem1, elem2, ..., elemN]` - `smallvec![elem; count]` ### Usage Examples **Initializing with a list of elements:** ```rust let v: SmallVec<[_; 128]> = smallvec![1, 2, 3]; assert_eq!(v[0], 1); assert_eq!(v[1], 2); assert_eq!(v[2], 3); ``` **Initializing with a repeated element and size:** ```rust let v: SmallVec<[_; 0x8000]> = smallvec![1; 3]; assert_eq!(v, SmallVec::from_buf([1, 1, 1])); ``` ### Notes - Unlike array expressions, this syntax supports all elements which implement `Clone`. - The number of elements does not have to be a constant. - This macro uses `clone` to duplicate an expression, so be cautious with types that have non-standard `Clone` implementations. For example, `smallvec![Rc::new(1); 5]` creates a vector of five references to the same boxed integer value. ``` -------------------------------- ### Rebuilding SmallVec from raw parts Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Demonstrates how to rebuild a SmallVec from raw parts, including memory manipulation. This is unsafe and requires careful handling of pointers and capacity. ```rust use std::mem; use std::ptr; fn main() { let mut v: SmallVec<[_; 1]> = smallvec![1, 2, 3]; // Pull out the important parts of `v`. let p = v.as_mut_ptr(); let len = v.len(); let cap = v.capacity(); let spilled = v.spilled(); unsafe { // Forget all about `v`. The heap allocation that stored the // three values won't be deallocated. mem::forget(v); // Overwrite memory with [4, 5, 6]. // // This is only safe if `spilled` is true! Otherwise, we are // writing into the old `SmallVec`'s inline storage on the // stack. assert!(spilled); for i in 0..len { ptr::write(p.add(i), 4 + i); } // Put everything back together into a SmallVec with a different // amount of inline storage, but which is still less than `cap`. let rebuilt = SmallVec::<[_; 2]>::from_raw_parts(p, len, cap); assert_eq!(&*rebuilt, &[4, 5, 6]); } } ``` -------------------------------- ### Create a SmallVec with Arguments Source: https://docs.rs/smallvec/latest/smallvec/index.html?search= Use the `smallvec` macro to create a `SmallVec` containing the provided arguments. This macro is available by default. ```rust smallvec![1, 2, 3]; ``` -------------------------------- ### rsplit Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html?search=std%3A%3Avec Returns an iterator over subslices separated by elements that match a predicate, starting from the end of the slice. ```APIDOC ## pub fn rsplit(&self, pred: F) -> RSplit<'_, T, F> where F: FnMut(&T) -> bool, Returns an iterator over subslices separated by elements that match the given predicate `pred`, starting from the end of the slice and working backwards. The elements that satisfy the predicate are not included in the resulting subslices. ### Parameters - **pred** (F) - A closure that takes a reference to an element and returns `true` if the element should be used as a separator. ### Returns - `RSplit<'_, T, F>` - An iterator over subslices, starting from the end. ``` -------------------------------- ### iter_mut Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Returns an iterator that allows modifying each value in the slice, yielding items from start to end. ```APIDOC ## pub fn iter_mut(&mut self) -> IterMut<'_, T> ### Description Returns an iterator that allows modifying each value. The iterator yields all items from start to end. ### Examples ``` let x = &mut [1, 2, 4]; for elem in x.iter_mut() { *elem += 2; } assert_eq!(x, &[3, 4, 6]); ``` ``` -------------------------------- ### Define SmallVec with a list of elements using smallvec_inline! Source: https://docs.rs/smallvec/latest/smallvec/macro.smallvec_inline.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Use this form to create a `SmallVec` with specific values. The inline storage size is determined by the number of elements provided. This is useful for initializing `SmallVec`s in constant contexts. ```rust const V: SmallVec<[i32; 3]> = smallvec_inline![1, 2, 3]; assert_eq!(V[0], 1); assert_eq!(V[1], 2); assert_eq!(V[2], 3); ``` -------------------------------- ### IntoIter::try_rfold Source: https://docs.rs/smallvec/latest/smallvec/struct.IntoIter.html?search= This is the reverse version of `Iterator::try_fold()`: it takes elements starting from the back of the iterator. ```APIDOC ## IntoIter::try_rfold ### Description This is the reverse version of `Iterator::try_fold()`: it takes elements starting from the back of the iterator. ### Method `try_rfold(&mut self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try` ``` -------------------------------- ### smallvec Macro Source: https://docs.rs/smallvec/latest/smallvec/index.html?search= Creates a `SmallVec` containing the provided arguments. ```APIDOC ## Macro ### `smallvec` Creates a `SmallVec` containing the arguments. ``` -------------------------------- ### fn type_id(&self) -> TypeId Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html?search= Gets the TypeId of the object. This is useful for runtime type identification and comparison. ```APIDOC ## fn type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Method `type_id` ### Parameters - `self` (&Self) - Required - A reference to the object. ### Return Value - `TypeId` - The unique identifier for the type of the object. ``` -------------------------------- ### from_elem Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Creates a SmallVec with `n` copies of a given element. ```APIDOC ## from_elem ### Description Creates a `SmallVec` with `n` copies of `elem`. ### Method `from_elem(elem: A::Item, n: usize) -> Self` ### Example ```rust use smallvec::SmallVec; let v = SmallVec::<[char; 128]>::from_elem('d', 2); assert_eq!(v, SmallVec::from_buf(['d', 'd'])); ``` ``` -------------------------------- ### as_array Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Attempts to get a reference to the underlying array if the specified size `N` matches the length of the slice. ```APIDOC ## pub fn as_array(&self) -> Option<&[T; N]> ### Description Gets a reference to the underlying array. ### Usage If `N` is not exactly equal to the length of `self`, then this method returns `None`. ``` -------------------------------- ### Any Implementation Source: https://docs.rs/smallvec/latest/smallvec/struct.DrainFilter.html Implementation of the Any trait. ```APIDOC ### impl Any for T where T: 'static + ?Sized, #### fn type_id(&self) -> TypeId Gets the `TypeId` of `self`. Read more ``` -------------------------------- ### get Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Returns a reference to an element or subslice based on the provided index. Returns None if the index is out of bounds. ```APIDOC ## pub fn get(&self, index: I) -> Option<&>::Output> ### Description Returns a reference to an element or subslice depending on the type of index. If given a position, returns a reference to the element at that position or `None` if out of bounds. If given a range, returns the subslice corresponding to that range, or `None` if out of bounds. ### Parameters * `index`: `I` where `I: SliceIndex<[T]>` - The index or range to access. ### Returns * `Option<&>::Output>`: A reference to the element or subslice, or `None` if the index is out of bounds. ### Example ```rust let v = [10, 40, 30]; assert_eq!(Some(&40), v.get(1)); assert_eq!(Some(&[10, 40][..]), v.get(0..2)); assert_eq!(None, v.get(3)); assert_eq!(None, v.get(0..4)); ``` ``` -------------------------------- ### as_mut_array Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html?search= Attempts to get a mutable reference to the underlying array if the specified size `N` matches the length of the slice. ```APIDOC ## as_mut_array ### Description Gets a mutable reference to the slice’s underlying array. If `N` is not exactly equal to the length of `self`, then this method returns `None`. ### Type Parameters - `const N: usize`: The expected size of the array. ### Returns - `Option<&mut [T; N]>`: A mutable reference to the array if its length matches `N`, otherwise `None`. ``` -------------------------------- ### as_array Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Attempts to get a reference to the underlying array if the specified size `N` matches the vector's length. ```APIDOC ## pub fn as_array(&self) -> Option<&[T; N]> ### Description Gets a reference to the underlying array. If `N` is not exactly equal to the length of `self`, then this method returns `None`. ``` -------------------------------- ### From Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html?search= Creates a SmallVec from an array. ```APIDOC ## from ### Description Converts to this type from the input type. ### Method `from(array: A) -> SmallVec` ``` -------------------------------- ### Get the first element of a slice Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Returns an Option containing a reference to the first element, or None if the slice is empty. ```rust let v = [10, 40, 30]; assert_eq!(Some(&10), v.first()); let w: &[i32] = &[]; assert_eq!(None, w.first()); ``` -------------------------------- ### clone_to_uninit Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Performs copy-assignment from self to dest. This is a nightly-only experimental API. ```APIDOC ## unsafe fn clone_to_uninit(&self, dest: *mut u8) ### Description Performs copy-assignment from `self` to `dest`. ### Signature `unsafe fn clone_to_uninit(&self, dest: *mut u8) -> ()` ``` -------------------------------- ### Get capacity of SmallVec Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Returns the total number of items the SmallVec can hold before it needs to reallocate its buffer (if it has spilled). ```rust assert!(v.capacity() >= 100); ``` -------------------------------- ### from_raw_parts Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Creates a `SmallVec` directly from the raw components of another `SmallVec`. This is highly unsafe, due to the number of invariants that aren’t checked. ```APIDOC ## from_raw_parts ### Description Creates a `SmallVec` directly from the raw components of another `SmallVec`. ### Safety This is highly unsafe, due to the number of invariants that aren’t checked: * `ptr` needs to have been previously allocated via `SmallVec` for its spilled storage (at least, it’s highly likely to be incorrect if it wasn’t). * `ptr`’s `A::Item` type needs to be the same size and alignment that it was allocated with * `length` needs to be less than or equal to `capacity`. * `capacity` needs to be the capacity that the pointer was allocated with. Violating these may cause problems like corrupting the allocator’s internal data structures. Additionally, `capacity` must be greater than the amount of inline storage `A` has; that is, the new `SmallVec` must need to spill over into heap allocated storage. This condition is asserted against. The ownership of `ptr` is effectively transferred to the `SmallVec` which may then deallocate, reallocate or change the contents of memory pointed to by the pointer at will. Ensure that nothing else uses the pointer after calling this function. ### Signature `pub unsafe fn from_raw_parts( ptr: *mut A::Item, length: usize, capacity: usize, ) -> SmallVec` ``` -------------------------------- ### rchunks Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end of the slice. The chunks are slices and do not overlap. ```APIDOC ## pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> ### Description Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end of the slice. The chunks are slices and do not overlap. See `rchunks_exact` for a variant of this iterator that returns chunks of always exactly `chunk_size` elements, and `chunks` for the same iterator but starting at the beginning of the slice. If your `chunk_size` is a constant, consider using `as_rchunks` instead, which will give references to arrays of exactly that length, rather than slices. ### Panics Panics if `chunk_size` is zero. ``` -------------------------------- ### partition Source: https://docs.rs/smallvec/latest/smallvec/struct.IntoIter.html Consumes an iterator, creating two collections from it. ```APIDOC ## fn partition(self, f: F) -> (B, B) ### Description Consumes an iterator, creating two collections from it. ### Method `partition` ``` -------------------------------- ### IntoIter::step_by Source: https://docs.rs/smallvec/latest/smallvec/struct.IntoIter.html?search=u32+-%3E+bool Creates an iterator starting at the same point, but stepping by the given amount at each iteration. This is part of the `Iterator` trait. ```rust fn step_by(self, step: usize) -> StepBy where Self: Sized, ``` -------------------------------- ### fn by_ref(&mut self) -> &mut Self Source: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html?search= Creates a "by reference" adapter for this instance of Write. This can be useful for chaining write operations or managing mutable access to the SmallVec. ```APIDOC ## fn by_ref(&mut self) -> &mut Self ### Description Creates a "by reference" adapter for this instance of `Write`. ### Method `by_ref` ### Parameters - `self` (*mut Self) - Required - A mutable reference to the `SmallVec` instance. ### Return Value - `*mut Self` - A mutable reference to the `SmallVec` instance, allowing for chained operations. ```