Book: Unclear mentions of public Vs. private struct fields

Created on 18 Jun 2018  路  2Comments  路  Source: rust-lang/book

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.

Bug

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!

All 2 comments

+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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikebenfield picture mikebenfield  路  3Comments

dlukes picture dlukes  路  3Comments

istarus picture istarus  路  4Comments

binarycrusader picture binarycrusader  路  4Comments

xStrom picture xStrom  路  3Comments