crustwarningsunused-variables

(void)variable alternative for Rust


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.


Solution

  • 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 -