Rust警告从未使用的功能,但使用该功能

发布于 2025-02-02 12:33:14 字数 1233 浏览 1 评论 0原文

这是我的src文件夹结构,

src/
|-- main.rs
|-- problems
|   |-- mod.rs
|   |-- p1.rs
|   `-- p2.rs
`-- utilities.rs

我有我的utilities.rs文件,其中包含我在p1.rsp2.rs中使用的2个函数。在p1.rsp2.rs

#[path = "../utilities.rs"]
mod utilities;

,据我了解,我可以使用utilities.rs utilities.rs < /代码> Utilities.rsp2.rs中定义的两个函数

pub fn get_lines(num: &str) -> Vec<String> {...}
pub fn split(s: &String, separator: &str) -> Vec<String> {...}

我都在使用它们,但是在p1.rs中,我仅使用get_lines。这会导致货物警告我“从未使用过”功能,当我运行或构建时,它是split。但是使用该功能,不仅在从未被调用的功能中使用,因此我不明白为什么货物警告我。看来我必须在每个文件中使用一个自制模块中的每个模块函数,因为如果我呼叫 split in p1.rs,则警告消失了。我不明白什么?

更清楚地,问题在于,当i 货物运行时,我会得到以下内容

warning: function is never used: `split`
  --> src/problems/../utilities.rs:23:8
   |
23 | pub fn split(s: &String, separator: &str) -> Vec<String> {
   |        ^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

this is my src folder structure

src/
|-- main.rs
|-- problems
|   |-- mod.rs
|   |-- p1.rs
|   `-- p2.rs
`-- utilities.rs

I have my utilities.rs file containing 2 functions I use in p1.rs and p2.rs. In both p1.rs and p2.rs are the lines

#[path = "../utilities.rs"]
mod utilities;

that, as far as I understand, allow me to use the functions defined in the utilities.rs
The two functions defined in utilities.rs are

pub fn get_lines(num: &str) -> Vec<String> {...}
pub fn split(s: &String, separator: &str) -> Vec<String> {...}

In p2.rs I use them both, but in p1.rs I only use get_lines. This causes cargo to warn me of a "never used" function, it being split, when I run or build. But the function is used, and not only in functions that are never called, so I don't understand why cargo is warning me. It would seem I have to use every single module function in every single file where I include a self-made module, because if I do call split in p1.rs then the warning disappears. What do I not understand?

To be clearer, the problem is that when I cargo run I get the following

warning: function is never used: `split`
  --> src/problems/../utilities.rs:23:8
   |
23 | pub fn split(s: &String, separator: &str) -> Vec<String> {
   |        ^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给我一枪 2025-02-09 12:33:14

通常,永远不要在普通情况下使用##[PATH]属性,因为它可以使您犯此错误。

理解Rust模块的关键是mod 定义了模块及其内容。每个mod项目与其他任何模块都是不同的模块。因此,如果您写了mod两次,以使两个文件都参考同一文件,该文件被编译了两次,生成了该文件中定义的两个副本。为什么要获得dead_code警告 - &nbsp;您已经定义了两个不同的功能,问题:: p1 :: p1 :: split() and code>和问题: :p2 ::实用程序:: split(),其中一个不使用。

通常,在简单结构的项目中,对于项目中的任何模块源文件,应该只有一个mod item 。在这种情况下,自然的位置可以找到mod项目在main.rs中,应该是:

mod utilities;

然后,您可以在中参考此模块p1.rsp2.rs as:

use crate::utilities;

另一方面,也许UTISITION仅与问题仅与模块相关联。在这种情况下,您将

  • 文件放置在src/Qualise/utilities.rs
  • mod ofities; in src/Questract/Quescors/mod.rs,并
  • 参考p1p2中的实用程序作为使用super :: utilities;(相对路径)或使用板条箱::问题::实用程序;(绝对路径)。

通常,引入新模块的过程是:

  • 选择一个模块作为模块的父模块。 (不要在目录和文件中思考,在模块中思考。)
  • 在该父模块中写mod my_module_name;
  • 鉴于此,将文件放在需要的位置。 如果您此时尝试编译程序,则编译器会告诉您缺少什么文件;或者如果您在IDE中使用Rust-Analyzer,则可以要求它创建文件。)
  • ( 您要使用使用使用,请根据其mod iss;用适当的绝对路径或相对路径参考它;并使用使用如果要为其路径制作便捷的简短别名。

You should generally never use the #[path] attribute in ordinary situations, because it lets you make this mistake.

The key to understanding Rust modules is that mod defines a module and its contents. Every mod item is a different module from any others. Therefore, if you write mod twice in such a way that both refer to the same file, that file gets compiled twice, producing two copies of what's defined in that file. That's why you're getting dead_code warnings — you've defined two different functions, problems::p1::utilities::split() and problems::p2::utilities::split(), and one of them isn't used.

As a general rule, in projects with simple structure, there should be only one mod item for any module source file in your project. In this case, the natural place to locate that mod item is in main.rs, and it should be:

mod utilities;

Then, you can refer to this module in your p1.rs or p2.rs as:

use crate::utilities;

On the other hand, maybe the utilities are more associated with the problems module only. In that case, you would

  • place the file at src/problems/utilities.rs,
  • write mod utilities; in src/problems/mod.rs, and
  • refer to the utilities in p1 and p2 as either use super::utilities; (relative path) or use crate::problems::utilities; (absolute path).

In general, the procedure for introducing a new module is:

  • Pick a module to be the parent module of your module. (Don't think in directories and files, think in modules.)
  • Write mod my_module_name; in that parent module.
  • Put the file where it needs to be, given that. (If you try to compile the program at this point, the compiler will tell you what file is missing; or if you use rust-analyzer in an IDE you can ask it to create the file.)
  • Everywhere you want to use that module, refer to it by the appropriate absolute or relative path according to where its mod is; and use use if you want to make a convenient short alias for its path.
清晰传感 2025-02-09 12:33:14

您可以在p1p2中使用板条箱导入而不是路径修饰符(我无法证明,但可能是有罪的)。

use crate::utilities;

You could use a crate import instead of the path modifier (which I cannot prove, but is probably the guilty one), in both p1 and p2:

use crate::utilities;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文