Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

(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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 -
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading