添加Excel功能后Rust模块引入报错E0433的问题求助
Rust模块引入错误排查与解决
问题
添加Excel功能后,出现两个模块引入错误:找不到crate根下的common,以及未声明的HspiceCompiler类型。
相关代码
use crate::common::offic::excel; // <<-- use clap::Parser; use jni::objects::*; use jni::JNIEnv; use std::path::Path; use HspiceCompiler::hspice::spice; // <<-- #[derive(Parser, Debug)] #[command(author, version, about)] pub struct Args { #[clap(help = "Hspice file name")] pub file_name: String, #[clap(short, long)] pub output_path: String, } fn main() { let args = Args::parse(); let spice_file = Path::new(&args.file_name); let output_path = Path::new(&args.output_path); loading(args); spice_file.try_exists().expect("Can't access hspice file"); let mut reader = spice::Reader::new(); let data_iter = reader.read(spice_file); reader.analysis_iter(data_iter); excel::write_to_excel(data_iter, output_path); } fn loading(args: Args) { ... }
错误信息
error[E0433]: failed to resolve: could not find `common` in the crate root --> src/bin/run.rs:1:12 | 1 | use crate::common::offic::excel; | ^^^^^^ could not find `common` in the crate root error[E0433]: failed to resolve: use of undeclared type `HspiceCompiler` --> src/bin/run.rs:5:5 | 5 | use HspiceCompiler::common::offic::excel; | ^^^^^^^^^^^^^^ use of undeclared type `HspiceCompiler`
解决方案
1. 区分二进制与库的模块引用逻辑
src/bin/run.rs属于项目的二进制目标文件,crate关键字在这里指向该二进制 crate,而非项目根库(src/lib.rs)。要引用根库中的模块,必须使用项目库的名称(即HspiceCompiler)。
2. 导出根库模块
在src/lib.rs中添加模块导出声明,确保外部能访问到所需模块:
pub mod common; pub mod hspice;
3. 修正use语句
将run.rs中第一行的模块引用改为:
use HspiceCompiler::common::offic::excel;
4. 确认Cargo.toml配置
确保项目同时包含库和二进制目标的配置,示例如下:
[package] name = "HspiceCompiler" version = "0.1.0" edition = "2021" [[bin]] name = "run" path = "src/bin/run.rs"
完成以上步骤后,模块引用错误即可解决。
内容的提问来源于stack exchange,提问作者StudioMenghan
相关产品推荐
相关产品推荐

