使方法对子文件夹可见

问题描述 投票:0回答:1

我的 Rust 项目结构简单:

/src
  /stuff
    /mod.rs
    /foo.rs
  /stuff2
    /mod.rs
    /foo2.rs

每个 mod.rs 包含一行:

pub mode foo;
pub mod foo2;

在 foo.rs 中,我有:

pub struct Foo {}

impl Foo {
  fn private_method() {}
  pub fn public_method() {}
  pub fn visible_only_within_stuff_method() {}
}

如何控制

visible_only_within_stuff_method()
的可见性,以便
/stuff
内的所有其他代码都可以看到它,但在它之外什么都看不到?我不希望它对里面的任何东西可用
/stuff2

我正在寻找的是 Java 中的 package-private 的大致等价物。

rust
1个回答
0
投票

您可以使用

pub(super)
使成员对父模块可见,因此这应该可行:

pub(super) fn visible_only_within_stuff_method() {}

进一步阅读:

© www.soinside.com 2019 - 2024. All rights reserved.