我的 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 的大致等价物。
您可以使用
pub(super)
使成员对父模块可见,因此这应该可行:
pub(super) fn visible_only_within_stuff_method() {}
进一步阅读: