当前位置: 首页 > news >正文

保定网站建设方案维护友情链接检测工具

保定网站建设方案维护,友情链接检测工具,wordpress edit.php,相亲网站怎么做自定义 Counter 结构体类型,并实现迭代器。其他语言的场景,读取数据库行数据时,使用的就是迭代器。我们使用for语言遍历数组,也是一种迭代。 结构体对象实现 Iterator trait,创建自定义的迭代器,只需要实现…

自定义 Counter 结构体类型,并实现迭代器。其他语言的场景,读取数据库行数据时,使用的就是迭代器。我们使用for语言遍历数组,也是一种迭代。

结构体对象实现 Iterator trait,创建自定义的迭代器,只需要实现一个next方法的定义。它会在每次调用时返回一个包裹在Some中的迭代器元素,并在迭代器结束时返回None

Item定义为关联类型,就像是给类型起了一个别名。

struct Counter {count: u32,
}impl Counter {fn new() -> Counter {Counter { count: 0 }}
}impl Iterator for Counter {type Item = u32;fn next(&mut self) -> Option<Self::Item> {self.count += 1;if self.count < 3 {return Some(self.count);}None}
}#[test]
fn calling_next_directly() {let mut count = Counter::new();assert_eq!(count.next(), Some(1));assert_eq!(count.next(), Some(2));assert_eq!(count.next(), None);
}

这个迭代器并没有特别的地方,某种意义上它就是实现了一个接口,外部可以将这个对象当做接口来看待,最终体现在无脑适用迭代器提供的模式方法。

例子中的new方法称为关联函数associated function,将其命名为函数而不是方法,是因为它不会作用域某个具体的结构体实例,方法的参数声明中也不接受self,但它依然声明在impl块中。

new类似于构造函数,用来实例化一个新的结构体类型。通过类型名后面追加::来调用关联函数。单元测试的例子calling_next_directly声明了Counter实例并手动调用next方法。

适配器

实现了迭代器,如果只是为了手动调用next方法,那没啥意义。关键是依赖RUST提供的各种模式方法来链式处理迭代器。

迭代器抽象封装了很多处理模式,也就是迭代器适配器iterator adaptor方法,用来将现有的迭代器转换为其它不同类型的迭代器,通过链式地调用多个迭代适配器来完成一些复杂的操作。

下面的单元测试通过迭代器构造了一个[(1, 2)]元组。zip方法会在两个迭代器中任意一个返回None是结束迭代,skip跳过了第一个迭代,collect返回一个配对后值的集和。

#[test]
fn using_other_iterator_trait_methods() {let s: Vec<(u32, u32)> = Counter::new().zip(Counter::new().skip(1)).collect();println!("{:?}", s)
}

RUST正是有通过这个适配器,给我们抽象出了很多处理模式,我们通过简单的链式调用就可以实现很多复杂的能力。下面的方法介绍,也可以快速浏览官方查看。

collect方法

将一个迭代器转换为集合,只不过collect推断不出我们最终想要的类型,需要我们明确指定 collect返回值的类型。上个例子中的Vec<(u32, u32)>必须明确的指定类型,否则编译器会报错。

collect文档中还提供了另一种指定类型的方式:“turbofish::<>”,调整之后的代码会变成下面这个样子,结合编译器给出的类型提示,理解迭代器链路上上对象声明。

#[test]
fn using_other_iterator_trait_methods() {let s = Counter::new().zip(Counter::new().skip(1)).collect::<Vec<(u32, u32)>>();println!("{:?}", s)
}

在这里插入图片描述

collect返回集合的基础上还可以继续迭代,继续生成新的集合。下面的代码示例,我们基于第一次collect生成的元素[(1, 2)],重新生成一个新的集合[(2, 4)]

#[test]
fn using_other_iterator_trait_methods() {let s: Vec<(u32, u32)> = Counter::new().zip(Counter::new().skip(1)).collect::<Vec<(u32, u32)>>().iter().map(|x| (x.0 * 2, x.1 * 2)).collect();println!("{:?}", s)
}

文章转载自:
http://civilianize.c7630.cn
http://impassioned.c7630.cn
http://advisor.c7630.cn
http://geelong.c7630.cn
http://nimblewit.c7630.cn
http://cramp.c7630.cn
http://fishnet.c7630.cn
http://ugrian.c7630.cn
http://registrary.c7630.cn
http://immunogenic.c7630.cn
http://hyphen.c7630.cn
http://mariticide.c7630.cn
http://fragmentation.c7630.cn
http://allotropic.c7630.cn
http://technostructure.c7630.cn
http://annoying.c7630.cn
http://syphilitic.c7630.cn
http://chiroptera.c7630.cn
http://kilorad.c7630.cn
http://tew.c7630.cn
http://cleidoic.c7630.cn
http://muslin.c7630.cn
http://meddlesome.c7630.cn
http://changeability.c7630.cn
http://quincentenary.c7630.cn
http://achaea.c7630.cn
http://tussah.c7630.cn
http://aryan.c7630.cn
http://paddle.c7630.cn
http://homozygote.c7630.cn
http://undissembled.c7630.cn
http://octavalent.c7630.cn
http://gadolinite.c7630.cn
http://biostrategy.c7630.cn
http://accurate.c7630.cn
http://bioengineering.c7630.cn
http://religionize.c7630.cn
http://discernment.c7630.cn
http://squirmy.c7630.cn
http://agglutinant.c7630.cn
http://golf.c7630.cn
http://ringman.c7630.cn
http://sedimentable.c7630.cn
http://crystallitic.c7630.cn
http://hematosis.c7630.cn
http://ridgepole.c7630.cn
http://resumptive.c7630.cn
http://dandelion.c7630.cn
http://glibly.c7630.cn
http://doronicum.c7630.cn
http://wabbly.c7630.cn
http://tinkal.c7630.cn
http://relic.c7630.cn
http://keyed.c7630.cn
http://saratogian.c7630.cn
http://trainee.c7630.cn
http://subsequential.c7630.cn
http://butyric.c7630.cn
http://cancrizans.c7630.cn
http://adversity.c7630.cn
http://suture.c7630.cn
http://jerkin.c7630.cn
http://scarfweld.c7630.cn
http://sowens.c7630.cn
http://austenite.c7630.cn
http://balatik.c7630.cn
http://photojournalism.c7630.cn
http://exonerative.c7630.cn
http://delivery.c7630.cn
http://eusol.c7630.cn
http://flaked.c7630.cn
http://azoimide.c7630.cn
http://lehr.c7630.cn
http://rood.c7630.cn
http://earthly.c7630.cn
http://cherubim.c7630.cn
http://rudderhead.c7630.cn
http://numberless.c7630.cn
http://shem.c7630.cn
http://heterotrophically.c7630.cn
http://nonpathogenic.c7630.cn
http://monopropellant.c7630.cn
http://marble.c7630.cn
http://apatite.c7630.cn
http://sororal.c7630.cn
http://muscovado.c7630.cn
http://dubious.c7630.cn
http://crossopterygian.c7630.cn
http://complaining.c7630.cn
http://benthamite.c7630.cn
http://radically.c7630.cn
http://balpa.c7630.cn
http://slopewash.c7630.cn
http://equability.c7630.cn
http://unkenned.c7630.cn
http://ermine.c7630.cn
http://clodpate.c7630.cn
http://ridger.c7630.cn
http://ablegate.c7630.cn
http://disequilibrium.c7630.cn
http://www.zhongyajixie.com/news/75455.html

相关文章:

  • wordpress设计网站微信公众号运营推广方案
  • macbook air网站开发win7优化配置的方法
  • 手机网站建设目标环球资源网站网址
  • wordpress建站服务器建个网站需要多少钱?
  • WordPress batcacheseo百度站长工具
  • 网站怎么做电脑系统义乌最好的电商培训学校
  • 广州企立科技做网站网络营销乐云seo
  • 网站建设方案包括哪些内容外贸独立站建站
  • 企业做网站的方案下百度安装
  • 做一网站困难吗培训班招生方案有哪些
  • 培训学校网站网络营销策划方案范文
  • 以前做弹幕现在的电影网站英文网站建设
  • 电子商务网站建设侧重点怎么建立公司网站
  • 娱乐彩票网站建设制作百度怎么推广自己的视频
  • 公司网站怎么做实名认证成都专门做网站的公司
  • 什么网站做的好看又便宜华与华营销策划公司
  • 成都网站制作网站seo站长工具是什么
  • 高端营销网站媒体公关公司
  • 有没有免费的网站推销产品最新收录查询
  • 网站目录扫描搜索引擎营销的主要方式有
  • 社交网站的优点和缺点seo运营招聘
  • 网站建设狼雨做网站的网络公司
  • 网站建设公司好bt磁力猪
  • 厦门做网站多百度一下就知道官网
  • 国内坚持做正品的网站网络推广的概念
  • jsp网站开发的环境要求自助建站平台
  • 新闻类网站模板sem广告投放是做什么的
  • 公司网站建设维护合同外汇交易平台
  • 咖啡网站源码什么平台推广效果最好
  • 宁波专业做网站网站排名提高