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

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

问题描述

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

我检查了 依赖指南,所有这些似乎都包括一个二进制文件.我还检查了 "Book",但其中没有一个示例以.rs"结尾.

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

虽然可能的重复"是包含此问题答案的好地方,但并不包含在内.这个问题的最佳答案中的语法更相关,并在它重复"的问题中掩盖了

解决方案

在 Rust 中基本上有两种(主要)方式来包含来自其他地方的代码:

1.包括"内部代码

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

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

然后你可以在你的main.rs中写这个:

mod include_me;fn 主(){//调用另一个文件(模块)中定义的函数include_me::some_function();}

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

因此,属于您的项目的所有内容都与 main.rs(或 lib.rs)所在的文件夹属于同一文件夹(或其子文件夹)说谎.然后这些文件被包括"在文件中.通过模块系统.要阅读对模块的良好介绍,请阅读 Rust 书中关于模块的章节.您还可以查看关于该主题的 Rust by Example.模块系统非常重要,因此对于学习 Rust 很重要.

2.包括"外部代码

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

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

.我的图书馆├── Cargo.toml└── src└── lib.rs

现在将include_me.rs中的所有内容复制到lib.rs中(调用一个库项目的根文件lib.rs只是约定俗成的)).假设 my_library 文件夹的完整路径是 ~/code/my_library.

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

.我的项目├── Cargo.toml└── src└── main.rs

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

[包]name = "my_project";版本=0.1.0";作者 = [你"]版本=2018"[依赖项]my_library = { path = "~/code/my_library";}

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

现在您可以在 main.rs 中写入:

使用 my_library::some_function();fn 主(){//调用另一个文件(extern crate)中定义的函数some_function();}

如果您想上传这两个项目中的任何一个,您必须与 crates.io(或另一个 crates 注册表,如果您的公司有)进行交互,但这是另一个主题.>

(注意:前段时间,需要在main.rs里面写extern crate my_library;.现在不需要了,但您可能会发现带有 extern crate 声明的旧代码.)

还有其他方法吗?

是的,但你不应该使用那些.有 include!() 允许您逐字包含其他文件,就像 C-land 中的 #include 一样.然而,强烈建议不要在模块系统能够解决您的问题的情况下使用它.include!() 仅在非常特殊的情况下才有用,通常与生成代码的更复杂的构建系统相关联.

How do I include a file with the full path my_project/src/include_me.rs in main.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.

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

EDIT: 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"

解决方案

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

1. "Including" internal code

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

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();
}

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

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. You could also check out Rust by Example on that topic. The module system is pretty central and thus important to learning Rust.

2. "Including" external code

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!

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

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.

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

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" }

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).

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();
}

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.

(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 code with extern crate declarations.)

Any other ways?

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天全站免登陆