Using FnMut, Fn, FnOnce as arguments to a function

197 views Asked by At

I don't quite understand when/how a function can use bounds of types Fn, FnOnce, FnMut. Can someone explain in what scenario these would be used and how one would use them?

As I understand it, I can create a function like this:

struct Dog {
    name: String,
}

struct Cat {
    name: String,
}


fn foo(a: impl FnOnce(Dog) -> Cat) {
    // call a once
}

But why would I pass a function into another function? Is there some kind of advantage of doing so such as bypassing function visibility, etc.?

0

There are 0 answers