2

As the title says is there an alternative for (void)variable like C/C++ in Rust.

I have a callback function which look something like:

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

Here I do not use the window variable, so I get the unused warning.

I know that I can disable the warning but I wanted to know whether we can deal with this in any other nice way.

Jaysmito Mukherjee
  • 1,467
  • 2
  • 10
  • 29
  • 1
    @DavidRanieri I already mentioned that I know I can disable the warning but I wanted to know if there was a different/better approach – Jaysmito Mukherjee Jul 30 '23 at 12:12
  • 1
    oops, I didn't pay attention to that part, sorry. In C++ there is also `[[maybe_unused]]`: https://stackoverflow.com/questions/39745817/stdignore-for-ignoring-unused-variable, but I don't see an equivalent for Rust. – David Ranieri Jul 30 '23 at 12:21
  • 1
    You need to name the variable `_window`. The underscore tells the compiler that it is intentionally unused. – Finomnis Jul 30 '23 at 12:23
  • @Finomnis thanks, its what i was looking for. but isnt there anything other than the naming convention? – Jaysmito Mukherjee Jul 30 '23 at 12:24
  • @JaysmitoMukherjee Not that I know of. Why do you look for something else? You could of course also manually call `drop(window)` at the beginning of the function, that should work too. But it isn't really what most people do, and I think it's important to follow conventions. And the underscore thing is what pretty much everyone does. – Finomnis Jul 30 '23 at 12:28
  • @Finomnis i was porting a c library, so some places(kinda rare) i have some _ variables and i wanted to keep a similar naming convention here too, so to avoid confusion – Jaysmito Mukherjee Jul 30 '23 at 12:29
  • @JaysmitoMukherjee Be aware that if you name variables `_xxx` you will no longer receive "unused variable" warnings for them, even if you do intend to use them. – Finomnis Jul 30 '23 at 12:48

1 Answers1

3

In Rust, an underscore (_) at the beginning of the name is used to mark variables as unused.

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 -
Finomnis
  • 18,094
  • 1
  • 20
  • 27
  • 4
    One more addition might me: If you have not benefit from an explicit name, the name can be completely replaced with `_` , IMO OP has no need, since the type points that it is a `Window` – Ömer Erden Jul 30 '23 at 12:32
  • 2
    @ÖmerErden Be aware that `_` and a variable with `_xxx` are **not** the same thing. `_` explicitely means "destroy right away", while `_xxx` means "destroy at the end of the scope". – Finomnis Jul 30 '23 at 12:49
  • 1
    @ÖmerErden See here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b5591f209e64809a9a324db6d4fa5ed5 – Finomnis Jul 30 '23 at 12:59
  • Yes it is an expected behavior since you can still use `_xxx` for the rest of the program(clippy doesn't like that though), but I am surprised that the same behavior doesn't apply if you declare it as a function parameter, example: https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=043cc2ab7b12cd69acb9c07fd9f26541 – Ömer Erden Jul 30 '23 at 13:14
  • @ÖmerErden Interesting! I didn't know that. – Finomnis Jul 30 '23 at 13:20
  • 1
    I guess they are not the same thing, `_` means that do not bind(doesn't always mean destroy right away). For your example it is a drop but for my case the object was already there, it just moved into the function scope, without binding to any identifier then it dropped at the end of the scope. – Ömer Erden Jul 30 '23 at 14:00