SHARE
TWEET

Untitled

a guest Jun 20th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn make_iter(first: &First) -> Box<dyn Iterator<Item=String> + '_> {
  2.     Box::new(first.make_objects().flat_map(|second| {
  3.         second.iter()
  4.             .filter(|third| third.as_str() != "t2")
  5.             .flat_map(|third| {
  6.                 vec![
  7.                     format!("{}.A", third),
  8.                     format!("{}.B", third),
  9.                 ].into_iter()
  10.             })
  11.             .chain(
  12.                 vec![
  13.                     format!("{}.A", second.name()),
  14.                     format!("{}.B", second.name()),
  15.                 ].into_iter()
  16.             )
  17.     }))
  18. }
  19.  
  20. pub fn main() {
  21.     let first = First{};
  22.     for i in make_iter(&first) {
  23.         println!("{}", i);
  24.     }
  25. }
  26.  
  27. struct First {}
  28.  
  29. impl First {
  30.     fn make_objects(&self) -> Box<dyn Iterator<Item=Second> + '_> {
  31.         Box::new(vec![
  32.             Second::new("s1".to_string()),
  33.             Second::new("s2".to_string()),
  34.             Second::new("s3".to_string()),
  35.         ].into_iter())
  36.     }
  37. }
  38.  
  39. struct Second {
  40.     name: String,
  41.     objects: Vec<String>,
  42. }
  43.  
  44. impl Second {
  45.     fn new(name: String) -> Second {
  46.         Second {
  47.             name,
  48.             objects: vec!["t1".to_string(), "t2".to_string(), "t3".to_string()],
  49.         }
  50.     }
  51.  
  52.     fn name(&self) -> &str {
  53.         &self.name
  54.     }
  55.  
  56.     fn iter(&self) -> Box<dyn Iterator<Item=&String> + '_> {
  57.         Box::new(self.objects.iter())
  58.     }
  59. }
  60.      
  61. error[E0597]: `second` does not live long enough
  62.   --> src/main.rs:4:9
  63.    |
  64. 4  |         second.iter()
  65.    |         ^^^^^^ borrowed value does not live long enough
  66. ...
  67. 18 |     }))
  68.    |     - `second` dropped here while still borrowed
  69.  
  70. error: aborting due to previous error
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