Reputation: 99
I have read that there is no way how to pass all outer local parameters to a nested function, but maybe there are some hacks to do it anyway? How can I avoid passing a lot of parameters into this function, for example:
let var1 = 5;
let var2 = 12.2;
let var3 = bar();
let var4 = tar() * var1;
// etc ... a lot of variables ...
fn foo() {
// want to have var1, var2, var3, var4 ...
}
Upvotes: 0
Views: 584
Reputation: 382160
What you want is called a closure:
fn main() {
let var1 = 5;
let var2 = 12.2;
let foo = || {
var1 as f64 + var2
};
println!("foo(): {}", foo()); // prints "foo(): 17.2"
}
Upvotes: 4