General function as argument
⚓ Rust 📅 2026-01-23 👤 surdeus 👁️ 1Is it possible to have a trait that gets a general function (for example, a function with two arguments and a return value, where both arguments and the return value are references), logs that the function was called, and runs the function, returning it's output.
I.e., to be able to do, something like, for example,
f.log_and_run(&arg1, &arg2)
Or maybe
log_and_run(f)(&arg1, &arg2)
(Dealing with general arity is not that hard since you can use markers. The hard part is that I don't know how to specify "all functions regardless of lifetime relationships")
P.s., gpt said that it's impossible since "there is no support for higher kinded types". Regardless, I wanted to ask to double-check, and also to learn whether there are workarounds (in particular, I tried to think of a way to do that even at the cost of exponential blow-up of markers, (since it wont be that bad for functions with few arguments) but I got stuck since it's not clear to me how to specify all the possible lifetime bounds of the return argument in a way where all the definitions never overlap).
Thanks in advance
1 post - 1 participant
🏷️ Rust_feed