struct slice<Ptr> { ... }
Ptr: Pointer
Fat pointers to a contiguous region of memory.
type &[T] = slice<&T>;
type &mut [T] = slice<&mut T>;
Slice fat "pointers" are just regular structs that compiler handles in a special way with regards
to syntax, implicit coercion and type inference. They are generic over the pointer-to-element type
rather than the element type itself. This is an implementation detail to ensure that &mut [T]
and
&[T]
are distinguished without having to have two distinct types for mutable and const slices.
Methods
impl slice { ... }
-
Empty slice
-
Create a slice from a pointer and length
Example
use std::mem::slice; let arr = [1, 2, 3]; let ptr: &i32 = &arr[0]; let slice = slice::from_raw(ptr, 2); assert_eq!(slice, &[1, 2]);
Run this example -
Returns the length of the slice
-
Returns a pointer to the first element of the slice.
Unlike
&slice[0]
, this is not range-checked, so it will not panic if the slice is empty, but may returnnull
, a dangling or an otherwise invalid pointer. -
Returns
true
if the slice has a length of 0, andfalse
otherwise. -
fn alloc<T>(len: usize) -> &mut [T]
Allocates an array of specified size on the heap.
The values are not initialized.
Example
use std::mem::slice; let hw = "Hello, World!"; let s = slice::alloc::<u8>(hw.len()); defer s.free(); hw.copy_to(&s[0]); assert_eq!(hw, s);
Run this example -
fn alloc_zeroed<T>(len: usize) -> &mut [T]
Allocates an array of specified size on the heap.
The values are zero-initialized.
Example
use std::mem::slice; let s = slice::alloc_zeroed::<u8>(10); defer s.free(); assert_eq!(s, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
Run this example -
fn realloc<T>(slice: &mut [T], len: usize) -> &mut [T]
Resizes a heap-allocated array. The original array must have been allocated with alloc.
If the new size is larger than the old size, the new elements are uninitialized.
Example
use std::mem::slice; let v = slice::alloc::<i32>(2); defer v.free(); v[0] = 1; v[1] = 2; v = v.realloc(3); v[2] = 3; assert_eq!(v, &[1, 2, 3]);
Run this example -
Copies a region of memory from
src
todst
.The memory regions must not overlap, use copy_to if they may be overlapping.
Example
let src = [1, 2, 3]; let dst: [i32; 3]; src[..].copy_to_nonoverlapping(&dst[0]); assert_eq!(dst, [1, 2, 3]);
Run this example -
Copies a region of memory from
src
todst
.The regions may overlap. Using copy_to_nonoverlapping can be more efficient if the ranges do not in fact overlap.
Example
let src = [1, 2, 3, 4, 5, 6]; src[..3].copy_to(&src[1]); src[4..].copy_to(&src[3]); assert_eq!(src, [1, 1, 2, 5, 6, 6]);
Run this example -
Returns the element at the given index.
If the index is out of bounds, the function will return
Option::none()
.Example
let hw = "Hello, world"; assert_eq!(hw[..5].get(0), Option::some('H')); assert_eq!(hw[..5].get(5), Option::none());
Run this example -
Returns a subslice of the given slice.
This is a safe version of range indexing (
slice[a..b]
,slice[a..]
, ...). Instead of panicking or causing UB when the range is out of bounds, this function returnsOption::none()
.Example
let hw = "Hello, world"; assert_eq!(hw.get_range(..5usize), Option::some("Hello")); assert_eq!(hw.get_range(7usize..), Option::some("world")); assert_eq!(hw.get_range(7usize..12), Option::some("world")); assert_eq!(hw.get_range(7usize..13), Option::none());
Run this example -
fn to_array<T, Arr>(self: &[T]) -> Arr
Arr: ArrayOf<T>Copy a slice into a fixed-size array.
The length of the result array must exactly match the length of the slice.
Example
let hw = "Hello, world"; let hw: [u8; 5] = hw[..5].to_array(); assert_eq!(hw[..] as &[u8], "Hello");
Run this example -
fn iter<Ptr>(self: slice<Ptr>) -> SliceIterator<Ptr>
Ptr: Pointer -
fn iter_ref<Ptr>(self: slice<Ptr>) -> SliceRefIterator<&*Ptr>
Ptr: Pointer -
fn iter_mut<T>(self: &mut [T]) -> SliceRefIterator<&mut T>
Returns an iterator over mutable pointers to the elements of the collection.
See IterableMut for details.
-
Fill slice with a value
Example
let v = [0, 0, 0]; v[..].fill(1); assert_eq!(v, [1, 1, 1]);
Run this example -
fn compare<T, Ptr>(lhs: &slice<Ptr>, rhs: &slice<Ptr>) -> Ordering
T: Comparable<T>Ptr: PointerOf<T> -
Frees the slice.
This requires that the slice was allocated with
alloc
oralloc_zeroed
.
Mixins
impl slice { ... }
-
-
Returns
false
if arguments are equal,true
otherwise -
mixin<T, Ptr> Comparable<slice<Ptr>>
T: Comparable<T>Ptr: PointerOf<T> -
Returns
true
iflhs
is strictly less thanrhs
,false
otherwise -
Returns
true
iflhs
is less or equal torhs
,false
otherwise -
Returns
true
iflhs
strictly greater thanrhs
,false
otherwise -
Returns
true
iflhs
greater than or equalrhs
,false
otherwise