简体   繁体   English

子模块如何从 lib.rs 中的根导入特征?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM