2 ms·
> Passing data to a function when it's not a reference means that you're giving ownership to that function. Only in Rust, but there's no apparent reason to mov
by weberc2 9y ago
> Passing data to a function when it's not a reference means that you're giving ownership to that function.
Only in Rust, but there's no apparent reason to move ownership. If Rust didn't move ownership during pass-by-value, it would still be trivial to prove the following code correct:
fn main() {
let x = String::new();
foo(x);
bar(x);
// x is destroyed
}
> Maybe I'm misunderstanding the problem you're having. Do you have an example of another language that lets you do what you want?
Here's the equivalent in C++:
int main() {
std::string x = "";
foo(x);
bar(x);
// x is destroyed
}
- wtetzner 9y ago> Here's the equivalent in C++: > int main() { std::string x = ""; foo(x); bar(x); // x is destroyed } Doesn't C++ do a copy in this case?
- weberc2 9y agoIt does; that's the point: you can copy without losing safety guarantees, so there's no obvious reason to use an affine type system.
- wtetzner 9y agoYou can copy in Rust too. You just call .clone(), or if you want your type to always be copied, have it implement the Copy trait. In that case, it does exactly what you want. But Rust is more explicit about copying in general, to make it harder for the performance cost to be hidden.