Why drop() an usize?

I'm trying to understand why there's a drop(usize_arg_here) in this code:

I'm sure it makes sense somehow but I'm failing to understand it. Need a little help please.

Thank you!

The only reason that springs to mind is to avoid unused variable warnings. There are a few ways of doing so, and this is one of them.

5 Likes

Yeah, that's almost certainly to silence "unused variable" warnings.

3 Likes

I thought they would've used underscore prefix for that, it seems easier/more obvious. Unless this feature wasn't present since the very beginning of rust and this is code that's that old.

        fn _grow(_stack_size: usize, callback: &mut dyn FnMut()) {
            callback();
        }

What if it's like future-proofing the code, just in case that usize type arg changes, in the future, to some crate-made wrapper like MyUsize ?! which presumably could then impl. Drop.

So, I mean, it's an attempt to write correct code*, regardless of the type of the arg.

* presumably it be important to drop it before calling the callback() for some reason, rather than the default of after the callback(), before _grow returns.

That is another way of doing it, and I'm pretty sure it's been around since before 1.0 released. Personally, I like let _ = stack_size;. I don't like underscoring parameters, because that underscore then shows up in documentation. They all do more or less the same thing, so it's really just down to personal preference.

5 Likes

Often people will avoid that for parameter names that show in rustdoc.

Everywhere else I agree, the underscore is better.

3 Likes

Rust-analyzer with inline hints also looks better when parameter name you pass matches one function consumes.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.