Hey all,
New to Rust, trying to understand what's the idiomatic way to do this.
fn main() {
let mut counter = 0;
let mut inc = || { counter += 1; };
inc();
counter += 2;
inc();
}
Obviously this doesn't compile, and I understand why, but there isn't anything inherently unsafe about it that I can think of. In fact, if you were to inline the closure code, it would compile. And you obviously can write this in pretty much any other language and expect it to work.
Aside: The underlying reason seems to be that Rust always implements closures as a struct. It could do things differently. For example, here, the compiler could recognize this closure never leaves the function and inline the closure code, or it could pass all the local references as parameters to the closure code on every invocation. If it did so, the code above would satisfy the borrow checker. Of course this approach would mean invoking a closure could fail borrow checks -- the current design means creating the closure may fail borrow checks, but invoking it never does.
My question is:
Is there an elegant, idiomatic way to achieve this?
Essentially, avoid repetition in a function by creating a small local helper function. Assume counter
is 4 local variables, the logic in inc
is 3-4 lines and it needs to be called several times inside a single function. I would ideally not want to pass 4 mutable references as parameters every time I invoke inc
, because that replaces one form of repetition with another.
Thank you!