小编wil*_*lit的帖子

在 Rust 中使用父目录中的模块

是否有可能以这种方式构建 Rust 项目?

目录结构:

src
??? a
?   ??? bin1.rs
??? b
?   ??? bin2.rs
??? common
    ??? mod.rs
Run Code Online (Sandbox Code Playgroud)

来自 Cargo.toml:

[[bin]]
name = "bin1"
path = "src/a/bin1.rs"

[[bin]]
name = "bin2"
path = "src/b/bin2.rs"
Run Code Online (Sandbox Code Playgroud)

我希望能够commonbin1.rs和 中使用该模块bin2.rs。可以通过在导入之前添加 path 属性来实现:

#[path="../common/mod.rs"]
mod code;
Run Code Online (Sandbox Code Playgroud)

有没有一种方式bin1.rs,并bin2.rs在使用common,而不必硬编码路径?

module rust

2
推荐指数
1
解决办法
870
查看次数

标签 统计

module ×1

rust ×1