Rust警告从未使用的功能,但使用该功能
这是我的src
文件夹结构,
src/
|-- main.rs
|-- problems
| |-- mod.rs
| |-- p1.rs
| `-- p2.rs
`-- utilities.rs
我有我的utilities.rs
文件,其中包含我在p1.rs
和p2.rs中使用的2个函数
。在p1.rs
和p2.rs
中
#[path = "../utilities.rs"]
mod utilities;
,据我了解,我可以使用utilities.rs
utilities.rs < /代码>
Utilities.rs
在p2.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,永远不要在普通情况下使用
##[PATH]
属性,因为它可以使您犯此错误。理解Rust模块的关键是
mod
定义了模块及其内容。每个mod
项目与其他任何模块都是不同的模块。因此,如果您写了mod
两次,以使两个文件都参考同一文件,该文件被编译了两次,生成了该文件中定义的两个副本。为什么要获得dead_code
警告 - &nbsp;您已经定义了两个不同的功能,问题:: p1 :: p1 :: split()
and code>和问题: :p2 ::实用程序:: split()
,其中一个不使用。通常,在简单结构的项目中,对于项目中的任何模块源文件,应该只有一个
mod
item 。在这种情况下,自然的位置可以找到mod
项目在main.rs
中,应该是:然后,您可以在
中参考此模块p1.rs
或p2.rs
as:另一方面,也许
UTISITION
仅与问题
仅与模块相关联。在这种情况下,您将src/Qualise/utilities.rs
,mod ofities;
insrc/Questract/Quescors/mod.rs
,并p1
和p2
中的实用程序作为使用super :: utilities;
(相对路径)或使用板条箱::问题::实用程序;
(绝对路径)。通常,引入新模块的过程是:
mod my_module_name;
。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. Everymod
item is a different module from any others. Therefore, if you writemod
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 gettingdead_code
warnings — you've defined two different functions,problems::p1::utilities::split()
andproblems::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 thatmod
item is inmain.rs
, and it should be:Then, you can refer to this module in your
p1.rs
orp2.rs
as:On the other hand, maybe the
utilities
are more associated with theproblems
module only. In that case, you wouldsrc/problems/utilities.rs
,mod utilities;
insrc/problems/mod.rs
, andp1
andp2
as eitheruse super::utilities;
(relative path) oruse crate::problems::utilities;
(absolute path).In general, the procedure for introducing a new module is:
mod my_module_name;
in that parent module.rust-analyzer
in an IDE you can ask it to create the file.)mod
is; and useuse
if you want to make a convenient short alias for its path.您可以在
p1
和p2
中使用板条箱导入而不是路径修饰符(我无法证明,但可能是有罪的)。You could use a crate import instead of the path modifier (which I cannot prove, but is probably the guilty one), in both
p1
andp2
: