Help me make this function work

fn join_paths<'a, A: AsRef<Path> + 'a, B: AsRef<Path> + 'a>(
    a: impl Iterator<Item = A>,
    b: B,
) -> impl Iterator<Item = PathBuf> + 'a {
    a.map(|a| a.as_ref().join(b.as_ref()))
}

I don't want to collect it into a Vec because I want it to be lazy, but lifetimes make it difficult.

By default closures tries to only borrow its environment, but you can't return something which contains references to local variables. Move closure solves it by moving all its environments into itself.

a.map(move |a| a.as_ref().join(b.as_ref()))

https://doc.rust-lang.org/stable/book/ch13-01-closures.html#capturing-the-environment-with-closures

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.