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()))
1 Like