This commit is contained in:
2020-05-03 00:56:07 +08:00
parent 4ac2e99f38
commit 0b973718b5
3 changed files with 68 additions and 0 deletions

54
iter/src/main.rs Normal file
View File

@@ -0,0 +1,54 @@
struct Seq {
start: i32,
end: i32,
}
impl Seq {
pub fn new(start: i32, end: i32) -> Self {
Self { start, end, }
}
}
impl Iterator for Seq {
type Item = i32;
fn next(&mut self) -> Option<i32> {
if self.start < self.end {
let a = self.start;
self.start += 1;
Some(a)
} else {
None
}
}
}
fn main() {
test_001();
test_002();
test_003();
}
fn test_001() {
println!("## test001");
let seq = Seq::new(10, 15);
for a in seq {
println!("-> {}", a);
}
}
fn test_002() {
println!("## test002");
let mut seq = Seq::new(10, 15);
while let Some(a) = seq.next() {
println!("-> {}", a);
}
}
fn test_003() {
println!("## test003");
let mut seq = Seq::new(10, 15).take(3);
while let Some(a) = seq.next() {
println!("-> {}", a);
}
}