- 结构体的可见性
- 参见:
- 参见:
结构体的可见性
结构体对字段的可见性有额外的规定(Structs have an extra level of visibility with their fields)。其可见性默认为私有,也可以加上 pub 修饰语来改变默认属性。只有当从定义在外部的模块访问一个结构体时,这可见性才显得重要,并具有隐藏信息的目的(封装,encapsulatoin)(原文:This visibility only matters when a struct is accessed from outside the module where it is defined, and has the goal of hiding information (encapsulation))。
mod my {// 一个公有的结构体,带有一个公有的泛型类型 `T` 的字段pub struct WhiteBox<T> {pub contents: T,}// 一个公开的结构体,带有一个私有的泛型类型 `T` 的字段#[allow(dead_code)]pub struct BlackBox<T> {contents: T,}impl<T> BlackBox<T> {// 一个公有的构造器方法pub fn new(contents: T) -> BlackBox<T> {BlackBox {contents: contents,}}}}fn main() {// 带有公有字段的公有的结构体,可以像平常一样构造let white_box = my::WhiteBox { contents: "public information" };// 并且它们的字段可以正常访问到。println!("The white box contains: {}", white_box.contents);// 带有私有字段的公有结构体不能使用字段名来构造。// 报错!`BlackBox` 含有私有字段。//let black_box = my::BlackBox { contents: "classified information" };// 试一试 ^ 将此行注释去掉// 不过带有私有字段的结构体可以使用公有的构造器来创建。let _black_box = my::BlackBox::new("classified information");// 并且一个结构体中的私有字段不能访问到。// 报错!`content` 字段是私有的。//println!("The black box contains: {}", _black_box.contents);// 试一试 ^ 将此行注释去掉}
参见:
泛型 and 方法
