SHARE
TWEET

Untitled

a guest Jul 20th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct VectorProducer {
  2.     pub pool: Vec<Vec<i32>>,
  3. }
  4.  
  5. impl VectorProducer {
  6.     fn new() -> VectorProducer {
  7.         VectorProducer{pool: Vec::new()}
  8.     }
  9.     fn get(&mut self) -> Vec<i32> {
  10.         self.pool.pop().unwrap_or(Vec::new())
  11.     }
  12.     fn put(&mut self, v: Vec<i32>) {
  13.         self.pool.push(v);
  14.     }
  15. }
  16.  
  17. impl Iterator for VectorProducer {
  18.     type Item = Vec<i32>;
  19.     fn next(&mut self) -> Option<Self::Item> {
  20.         let mut buffer = self.get();
  21.         if buffer.len() == 0 {
  22.             for i in 0..10 {
  23.                 buffer.push(i);
  24.             }
  25.         }
  26.         Some(buffer)
  27.     }
  28. }
  29.  
  30. fn main() {
  31.     println!("Hello, world!");
  32.     let mut vp = VectorProducer::new();
  33.     loop {
  34.         match vp.next() {
  35.             Some(v) => {
  36.                 println!("{:?}",v);
  37.                 vp.put(v);
  38.             },
  39.             None => break,
  40.         }
  41.     }
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top