3 ms·
> When we pass or return an Event by value, it's at worst a memcpy of a few dozen bytes. There's no implicit heap allocation, garbage collection, or anything li
by Jare 12y ago
> When we pass or return an Event by value, it's at worst a memcpy of a few dozen bytes. There's no implicit heap allocation, garbage collection, or anything like that.
I'm confused by this (total Rust newbie): wouldn't pass by value imply creating a new copy of the String with its own heap-allocated buffer? What does "no implicit heap allocation" refer to?
- steveklabnik 12y agohttp://en.wikipedia.org/wiki/Return_value_optimization http://en.wikipedia.org/wiki/Return_value_optimization TL;DR: Modern languages can tell when you're going to be returning a value, and just place the return in the right place, rather than making a copy that'd just get thrown away. I believe this is what they're referring to. It also might be talking about how just the tag gets copied, and not all the values. I think.
- Jare 12y agoYeah RVO is clear, I'm confused about the "pass by value" which I assume is as a parameter to a function, where a creating a copy is unavoidable in the general case.
- steveklabnik 12y agoI pinged the author, and they said: > there is no implicit heap allocation, there is explicit heap allocation when you create the String, but not when you're just moving it around
- dbaupp 12y agoOwnership of the String is moved and the original variable cannot be used until it is reinitialised, it is not copied. Pass by value is literally always a shallow byte copy in Rust (not following pointers) and the Rust compiler must disallow further use of many variables to ensure safety.
- Jare 12y agoRight! Thank you!