Guest User

Untitled

a guest
May 26th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. use std::cell::Cell;
  2.  
  3. struct Neighbors<'a> {
  4. array: &'a [Cell<i32>],
  5. idx: std::ops::Range<usize>,
  6. }
  7.  
  8. impl<'a> Neighbors<'a> {
  9. fn new(array: &'a [Cell<i32>]) -> Self {
  10. let idx = 1..array.len() - 1;
  11. Self { array, idx }
  12. }
  13. }
  14.  
  15. impl<'a> Iterator for Neighbors<'a> {
  16. type Item = (&'a Cell<i32>, &'a Cell<i32>, &'a Cell<i32>);
  17.  
  18. fn next(&mut self) -> Option<Self::Item> {
  19. let array = &self.array;
  20. self.idx.next().map(move |i| {
  21. let (a, b) = array.split_at(i);
  22. let (b, c) = b.split_at(1);
  23.  
  24. let a = a.last().unwrap();
  25. let b = b.first().unwrap();
  26. let c = c.first().unwrap();
  27.  
  28. (a, b, c)
  29. })
  30. }
  31. }
  32.  
  33. fn main() {
  34. let array = [Cell::new(1), Cell::new(2), Cell::new(3), Cell::new(4), Cell::new(5)];
  35. for (a, b, c) in Neighbors::new(&array) {
  36. println!("{} -> {} <- {}", a.get(), b.get(), c.get());
  37. }
  38. }
Add Comment
Please, Sign In to add comment