我该如何“使用"?或导入本地Rust文件? [英] How do I "use" or import a local Rust file?

查看:612
本文介绍了我该如何“使用"?或导入本地Rust文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在main.rs中包含具有完整路径my_project/src/include_me.rs的文件?

How do I include a file with the full path my_project/src/include_me.rs in main.rs?

我已经检查了依赖性指南,它们似乎都包含二进制文件.我还检查了本书" ,但其中没有一个示例也以".rs"结尾.

I've checked the dependencies guide, and all of them appear to be including a binary. I've also checked "The Book", but none of the examples there end in ".rs" either.

我如何使include_me.rs与项目的其余部分一起编译?

How do I make include_me.rs compile with the rest of the project?

虽然可能的重复项"是包含此问题答案的好地方,但并不包含此问题.该问题的最高答案中的语法具有更大的相关性,并在重复"的问题中被掩盖了

while the "possible duplicate" would be a great place to include the answer to this question, it is not inclusive of it. The syntax in the top answer for this question is significantly more relevant, and glossed over in the question it "duplicates"

推荐答案

Rust基本上有两种(主要)方法可以包含其他地方的代码:

There are basically two (main) ways in Rust to include code from somewhere else:

如果include_me.rs属于您的项目,则应将其移至main.rs所在的同一文件夹中:

If your include_me.rs belongs to your project, you should move it to the same folder main.rs lies in:

└── src
    ├── include_me.rs
    └── main.rs

然后您可以将其写在main.rs中:

Then you can write this in your main.rs:

mod include_me;

fn main() {
    // Call a function defined in the other file (module)
    include_me::some_function();
}

mod声明使Rust编译器自动查找相应的.rs文件!

A mod declaration makes the Rust compiler look for the corresponding .rs files automatically!

因此,属于您项目的所有内容都与main.rs(或lib.rs)所在的文件夹位于同一文件夹(或其子文件夹)中.然后通过模块系统包含"文件.要阅读有关模块的详细介绍,请阅读 Rust书中的模块.模块系统非常重要,因此对于学习Rust十分重要.

So everything that belongs to your project, belongs in the same folder (or a subfolder thereof) as the folder where main.rs (or lib.rs) is lying. The files are then "included" via the module system. To read a good introduction into modules, please read the chapter on modules in the Rust book. The module system is pretty central and thus important to learning Rust.

如果您的include_me.rs不属于您的实际项目,而是您在多个项目中使用的有用东西的集合,则应将其视为.要包含此类外部库的代码,您必须将其作为外部包装箱包含在内.为了让您的生活更轻松,您真的想使用货运!

If your include_me.rs is something that doesn't belong to your actual project, but is rather a collection of useful things you are using in multiple projects, it should be seen as a library. To include code of such external libraries, you have to include it as an extern crate. And to make your life easier, you really want to use Cargo!

因此,让我们准备作为货物库项目的include_me.rs.您需要以下文件结构(由cargo new my_library --lib自动生成):

So let's prepare your include_me.rs as Cargo library project. You need the following file structure (which is automatically generated by cargo new my_library --lib):

. my_library
  ├── Cargo.toml
  └── src
      └── lib.rs

现在将所有内容从include_me.rs复制到lib.rs(按惯例,调用库项目lib.rs的根文件).假设my_library文件夹的完整路径为~/code/my_library.

Now copy all the contents from include_me.rs into lib.rs (it is just convention to call the root file of a library project lib.rs). Let's say that the my_library folder's full path is ~/code/my_library.

现在,让我们准备您的主项目的Cargo项目.它有一个类似的文件 结构(但是是main.rs而不是lib.rs,因为它是可执行项目而不是库项目):

Now let's prepare your main project's Cargo project. It has a similar file structure (but a main.rs instead of lib.rs, because it's a executable-project, not a library-project):

. my_project
├── Cargo.toml
└── src
    └── main.rs

要声明对my_library的依赖,必须将其放入Cargo.toml:

To declare your dependency on my_library, you have to put this into your Cargo.toml:

[package]
name = "my_project"
version = "0.1.0"
authors = ["you"]
edition = "2018"

[dependencies]
my_library = { path = "~/code/my_library" }

您也可以使用相对路径("../my_library"),但是只有当您知道两个项目相对于彼此始终位于它们所在的位置(例如,它们都在同一存储库中进行管理)时,这才有意义.

You can also use relative paths ("../my_library"), but it only makes sense if you know that the two projects always stay where they are, relative to one another (like if they are both managed in the same repository).

现在,您可以在您的main.rs中编写:

Now you can, in your main.rs, write:

use my_library::some_function();

fn main() {
    // Call a function defined in the other file (extern crate)
    some_function();
}

如果要上载这两个项目中的任何一个,则必须与crates.io(如果您的公司有一个,则与另一个板条箱注册表)进行交互,但这是另一个主题.

If you want to upload any of those two projects, you have to interact with crates.io (or another crates registry, if your company has one), but this is another topic.

(注意:前一段时间,有必要在main.rs内编写extern crate my_library;.这不再是必需的,但是您可能会发现带有extern crate声明的旧代码.)

(Note: some time ago, it was necessary to write extern crate my_library; inside main.rs. This is not necessary anymore, but you might find old cold with extern crate declarations.)

是的,但是您不应该使用那些. include!()可让您逐字包括其他文件,就像C-land的#include一样. 但是,强烈建议在模块系统能够解决您的问题的情况下使用此功能. include!()仅在非常特殊的情况下有用,通常与生成代码的更复杂的构建系统链接.

Yes, but you shouldn't use those. There is the include!() macro which allows you to verbatim include other files, just like the #include from C-land. However, it is strongly discouraged to use this in situations where the module system would be able to solve your problem. include!() is only useful in very special situations, often linked to a more complex build system which generates code.

这篇关于我该如何“使用"?或导入本地Rust文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆