In the 2nd edition of the text in CH13 section 2 (Iterators) is the following excerpt:
The count field is private because we want the implementation of Counter to manage its value.
I don't recall reading about the semantics of accessing non-pub fields on struct instance when a struct itself is not declared as pub
From my understanding:
struct Counter {
count: u32,
}
Instantiating a Counter in the same module where the struct is defined means that the count field is accessible. However, if count is attempted to be accessed in another module, there will be a compiler error.
The point of this issue is to assess whether the there should be more explicit mention of these semantics in this chapter, and in the chapters where field access rules are directly or indirectly mentioned.
+1 from me. I wasn't able to find anywhere in the book that discusses public fields explicitly, although there are some references in 17.1 to "leaving fields private" or "making list public" that imply it is possible. It made me very confused about constructing new structs.
We have added a section on the visibility of structs and fields; it hasn't made it to doc.rust-lang.org/stable/book yet but will soon :) Thanks!
Most helpful comment
We have added a section on the visibility of structs and fields; it hasn't made it to doc.rust-lang.org/stable/book yet but will soon :) Thanks!