I can not find within the documentation of Vec<T>
how to retrieve a slice from a specified range.
Is there something like this in the standard library:
let a = vec![1, 2, 3, 4];
let suba = a.subvector(0, 2); // Contains [1, 2];
I can not find within the documentation of Vec<T>
how to retrieve a slice from a specified range.
Is there something like this in the standard library:
let a = vec![1, 2, 3, 4];
let suba = a.subvector(0, 2); // Contains [1, 2];
If you wish to convert the entire
Vec
to a slice, you can use deref coercion:This coercion is automatically applied when calling a function:
You can also call
Vec::as_slice
, but it's a bit less common:See also:
The documentation for
Vec
covers this in the section titled "slicing".You can create a
slice
of aVec
orarray
by indexing it with aRange
(orRangeInclusive
,RangeFrom
,RangeTo
,RangeToInclusive
, orRangeFull
), for example: