Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

rust - Access submodule from another submodule when both submodules are in the same main module

I'm using rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14) and I've got the following setup:

my_app/
|
|- my_lib/
|   |
|   |- foo
|   |   |- mod.rs
|   |   |- a.rs
|   |   |- b.rs
|   |
|   |- lib.rs
|   |- Cargo.toml
|
|- src/
|   |- main.rs
|
|- Cargo.toml

src/main.rs:

extern crate my_lib;

fn main() {
  my_lib::some_function();
}

my_lib/lib.rs:

pub mod foo;

fn some_function() {
  foo::do_something();
}

my_lib/foo/mod.rs:

pub mod a;
pub mod b;

pub fn do_something() {
  b::world();
}

my_lib/foo/a.rs:

pub fn hello() {
  println!("Hello world");
}

my_lib/foo/b.rs:

use a;
pub fn world() {
  a::hello();
}

Cargo.toml:

[dependencies.my_lib]
path = "my_lib"

When I try to compile it I get the following error thrown for the use statement in B.rs:

unresolved import `A`. There is no `A` in `???`

What am I missing? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is that use paths are absolute, not relative. When you say use A; what you are actually saying is "use the symbol A in the root module of this crate", which would be lib.rs.

What you need to use is use super::A;, that or the full path: use foo::A;.

I wrote up a an article on Rust's module system and how paths work that might help clear this up if the Rust Book chapter on Crates and Modules doesn't.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...