#[repr(transparent)] pub struct IoSlice<'a>(_);
A buffer type used with Write::write_vectored
.
It is semantically a wrapper around an &[u8]
, but is guaranteed to be ABI compatible with the iovec
type on Unix platforms and WSABUF
on Windows.
impl<'a> IoSlice<'a>
[src]
pub fn new(buf: &'a [u8]) -> IoSlice<'a>
[src]
Creates a new IoSlice
wrapping a byte slice.
Panics on Windows if the slice is larger than 4GB.
pub fn len(&self) -> usize
[src]1.0.0
pub fn is_empty(&self) -> bool
[src]1.0.0
pub fn first(&self) -> Option<&T>
[src]1.0.0
Returns the first element of the slice, or None
if it is empty.
pub fn split_first(&self) -> Option<(&T, &[T])>
[src]1.5.0
Returns the first and all the rest of the elements of the slice, or None
if it is empty.
pub fn split_last(&self) -> Option<(&T, &[T])>
[src]1.5.0
Returns the last and all the rest of the elements of the slice, or None
if it is empty.
pub fn last(&self) -> Option<&T>
[src]1.0.0
Returns the last element of the slice, or None
if it is empty.
pub fn get<I>(&self, index: I) -> Option<&<I as SliceIndex<[T]>>::Output> where
I: SliceIndex<[T]>,
[src]1.0.0
Returns a reference to an element or subslice depending on the type of index.
None
if out of bounds.None
if out of bounds.pub unsafe fn get_unchecked<I>(
&self,
index: I
) -> &<I as SliceIndex<[T]>>::Output where
I: SliceIndex<[T]>,
[src]1.0.0
Returns a reference to an element or subslice, without doing bounds checking.
This is generally not recommended, use with caution! For a safe alternative see get
.
pub const fn as_ptr(&self) -> *const T
[src]1.0.0
Returns a raw pointer to the slice's buffer.
The caller must ensure that the slice outlives the pointer this function returns, or else it will end up pointing to garbage.
The caller must also ensure that the memory the pointer (non-transitively) points to is never written to (except inside an UnsafeCell
) using this pointer or any pointer derived from it. If you need to mutate the contents of the slice, use as_mut_ptr
.
Modifying the container referenced by this slice may cause its buffer to be reallocated, which would also make any pointers to it invalid.
pub fn iter(&self) -> Iter<T>
[src]1.0.0
impl<'a, T> Iterator for Iter<'a, T> type Item = &'a T;
Returns an iterator over the slice.
pub fn windows(&self, size: usize) -> Windows<T>
[src]1.0.0
impl<'a, T> Iterator for Windows<'a, T> type Item = &'a [T];
Returns an iterator over all contiguous windows of length size
. The windows overlap. If the slice is shorter than size
, the iterator returns no values.
Panics if size
is 0.
let slice = ['r', 'u', 's', 't']; let mut iter = slice.windows(2); assert_eq!(iter.next().unwrap(), &['r', 'u']); assert_eq!(iter.next().unwrap(), &['u', 's']); assert_eq!(iter.next().unwrap(), &['s', 't']); assert!(iter.next().is_none());
If the slice is shorter than size
:
pub fn chunks(&self, chunk_size: usize) -> Chunks<T>
[src]1.0.0
impl<'a, T> Iterator for Chunks<'a, T> type Item = &'a [T];
Returns an iterator over chunk_size
elements of the slice at a time, starting at the beginning of the slice.
The chunks are slices and do not overlap. If chunk_size
does not divide the length of the slice, then the last chunk will not have length chunk_size
.
See chunks_exact
for a variant of this iterator that returns chunks of always exactly chunk_size
elements, and rchunks
for the same iterator but starting at the end of the slice of the slice.
Panics if chunk_size
is 0.
pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<T>
[src]1.31.0
impl<'a, T> Iterator for ChunksExact<'a, T> type Item = &'a [T];
Returns an iterator over chunk_size
elements of the slice at a time, starting at the beginning of the slice.
The chunks are slices and do not overlap. If chunk_size
does not divide the length of the slice, then the last up to chunk_size-1
elements will be omitted and can be retrieved from the remainder
function of the iterator.
Due to each chunk having exactly chunk_size
elements, the compiler can often optimize the resulting code better than in the case of chunks
.
See chunks
for a variant of this iterator that also returns the remainder as a smaller chunk, and rchunks_exact
for the same iterator but starting at the end of the slice.
Panics if chunk_size
is 0.
pub fn rchunks(&self, chunk_size: usize) -> RChunks<T>
[src]1.31.0
impl<'a, T> Iterator for RChunks<'a, T> type Item = &'a [T];
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. If chunk_size
does not divide the length of the slice, then the last chunk will not have length chunk_size
.
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.
Panics if chunk_size
is 0.
pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<T>
[src]1.31.0
impl<'a, T> Iterator for RChunksExact<'a, T> type Item = &'a [T];
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. If chunk_size
does not divide the length of the slice, then the last up to chunk_size-1
elements will be omitted and can be retrieved from the remainder
function of the iterator.
Due to each chunk having exactly chunk_size
elements, the compiler can often optimize the resulting code better than in the case of chunks
.
See rchunks
for a variant of this iterator that also returns the remainder as a smaller chunk, and chunks_exact
for the same iterator but starting at the beginning of the slice.
Panics if chunk_size
is 0.
pub fn split_at(&self, mid: usize) -> (&[T], &[T])
[src]1.0.0
Divides one slice into two at an index.
The first will contain all indices from [0, mid)
(excluding the index mid
itself) and the second will contain all indices from [mid, len)
(excluding the index len
itself).
Panics if mid > len
.
let v = [1, 2, 3, 4, 5, 6]; { let (left, right) = v.split_at(0); assert!(left == []); assert!(right == [1, 2, 3, 4, 5, 6]); } { let (left, right) = v.split_at(2); assert!(left == [1, 2]); assert!(right == [3, 4, 5, 6]); } { let (left, right) = v.split_at(6); assert!(left == [1, 2, 3, 4, 5, 6]); assert!(right == []); }
pub fn split<F>(&self, pred: F) -> Split<T, F> where
F: FnMut(&T) -> bool,
[src]1.0.0
impl<'a, T, P> Iterator for Split<'a, T, P> where P: FnMut(&T) -> bool, type Item = &'a [T];
Returns an iterator over subslices separated by elements that match pred
. The matched element is not contained in the subslices.
let slice = [10, 40, 33, 20]; let mut iter = slice.split(|num| num % 3 == 0); assert_eq!(iter.next().unwrap(), &[10, 40]); assert_eq!(iter.next().unwrap(), &[20]); assert!(iter.next().is_none());
If the first element is matched, an empty slice will be the first item returned by the iterator. Similarly, if the last element in the slice is matched, an empty slice will be the last item returned by the iterator:
let slice = [10, 40, 33]; let mut iter = slice.split(|num| num % 3 == 0); assert_eq!(iter.next().unwrap(), &[10, 40]); assert_eq!(iter.next().unwrap(), &[]); assert!(iter.next().is_none());
If two matched elements are directly adjacent, an empty slice will be present between them:
pub fn rsplit<F>(&self, pred: F) -> RSplit<T, F> where
F: FnMut(&T) -> bool,
[src]1.27.0
impl<'a, T, P> Iterator for RSplit<'a, T, P> where P: FnMut(&T) -> bool, type Item = &'a [T];
Returns an iterator over subslices separated by elements that match pred
, starting at the end of the slice and working backwards. The matched element is not contained in the subslices.
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);
As with split()
, if the first or last element is matched, an empty slice will be the first (or last) item returned by the iterator.
pub fn splitn<F>(&self, n: usize, pred: F) -> SplitN<T, F> where
F: FnMut(&T) -> bool,
[src]1.0.0
impl<'a, T, P> Iterator for SplitN<'a, T, P> where P: FnMut(&T) -> bool, type Item = &'a [T];
Returns an iterator over subslices separated by elements that match pred
, limited to returning at most n
items. The matched element is not contained in the subslices.
The last element returned, if any, will contain the remainder of the slice.
Print the slice split once by numbers divisible by 3 (i.e., [10, 40]
, [20, 60, 50]
):
pub fn rsplitn<F>(&self, n: usize, pred: F) -> RSplitN<T, F> where
F: FnMut(&T) -> bool,
[src]1.0.0
impl<'a, T, P> Iterator for RSplitN<'a, T, P> where P: FnMut(&T) -> bool, type Item = &'a [T];
Returns an iterator over subslices separated by elements that match pred
limited to returning at most n
items. This starts at the end of the slice and works backwards. The matched element is not contained in the subslices.
The last element returned, if any, will contain the remainder of the slice.
Print the slice split once, starting from the end, by numbers divisible by 3 (i.e., [50]
, [10, 40, 30, 20]
):
pub fn contains(&self, x: &T) -> bool where
T: PartialEq<T>,
[src]1.0.0
Returns true
if the slice contains an element with the given value.
pub fn starts_with(&self, needle: &[T]) -> bool where
T: PartialEq<T>,
[src]1.0.0
Returns true
if needle
is a prefix of the slice.
let v = [10, 40, 30]; assert!(v.starts_with(&[10])); assert!(v.starts_with(&[10, 40])); assert!(!v.starts_with(&[50])); assert!(!v.starts_with(&[10, 50]));
Always returns true
if needle
is an empty slice:
pub fn ends_with(&self, needle: &[T]) -> bool where
T: PartialEq<T>,
[src]1.0.0
Returns true
if needle
is a suffix of the slice.
let v = [10, 40, 30]; assert!(v.ends_with(&[30])); assert!(v.ends_with(&[40, 30])); assert!(!v.ends_with(&[50])); assert!(!v.ends_with(&[50, 30]));
Always returns true
if needle
is an empty slice:
pub fn binary_search(&self, x: &T) -> Result<usize, usize> where
T: Ord,
[src]1.0.0
Binary searches this sorted slice for a given element.
If the value is found then Result::Ok
is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. If the value is not found then Result::Err
is returned, containing the index where a matching element could be inserted while maintaining sorted order.
Looks up a series of four elements. The first is found, with a uniquely determined position; the second and third are not found; the fourth could match any position in [1, 4]
.
pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize> where
F: FnMut(&'a T) -> Ordering,
[src]1.0.0
Binary searches this sorted slice with a comparator function.
The comparator function should implement an order consistent with the sort order of the underlying slice, returning an order code that indicates whether its argument is Less
, Equal
or Greater
the desired target.
If the value is found then Result::Ok
is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. If the value is not found then Result::Err
is returned, containing the index where a matching element could be inserted while maintaining sorted order.
Looks up a series of four elements. The first is found, with a uniquely determined position; the second and third are not found; the fourth could match any position in [1, 4]
.
let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; let seek = 13; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Ok(9)); let seek = 4; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(7)); let seek = 100; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(13)); let seek = 1; let r = s.binary_search_by(|probe| probe.cmp(&seek)); assert!(match r { Ok(1..=4) => true, _ => false, });
pub fn binary_search_by_key<'a, B, F>(
&'a self,
b: &B,
f: F
) -> Result<usize, usize> where
B: Ord,
F: FnMut(&'a T) -> B,
[src]1.10.0
Binary searches this sorted slice with a key extraction function.
Assumes that the slice is sorted by the key, for instance with sort_by_key
using the same key extraction function.
If the value is found then Result::Ok
is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. If the value is not found then Result::Err
is returned, containing the index where a matching element could be inserted while maintaining sorted order.
Looks up a series of four elements in a slice of pairs sorted by their second elements. The first is found, with a uniquely determined position; the second and third are not found; the fourth could match any position in [1, 4]
.
let s = [(0, 0), (2, 1), (4, 1), (5, 1), (3, 1), (1, 2), (2, 3), (4, 5), (5, 8), (3, 13), (1, 21), (2, 34), (4, 55)]; assert_eq!(s.binary_search_by_key(&13, |&(a,b)| b), Ok(9)); assert_eq!(s.binary_search_by_key(&4, |&(a,b)| b), Err(7)); assert_eq!(s.binary_search_by_key(&100, |&(a,b)| b), Err(13)); let r = s.binary_search_by_key(&1, |&(a,b)| b); assert!(match r { Ok(1..=4) => true, _ => false, });
pub unsafe fn align_to<U>(&self) -> (&[T], &[U], &[T])
[src]1.30.0
Transmute the slice to a slice of another type, ensuring alignment of the types is maintained.
This method splits the slice into three distinct slices: prefix, correctly aligned middle slice of a new type, and the suffix slice. The method does a best effort to make the middle slice the greatest length possible for a given type and input slice, but only your algorithm's performance should depend on that, not its correctness.
This method has no purpose when either input element T
or output element U
are zero-sized and will return the original slice without splitting anything.
This method is essentially a transmute
with respect to the elements in the returned middle slice, so all the usual caveats pertaining to transmute::<T, U>
also apply here.
Basic usage:
pub fn is_sorted(&self) -> bool where
T: PartialOrd<T>,
[src]
Checks if the elements of this slice are sorted.
That is, for each element a
and its following element b
, a <= b
must hold. If the slice yields exactly zero or one element, true
is returned.
Note that if Self::Item
is only PartialOrd
, but not Ord
, the above definition implies that this function returns false
if any two consecutive items are not comparable.
pub fn is_sorted_by<F>(&self, compare: F) -> bool where
F: FnMut(&T, &T) -> Option<Ordering>,
[src]
Checks if the elements of this slice are sorted using the given comparator function.
Instead of using PartialOrd::partial_cmp
, this function uses the given compare
function to determine the ordering of two elements. Apart from that, it's equivalent to is_sorted
; see its documentation for more information.
pub fn is_sorted_by_key<F, K>(&self, f: F) -> bool where
F: FnMut(&T) -> K,
K: PartialOrd<K>,
[src]
Checks if the elements of this slice are sorted using the given key extraction function.
Instead of comparing the slice's elements directly, this function compares the keys of the elements, as determined by f
. Apart from that, it's equivalent to is_sorted
; see its documentation for more information.
pub fn is_ascii(&self) -> bool
[src]1.23.0
Checks if all bytes in this slice are within the ASCII range.
pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool
[src]1.23.0
Checks that two slices are an ASCII case-insensitive match.
Same as to_ascii_lowercase(a) == to_ascii_lowercase(b)
, but without allocating and copying temporaries.
pub fn to_vec(&self) -> Vec<T> where
T: Clone,
[src]1.0.0
Copies self
into a new Vec
.
pub fn repeat(&self, n: usize) -> Vec<T> where
T: Copy,
[src]
Creates a vector by repeating a slice n
times.
This function will panic if the capacity would overflow.
Basic usage:
#![feature(repeat_generic_slice)] fn main() { assert_eq!([1, 2].repeat(3), vec![1, 2, 1, 2, 1, 2]); }
A panic upon overflow:
pub fn to_ascii_uppercase(&self) -> Vec<u8>
[src]1.23.0
Returns a vector containing a copy of this slice where each byte is mapped to its ASCII upper case equivalent.
ASCII letters 'a' to 'z' are mapped to 'A' to 'Z', but non-ASCII letters are unchanged.
To uppercase the value in-place, use make_ascii_uppercase
.
pub fn to_ascii_lowercase(&self) -> Vec<u8>
[src]1.23.0
Returns a vector containing a copy of this slice where each byte is mapped to its ASCII lower case equivalent.
ASCII letters 'A' to 'Z' are mapped to 'a' to 'z', but non-ASCII letters are unchanged.
To lowercase the value in-place, use make_ascii_lowercase
.
impl<'a> Deref for IoSlice<'a>
[src]
impl<'a> Debug for IoSlice<'a>
[src]
impl<'a> UnwindSafe for IoSlice<'a>
impl<'a> RefUnwindSafe for IoSlice<'a>
impl<'a> Unpin for IoSlice<'a>
impl<'a> !Send for IoSlice<'a>
impl<'a> !Sync for IoSlice<'a>
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
impl<T> From<T> for T
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
fn borrow(&self) -> &T
[src]
impl<'_, F> Future for &'_ mut F where F: Unpin + Future + ?Sized, type Output = <F as Future>::Output; impl<'_, I> Iterator for &'_ mut I where I: Iterator + ?Sized, type Item = <I as Iterator>::Item; impl<'_, R: Read + ?Sized> Read for &'_ mut R impl<'_, W: Write + ?Sized> Write for &'_ mut W
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
fn borrow_mut(&mut self) -> &mut T
[src]
impl<'_, F> Future for &'_ mut F where F: Unpin + Future + ?Sized, type Output = <F as Future>::Output; impl<'_, I> Iterator for &'_ mut I where I: Iterator + ?Sized, type Item = <I as Iterator>::Item; impl<'_, R: Read + ?Sized> Read for &'_ mut R impl<'_, W: Write + ?Sized> Write for &'_ mut W
impl<T> Any for T where
T: 'static + ?Sized,
[src]
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/io/struct.IoSlice.html