Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"unresolved import" when calling functions across modules in Rust 2018

Tags:

rust

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};
like image 250
Pucker Avatar asked Sep 12 '25 13:09

Pucker


1 Answers

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.

like image 160
Sven Marnach Avatar answered Sep 14 '25 02:09

Sven Marnach