3 ms·
> it's currently impossible in stable Rust to accept a closure that itself accepts a closure as an argument You can make it work this way, if the second closur
by oconnor663 8y ago
> it's currently impossible in stable Rust to accept a closure that itself accepts a closure as an argument
You can make it work this way, if the second closure doesn't actually close over anything:
let apply_twice = |f: fn(u64) -> u64, x| f(f(x));
let plus_3 = |x| x + 3;
let x = apply_twice(plus_3, 1);
Or this way, even with closed-over variables, using dynamic dispatch:
let apply_twice = |f: &dyn Fn(u64) -> u64, x| f(f(x));
let plus_3 = |x| x + 3;
let x = apply_twice(&plus_3, 1);
I don't know if it's possible to take a generic closure by value, though.
> the use of assert_eq!(expected, actual), as I've mostly seen the other order
That's interesting, in my head (expected, actual) is the usual order. For example, https://godoc.org/github.com/stretchr/testify/require#Equal https://godoc.org/github.com/stretchr/testify/require#Equal.