3.3 可见性

03-可见性 — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/modules/visibility.html

3.3 可见性

模块是一道隐私边界:

  • 模块中的项默认是私有的(隐藏实现细节)。
  • 父模块与兄弟模块中的项始终可见。
  • 换言之,若某个项在模块 foo 中可见,则在 foo 的所有后代模块中也可见。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Copyright 2022 Google LLC
// SPDX-License-Identifier: Apache-2.0
mod outer {
    fn private() {
        println!("outer::private");
    }

    pub fn public() {
        println!("outer::public");
    }

    mod inner {
        fn private() {
            println!("outer::inner::private");
        }

        pub fn public() {
            println!("outer::inner::public");
            super::private();
        }
    }
}

fn main() {
    outer::public();
}
  • 使用 pub 关键字让模块变为公开。

此外,还有更高级的 pub(...) 说明符,可限制公开可见性的范围。

  • 参见 Rust Reference。
  • 配置 pub(crate) 可见性是常见模式。
  • 较少见地,也可以把可见性授予某个具体路径。
  • 无论如何,可见性必须授予某个祖先模块(及其所有后代)。
最后修改 August 11, 2026: 更新 (70a5af133)