简体   繁体   English

如何在 src 中布局我的 Rust 模块以便在我的应用程序中访问

[英]how to layout my Rust modules in src for access across my application

I have created a package structure but the compiler says it can't find my module.我创建了一个包结构,但编译器说它找不到我的模块。 I am new to rust, with a background in Java & C#我是 Rust 新手,有 Java 和 C# 背景

I have tried using self and super prefixes but can't get the code to compile我尝试使用 self 和 super 前缀,但无法编译代码

I have the following structure:我有以下结构:

src
|_lib.rs
|_common
| |_mod.rs
| |_service.rs
|
|_animals
  |_mod.rs
  |_domestic
  | |_mod.rs
  | |_dog.rs
  |_wild
  |_mod.rs

here are the simplified files:这是简化的文件:

dog.rs狗.rs

pub struct Dog {
  ...
}

impl Dog {
    ...
}

domestic > mod.rs国内 > mod.rs

pub mod dog;

animals > mod.rs动物 > mod.rs

pub mod domestic;
pub mod wild;

src > lib.rs src > lib.rs

pub mod common;
pub mod animals;

common > service普通 > 服务

use animals::domestic::dog;

From what I have read (and possibly misunderstood) in the Rust book, this should work.从我在 Rust 书中读到(并且可能被误解)的内容来看,这应该可行。

But the compiler throws the following error:但是编译器抛出以下错误:

could not find `animals` in `{{root}}`

Have I set this out in a 'Rust' way?我是否以“Rust”的方式提出了这一点? and what do I need to change to get it to compile.我需要改变什么才能编译它。

Thank you谢谢

In commons > service commons > service

use crate::animals::domestic::dog;

The crate keyword tells the compiler to start from the root of the package; crate关键字告诉编译器从包的根目录开始; ie what follows is an absolute path.即接下来是绝对路径。

Alternatively, you can go with relative paths and use the super keyword to go one level up.或者,您可以使用相对路径并使用super关键字上一级。

See this Rust book entry for details有关详细信息,请参阅此 Rust 书籍条目

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

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