“unresolved import” when calling functions across

2019-07-11 04:38发布

问题:

Here is the file tree of my demo project:

.
├── Cargo.lock
├── Cargo.toml
├── src
    ├── lib.rs
    ├── ooo.rs
    └── xxx.rs

In lib.rs:

mod xxx;
mod ooo;

In xxx.rs:

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

In ooo.rs:

use xxx::hello;

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

When I execute cargo build, it doesn't succeed:

   Compiling ooo v0.1.0 (/Users/eric/ooo)
error[E0432]: unresolved import `xxx`
 --> src/ooo.rs:1:5
  |
1 | use xxx::hello;
  |     ^^^ Could not find `xxx` in `{{root}}`

I know that if I use super::ooo::hello instead of ooo::hello, it will succeed, but is there any way I can use ooo::hello and succeed?

For example, this works in the redis-rs project in src/client.rs where connection and types are the modules in this crate:

use connection::{connect, Connection, ConnectionInfo, ConnectionLike, IntoConnectionInfo};
use types::{RedisFuture, RedisResult, Value};

回答1:

You appear to be using the beta version of the 2018 edition of Rust rather than the stable release. In the new version, you need to explicitly mark imports from the current crate with the crate keyword:

use crate::xxx::hello;

See the section on "path clarity" in the edition guide for more details.



标签: rust