Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ability for Closure to Allow Argument But Never Use It #4077

Closed
therealgymmy opened this issue Nov 30, 2012 · 3 comments
Closed

Add Ability for Closure to Allow Argument But Never Use It #4077

therealgymmy opened this issue Nov 30, 2012 · 3 comments

Comments

@therealgymmy
Copy link
Contributor

I'm not sure how to phrase this properly, but basically I believe it'd be nice to see the following possible in rust.

A closure that takes in an argument, but never actually binds that argument to a name.

The reason being that in a loop, sometimes we don't actually need to know which iteration we are in. We just need to repeat a certain action for an arbitrary number of times.

For example,

// Imaginary syntax
each( [0, ..10], | :uint | {    // or maybe does not need |:uint| at all...
    doStuff(); // for 10 times

               // we don't actually need to use the element in the vector
               // Hence, really no need to assign the param a name,
               // and get a warning for not using it.
});

Essentially, argument wise, the closure will treat its argument the same way as the following C/C++ function.

int foo (uint32_t) {    // never use the param
   doStuff();
}

In C++, we could use the above function for something like this.

// Performance here is probably not nearly as good as hand-written loops,
// but that's general idea.
std::vector<uint32_t> num {1, 2, 3, 4, 5, 6};
std::for_each(num.begin(), num.end(), foo());

Alternatively, perhaps we could enhance the loop expression and allow it to take an argument to specify the number of iterations we want.

For example,

loop (until 10) {
    doStuff();
}
@catamorphism
Copy link
Contributor

You can write a closure with an argument it doesn't use, so that the compiler doesn't warn about it:

|_x| { ...whatever... }

Names beginning with an underscore are exempt from unused-variable warnings.

You can also write a closure with no arguments:

do repeat(5) || { ...whatever... };

where repeat could look like:

fn repeat(i: uint, action: fn()) {
     if i > 0 {
         action();
         repeat(i - 1, action);
     }
}

Is either of those what you're looking for?

@therealgymmy
Copy link
Contributor Author

Awesome!
I guess I need to dig into the language a bit more next time before making a request :-p

@catamorphism
Copy link
Contributor

No problem! It's always ok to ask questions like this on IRC, too (see https://github.com/mozilla/rust/wiki/Note-development-policy#Communication ).

flip1995 pushed a commit to flip1995/rust that referenced this issue Jan 9, 2025
flip1995 pushed a commit to flip1995/rust that referenced this issue Jan 9, 2025
…curisvely (rust-lang#13891)

fixes: rust-lang#4077

Continuation of rust-lang#11546. r? @y21 if you don't mind?

changelog: [`needless_continue`] lint if the last stmt in loop is
`continue` recurisvely
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants