Hacker News new | ask | show | jobs
by maxton 2215 days ago
I'm not very familiar with Rust, but I don't understand why you wouldn't just use a reference-to-HeavyThing as the function argument, so that the object isn't moved and then dropped in the `get_size` function?
4 comments

If you never drop it, you have a memory leak. If the caller drops it, it's still the same as the `get_size` dropping it in terms of performance impact.

Generally you'd only pass ownership when that's needed for some reason. So this toy example might not be realistic but it does demonstrate the performance impact.

For these contrived cases, yes, you would just pass a reference to the function but I think the point is to simplify the case down to demonstrate a point.
In the olden days, it was just out.flush(); out.close();
So the caller of the function still needs to free HeavyThing in the same thread.
You’re spot on: this is simply a bad example that you would never see in a real application.