【发布时间】:2021-10-23 17:02:13
【问题描述】:
我有以下项目,我想将一些模块分成子文件夹。 当前的文件结构很简单:
src/
->main.rs
->cpu/
->cpu.rs
main.rs 只包含:
fn main() {
println!("Hello, world!");
let c : cpu::Cpu;
cpu::thing();
}
并且 cpu/cpu.rs 包含一个结构声明和一个测试函数:
pub struct Cpu {
memory : [u8; 16],
}
pub fn thing() -> u8 {
2 // whatever, it's a test
}
如何将 cpu.rs 公共函数和结构包含到 main.rs 中?
无论我尝试什么,如果我使用 use crate::cpu::{Cpu, thing};,我都会收到错误 (could not find `cpu` in the crate root),而使用 mod cpu; 似乎只能在同一个目录上工作。
在线搜索解决方案非常令人恼火,因为答案在 2015 年和 2018 年的 rust 版本之间混在一起,过于复杂或在同一个文件夹中完成。
【问题讨论】:
标签: rust