Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::cell::Cell;
- struct Neighbors<'a> {
- array: &'a [Cell<i32>],
- idx: std::ops::Range<usize>,
- }
- impl<'a> Neighbors<'a> {
- fn new(array: &'a [Cell<i32>]) -> Self {
- let idx = 1..array.len() - 1;
- Self { array, idx }
- }
- }
- impl<'a> Iterator for Neighbors<'a> {
- type Item = (&'a Cell<i32>, &'a Cell<i32>, &'a Cell<i32>);
- fn next(&mut self) -> Option<Self::Item> {
- let array = &self.array;
- self.idx.next().map(move |i| {
- let (a, b) = array.split_at(i);
- let (b, c) = b.split_at(1);
- let a = a.last().unwrap();
- let b = b.first().unwrap();
- let c = c.first().unwrap();
- (a, b, c)
- })
- }
- }
- fn main() {
- let array = [Cell::new(1), Cell::new(2), Cell::new(3), Cell::new(4), Cell::new(5)];
- for (a, b, c) in Neighbors::new(&array) {
- println!("{} -> {} <- {}", a.get(), b.get(), c.get());
- }
- }
Add Comment
Please, Sign In to add comment