4

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};
3
  • 2
    The example you've provided works just fine. Either you've pasted the wrong code, or you're omitting something important. Commented Sep 30, 2018 at 11:57
  • The answer below is right.. I use the 2018 edition, the old way won't work.. Commented Sep 30, 2018 at 13:51
  • The opposite is true; the old way does work with this code. Using a beta feature and not mentioning it counts as omitting something important. Commented Sep 30, 2018 at 14:02

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Not sure if things have changed since then, but I'm still having the same issue as OP, even when using the crate prefix, in following your answer. I have a file called helpers.rs in the same directory as my main.rs. helpers.rs has a simple module defined in it, and I wish to use one of the functions from that module in my main.rs. The first line of my main.rs is use crate::helpers;, but when I try to compile I get the error message "no 'helpers' in the root (E0432)".
same here i am having the import issue, even after using the crate import statement.
@VeerPratap Do you havve a mod declaration for your module in main.rs or lib.rs?
0

I think you may have caused it by not introducing it in the main.rs。I'll use my experience as an example。 Below is my file directory。

└── src
    ├── console.rs
    ├── main.rs
    └── sbi.rs

I want to introduce the functions in the sbi.rs in the console.rs. The first ,you should do is import sbi.rs module in your main.rs

in main.rs

mod sbi;

in console.rs

use crate::sbi::console_putchar;

in sbi.rs

    pub fn console_putchar(c: usize) {
    #[allow(deprecated)]
    sbi_rt::legacy::console_putchar(c);
}

If you're compiling a lab project,There are no main.rs file。You need to import the module first in the file you want to import

in console.rs

mod sbi;
use crate::sbi::console_putchar;

in sbi.rs

    pub fn console_putchar(c: usize) {
    #[allow(deprecated)]
    sbi_rt::legacy::console_putchar(c);
}

That's how I solved this problem, I hope it helps you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.