I have a Rust project with the following structure:
.
├── Cargo.toml
└── src
├── main.rs
├── generator.rs
└── lib.rs
With the following code in lib.rs and main.rs respectively:
# lib.rs
pub mod generator
# main.rs
use clap::{Parser, Subcommand};
use mypackage::generator;
The above builds. However, I want to create another binary crate in the same package and I read here that multiple binary crates can be crated in the src/bin
directory. But when I move the main.rs
file as check.rs
in src/bin
, leading to the directory as follows:
.
├── Cargo.toml
└── src
├── bin
│ └── check.rs
├── generator.rs
└── lib.rs
I get the following error:
error[E0432]: unresolved imports `mypackage::generator`
--> src/bin/check.rs:5:5
|
5 | generator::Generator,
| ^^^^^^^^^ could not find `generator` in `mypackage`
error[E0432]: unresolved import `clap`
--> src/bin/check.rs:3:5
|
3 | use clap::{Parser, Subcommand};
| ^^^^ use of undeclared crate or module `clap`
I did not change my Cargo.toml as it seems this should've worked as it is based on the answer on this StackOverflow post. Any clues as to why this is going wrong?
More importantly, why is the binary crate not even able to resolve the
clap
package which is listed in Cargo.toml as a dependency too?
Edit
This is what my Cargo.toml
file looks like:
[package]
name = "mypackage"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = {version = "4.1.8", features = ["derive"], optional = true}
dot = {version = "0.1.4", optional = true}
enumset = "1.0.12"
inkwell = {version = "0.1.1", features = ["llvm15-0"], optional = true}
itertools = "0.10.5"
lalrpop-util = "0.19.8"
once_cell = "1.17.1"
rayon = "1.7.0"
regex = "1.7.1"
snailquote = "0.3.1"
strum = {version = "0.24.1", features = ["derive"]}
tracing = "0.1.37"
tracing-subscriber = {version = "0.3.16", features = ["env-filter"]}
[build-dependencies]
lalrpop = "0.19.9"
[features]
default = []
generate = []
bin = ["generate", "dep:clap"]
And I am running a simple cargo build
to build my package.