diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 7ec65f2..53873df 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -8,4 +8,4 @@ - [std::vec](./vec/about.md) - [Drain](./vec/Drain.md) - + - [IntoIter](./vec/IntoIter.md) diff --git a/src/vec/Drain.md b/src/vec/Drain.md index 539ebec..9bf95b2 100644 --- a/src/vec/Drain.md +++ b/src/vec/Drain.md @@ -212,7 +212,7 @@ where { /* trait methods */ } ``` -### `Interator` +### `Iterator` ```rust,ignore impl Iterator for Drain<'_, T, A> diff --git a/src/vec/IntoIter.md b/src/vec/IntoIter.md new file mode 100644 index 0000000..05544de --- /dev/null +++ b/src/vec/IntoIter.md @@ -0,0 +1,197 @@ +# 構造体 `std::vec::IntoIter` + +```rust,ignore +pub struct IntoIter +where + A: std::alloc::Allocator +{ /* private fields */ } +``` + +## 解説 + +ベクターの所有権を放棄し、各要素を順に返すイテレータ。 +`Vec`の`into_iter`メソッド(`IntoIterator`トレイトの実装)によって作成される。 + +## 例 + +```rust +let v = vec![0, 1, 2]; +// vの所有権がiterに移動する +let iter: std::vec::IntoIter = v.into_iter(); + +// 所有権を持っているので、要素を直接消費できる +for x in iter { + println!("{}", x); +} +``` + +## 実装 + +```rust,ignore +impl IntoIter +where + A: std::alloc::Allocator +{ + pub fn as_slice(&self) -> &[T]; + + pub fn as_mut_slice(&mut self) -> &mut [T]; + + // nightly-only + pub fn allocator(&self) -> &A; +} +``` + +### `as_slice` + +まだ消費されていない要素をスライスとして返す関数。 + +```rust +let vec = vec!['a', 'b', 'c']; +let mut into_iter = vec.into_iter(); + +// 最初はすべての要素が見える +assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']); + +// 1つ消費する +let _ = into_iter.next().unwrap(); + +// 残りの要素が返される +assert_eq!(into_iter.as_slice(), &['b', 'c']); +``` + +### `as_mut_slice` + +まだ消費されていない要素を可変スライスとして返す関数。 + +```rust +let vec = vec!['a', 'b', 'c']; +let mut into_iter = vec.into_iter(); + +// 消費前のスライスを書き換える +into_iter.as_mut_slice()[2] = 'z'; + +assert_eq!(into_iter.next().unwrap(), 'a'); +assert_eq!(into_iter.next().unwrap(), 'b'); +assert_eq!(into_iter.next().unwrap(), 'z'); // 書き換えた値が返る +``` + +### `allocator` + +> [!NOTE] +> nightlyでのみ使用可能 + +内部で使用しているアロケーターへの参照を返す関数。 + +## トレイト実装 + +### `AsRef` + +```rust,ignore +impl AsRef<[T]> for IntoIter +where + A: std::alloc::Allocator +{ /* trait methods */ } +``` + +### `Clone` + +```rust,ignore +impl Clone for IntoIter +where + T: Clone, + A: std::alloc::Allocator + Clone +{ /* trait methods */ } +``` + +### `std::fmt::Debug` + +```rust,ignore +impl std::fmt::Debug for IntoIter +where + T: std::fmt::Debug, + A: std::alloc::Allocator +{ /* trait methods */ } +``` + +### `Default` + +```rust,ignore +impl Default for IntoIter +where + A: std::alloc::Allocator + Default +{ /* trait methods */ } +``` + +### `DoubleEndedIterator` + +```rust,ignore +impl DoubleEndedIterator for IntoIter +where + A: std::alloc::Allocator +{ /* trait methods */ } +``` + +### `Drop` + +```rust,ignore +impl Drop for IntoIter +where + A: std::alloc::Allocator +{ /* trait methods */ } +``` + +### `ExactSizeIterator` + +```rust,ignore +impl ExactSizeIterator for IntoIter +where + A: std::alloc::Allocator +{ /* trait methods */ } +``` + +### `Iterator` + +```rust,ignore +impl Iterator for IntoIter +where + A: std::alloc::Allocator +{ /* trait methods */ } +``` + +### `std::iter::FusedIterator` + +```rust,ignore +impl std::iter::FusedIterator for IntoIter +where + A: std::alloc::Allocator +{ /* trait methods */ } +``` + +### `Send` + +```rust,ignore +impl Send for IntoIter +where + T: Send, + A: std::alloc::Allocator + Send +{ /* trait methods */ } +``` + +### `Sync` + +```rust,ignore +impl Sync for IntoIter +where + T: Sync, + A: std::alloc::Allocator + Sync +{ /* trait methods */ } +``` + +### `std::iter::TrustedLen` + +```rust,ignore +impl std::iter::TrustedLen for IntoIter +where + A: std::alloc::Allocator +{ /* trait methods */ } +``` diff --git a/src/vec/about.md b/src/vec/about.md index eeb0fe9..36e53c4 100644 --- a/src/vec/about.md +++ b/src/vec/about.md @@ -25,5 +25,5 @@ Rust 1.0.0 ~ | 名前 | 説明 | | --- | --- | | [`Drain`](./Drain.md) | ベクター内の指定された区間を削除し、取り除かれた要素を与えるイテレータ | -| `IntoIter` | ベクター内の指定された区間の所有権を奪うイテレータ | -| `Splite` | ベクター内の指定された区間を与えられたイテレーターと入れ替え、元の要素を返すイテレータ | +| [`IntoIter`](./IntoIter.md) | ベクター内の指定された区間の所有権を奪うイテレータ | +| `Splice` | ベクター内の指定された区間を与えられたイテレーターと入れ替え、元の要素を返すイテレータ |