Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> 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?



Ownership 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.


Right! Thank you!


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.


Yeah 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.


I 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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: