c - Rust 的 (void) 变量替代品

标签 c rust warnings unused-variables

正如标题所说,Rust 中是否有像 C/C++ 这样的 (void)variable 的替代方案。

我有一个回调函数,看起来像:

fn on_window_event(window: &cgl_rs::Window, event: &cgl_rs::Event) -> bool {
    // ... some code ...
    false
}

这里我没有使用window变量,所以我收到未使用的警告。

我知道我可以禁用该警告,但我想知道我们是否可以用其他好的方式来处理这个问题。

最佳答案

在 Rust 中,名称开头的下划线 (_) 用于将变量标记为未使用。

pub fn foo(a: i32, b: i32) -> i32 {
    b * 2
}
$ cargo check
warning: unused variable: `a`
 --> src\lib.rs:1:12
  |
1 | pub fn foo(a: i32, b: i32) -> i32 {
  |            ^ help: if this is intentional, prefix it with an underscore: `_a`
  |
  = note: `#[warn(unused_variables)]` on by default
pub fn foo(_a: i32, b: i32) -> i32 {
    b * 2
}
$ cargo check
- no warning -

关于c - Rust 的 (void) 变量替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76797683/

相关文章:

c - 宏的结果是什么?

rust - 为什么 no_std crate 可以依赖于使用 std 的 crate?

java - HttpClient 警告 : Cookie rejected: Illegal domain attribute

c++ - 读取给定路径中保存的 EVTX 文件

在多线程程序中关闭文件描述符

c - 为什么有时 linux 内核中枚举的第一个成员用 0 初始化

rust - 如何从 Rust 中的内存缓冲区执行原始指令?

data-structures - 在运行时使用反射枚举结构的字段

c++ - 如何在 GCC 中使用 pragma 指令禁用所有警告

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP