You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
fnprint_hello(){println!("hello");}mod inner {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
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.
The text was updated successfully, but these errors were encountered:
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:
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).
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: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
or
On this topic, the Rust Reference says:
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.The text was updated successfully, but these errors were encountered: