3 ms·
> Here's the equivalent in C++: > int main() { std::string x = ""; foo(x); bar(x); // x is destroyed } Doesn't C++ do a copy
by 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.