Struct Drain

pub struct Drain<'a, T: 'a, A: Allocator + 'a = Global> { pub(in ::vec) tail_start: usize, pub(in ::vec) tail_len: usize, pub(in ::vec) iter: Iter<'a, T>, pub(in ::vec) vec: NonNull<Vec<T, A>> }

A draining iterator for Vec<T>.

This struct is created by Vec::drain. See its documentation for more.

Example

let mut v = vec![0, 1, 2];
let iter: std::vec::Drain<'_, _> = v.drain(..);

Fields

tail_start: usize

Index of tail to preserve

tail_len: usize

Length of tail

iter: Iter<'a, T>

Current remaining range to remove

vec: NonNull<Vec<T, A>>

Implementations

impl<'a, T, A: Allocator> Drain<'a, T, A>

fn as_slice(&self) -> &[T]

Returns the remaining items of this iterator as a slice.

Examples

let mut vec = vec!['a', 'b', 'c'];
let mut drain = vec.drain(..);
assert_eq!(drain.as_slice(), &['a', 'b', 'c']);
let _ = drain.next().unwrap();
assert_eq!(drain.as_slice(), &['b', 'c']);
fn allocator(&self) -> &A

Returns a reference to the underlying allocator.

fn keep_rest(self)

Keep unyielded elements in the source Vec.

Examples

#![feature(drain_keep_rest)]

let mut vec = vec!['a', 'b', 'c'];
let mut drain = vec.drain(..);

assert_eq!(drain.next().unwrap(), 'a');

// This call keeps 'b' and 'c' in the vec.
drain.keep_rest();

// If we wouldn't call `keep_rest()`,
// `vec` would be empty.
assert_eq!(vec, ['b', 'c']);

impl<T, A: Allocator> Drain<'_, T, A>

unsafe fn fill<I: Iterator<Item = T>>(&mut self, replace_with: &mut I) -> bool

The range from self.vec.len to self.tail_start contains elements that have been moved out. Fill that range as much as possible with new elements from the replace_with iterator. Returns true if we filled the entire range. (replace_with.next() didn’t return None.)

unsafe fn move_tail(&mut self, additional: usize)

Makes room for inserting more elements before the tail.

Trait Implementations

impl<'a, T, A: Allocator> AsRef<[T]> for Drain<'a, T, A>

fn as_ref(&self) -> &[T]

impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A>

fn next_back(&mut self) -> Option<T>

impl<T, A: Allocator> Drop for Drain<'_, T, A>

fn drop(&mut self)

impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A>

fn is_empty(&self) -> bool

impl<T, A: Allocator> FusedIterator for Drain<'_, T, A>

impl<T, A: Allocator> Iterator for Drain<'_, T, A>

type Item = T;
fn next(&mut self) -> Option<T>
fn size_hint(&self) -> (usize, Option<usize>)

impl<T, A: Allocator> TrustedLen for Drain<'_, T, A>

impl<T: Debug, A: Allocator> Debug for Drain<'_, T, A>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl<T: Send, A: Send + Allocator> Send for Drain<'_, T, A>

impl<T: Sync, A: Sync + Allocator> Sync for Drain<'_, T, A>

Auto Trait Implementations

impl<'a, T, A> Freeze for Drain<'a, T, A> where Iter<'a, T>: Freeze, NonNull<Vec<T, A>>: Freeze,

impl<'a, T, A> RefUnwindSafe for Drain<'a, T, A> where Iter<'a, T>: RefUnwindSafe, NonNull<Vec<T, A>>: RefUnwindSafe,

impl<'a, T, A> Unpin for Drain<'a, T, A> where Iter<'a, T>: Unpin, NonNull<Vec<T, A>>: Unpin,

impl<'a, T, A> UnsafeUnpin for Drain<'a, T, A> where Iter<'a, T>: UnsafeUnpin, NonNull<Vec<T, A>>: UnsafeUnpin,

impl<'a, T, A> UnwindSafe for Drain<'a, T, A> where Iter<'a, T>: UnwindSafe, NonNull<Vec<T, A>>: UnwindSafe,

Blanket Implementations

impl<I> IntoIterator for Drain<'a, T, A> where I: Iterator,

type Item = <I as Iterator>::Item;
type IntoIter = I;
fn into_iter(self) -> I

impl<T> Any for Drain<'a, T, A> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Drain<'a, T, A> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Drain<'a, T, A> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for Drain<'a, T, A>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for Drain<'a, T, A> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for Drain<'a, T, A>

impl<T, U> Into<U> for Drain<'a, T, A> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Drain<'a, T, A> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Drain<'a, T, A> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>