how to import a rust file from the directory below?

ghz 8months ago ⋅ 98 views

I'm learning to work with diesel but I can't import created modules

I have already tried several import methods but every time it gives me an error

this is the last try:

use crate::src::models;
use crate::lib;

my error:

error[E0432]: unresolved import `crate::src`
 --> src/bin/test.rs:3:12
  |
3 | use crate::src::models;
  |            ^^^ could not find `src` in the crate root

error[E0432]: unresolved import `crate::lib`
 --> src/bin/test.rs:4:5
  |
4 | use crate::lib;
  |     ^^^^^^^^^^ no `lib` in the root

my file directory:

|src
|->models.rs
|->lib.rs
|
||bin
||->test.rs

Answers

The error message you're encountering indicates that the import paths you've specified cannot be resolved within the crate. This often happens when the paths you're trying to import are not valid or do not exist in the crate's file structure.

In your case, it seems like you're trying to import modules from the src directory, which is the root directory of your project. However, you need to specify the correct path relative to the current module.

Assuming your file structure is like this:

project_root
├── src
│   ├── models.rs
│   └── lib.rs
└── bin
    └── test.rs

And you want to import models.rs and lib.rs into test.rs, you should use relative paths like this:

// For models.rs
use crate::models;

// For lib.rs
use crate::lib;

This assumes that your test.rs file is at the same level as the src directory.

If test.rs is within the bin directory, you might need to adjust the import paths accordingly, like this:

// For models.rs
use crate::../src::models;

// For lib.rs
use crate::../src::lib;

Make sure to replace ../ with the appropriate number of parent directory traversals needed to reach the src directory from test.rs.

Additionally, make sure that your models.rs and lib.rs files are defined correctly and have the appropriate mod declarations. For example, in your lib.rs, you should have something like this:

// Inside lib.rs
mod models;

// Your module content

And in your models.rs, you should have your model definitions.