Understanding closures
Closures are essentially functions; however, they have a few differences. It has to be noted that functions can be defined on the fly via || brackets as opposed to () brackets. A simple example of this is printing a parameter:
let test_function: fn(String) = |string_input: &str| {
println!("{}", string_input);
};
test_function("test");
What happens here is that we assign the test_function variable to our function that prints out the input. The type is an fn type. There are some advantages to this. Because it's a variable that is assigned, we can exploit scopes.
Normal functions defined by themselves are available through the file/module that they are imported or defined in. However, there will be times in web development where we want the availability of the function to be restricted to a certain lifetime. Shifting the closure into an inner scope can easily achieve this:
{
...