网页 综合 更新时间 2026-11-13 17:54:14

slice - Rust

自动蜘蛛 doc.rust-lang.org 正文约 3982 字
A dynamically-sized view into a contiguous sequence, [T].
Contiguous here means that elements are laid out so that every element is the same distance from its neighbors.
See also the std::slice module.
Slices are a view into a block of memory represented as a pointer and a length.
Slices are either mutable or shared. The shared slice type is &[T], while the mutable slice type is &mut [T], where T represents the element type. For example, you can mutate the block of memory that a mutable slice points to:
It is possible to slice empty subranges of slices by using empty ranges (including slice.len()..slice.len()):
It is not allowed to use subranges that start with lower bound bigger than slice.len():
As slices store the length of the sequence they refer to, they have twice the size of pointers to Sized types. Also see the reference on dynamically sized types.
Some traits are implemented for slices if the element type implements that trait. This includes Eq, Hash and Ord.
The slices implement IntoIterator. The iterator yields references to the slice elements.
The mutable slice yields mutable references to the elements:
This iterator yields mutable references to the slice’s elements, so while the element type of the slice is i32, the element type of the iterator is &mut i32.
Constructs a new boxed slice with uninitialized contents.
Constructs a new boxed slice with uninitialized contents, with the memory being filled with 0 bytes.
See MaybeUninit::zeroed for examples of correct and incorrect usage of this method.
Constructs a new boxed slice with uninitialized contents. Returns an error if the allocation fails.
Constructs a new boxed slice with uninitialized contents, with the memory being filled with 0 bytes. Returns an error if the allocation fails.
See MaybeUninit::zeroed for examples of correct and incorrect usage of this method.
Constructs a new boxed slice with uninitialized contents in the provided allocator.
Constructs a new boxed slice with uninitialized contents in the provided allocator, with the memory being filled with 0 bytes.
See MaybeUninit::zeroed for examples of correct and incorrect usage of this method.
Constructs a new boxed slice with uninitialized contents in the provided allocator. Returns an error if the allocation fails.
Constructs a new boxed slice with uninitialized contents in the provided allocator, with the memory being filled with 0 bytes. Returns an error if the allocation fails.
See MaybeUninit::zeroed for examples of correct and incorrect usage of this method.
Converts the boxed slice into a boxed array.
This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
Returns the original Box<[T]> in the Err variant if self.len() does not equal N.
Converts to Box<[T], A>.
As with MaybeUninit::assume_init, it is up to the caller to guarantee that the values really are in an initialized state. Calling this when the content is not yet fully initialized causes immediate undefined behavior.
Copies the elements from src to self, returning a mutable reference to the now initialized contents of self.
If T does not implement Copy, use write_clone_of_slice instead.
This is similar to slice::copy_from_slice.
This function will panic if the two slices have different lengths.
Clones the elements from src to self, returning a mutable reference to the now initialized contents of self. Any already initialized elements will not be dropped.
If T implements Copy, use write_copy_of_slice instead.
This is similar to slice::clone_from_slice but does not drop existing elements.
This function will panic if the two slices have different lengths, or if the implementation of Clone panics.
If there is a panic, the already cloned elements will be dropped.
Fills a slice with elements by cloning value, returning a mutable reference to the now initialized contents of the slice. Any previously initialized elements will not be dropped.
This is similar to slice::fill.
访问原文 返回搜索 https://doc.rust-lang.org/stable/std/primitive.slice.html
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。