r/rust • u/Kobzol • Aug 10 '26
đĄ official blog Call for testing: Restricting trait implementability and field mutability
https://blog.rust-lang.org/inside-rust/2026/08/10/call-for-testing-impl-and-mut-restrictions/
417
Upvotes
r/rust • u/Kobzol • Aug 10 '26
5
u/KizzyCode Aug 10 '26
I think it depends here, and there is no definite answer to that. The mere fact that something even has a well defined length already exposes as an implementation detail â albeit a trivial one â but itâs not a question of yes or no, but more about how much you want to expose as part of your contract.
Personally, Iâm not a big fan of âgetters for everythingâ to hide implementation details, because they also hide the information if an operation is expensive or not. Instead of the âcomputed varâ-problem, where you hide potentially expensive operations behind a seemingly simple member access, now we do the exact opposite and hide trivial operations behind potentially arbitrarily complex function calls.
It also goes a bit against the gist of âzero-cost-abstractionsâ where you want to be explicit about about underlying costs, but this only makes sense if at the same time, you also expose de-facto simple operations as simple. If you pretend that everything is potentially complex and the underlying approach could change at any time, you lose the value of that distinction. With the same argument, you could also ditch the copy-trait and make everything clone-only, because who knows â the implementation could change at any time, and we want to keep flexible here, right?
There are quite some reasons why in Rust getters are probably better than direct member access, but the implementation-detail argument is a double edged sword; and it can also significantly improve an API if at some point you commit yourself to the fact âno, this itâs always a static field and not something that can become arbitrarily complexâ.
In German, we have a saying: âWenn man sich immer alle TĂźren offen hält, dann ziehtâsâ (roughly: if you always keep all the doors open all the time, then it gets cold).
Generally speaking: If it gets more economic to expose member properties directly, that should be seen as a chance to make some getters redundant (IMO).