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のみ
ベクター内の指定された区間を削除し、取り除かれた要素を与えるイテレータ。
Vecのdrainメソッドによって作成される。
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);
}**Rust 1.46.0 ~**
pub fn as_slice(&self) -> &[T]まだ削除されていない要素をスライスで返す関数。
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]); Note
nightlyでのみ使用可能
pub fn allocator(&self) -> &A内部で使用しているアロケーターを返す関数。
#![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>()
);Note
nightlyでのみ使用可能
pub fn keep_rest(self)一度も消費していない要素をもとのベクターに返還する関数。
#![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]);impl<'a, T, A> AsRef<[T]> for Drain<'a, T, A>
where
A: std::alloc::Allocator
{ /* trait methods */ }impl<T, A> std::fmt::Debug for Drain<'_, T, A>
where
T: std::fmt::Debug,
A: std::alloc::Allocator,
{ /* trait methods */ }impl<T, A> DoubleEndedIterator for Drain<'_, T, A>
where
A: std::alloc::Allocator
{ /* trait methods */ }impl<T, A> Drop for Drain<'_, T, A>
where
A: std::alloc::Allocator
{ /* trait methods */ }impl<T, A> ExactSizeIterator for Drain<'_, T, A>
where
A: std::alloc::Allocator
{ /* trait methods */ }impl<T, A> Iterator for Drain<'_, T, A>
where
A: std::alloc::Allocator
{ /* trait methods */ }impl<T, A> std::iter::FusedIterator for Drain<'_, T, A>
where
A: std::alloc::Allocator
{ /* trait methods */ }impl<T, A> Send for Drain<'_, T, A>
where
T: Send,
A: Send + std::alloc::Allocator
{ /* trait methods */ }impl<T, A> Sync for Drain<'_, T, A>
where
T: Sync,
A: Sync + std::alloc::Allocator
{ /* trait methods */ }impl<T, A> std::iter::TrustedLen for Drain<'_, T, A>
where
A: std::alloc::Allocator
{ /* trait methods */ }impl<'a, T, A> std::marker::Freeze for Drain<'a, T, A> {
/* trait methods */
}impl<'a, T, A> std::panic::RefUnwindSafe for Drain<'a, T, A>
where
T: std::panic::RefUnwindSafe,
A: std::panic::RefUnwindSafe
{ /* trait methods */ }impl<'a, T, A> Unpin for Drain<'a, T, A> {
/* trait methods */
}impl<'a, T, A> std::panic::UnwindSafe for Drain<'a, T, A>
where
T: std::panic::RefUnwindSafe,
A: std::panic::RefUnwindSafe
{ /* trait methods */ }