I have the following rust module.
test.rs:
pub fn foo(xs: &[f32]) -> Vec<f32> {
xs.iter()
.flat_map(|x| xs.iter().map(|y| *x - y))
.collect()
}
If I compile this module (as rlib) I got an error:
$ rustc -o test.s --crate-type rlib test.rs
error[E0373]: closure may outlive the current function, but it borrows `x`, which is owned by the current function
[...]
The same can be as well experimented by using godbolt (which does pretty much the same thing).
However, if I compile the same function within a binary crate. The compiler accepts this code.
For example, by using rust-playground.
(Note we are using the same compiler version here; that is, 1.63.0)
Question
Why compiler behaves differently in those two cases?
I suspect linker might kick in here, but I would like to have more technical confirmation.