大望路网站建设公司,个人简历通用免费模板,wordpress主题图片丢失,php做的卖水果网站有哪些文章目录Rust迭代器Rust迭代器的实现Iterator特型IntoIterator特型for循环与迭代器迭代器类型再看for循环实现自定义迭代器方式一方式二相关参考Rust迭代器
Rust语言内置了迭代器模式#xff0c;用于实现对一个项的序列进行特定的处理#xff0c;通常配合for循环使用。当我们…
文章目录Rust迭代器Rust迭代器的实现Iterator特型IntoIterator特型for循环与迭代器迭代器类型再看for循环实现自定义迭代器方式一方式二相关参考Rust迭代器
Rust语言内置了迭代器模式用于实现对一个项的序列进行特定的处理通常配合for循环使用。当我们使用for...in遍历一个容器向量时其实就是在使用迭代器
let vec vec![1, 2, 3];for val in vec {println!({} , val);
}为了方便开发者的使用for...in表达式隐藏了很多迭代器的使用和实现细节这些我们将在后面慢慢看到。
Rust迭代器的实现
Rust迭代器的实现基于两个关键的特型Iterator和IntoIterator在Rust中迭代器指的是任何实现std::iter::Iterator特型的值而IntoIterator用于生成某种类型的迭代器。
Iterator特型
Rust标准库中Iterator特型定义如下
trait Iterator {type Item; // 产生值的类型fn next(mut self) - OptionSelf::Item; // 迭代器... //其它迭代器方法
}Item是迭代器产生值的类型。next方法返回Option枚举值当值为Some(v)时v存储迭代器的下一个值值为None时表示序列终止。
IntoIterator特型
如果某种类型是可迭代的那么它可以实现std::iter::IntoIterator特型。IntoIterator提供into_iter方法用于接收一个值然后基于这个值返回一个迭代器。Rust标准库中IntoIterator特型定义如下
trait IntoIterator where Self::IntoIter::Item Self::Item {type Item; // 产生值的类型type IntoIter: Iterator; // 迭代器的类型fn into_iter(self) - Self::IntoIter; // 返回一个迭代器
}IntoIter是迭代器自身的类型Item是迭代器产生值的类型。接下来我们再来看for循环。
for循环与迭代器
for循环的语法形式如下
for loop_variable in collection {code()
}这里变量collections就是实现了Iterator和IntoIterator的类型。Rust在后台会对上述代码进行扩展因此for循环的实际执行代码如下
{let mut _iter std::iter::IntoIterator::into_iter(collections);loop {match _iter.next() {Some(loop_variable) {code()},None break,}}
}可以看到for循环的本质就是使用into_iterator()方法将一些集合类型转换为迭代器再使用next()方法逐个遍历序列。
迭代器类型
Rust中大多数集合类型提供了不止一个IntoIterator的实现可用来产生迭代项不可变引用、可变引用或者值类型的迭代器对应方法和描述如下表
方法描述等价形式into_iter()返回一个只读不可重入迭代器迭代器元素的类型为 Tv.into_iter()iter()返回一个只读可重入迭代器迭代器元素的类型为 T(v).into_iter()iter_mut()返回一个可修改可重入迭代器迭代器元素的类型为 mut T(mut v).into_iter()
其中要说明的是iter和iter_mut并不是属于某种特型的方法它们是这些集合类型额外提供的方法可以理解为是一种简化版的使用形式。
再看for循环
由于for循环会对其操作数应用into_iterator()方法因此这三个实现就可以消费集合并取得其元素的所有权以及支持迭代集合的不可变引用、可变引用
for element in collection {} 等效于 for element in collection.into_iter() {} // 执行后collection所有权会被转移无法再访问
for element in collection {} 等效于 for element in collection.iter() {}
for element in mut collection {} 等效于 for element in collection.iter_mut() {}实现自定义迭代器
Rust中实现自定义迭代器本质上就是为自定义类型实现Iterator和IntoIterator特型。
方式一
struct Counter {max: i32,// count tracks the state of this iterator.count: i32,
}impl Counter {fn new(max: i32) - Counter {Counter { count: -1, max: max }}
}impl Iterator for Counter {type Item i32;fn next(mut self) - OptionSelf::Item {self.count 1;if self.count self.max {Some(self.count)} else {None}}
}// 使用for循环遍历迭代器使用
for i in Counter::new(10) {println!({}, i);
}
方式二
struct Counter {max: i32,// No need to track the state, because this isnt an iterator.
}impl Counter {fn new(max: i32) - Counter {Counter { max: max }}
}impl IntoIterator for Counter {type Item i32;type IntoIter std::ops::RangeSelf::Item;fn into_iter(self) - Self::IntoIter {std::ops::Range{ start: 0, end: self.max }}
}for i in Counter::new(10) {println!({}, i);
}相关参考
《Rust程序设计》《Rust程序设计语言》Rust 迭代器 Iteratorhttps://www.twle.cn/c/yufei/rust/rust-basic-iterator.html 文章转载自: http://www.morning.lgtzd.cn.gov.cn.lgtzd.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.jqswf.cn.gov.cn.jqswf.cn http://www.morning.ymfzd.cn.gov.cn.ymfzd.cn http://www.morning.rqgjr.cn.gov.cn.rqgjr.cn http://www.morning.mlntx.cn.gov.cn.mlntx.cn http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.pnjsl.cn.gov.cn.pnjsl.cn http://www.morning.sxmbk.cn.gov.cn.sxmbk.cn http://www.morning.dpsgq.cn.gov.cn.dpsgq.cn http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.gwjnm.cn.gov.cn.gwjnm.cn http://www.morning.nlmm.cn.gov.cn.nlmm.cn http://www.morning.ktnmg.cn.gov.cn.ktnmg.cn http://www.morning.tqjks.cn.gov.cn.tqjks.cn http://www.morning.fflnw.cn.gov.cn.fflnw.cn http://www.morning.rfwgg.cn.gov.cn.rfwgg.cn http://www.morning.gbqgr.cn.gov.cn.gbqgr.cn http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.rgksz.cn.gov.cn.rgksz.cn http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.ykmtz.cn.gov.cn.ykmtz.cn http://www.morning.sgrdp.cn.gov.cn.sgrdp.cn http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn http://www.morning.splcc.cn.gov.cn.splcc.cn http://www.morning.cyjjp.cn.gov.cn.cyjjp.cn http://www.morning.gwxsk.cn.gov.cn.gwxsk.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.ryznd.cn.gov.cn.ryznd.cn http://www.morning.qwmdx.cn.gov.cn.qwmdx.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.wtnyg.cn.gov.cn.wtnyg.cn http://www.morning.lksgz.cn.gov.cn.lksgz.cn http://www.morning.bwnd.cn.gov.cn.bwnd.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.ysckr.cn.gov.cn.ysckr.cn http://www.morning.gqfks.cn.gov.cn.gqfks.cn http://www.morning.fglyb.cn.gov.cn.fglyb.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.tgtwy.cn.gov.cn.tgtwy.cn http://www.morning.pyzt.cn.gov.cn.pyzt.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.yqkmd.cn.gov.cn.yqkmd.cn http://www.morning.txqsm.cn.gov.cn.txqsm.cn http://www.morning.qbfwb.cn.gov.cn.qbfwb.cn http://www.morning.pjtnk.cn.gov.cn.pjtnk.cn http://www.morning.bpmdh.cn.gov.cn.bpmdh.cn http://www.morning.tkrdg.cn.gov.cn.tkrdg.cn http://www.morning.jghqc.cn.gov.cn.jghqc.cn http://www.morning.rfmzs.cn.gov.cn.rfmzs.cn http://www.morning.ffcsr.cn.gov.cn.ffcsr.cn http://www.morning.fbccx.cn.gov.cn.fbccx.cn http://www.morning.wtsr.cn.gov.cn.wtsr.cn http://www.morning.xqwq.cn.gov.cn.xqwq.cn http://www.morning.jkmjm.cn.gov.cn.jkmjm.cn http://www.morning.krywy.cn.gov.cn.krywy.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.ghyfm.cn.gov.cn.ghyfm.cn http://www.morning.hwljx.cn.gov.cn.hwljx.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.jrwbl.cn.gov.cn.jrwbl.cn http://www.morning.hlyfn.cn.gov.cn.hlyfn.cn http://www.morning.hdscx.cn.gov.cn.hdscx.cn http://www.morning.rzdpd.cn.gov.cn.rzdpd.cn http://www.morning.ywgrr.cn.gov.cn.ywgrr.cn http://www.morning.hgkbj.cn.gov.cn.hgkbj.cn http://www.morning.lbywt.cn.gov.cn.lbywt.cn http://www.morning.knpmj.cn.gov.cn.knpmj.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn http://www.morning.srrrz.cn.gov.cn.srrrz.cn http://www.morning.xkppj.cn.gov.cn.xkppj.cn http://www.morning.jtcq.cn.gov.cn.jtcq.cn http://www.morning.slwqt.cn.gov.cn.slwqt.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.pmlgr.cn.gov.cn.pmlgr.cn http://www.morning.mltsc.cn.gov.cn.mltsc.cn