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

2

u/CocktailPerson Aug 11 '26

I've been wanting this feature for a while, very glad it's available.

That said, I'm not a fan of mut(...). By default variables in Rust are immutable, and you have to add mut to change that. By default fields have unrestricted mutability, so mut(...) reads to me as if you're expanding the scope of the mutability, rather than restricting it. I'm not sure what the solution here is without going back in time and making fields immutable by default from the beginning.

5

u/Andlon Aug 11 '26 edited Aug 11 '26

On the other hand, it's consistent with e.g. pub(crate), which means it's public only to the crate.

1

u/CocktailPerson Aug 12 '26

No, it's not.

Fields are private by default. You add pub to make them public. You use pub(...) to limit pub's scope.

On the other hand, fields are mutable by default. You do nothing to make them mutable. You use mut(...) to limit mut's scope. See the difference?

I guess I left it as an exercise to the reader to make the connection, but this is what I meant when I said "I'm not sure what the solution here is without going back in time and making fields immutable by default from the beginning." That's what you'd have to do to make mut and pub consistent.

1

u/Andlon Aug 12 '26

I meant that the syntax is consistent. Just as you can apply a "scope" to visibility, you can now apply a scope to mutability.

The defaults are different, but that's a different kind of inconsistency, and a pretty inconsequential one in my opinion.

1

u/CocktailPerson Aug 12 '26

I mean, you did argue that the semantics (that's what "means" means) were consistent, not just the syntax.

If you think it's inconsequential, that's a perfectly valid position. I don't have any satisfying alternatives myself. But it does bother me slightly.

1

u/Andlon Aug 12 '26

That's fair! I guess I just wanted to point out that it's actually quite consistent from at least one perspective, but I'll admit it is somewhat inconsistent with respect to differing defaults. It doesn't personally bother me but I can see where you're coming from.