rust 自定义迭代器的实现方法
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>1. 什么是迭代器?🧠</li><li>2. 核心揭秘:IteratorTrait</li><ul class="second_class_ul"><li>type Item;</li><li>fn next(&mut self) -> Option<Self::Item></li></ul><li>3. 实践一:你的第一个迭代器 (简单的计数器) 🌍</li><ul class="second_class_ul"><li>第 1 步:定义结构体 (保存状态)</li><li>第 2 步:实现IteratorTrait</li><li>第 3 步:使用它!</li></ul><li>4. 实践二:让自定义结构体“可迭代” 🌟</li><ul class="second_class_ul"><li>第 1 步:定义集合和它的迭代器结构体</li><li>第 2 步:为 `MyListIter 实现Iterator</li><li>第 3 步:关键!为MyList实现IntoIterator</li><li>第 4 步:见证奇迹!</li></ul><li>5. 你免费获得的“超能力” 😎</li><ul class="second_class_ul"></ul><li>6. 总结 & 下一步</li><ul class="second_class_ul"></ul></ul></div><p class="maodian"></p><h2>1. 什么是迭代器?🧠</h2><p>简单来说,<strong>迭代器就是一个“知道如何获取下一个元素”的东西</strong>。</p>
<p>它是一种设计模式,允许你遍历一个序列(比如数组、列表或你自定义的任何东西),而不需要关心序列内部是怎么存储的。</p>
<p>在 Rust 中,你最常见的迭代器用法就是 <code>for</code> 循环:r` 循环:</p>
<div class="jb51code"><pre class="brush:plain;">let numbers = vec!;
// 这里的 `numbers.iter()` 就创建了一个迭代器
for num in numbers.iter() {
println!("Got: {}", num);
}
</pre></div>
<p><code>for</code> 循环就是不断地问这个迭代器:“嘿,还有下一个吗?有的话请给我。” 直到迭代器回答:“抱歉,没有了。”</p>
<p>我们的目标就是学会如何创造这种“东西”。</p>
<p class="maodian"></p><h2>2. 核心揭秘:IteratorTrait</h2>
<p>在 Rust 中,“迭代器”并不是一个具体的类型,而是**任何了 <code>Iterator</code> Trait 的类型**。</p>
<p>这个 Trait (特质) 的定义简化后是这样的:</p>
<div class="jb51code"><pre class="brush:plain;">pub trait Iterator {
// 1. 关联类型:告诉 Rust 你迭代的“东西”是什么类型
type Item;
// 2. 核心方法:获取下一个元素
// 这是你唯一必须实现的方法!
fn next(&mut self) -> Option<Self::Item>;
// --- 下面还有很多其他方法 (map, filter, sum...) ---
// 但它们都有默认实现,你暂时不用管!
}
</pre></div>
<p>是不是看起来很简单?我们来拆解一下你必须关心的两个部分:</p>
<p class="maodian"></p><h3>type Item;</h3>
<p>这是一个关联类型。你只需要告诉 Rust:“我这个迭代器,每次‘吐’出来的元素是 i32 类型”或者“是 String 类型”。</p>
<p>例如:type Item = u32; 或 type Item = &String;</p>
<p class="maodian"></p><h3>fn next(&mut self) -> Option<Self::Item></h3>
<p>这就是魔法发生的地方!🌟</p>
<ul><li><p>&mut self:为什么是 &mut (可变借用)?因为迭代器需要**状态**。比如,一个计数器需要知道“我当前数到几了”,每次调用 next 之后,这个状态就要改变(比如 +1)。</p></li><li><p>Option<Self::Item>:这是迭代器设计的精髓!</p>
<ul><li>**`Some(ue)**:如果序列中还有下一个元素,就返回 Some(那个元素)`。</li><li>None:如果序列已经结束了,就返回 None。for 循环看到 None 就会自动停止。</li></ul></li></ul>
<p class="maodian"></p><h2>3. 实践一:你的第一个迭代器 (简单的计数器) 🌍</h2>
<p>我们来写一个最简单的迭代器:一个从 1 数到 5 的计数器。</p>
<p class="maodian"></p><h3>第 1 步:定义结构体 (保存状态)</h3>
<p>迭代器需要“记忆”,所以我们需要一个结构体来保存它的“状态”。对于计数器,我们需要知道“当前数到几了” (<code>current</code>) 和“什么时候停” (<code>max</code>)。</p>
<div class="jb51code"><pre class="brush:plain;">// 我们的计数器结构体
struct Counter {
current: u32,
max: u32,
}
// 顺便给它一个 "构造函数" (new)
impl Counter {
fn new(max: u32) -> Counter {
Counter { current: 1, max } // 我们从 1 开始数
}
}
</pre></div>
<p class="maodian"></p><h3>第 2 步:实现IteratorTrait</h3>
<p>现在,我们来告诉 Rust 如何让 <code>Counter</code> 变成一个迭代器。</p>
<div class="jb51code"><pre class="brush:plain;">impl Iterator for Counter {
// 1. 告诉 Rust 我们迭代的是 u32
type Item = u32;
// 2. 实现核心逻辑!
fn next(&mut self) -> Option<Self::Item> {
if self.current <= self.max {
// 只要当前值 <= 5
// 准备好要返回的当前值
let val_to_return = self.current;
// 更新状态:让 current + 1,为下一次做准备
self.current += 1;
// 把值用 Some() 包裹起来返回
Some(val_to_return)
} else {
// 如果 current 已经 > max (比如到了 6)
// 迭代结束!返回 None
None
}
}
}
</pre></div>
<p class="maodian"></p><h3>第 3 步:使用它!</h3>
<p>恭喜你!你已经写好了一个完整的迭代器!🎉 让我们用用看:</p>
<div class="jb51code"><pre class="brush:plain;">fn main() {
let counter = Counter::new(5); // 创建一个 1 到 5 的计数器
// `for` 循环现在可以识别我们的 Counter 了!
println!("Running for loop:");
for number in counter {
println!("{}", number);
}
// 注意:`for` 循环会“消耗掉”迭代器。
// 如果想再用一次,需要重新创建:
let counter2 = Counter::new(3);
// 你也可以手动调用 next() 看看发生了什么
println!("\nManual next() calls:");
let mut counter3 = Counter::new(2); // 必须是 mut,因为 next() 需要 &mut self
println!("{:?}", counter3.next()); // Some(1)
println!("{:?}", counter3.next()); // Some(2)
println!("{:?}", counter3.next()); // None (迭代结束)
println!("{:?}", counter3.next()); // None (之后永远是 None)
}
</pre></div>
<p><strong>输出:</strong></p>
<div class="jb51code"><pre class="brush:plain;">Running for loop:
1
2
3
4
5
Manual next() calls:
Some(1)
Some(2)
None
None
</pre></div>
<p>你已经掌握了 80% 的精髓了!太棒了!🥳</p>
<p class="maodian"></p><h2>4. 实践二:让自定义结构体“可迭代” 🌟</h2>
<p>在实践一中,<code>Counter</code> 本身就是迭代器。但更常见的情况是:你有一个<strong>集合</strong>(比如 `Myook<code>),你想**为它创建一个迭代器**(比如 </code>BookPageIterator`)。</p>
<p>就像 <code>Vec</code> (集合) 和 <code>VecIter</code> (它的迭代器) 的关系一样。</p>
<p>我们希望实现这样的效果:</p>
<div class="jb51code"><pre class="brush:plain;">let my_list = MyList::new();
for item in &my_list { // 注意这里是 &my_list
// ...
}
</pre></div>
<p>要实现这个,我们需要两个 Trait:<code>Iterator</code> (老朋友) 和 <code>IntoIterator</code> (新朋友)。</p>
<p><code>IntoIterator</code> Trait 就像一个“转换器”,它告诉 <code>for</code> 循环:“嘿,我知道如何把我(<code>&MyList</code>)转换成一个真正的迭代器!”</p>
<p class="maodian"></p><h3>第 1 步:定义集合和它的迭代器结构体</h3>
<div class="jb51code"><pre class="brush:plain;">// 我们的集合
struct MyList {
items: Vec<String>,
}
impl MyList {
fn new() -> Self {
Self {
items: vec![
"Rust".to_string(),
"is".to_string(),
"Awesome".to_string(),
],
}
}
}
// ------------------------------------
// 专门为 MyList 服务的迭代器结构体
// 它需要“借用” MyList 的数据
// 'a 是生命周期,表示它借用的数据至少和 'a 活得一样久
struct MyListIter<'a> {
list: &'a MyList, // 持有对 MyList 的引用
index: usize, // 跟踪迭代到第几个了
}
</pre></div>
<p class="maodian"></p><h3>第 2 步:为 `MyListIter 实现Iterator</h3>
<p>这和 <code>Counter</code> 的例子几乎一样,只是现在我们是从 <code>Vec</code> 中取数据。</p>
<div class="jb51code"><pre class="brush:plain;">// 'a 也要在这里声明
impl<'a> Iterator for MyListIter<'a> {
// 这一次,我们迭代的是对 String 的引用
type Item = &'a String;
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.list.items.len() {
// 还有元素
let item = &self.list.items;
self.index += 1;
Some(item)
} else {
// 没元素了
None
}
}
}
</pre></div>
<p class="maodian"></p><h3>第 3 步:关键!为MyList实现IntoIterator</h3>
<p>这是连接 <code>for</code> 循环和 <code>MyListIter</code> 的“胶水”。我们希望 <code>for item in &my_list</code> 能工作,所以我们要为 <code>&MyList</code> 实现 <code>IntoIterator</code>。</p>
<div class="jb51code"><pre class="brush:plain;">// 为 &MyList (对 MyList 的不可变引用) 实现 IntoIterator
impl<'a> IntoIterator for &'a MyList {
// 迭代项还是 &String
type Item = &'a String;
// 告诉 for 循环:你调用 into_iter() 时,
// 我会返回一个 MyListIter<'a> 实例
type IntoIter = MyListIter<'a>;
// `for` 循环会自动调用这个方法!
// 这里的 self 就是 &'a MyList
fn into_iter(self) -> Self::IntoIter {
// 创建我们刚才定义的迭代器实例
MyListIter {
list: self, // self 就是 &MyList
index: 0, // 从 0 开始
}
}
}
</pre></div>
<p class="maodian"></p><h3>第 4 步:见证奇迹!</h3>
<div class="jb51code"><pre class="brush:plain;">fn main() {
let my_list = MyList::new();
// 感谢 IntoIterator,这行代码现在可以完美工作了!
// 1. `for` 循环看到 &my_list
// 2. 它调用 (&my_list).into_iter()
// 3. 我们的代码返回了一个 MyListIter
// 4. `for` 循环不断调用 MyListIter.next()
for item in &my_list {
println!("Item: {}", item);
}
}
</pre></div>
<p><strong>输出:</strong></p>
<blockquote><p>Item: Rust<br />Item: is<br />Item: Awesome</p></blockquote>
<p>你做到了!这已经是 Rust 中非常地道的迭代器实现方式了!👍</p>
<p class="maodian"></p><h2>5. 你免费获得的“超能力” 😎</h2>
<p>最爽的部分来了!</p>
<p>当你辛辛苦苦地实现了 <code>Iterator</code> Trait(哪怕只写了 <code>next()</code> 方法),Rust 编译器会<strong>免费赠送</strong>给你一大堆超级好用的“迭代器适配器” (Iterator Adapters)!</p>
<p>比如 <code>.map()</code>, <code>.filter()</code>, <code>.zip()</code>, <code>.sum()</code>, <code>.collect()</code>… 全都能用了!</p>
<p>看看我们刚才的 <code>Counter</code>:</p>
<div class="jb51code"><pre class="brush:plain;">let sum: u32 = Counter::new(5) // 我们的迭代器 (1, 2, 3, 4, 5)
.zip(Counter::new(5).skip(1)) // ( (1,2), (2,3), (3,4), (4,5) )
.map(|(a, b)| a * b) // ( 2, 6, 12, 20 )
.filter(|x| *x > 10) // ( 12, 20 )
.sum(); // 12 + 20 = 32
println!("The complex sum is: {}", sum); // 32
</pre></div>
<p>我们只写了 <code>next()</code>,但 <code>zip</code>, <code>skip</code>, <code>map</code>, <code>filter</code>, <code>sum</code> 都能在我们的 <code>Counter</code> 上使用!这就是 Rust Trait 和迭代器模式的强大之处!</p>
<p class="maodian"></p><h2>6. 总结 & 下一步</h2>
<p>我们来回顾一下关键点:</p>
<ol><li>迭代器是任何实现了 Iterator Trait 的东西。</li><li>Iterator Trait 的核心是 type Item; (迭代什么) 和 `fn next(&mut self) -> OptionSelf::Item 你需要一个 struct 来保存迭代的状态 (比如 current 索引)。</li><li>`next)方法通过返回Some(value)来提供值,通过返回None` 来停止迭代。</li><li>要让你自己的集合(如 MyList)支持 for item in &collection,你需要为 &collection 实现 IntoIterator Trait,让它返回你自定义的迭代器(如 MyListIter)。</li><li>一旦实现了 Iterator,你就免费获得了所有适配器 (map, `filter …)。</li></ol>
<p>到此这篇关于rust 自定义迭代器的实现方法的文章就介绍到这了,更多相关rust 自定义迭代器内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>Rust可迭代类型迭代器正确创建自定义可迭代类型的方法</li><li>详解rust 自动化测试、迭代器与闭包、智能指针、无畏并发</li><li>Rust语言从入门到精通系列之Iterator迭代器深入详解</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]