Iamnotagenius

Borrow checker fuckery

Nov 5th, 2025
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.29 KB | None | 0 0
  1. #![feature(random)]
  2. use std::{iter::repeat, random::random, str::Split, thread::sleep, time::Duration};
  3.  
  4. fn main() {
  5.     let randomize = |s| random::<bool>(..).then_some(s);
  6.     let mut sources = vec![
  7.         repeat("1234&567".to_owned()).filter_map(randomize),
  8.         repeat("45&56&78&89".to_owned()).filter_map(randomize),
  9.         repeat("789".to_owned()).filter_map(randomize),
  10.     ];
  11.     let mut texts: Vec<String> = vec![
  12.         "1234&567".to_owned(),
  13.         "45&56&78&89".to_owned(),
  14.         "789".to_owned(),
  15.     ];
  16.     let mut iters: Vec<Split<_>> = texts.iter().map(|s| s.split('&')).collect();
  17.     loop {
  18.         for iter in iters.iter_mut() {
  19.             if let Some(s) = iter.next() {
  20.                 print!("{}", s);
  21.             }
  22.         }
  23.         println!("");
  24.  
  25.         let new_strings: Vec<(usize, String)> = sources
  26.             .iter_mut()
  27.             .enumerate()
  28.             .filter_map(|(i, s)| s.next().map(|s| (i, s)))
  29.             .collect();
  30.  
  31.         for (i, s) in new_strings {
  32.             drop(iters);
  33.             texts[i] = s;
  34.             // Want to rebuild one iterator for a new string
  35.             // iters[i] = s.split('&');
  36.             iters = texts.iter().map(|s| s.split('&')).collect();
  37.         }
  38.         sleep(Duration::from_millis(100));
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment