Making structs and enums public allows other code to see and use them. If we use pub before a struct definition, we make the struct public, but the struct’s fields will still be private. We can make each field public or not on a case-by-case basis. In contrast, if we make an enum public, all of its variants are then public. We only need the pub before the enum keyword. Here is an example of making a struct public:
mod back_of_house {
pub struct Breakfast {
pub toast: String,
seasonal_fruit: String,
}
}
This struct is public, so other code can see and use it. The toast field is also public, so other code can see and modify the value of the toast field. The seasonal_fruit field is private, so other code can't see or modify the value of the seasonal_fruit field. Here is an example of making an enum public:
mod back_of_house {
pub enum Appetizer {
Soup,
Salad,
}
}
This enum is public, so other code can see and use it. The Soup and Salad variants are also public, so other code can use them.