You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust项目跨文件导入ListNode结构体遇错及运行问题求助

Rust项目导入ListNode报错的解决方案

项目结构

./
├── 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"

问题描述

在083_Remove_Duplicates_from_Sorted_List.rs中使用ListNode时出现导入错误:

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`

尝试改用use crate::leetcode::utils::list_node::ListNode;后,出现新错误:

failed to resolve: could not find `leetcode` in the crate root
could not find `leetcode` in the crate rootrustc (Click for full compiler diagnostic)

编辑1:导入错误消除但运行报错

调整导入路径后,导入错误解决,但运行程序时出现新错误:

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`.

添加extern crate leetcode;后仍报错:

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`.

编辑2:最终解决方案

上述运行错误由VSCode的Code Runner扩展配置导致。将Code Runner的Rust运行配置从:

"rust": "cd $dir && rustc --out-dir bin $fileName && $dir/bin/$fileNameWithoutExt",

修改为:

"rust": "cd $dir && cargo run --bin $fileNameWithoutExt --quiet",

也可以使用SHIFT+CTRL+B运行.vscode/tasks.json中定义的本地任务配置。


内容的提问来源于stack exchange,提问作者Ουιλιαμ Αρκευα

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 16:49:53