add iter
This commit is contained in:
5
iter/Cargo.lock
generated
Normal file
5
iter/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "iter"
|
||||
version = "0.1.0"
|
||||
9
iter/Cargo.toml
Normal file
9
iter/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "iter"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
54
iter/src/main.rs
Normal file
54
iter/src/main.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user