简体   繁体   English

为什么 rust 抱怨未使用的 function 仅用于测试?

[英]Why is rust complaining about an unused function when it is only used from tests?

When a function is only called from tests rust complains that it is never used.当仅从测试中调用 function 时,rust 抱怨它从未被使用过。 Why does this happen and how to fix this?为什么会发生这种情况以及如何解决这个问题?

Example:例子:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=52d8368dc5f30cf6e16184fcbdc372dc https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=52d8368dc5f30cf6e16184fcbdc372dc

fn greet() {
    println!("Hello!")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_greet() {
        greet();
    }
}

I get the following compiler warning:我收到以下编译器警告:

   Compiling playground v0.0.1 (/playground)
warning: function is never used: `greet`
 --> src/lib.rs:1:4
  |
1 | fn greet() {
  |    ^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

In rust fn is private by default.在 rust fn 默认是私有的。 greet() is not accessible outside your module. greet() 在你的模块之外是不可访问的。 If greet() is not used inside it except in tests, then rust is correctly flagging it as dead code.如果 greet() 除了测试之外没有在其中使用,那么 rust 会正确地将其标记为死代码。

If greet() is supposed to be part of your public interface mark it as pub:如果 greet() 应该是你的公共接口的一部分,将其标记为 pub:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8a8c50b97fe3f1eb72a01a6252e9bfe6 https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8a8c50b97fe3f1eb72a01a6252e9bfe6

pub fn greet() {
    println!("Hello!")
}

If greet() is a helper intended to be only used in tests move it inside mod tests:如果 greet() 是一个仅用于测试的帮助程序,请将其移至 mod 测试中:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3dc51a36b4d5403ca655dec0210e4098 https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3dc51a36b4d5403ca655dec0210e4098

#[cfg(test)]
mod tests {
    fn greet() {
        println!("Hello!")
    }
    
    #[test]
    fn test_greet() {
        greet();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rust 投诉scope后借用 - Rust complaining about borrowing after scope function 使用了`print`,但为什么它警告我`unused`? - function `print` is used but why it warn me `unused`? Rust:为什么将未使用的 (_) 参数传递给 function 会改变结果 - Rust: Why passing unused (_) parameter to function changes the outcome Rust 的借用检查器在这里真正抱怨的是什么? - What is Rust's borrow checker really complaining about here? 为什么 Rust 中的 Cell 只能用于 Copy 而不是 Clone 类型? - Why can Cell in Rust only be used for Copy and not Clone types? 当一个函数返回分配了相同生命周期的引用时,Rust Borrow Checker仅抱怨多次借用是可变的 - Rust Borrow checker only complains about borrowing as mutable multiple times when a function that returns a reference with the same lifetime assigned 为什么 Rust 说我的变量未使用? - Why is Rust saying my variable is unused? Rust会优化掉未使用的function arguments吗? - Will Rust optimize away unused function arguments? 如果生命周期未被使用,为什么在引用类型上实现特征时需要生命周期,在Rust <1.31? - Why is a lifetime needed when implementing a trait on a reference type if the lifetime is otherwise unused, in Rust < 1.31? Rust 编译器抱怨在代码不存在时在循环中使用移动的值 - Rust compiler complaining of using moved value in loop when code is not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM