Rust非公式日本語解説
Vec

std::vec::Drain - Rust非公式日本語解説

構造体 std::vec::Drain

pub struct Drain<'a, T, A = std::alloc::Global>
where
    T: 'a,
    A: std::alloc::Allocator + 'a
{ /* private fields */ }

解説

ベクター内の指定された区間を削除し、取り除かれた要素を与えるイテレータ。 Vecdrainメソッドによって作成される。

let mut v = vec![0, 1, 2, 3];
let u = v.drain(1..).collect::<Vec<_>>();

// 前から2番目以降の要素が削除されている
assert_eq!(v, &[0]);

// 削除された要素が返されている
assert_eq!(u, &[1, 2, 3]);

実装

impl<T, A> Drain<'_, T, A>
where
    A: std::aloc::Allocator
{ /* private fields */ }
impl<'a, T, A> Drain<'a, T, A>
where
    A: std::alloc::Allocator
{
    pub fn as_slice(&self) -> &[T];

    // nightly-only
    pub fn allocator(&self) -> &A;

    // nightly-only
    pub fn keep_rest(self);
}

as_slice

まだ削除されていない要素をスライスで返す関数。

let mut v = vec![0, 1, 2, 3];
let mut drain = v.drain(..);

// まだ何も消費していない
assert_eq!(drain.as_slice(), &[0, 1, 2, 3]);

// 1個前に進める(1個消費する)
let _ = drain.next().unwrap();

// 1番目の要素が削除される
assert_eq!(drain.as_slice(), &[1, 2, 3]);

allocator

nightlyでのみ使用可能

内部で使用しているアロケーターを返す関数。

#![feature(allocator_api)]

use std::any::{Any, TypeId};

let v = vec![0, 1, 2, 3];
assert_eq!(
    // allocatorは参照を返すためcloneする
    v.allocator().clone().type_id(),

    // Vecのアロケーターはデフォルトでstd::alloc::Global
    TypeId::of::<std::alloc::Global>()
);

keep_rest

nightlyでのみ使用可能

一度も消費していない要素をもとのベクターに返還する関数。

#![feature(drain_keep_rest)]

let mut v = vec![0, 1, 2, 3];
let mut drain = v.drain(..);

// ひとつ前に進める(一つ消費する)
drain.next().unwrap();

// vに消費していない要素を返還する
drain.keep_rest();
assert_eq!(v, [1, 2, 3]);

トレイト実装

AsRef

impl<'a, T, A> AsRef<[T]> for Drain<'a, T, A>
where
    A: std::alloc::Allocator
{ /* trait methods */ }

std::fmt::Debug

impl<T, A> std::fmt::Debug for Drain<'_, T, A>
where
    T: std::fmt::Debug,
    A: std::alloc::Allocator,
{ /* trait methods */ }

DoubleEndedIterator

impl<T, A> DoubleEndedIterator for Drain<'_, T, A>
where
    A: std::alloc::Allocator
{ /* trait methods */ }

Drop

impl<T, A> Drop for Drain<'_, T, A>
where
    A: std::alloc::Allocator
{ /* trait methods */ }

ExactSizeIterator

impl<T, A> ExactSizeIterator for Drain<'_, T, A>
where
    A: std::alloc::Allocator
{ /* trait methods */ }

Iterator

impl<T, A> Iterator for Drain<'_, T, A>
where
    A: std::alloc::Allocator
{ /* trait methods */ }

std::iter::FusedIterator

impl<T, A> std::iter::FusedIterator for Drain<'_, T, A>
where
    A: std::alloc::Allocator
{ /* trait methods */ }

Send

impl<T, A> Send for Drain<'_, T, A>
where
    T: Send,
    A: Send + std::alloc::Allocator
{ /* trait methods */ }

Sync

impl<T, A> Sync for Drain<'_, T, A>
where
    T: Sync,
    A: Sync + std::alloc::Allocator
{ /* trait methods */ }

std::iter::TrustedLen

impl<T, A> std::iter::TrustedLen for Drain<'_, T, A>
where
    A: std::alloc::Allocator
{ /* trait methods */ }

自動トレイト実装

std::marker::Freeze

impl<'a, T, A> std::marker::Freeze for Drain<'a, T, A> {
    /* trait methods */
}

std::panic::RefUnwindSafe

impl<'a, T, A> std::panic::RefUnwindSafe for Drain<'a, T, A>
where
    T: std::panic::RefUnwindSafe,
    A: std::panic::RefUnwindSafe
{ /* trait methods */ }

Unpin

impl<'a, T, A> Unpin for Drain<'a, T, A> {
    /* trait methods */
}

std::panic::UnwindSafe

impl<'a, T, A> std::panic::UnwindSafe for Drain<'a, T, A>
where
    T: std::panic::RefUnwindSafe,
    A: std::panic::RefUnwindSafe
{ /* trait methods */ }

ブランケット実装

impl<T> Any for T
where
    T: 'static + ?Sized
{ /* trait methods */ }
impl<T> Borrow<T> for T
where
    T: ?Sized
{ /* trait methods */ }
impl<T> BorrowMut<T> for T
where
    T: ?Sized
{ /* trait methods */ }
impl<T> From<T> for T { 
    /* trait methods */ 
}
impl<T, U> Into<U> for T
where
    U: From<T>
{ /* trait methods */ }
impl<I> IntoIterator for I
where
    I: Iterator,
{ /* trait methods */ }
impl<T, U> TryFrom<U> for T
where
    U: Into<T>
{ /* trait methods */ }
impl<T, U> TryInto<U> for T
where
    U: TryFrom<T>
{ /* trait methods */ }

On this page