不能使用 "super" 来引用由 "use" 从另一个 crate 引入的名称

Can't use "super" to refer to a name that was brought in by "use" from another crate

我在模块内使用 super 来引用父名称空间中的名称。但是,当我在父命名空间中引用一个用 use 语句引入的名称时,我无法使它起作用。我做错了什么?

// crate mylib

pub fn lib_hello() {println!("Hello from mylib!");}


// crate mybin

extern crate mylib;
use mylib::lib_hello;

fn local_hello() {println!("Hello from here!");}

mod mymod {
    fn f() { super::local_hello() } // Ok
    fn g() { super::lib_hello() }   // error: unresolved name `super::lib_hello`
}

fn main() {
    lib_hello(); // Ok
}

编辑:从 local_hello

中删除 pub

进一步说明我的问题:函数 local_hello() 在 crate 命名空间中被声明为私有的。函数 lib_hello()use 一起引入,并且也成为 crate 命名空间中的私有名称。此时名称 local_hellolib_hello 具有同等地位:它们都在 crate 命名空间中,并且都是私有的。在 mymod 中,我使用 super 来引用 crate 命名空间,并且可以访问 local_hello 但不能访问 lib_hello。给出了什么?

我知道 Python 和 C++ 命名空间。也许我需要忘记一些关键点?

use 语句仅导入到本地范围。如果要再导出,请使用 pub use

// crate mylib

pub fn lib_hello() {println!("Hello from mylib!");}


// crate mybin

extern crate mylib;
pub use mylib::lib_hello;

pub fn local_hello() {println!("Hello from here!");}

mod mymod {
    fn f() { super::local_hello() } // Ok
    fn g() { super::lib_hello() }   // error: unresolved name `super::lib_hello`
}

fn main() {
    lib_hello(); // Ok
}

pub use 将使项目看起来像是存在于从中重新导出的模块中。