Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by lukaszwojtow on Saturday December 31, 2022 @12:01PM
(#34198313)
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.
[ Parent
| on HN ]
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by carlmr on Saturday December 31, 2022 @12:05PM
(#34198349)
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.
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by QuadDamaged on Saturday December 31, 2022 @01:10PM
(#34199001)
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.
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by theptip on Saturday December 31, 2022 @01:41PM
(#34199339)
Rust noob here - is it common to see a struct lose Copy as things grow?
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by zozbot234 on Saturday December 31, 2022 @01:51PM
(#34199446)
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.
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by eterevsky on Saturday December 31, 2022 @03:00PM
(#34200141)
If it's a 3D real-valued vector, or similarly basic structure, you can be fairly certain, that it will stay copyable.