rust - 子模块如何从 lib.rs 的根目录导入特征?
问题描述
我有一个src/lib.rs
包含:
pub trait Compile {
fn from_source(src: &src) {
parser::parse(src);
}
}
还有一个src/compiler/interpreter.rs
use crate::Compile; // ERROR HERE - No Compile in the root
pub struct Interpreter;
impl Compile for Interpreter {}
我也有一个src/compiler.rs
pub mod interpreter;
我希望能够在我的解释器 impl 中使用 Compile 特征,但是我似乎无法弄清楚如何导入特征。有什么想法吗?
它可以src/main.rs
通过这样做来做到这一点:
mod lib;
use lib::Compile;
解决方案
我解决这个问题的方法是src/main.rs
我使用:
use {ROOT CRATE PACKAGE NAME}::Compile;
这必须Compile
纳入crate
范围,所以现在我可以这样做:use crate::Compile
在我的compiler
子模块中。
推荐阅读
- mysql - 将 mysl 结果存储在 txt 文件中时,由 '\n' 终止的行不起作用
- c++ - 如何让 c++ 中的 ioctl 只管理鼠标的 4 个按钮中的 2 个?
- azure-ad-b2c - 资源所有者密码凭证流是否与来自外部身份提供者的帐户一起使用
- cordova - Ionic 4 - ionic cordova android build 上的错误“源和目标不能相同”
- javascript - 获取变量的 JSON 问题
- javascript - 如何将 react-redux 项目导出为 node_module
- ruby - 在 s3 视频上传上触发 Lambda?
- avro - 空手道模拟是否支持 AVRO 对象测试?
- excel - 如何在VBA中删除带有条件的重复行
- javascript - 将所需数据转换为具有嵌套对象的对象,其中包含键和值