Unable to `use` diesel related functions

I have defined some table & struct in src/schema.rs and src/models.
I also wrote some pub fn inside src/lib.rs.
But I just cannot use those pub fns in other rs files so moved into src/io.rs later.
Whatever they're in src/lib.rs or in src/io.rs separately, when calling them by use crate::fn_name or use crate::io::fn_name, the compiler would warn me that:

# cargo build
   Compiling minstl v0.2.4 (/home/user/minstl)
error[E0433]: failed to resolve: use of undeclared crate or module `libmi`
 --> src/http.rs:6:5
  |
6 | use libmi::auth::LoginReq;
  |     ^^^^^ use of undeclared crate or module `libmi`

error[E0412]: cannot find type `LoginReq` in this scope
  --> src/http.rs:35:36
   |
35 | async fn req_login(data: web::Json<LoginReq>, req: HttpRequest) -> Result<HttpResponse> {
   |                                    ^^^^^^^^ not found in this scope
   |
help: consider importing this struct
   |
1  | use crate::auth::LoginReq;
   |

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0412, E0433.
For more information about an error, try `rustc --explain E0412`.
error: could not compile `minstl`

To learn more, run the command again with --verbose.

libmi is the name of lib I defined in Cargo.toml.
and it is still unfeasible to use the method recommended in help:

# cargo build
   Compiling minstl v0.2.4 (/home/user/minstl)
error[E0432]: unresolved import `crate::auth`
 --> src/http.rs:1:12
  |
1 | use crate::auth::LoginReq;
  |            ^^^^
  |            |
  |            unresolved import
  |            help: a similar path exists: `libmi::auth`

error[E0432]: unresolved import `crate::io`
 --> src/http.rs:2:12
  |
2 | use crate::io::{create_request, establish_connection};
  |            ^^
  |            |
  |            unresolved import
  |            help: a similar path exists: `libmi::io`

error[E0432]: unresolved import `crate::shell`
 --> src/http.rs:3:12
  |
3 | use crate::shell::StmtReq;
  |            ^^^^^
  |            |
  |            unresolved import
  |            help: a similar path exists: `libmi::shell`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0432`.
error: could not compile `minstl`

To learn more, run the command again with --verbose.

code is here:
https://github.com/halldong/minstl/blob/main/src/http.rs#L1-L3

I'm so confused. Is there anyone can help?

But it looks like your crate is actually called minstl, isn't it? Did you try changing crate:: and libmi:: to the minstl:: namespace?

yeah, I called minstl before, and it's not work.
Then I defined a lib named 'libmi' in Cargo.toml and that's what you see now.

https://github.com/halldong/minstl/blob/main/Cargo.toml#L29-L30

You've defined the http module both in main.rs and lib.rs.

// main.rs
mod http;
// ...
// lib.rs
// ...
pub mod http;
// ...

This basically means that the code in http.rs is copied to both to the library crate (lib.rs) and binary crate (main.rs).

use crate is an error in the binary crate, and use libmi is an error in the library crate, so the compiler always hits one of these issues, and when you try to follow its suggestion you trigger the other error. The fix is to remove mod http; from main.rs. You should usually only have one mod statement that corresponds to a given file, and most of the time you want them in the library crate.

Yes I'm so confused about such crate/super/self/[project name] usage.
And I have never thought such a clean solution.
Thanks Heliozoa and thanks for your explanation !

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.