Skip to content

Documentation: accessing the private items of a parent module #31309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
swatteau opened this issue Jan 30, 2016 · 2 comments
Closed

Documentation: accessing the private items of a parent module #31309

swatteau opened this issue Jan 30, 2016 · 2 comments

Comments

@swatteau
Copy link

When accessing items defined in the parent module, it is tempting to write use super::*;. However, this does not allow accessing private items. For example, this fails:

fn print_hello() {
    println!("hello");
}

mod inner {
    use super::*;
    pub fn do_something() {
        print_hello(); // error: unresolved name `print_hello` [E0425]
    }
}

This is a mistake I've made several times (I suspect many others have too) and the solution is to be explicit about the item used, either with

use super::print_hello;

or

super::print_hello();

On this topic, the Rust Reference says:

Use declarations support a number of convenient shortcuts:

  • ...
  • Binding all paths matching a given prefix, using the asterisk wildcard syntax use a::b::*;
  • ...

I find this a bit misleading because the asterisk wildcard syntax really only binds public paths matching a given prefix.

I think it would be nice to add a paragraph in the Rust Book to explain how to access private items properly and to state clearly that use super::* won't help.

@steveklabnik
Copy link
Member

I just added one to the book. The PR may still be open, or it may have been merged in the last day or two; I'm on mobile or I'd check.

On Jan 30, 2016, 11:11 -0500, Sébastien [email protected], wrote:

When accessing items defined in the parent module, it is tempting to writeuse super::*;However, this does not allow accessing private items For example, this fails:

fnprint_hello() { println!("hello");}modinner {usesuper::*;pubfndo_something() {print_hello();// error: unresolved name print_hello [E0425]} }

This is a mistake I've made several times (I suspect many others have too) and the solution is to be explicit about the item used, either with

usesuper::print_hello;

or

super::print_hello();

On this topic, theRust Referencesays:

Use declarations support a number of convenient shortcuts:

  • Binding all paths matching a given prefix, using the asterisk wildcard syntaxuse a::b::*;

I find this a bit misleading because the asterisk wildcard syntax really only bindspublicpaths matching a given prefix

I think it would be nice to add a paragraph in the Rust Book to explain how to access private items properly and to state clearly thatuse super::*won't help


Reply to this email directly orview it on GitHub(#31309).

@steveklabnik
Copy link
Member

Yes, it was #31226

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants