I have the following Rust project:
./
├── Cargo.lock
├── Cargo.toml
├── src
│ ├── Easy
│ │ ├── 001_Two_Sum.rs
│ │ ├── 083_Remove_Duplicates_from_Sorted_List.rs
│ │ └── bin
│ ├── lib.rs
│ └── utils
│ ├── list_node
│ │ └── mod.rs
│ └── mod.rs
└── target
src/lib.rs
pub mod utils;
pub use crate::utils::list_node::ListNode;
src/utils/mod.rs
pub mod list_node;
src/utils/list_node/mod.rs
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>
}
impl ListNode {
#[inline]
pub fn new(val: i32) -> Self {
ListNode {
next: None,
val
}
}
}
src/Easy/083_Remove_Duplicates_from_Sorted_List.rs
struct Solution;
use crate::utils::list_node::ListNode;
impl Solution {
pub fn delete_duplicates(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let n = ListNode::new(1);
}
}
fn main() {
println!("\nJob done!");
}
Cargo.toml
[package]
name = "leetcode"
version = "0.1.0"
edition = "2021"
[dependencies]
cargo-watch = "8.5.2"
[[bin]]
name = "001_Two_Sum"
path = "src/Easy/001_Two_Sum.rs"
[[bin]]
name = "083_Remove_Duplicates_from_Sorted_List"
path = "src/Easy/083_Remove_Duplicates_from_Sorted_List.rs"
I'm trying to use ListNode
struct in 083_Remove_Duplicates_from_Sorted_List.rs
file but I'm getting this error message:
failed to resolve: unresolved import
unresolved importrustc (Click for full compiler diagnostic)
083_Remove_Duplicates_from_Sorted_List.rs(3, 12): a similar path exists: `leetcode::utils`
If I use: use crate::leetcode::utils::list_node::ListNode;
I get this another error message:
failed to resolve: could not find `leetcode` in the crate root
could not find `leetcode` in the crate rootrustc (Click for full compiler diagnostic)
Previously I tried other configurations (found on Internet and suggested by IAs) but nothing seems to work.
I'm working on EndeveaourOS distro with VSCode. Any suggestion?
Thanks to Carson's answer that error message was removed but I'm getting a new error when running the program:
error[E0433]: failed to resolve: maybe a missing crate `leetcode`?
--> 021_Merge_Two_Sorted_Lists.rs:1:5
|
1 | use leetcode::utils::list_node::ListNode;
| ^^^^^^^^ maybe a missing crate `leetcode`?
|
= help: consider adding `extern crate leetcode` to use the `leetcode` crate
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0433`.
I tried adding this line extern crate leetcode
on program being run but I got this error:
error[E0463]: can't find crate for `leetcode`
--> 021_Merge_Two_Sorted_Lists.rs:1:1
|
1 | extern crate leetcode;
| ^^^^^^^^^^^^^^^^^^^^^^ can't find crate
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0463`.
The error message described in Edit 1 was caused by configuration of Code Runner extension. I changed this:
"rust": "cd $dir && rustc --out-dir bin $fileName && $dir/bin/$fileNameWithoutExt",
to this:
"rust": "cd $dir && cargo run --bin $fileNameWithoutExt --quiet",
Alternatively, I could use SHIFT+CTRL+B to run the local configuration defined in .vscode/tasks.json
.
Change use crate::utils::...
to use leetcode::utils::...
.
With your split lib
, [[bin]]
setup, you are defining three crates:
leetcode
as defined in lib.rs
001_Two_Sum
083_Remove_Duplicates_from_Sorted_List
When you use crate::
inside 001_Two_Sum
, crate
refers to 001_Two_Sum
, not leetcode
.