78 lines
1.3 KiB
Rust
78 lines
1.3 KiB
Rust
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();
|
|
test_004();
|
|
}
|
|
|
|
// -> 10 11 12 13 14
|
|
fn test_001() {
|
|
println!("## test001");
|
|
let seq = Seq::new(10, 15);
|
|
print!("->");
|
|
for a in seq {
|
|
print!(" {}", a);
|
|
}
|
|
println!();
|
|
}
|
|
|
|
// -> 10 11 12 13 14
|
|
fn test_002() {
|
|
println!("## test002");
|
|
let mut seq = Seq::new(10, 15);
|
|
print!("->");
|
|
while let Some(a) = seq.next() {
|
|
print!(" {}", a);
|
|
}
|
|
println!();
|
|
}
|
|
|
|
// -> 10 11 12
|
|
fn test_003() {
|
|
println!("## test003");
|
|
let mut seq = Seq::new(10, 15).take(3);
|
|
print!("->");
|
|
while let Some(a) = seq.next() {
|
|
print!(" {}", a);
|
|
}
|
|
println!();
|
|
}
|
|
|
|
// -> 10
|
|
// -> 10 11 12 13 14
|
|
fn test_004() {
|
|
println!("## test004");
|
|
let mut seq = Seq::new(10, 15).peekable();
|
|
println!("-> {}", seq.peek().unwrap());
|
|
print!("->");
|
|
while let Some(a) = seq.next() {
|
|
print!(" {}", a);
|
|
}
|
|
println!();
|
|
}
|