(void)Rust 的变量替代品

问题描述 投票:0回答:1

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

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

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

这里我没有使用

window
变量,所以我收到了未使用的警告。

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

c rust warnings unused-variables
1个回答
0
投票

在 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 -
© www.soinside.com 2019 - 2024. All rights reserved.