[英]How can a submodule import a trait from the root in lib.rs?
I have a src/lib.rs
that contains:我有一个
src/lib.rs
包含:
pub trait Compile {
fn from_source(src: &src) {
parser::parse(src);
}
}
And a src/compiler/interpreter.rs
还有一个
src/compiler/interpreter.rs
use crate::Compile; // ERROR HERE - No Compile in the root
pub struct Interpreter;
impl Compile for Interpreter {}
I also have a src/compiler.rs
我还有一个
src/compiler.rs
pub mod interpreter;
I want to be able to use the Compile trait within my interpreter impl however I cannot seem to figure out how import the trait.我希望能够在我的解释器实现中使用 Compile 特性,但是我似乎无法弄清楚如何导入该特性。 Any thoughts?
有什么想法吗?
Its possible to do this in src/main.rs
by doing:可以通过以下方式在
src/main.rs
中执行此操作:
mod lib;
use lib::Compile;
The way I solved this issue was in src/main.rs
I used:我解决这个问题的方法是在我使用的
src/main.rs
中:
use {ROOT CRATE PACKAGE NAME}::Compile;
This must bring Compile
into the crate
scope so now I can just do: use crate::Compile
inside my compiler
sub module.这必须将
Compile
放入crate
scope 中,所以现在我可以这样做:在我的compiler
子模块中use crate::Compile
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.