slice
import std::core::slice; — in the prelude · source
Types
Slice<T>
type struct Slice<T> {
ptr: T*;
length: u64;
static from_raw(ptr: T*, length: u64) -> Slice<T>;
length(&this) -> u64;
is_empty(&this) -> boolean;
get(&this, index: u64) -> Option<T>
where T: Copy;
get_ref(&this, index: u64) -> Option<T*>;
as_ptr(&this) -> T*;
subslice(&this, start: u64, end: u64) -> Slice<T>;
iter(&this) -> implement Iterator<T>
where T: Copy;
}
A Slice<T> is a (pointer, length) pair: a borrowed view into a contiguous run of values. Algorithms take slices, collections hand them out with as_slice(), and so each algorithm gets written once. for (x in arr) over a fixed-size T[N] lowers to Slice::from_raw, which is why the module is in the prelude.
A slice does not own its storage. The backing memory has to outlive every slice pointing into it.
from_raw(ptr, length) is the constructor: you certify that the range is valid, initialized memory. subslice(start, end) is [start, end) and panics past either bound. as_ptr is the raw pointer to the first element — respect length.
get and iter are gated on T: Copy because they produce an independent bitwise duplicate while the backing storage still owns the element — sound only when T carries no drop obligation. For an owning T, walk by index with length() and get_ref.
Properties
| Property | Type |
|---|---|
ptr | T* |
length | u64 |
Constructors
static from_raw(ptr: T*, length: u64) -> Slice<T>;
static from_raw(ptr: T*, length: u64) -> Slice<T>;Construct from a raw pointer and length. The caller certifies that [ptr, ptr + length) is valid initialized memory for as long as the slice exists.
Methods
length(&this) -> u64;
is_empty(&this) -> boolean;
get(&this, index: u64) -> Option<T> where T: Copy;
get(&this, index: u64) -> Option<T> where T: Copy;Bounds-checked read by value. Gated on T: Copy: the result is an independent bitwise duplicate while the backing storage still owns the element, so this is only sound when T carries no Drop obligation. For owning T, borrow in place with get_ref.
get_ref(&this, index: u64) -> Option<T*>;
get_ref(&this, index: u64) -> Option<T*>;Bounds-checked borrow. Returns a pointer to the element in place, sound for every T. The pointer is valid only while the backing storage outlives the slice.
as_ptr(&this) -> T*;
as_ptr(&this) -> T*;Raw pointer to the first element. Callers must respect length.
subslice(&this, start: u64, end: u64) -> Slice<T>;
subslice(&this, start: u64, end: u64) -> Slice<T>;The sub-slice [start, end). Panics if start > end or end > length.
iter(&this) -> implement Iterator<T> where T: Copy;
iter(&this) -> implement Iterator<T> where T: Copy;Return an iterator that yields each element by value (bitwise copy). Gated on T: Copy for the same reason as get; for owning T, walk by index with length() + get_ref.
SliceIter<T>
type struct SliceIter<T> {
ptr: T*;
remaining: u64;
}
The concrete type behind iter(): a pointer and a remaining count. Its count is O(1), where the trait default would walk the sequence.
Properties
| Property | Type |
|---|---|
ptr | T* |
remaining | u64 |
implement<T> trait Iterator<T> for struct SliceIter<T>
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Slice<T> {
ptr: T*;
length: u64;
}Slice<T>: a borrowed view into a contiguous sequence.
type parameter TSlice.ptr: T*type parameter TSlice.length: u64static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Slice::from_raw(ptr: T*, length: u64) -> Slice<T>Construct from a raw pointer and length. The caller certifies that [ptr, ptr + length) is valid initialized memory for as long as the slice exists.
(parameter) ptr: T*(parameter) length: u64type struct Slice<T> {
ptr: T*;
length: u64;
}Slice<T>: a borrowed view into a contiguous sequence.
std::core::slice&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
Slice::is_empty(&this) -> booleanSlice::get(&this, index: u64) -> Option<T>
where T: CopyBounds-checked read by value. Gated on T: Copy: the result is an independent bitwise duplicate while the backing storage still owns the element, so this is only sound when T carries no Drop obligation. For owning T, borrow in place with get_ref.
(parameter) index: u64where T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait Copy {}Types whose values can be duplicated by a bitwise copy. Integers, floats, pointers, and pure-POD structs qualify; anything that owns resources (heap storage, file handles, refcount) does not.
std::core::markerSlice::get_ref(&this, index: u64) -> Option<T*>Bounds-checked borrow. Returns a pointer to the element in place, sound for every T. The pointer is valid only while the backing storage outlives the slice.
Slice::as_ptr(&this) -> T*Raw pointer to the first element. Callers must respect length.
Slice::subslice(&this, start: u64, end: u64) -> Slice<T>The sub-slice [start, end). Panics if start > end or end > length.
(parameter) start: u64(parameter) end: u64Slice::iter(&this) -> implement Iterator<T>
where T: CopyReturn an iterator that yields each element by value (bitwise copy). Gated on T: Copy for the same reason as get; for owning T, walk by index with length() + get_ref.
-> implement Trait<T>An opaque type: some one concrete type that implements the trait, without naming it. The compiler infers the real type and uses it everywhere; callers see only the trait's methods. How the stdlib returns iterators without exposing their engines.
language referencetype trait Iterator {
type Item;
next(mut &this) -> Option<This::Item>;
count(mut &this) -> u64;
fold<Acc>(mut &this, initial: Acc, f: (Acc, This::Item) -> Acc) -> Acc;
for_each(mut &this, f: (This::Item) -> void) -> void;
take(this, n: u64) -> TakeIter<This>;
skip(this, n: u64) -> SkipIter<This>;
map<B>(this, f: (This::Item) -> B) -> MapIter<This, B>;
filter(this, pred: (&This::Item) -> boolean) -> FilterIter<This>;
chain<J>(this, other: J) -> ChainIter<This, J>;
// … 7 more
}type struct SliceIter<T> {
ptr: T*;
remaining: u64;
}SliceIter.ptr: T*type parameter TSliceIter.remaining: u64import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
Slice.length: u64for (mut i: i32 = 0; i < n; i++) { ... }
for (item in iterable) { ... }Two forms. The C-style for (init; condition; step) scopes its loop variable to the body. for (x in expr) iterates anything with next() -> Option<T>: an iterator, a range such as 0..n, an Array or Slice (their iter() is inserted for you), or a fixed-size array.
xfor (item in iterable) { ... }Separates the loop variable from the sequence in a for loop. The sequence is evaluated once; the loop lowers to loop { match (it.next()) { Some(x) => .., None => break } }.
static Slice::from_raw(ptr: T*, length: u64) -> Slice<T>Construct from a raw pointer and length. The caller certifies that [ptr, ptr + length) is valid initialized memory for as long as the slice exists.
Slice.ptr: T*Slice::subslice(&this, start: u64, end: u64) -> Slice<T>The sub-slice [start, end). Panics if start > end or end > length.
Slice::as_ptr(&this) -> T*Raw pointer to the first element. Callers must respect length.
Slice::get(&this, index: u64) -> Option<T>
where T: CopyBounds-checked read by value. Gated on T: Copy: the result is an independent bitwise duplicate while the backing storage still owns the element, so this is only sound when T carries no Drop obligation. For owning T, borrow in place with get_ref.
Slice::iter(&this) -> implement Iterator<T>
where T: CopyReturn an iterator that yields each element by value (bitwise copy). Gated on T: Copy for the same reason as get; for owning T, walk by index with length() + get_ref.
Slice::get_ref(&this, index: u64) -> Option<T*>Bounds-checked borrow. Returns a pointer to the element in place, sound for every T. The pointer is valid only while the backing storage outlives the slice.
Slice::is_empty(&this) -> booleanSliceIter::count(mut &this) -> u64via trait Iterator
Override the default; slice length is known O(1).
std::core::sliceSliceIter.remaining: u64implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype struct SliceIter<T> {
ptr: T*;
remaining: u64;
}