简体   繁体   English

Rust - 在父目录中使用模块

[英]Rust - Use module in a parent directory

I have a file structure looking somewhat like the following:我的文件结构类似于以下内容:

src/
--clients/
----queue_client/
------mod.rs
--data_evaluator/
----data_evaluator.rs

In data_evaluator, I want to use the queue_client module, but when I do mod queue_client in data_evaluator.rs - I get the following error - File not found for module queue_client .在 data_evaluator 中,我想使用queue_client模块,但是当我在 data_evaluator.rs 中执行mod queue_client data_evaluator.rs - 我收到以下错误 - File not found for module queue_client It only finds the module if I move it into the data_evaluator folder.如果我将它移动到data_evaluator文件夹中,它只会找到该模块。

My question is, how do I correctly use modules that are outside of the consumer code's directory?我的问题是,如何正确使用消费者代码目录之外的模块? Apologies if there is an easy way to do this, I did try searching for quite a while and couldn't find a way.抱歉,如果有一种简单的方法可以做到这一点,我确实尝试了很长时间,但找不到方法。

You seem to be a bit confused.你好像有点糊涂了。 In Rust, you build the module tree.在 Rust 中,构建模块树。 You use mod to register a module as a submodule of your current module.您使用mod将模块注册为当前模块的子模块。 You use use to use a module within your current module.您使用use来使用当前模块中的模块。 This article may clear some things up: http://www.sheshbabu.com/posts/rust-module-system/这篇文章可能会澄清一些事情: http://www.sheshbabu.com/posts/rust-module-system/

Aside from that, to use a module that's higher in the tree than your current module, you use crate to get to the root of your module tree.除此之外,要使用树中比当前模块更高的模块,您可以使用crate到达模块树的根目录。 So in your case, crate::clients::queue_client .所以在你的情况下, crate::clients::queue_client

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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