I am using super
inside a module to refer to names in the parent namespace. However, I cannot get this to work when I refer to a name in the parent namespace that was brought in with a use
statement. What am I doing wrong?
// 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
}
edit: remove pub
from local_hello
Further clarification on what I am asking: The function local_hello()
is declared as private in the crate namespace. The function lib_hello()
is brought in with use
and also becomes a private name in the crate namespace. At this point the names local_hello
and lib_hello
have equal stature: they are both in the crate namespace, and both are private. In mymod
I use super
to refer to the crate namespace and can get access to local_hello
but not lib_hello
. What gives?
I know about Python and C++ namespaces. Perhaps there is some crucial bit I need to unlearn?
use
statements only import to the local scope. If you want to reexport, 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
will make the item seem like it exists in the module being reexported from.