Hacker News new | ask | show | jobs
by lukaszwojtow 1270 days ago
I always prefer by-borrow. That's because in the future this struct may become non-copy and that means some unnecessary refactoring. My thinking is a bit like "don't take ownership if not needed" - the "not needed" part is the most important thing. Don't require things that are not needed.
5 comments

If a struct might lose Copy you shouldn't implement Copy at all, to preserve forward compatibility. You can still derive Clone in most cases; using .clone() does not per se add any overhead.
Exactly, and if performance at some point matters: benchmark!

And I would bet 9 times out of 10 it won't be the bottleneck or even make a measurable difference.

Exactly why IMHO the rust stdlib is so easy to understand. Ownership only when required as a design principle tends to make the design of the overall system more consistent / easier to approach.
If it's a 3D real-valued vector, or similarly basic structure, you can be fairly certain, that it will stay copyable.
I agree. Being copyable is part of the signature for something like this. Explicitly so in rust.
Rust noob here - is it common to see a struct lose Copy as things grow?
> don't take ownership if not needed

That's my approach too as a Rust newbie. Borrow by default and take ownership only when needed, for the best ergononmics.