Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn make_iter(first: &First) -> Box<dyn Iterator<Item=String> + '_> {
- Box::new(first.make_objects().flat_map(|second| {
- second.iter()
- .filter(|third| third.as_str() != "t2")
- .flat_map(|third| {
- vec![
- format!("{}.A", third),
- format!("{}.B", third),
- ].into_iter()
- })
- .chain(
- vec![
- format!("{}.A", second.name()),
- format!("{}.B", second.name()),
- ].into_iter()
- )
- }))
- }
- pub fn main() {
- let first = First{};
- for i in make_iter(&first) {
- println!("{}", i);
- }
- }
- struct First {}
- impl First {
- fn make_objects(&self) -> Box<dyn Iterator<Item=Second> + '_> {
- Box::new(vec![
- Second::new("s1".to_string()),
- Second::new("s2".to_string()),
- Second::new("s3".to_string()),
- ].into_iter())
- }
- }
- struct Second {
- name: String,
- objects: Vec<String>,
- }
- impl Second {
- fn new(name: String) -> Second {
- Second {
- name,
- objects: vec!["t1".to_string(), "t2".to_string(), "t3".to_string()],
- }
- }
- fn name(&self) -> &str {
- &self.name
- }
- fn iter(&self) -> Box<dyn Iterator<Item=&String> + '_> {
- Box::new(self.objects.iter())
- }
- }
- error[E0597]: `second` does not live long enough
- --> src/main.rs:4:9
- |
- 4 | second.iter()
- | ^^^^^^ borrowed value does not live long enough
- ...
- 18 | }))
- | - `second` dropped here while still borrowed
- error: aborting due to previous error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement