Limited mutation with visibility

⚓ Rust    📅 2026-04-15    👤 surdeus    👁️ 5      

surdeus

I have, simplified:

pub struct AState P
....
  bf: B,
...
}

and in another module

pub struct B {
   pub f1: String,
   pub f2: String,
...
}

Due to the nature of the program (iced) there are thigns with mut reference to an instance of AState. I have made the fields of B public so that the various view and update logics can get at the fields, as I feel the structure is stable enough to expose. The only way B instances themselves get "changed" is by creating a new instance, and replacing the one stored in teh instance of AState. So I would prefer to mark, so I don't confuse myself when maintaining this code, that the fields of B are accessible but should never be directly mutated. Is there a way I missed to mark that, or is the only way by constructing accessor methods and makign the fields private? (Part of the reason I am exploring this is that folks in an earlier thread, if I understood them properly, indicated that if the structure is stable and clear, making the fields pub is better than defining accessors.)
Thanks,
Joel

5 posts - 3 participants

Read full topic

🏷️ Rust_feed