2 ms·
Don't you need to know when all borrows end? E.g. here: fn main() { let x = [1, 2, 3]; println!("{:?}", x); let y = x.map(|x| x % 2 == 0);
by yakubin 4y ago
Don't you need to know when all borrows end? E.g. here:
fn main() {
let x = [1, 2, 3];
println!("{:?}", x);
let y = x.map(|x| x % 2 == 0);
println!("{:?}", y);
}
x is borrowed by println (or rather by code that println expands to) and then it is moved by array::map. The compiler needs to know that before x.map(), x is not borrowed anymore. Otherwise it could be that println actually stored a reference to x somewhere and now you have a dangling reference possibly breaking type-safety (referring to an array of bools through a reference to array of ints, if the map is implemented as an in-place operation).
- quietbritishjim 4y agoSure but that is already a risk in C++ at the moment. void foo() { if (true) { Foo f; stores_a_reference_to_foo(&f); } uses_stored_reference_to_foo(); } I see that destructive moves would in theory introduce more opportunities for that, but I don't really see it making a big difference in practice - routines that take a pointer/reference and hold onto them (without taking ownership) already need a lot of care in C++.
- jcranmer 4y agoWithout a borrow checker, you won't be able to eliminate dangling reference issues. But that's an orthogonal issue to move safety.