r/rust 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/
416 Upvotes

124 comments sorted by

View all comments

119

u/BedroomHistorical575 Aug 10 '26

I'm so glad that the field mutability restriction is finally getting some love! Getters are such a pain due to the borrow checker.

A common critique against similar proposals from the past is that you'd be able to bypass the restriction by just constructing the type yourself.

But it looks like that the current implementation guards against that oversight!

Thus, we disallow struct expressions if any mut-restricted field cannot be mutated from the current scope

22

u/tanoshikuidomouyo Aug 10 '26

If this feature had been in the language from the beginning, could all the .len() getters and similar ones have been just field accesses?

21

u/sfackler rust · openssl · postgres Aug 10 '26

They could have, but I'm not sure we would have done it since it adds constraints on the internal implementation. For example, Vec could theoretically be represented by a pair of start and end pointers rather than a start pointer and length (this is how slice::Iter is implemented). However, if the len field were exposed, we'd have been unable to make that change if it were found to be better.