logoalt Hacker News

gridspyyesterday at 8:47 PM0 repliesview on HN

You know how to add logic on the outside of a function, by putting that function into a larger one and calling the function in the middle.

However, how do you inject logic INTO the middle of a function?

Say you have a function which can iterate over any list and given a condition do a filter. How do you inject the condition logic into that filter function?

In the C days you would use a function pointer for this. C++ introduced templating so you could do this regardless of type. Lambdas make the whole process more ergonomic, it's just declaring a one-shot function in place with some convenient syntax.

In rust instead of the full blown

fn filter_condition(val: ValType) -> bool { // logic }

I can declare a function in place with |val|{logic} - the lambda is just syntactic sugar to make your life easier.