r/learnrust • u/Ayano-Keiko • 2h ago
No `main` function found in crate for library crate
I use RustRover IDEA. And the IDEA prompt me an error as No 'main' function found in crate
rust compiler seems treat the library crate as binary crate. And when I add
[lib]
path = "lib.rs"
crate-type = ["cdylib"]I have try both path = "lib.rs" and path = "src/lib.rs". path = "lib.rs" works but cannot load other module like mod1.rs and module/mod.rs in the same directory as lib.rs. And after I remove the [lib]. No 'main'function found error return.
I try `cargo test' and the error message shows
error[E0583]: file not found for module `mod1`
--> src/lib.rs:5:1
|
5 | mod mod1;
| ^^^^^^^^^^^^^
|
= help: to create the module `mod1`, create file "src/lib/mod1.rs" or "src/lib/mod1/mod.rs"
= note: if there is a `mod mod1` elsewhere in the crate already, import it with `use crate::...` instead
error[E0583]: file not found for module `mod2`
--> src/lib.rs:6:1
|
6 | mod mod2;
| ^^^^^^^^^^^
|
= help: to create the module `mod2`, create file "src/lib/mod2.rs" or "src/lib/mod2/mod.rs"
= note: if there is a `mod mod2` elsewhere in the crate already, import it with `use crate::...` instead
error[E0583]: file not found for module `module`
--> src/lib.rs:7:1
|
7 | mod module;
| ^^^^^^^^^^
|
= help: to create the module `module`, create file "src/lib/module.rs" or "src/lib/module/mod.rs"
= note: if there is a `mod module` elsewhere in the crate already, import it with `use crate::...` instead
error[E0425]: cannot find function `my_func` in the crate root
--> src/lib.rs:137:16
|
137 | crate::my_func(
| ^^^^^^^^^^^^^^^^^^^ not found in the crate root
|
help: consider importing this function
|
134 + use crate::lib::my_func;
|
help: if you import `my_func`, refer to it directly
|
137 - crate::my_func(
137 + my_func(
|
error: could not compile `<crate-name>` (bin "crate-name>" test) due to 4 previous errors; 5 warnings emitted