E1001#
内联函数
This function is not used by any other part of your code, nor marked with pub
visibility.
Note that this warning might uncover other bugs in your code. For example, if there are two functions in your codebase that has similar name, you might just call the other function by mistake.
错误示例#
fn greeting() -> String {
"Hello!"
}
fn main {
fn local_greeting() -> String {
"Hello, world!"
}
}
建议#
有几种方式可以修复这个警告:
如果这个函数确实没有用,你可以移除函数的定义
如果这个函数是在顶层(即不是局部的),并且是你模块的公开 API 的一部分,你可以给函数添加
pub
关键字。pub fn greeting() -> String { "Hello!" }
如果你在函数名上犯了拼写错误,你可以在调用的地方将函数重命名为正确的名字。
在某些情况下,你可能想要保持函数为私有且同时不使用。在这种情况下,你可以对该函数调用 ignore()
以强制使用它。
fn greeting() -> String {
"Hello, world!"
}
fn init {
ignore(greeting)
}
fn main {
fn local_greeting() -> String {
"Hello, world!"
}
ignore(local_greeting)
}