Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

構造体 std::vec::Drain

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

バージョン

Rust 1.6.0 ~

解説

Note

ジェネリクスAを明示的に使えるのはnightlyのみ

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

#![allow(unused)]
fn main() {
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

Rust 1.46.0 ~

#![allow(unused)]
fn main() {
pub fn as_slice(&self) -> &[T]
}

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

#![allow(unused)]
fn main() {
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

Note

nightlyでのみ使用可能

#![allow(unused)]
fn main() {
pub fn allocator(&self) -> &A
}

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

#![allow(unused)]
#![feature(allocator_api)]

fn main() {
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

Note

nightlyでのみ使用可能

#![allow(unused)]
fn main() {
pub fn keep_rest(self)
}

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

#![allow(unused)]
#![feature(drain_keep_rest)]

fn main() {
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 */ }

Interator

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 */ }