Skip to content

Allow pub attribute for struct fields #224

@katat

Description

@katat

For struct fields, sometimes we want to control the accessibility of the fields, so they are only accessible within the struct methods.

For example:

struct Room {
	pub beds: Field, // public
        size: Field // private
}

Uint8.update_size(self, size: Field) {
        self.size = size; 
}

fn main() {
       let room = Room {beds: 2, size: 10};
       uint.beds = 2; // allowed
       uint.size = 5;  // not allowed
       uint.update_size(5); // allowed
}

When there is a pub attribute for a struct field, the field can be accessible from external. Otherwise, the private fields are only accessible in the struct methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions