Understanding closures
Closures are, essentially, functions, but they are also anonymous, meaning that they do not have names. This means that closures can be passed around into functions and structs. However, before we delve into passing closures around, let us explore closures by defining a basic closure in a blank Rust program (you can use the Rust playground if you prefer) with the following code:
fn main() { let test_closure = |string_input| { println!("{}", string_input); }; test_closure("test"); }
Running the preceding code will give us the following printout:
test
In the preceding output, we can see that our closure behaves like a function. However, instead of using curly brackets to define the inputs, we use pipes.
You might have noticed in the preceding closure that we have not defined the data type for the string_input...