I have a package that contains both a binary crate and a library crate. The structure of the package looks like this
.
├── Cargo.lock
├── Cargo.toml
├── src
│ ├── bin
│ │ └── bin.rs
│ ├── lib.rs
│ ├── foo
│ │ ├── concrete.rs
│ │ └── traits.rs
│ └── foo.rs
Cargo.toml:
[package]
name = "mycrate"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
src/lib.rs:
pub mod foo;
src/foo.rs:
pub mod concrete;
pub mod traits;
src/foo/traits.rs:
pub trait A {
...
}
src/foo/concrete.rs
use crate::foo::traits::A;
pub struct B {
...
}
impl B {
...
}
impl A for B {
...
}
And finally src/bin/bin.rs:
use mycrate::foo::concrete::B;
use mycrate::foo::traits::A;
fn main() {
let b: B = ...
// use methods defined in trait A
}
The problem is in the last file both my use statements are having problems according to rust-analyzer
use mycrate::foo::traits::A;
For the above I am getting an unused import warning (but if I don't include it the code won't compile because I use methods defined in A so it needs to be in scope)
use mycrate::foo::concrete::B;
And for above I am getting an unresolved import mycrate::foo::concrete, could not find concrete in foo
error.
Strangely if I run cargo run
or cargo build
it works without any warnings or errors.
rustup --version
(https://www.rust-lang.org/tools/install):
rustup 1.26.0 (5af9b9484 2023-04-05)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.71.1 (eb26296b5 2023-08-03)`
rust-analyzer --version
(https://formulae.brew.sh/formula/rust-analyzer):
rust-analyzer 0.0.0 (99718d0c8 2023-07-22)
hx --version
(my text editor installed from https://formulae.brew.sh/formula/helix)
helix 23.05 (7f5940be)
hx --health | grep rust
rust ✓ rust-analyzer ✘ lldb-vscode ✓ ✓ ✓
I am on a MacBook Pro with Apple M1 Max chip running macOS 13.0.1 (probably not relevant, but including just in case)