Usage of fn (not Fn) in where clause

We have the function definition:

async fn func<Q, Fut, T>(q: Q, t: T) -> Result<()>
  where
    Q: fn(T, &str) -> Fut,
    Fut: Future<Output = Result<Option<T>, Error>> + Send + 'static,
    T: 'static + DeserializedOwned + Send + PartialEq

This is showing us an error that you cannot use fn in the Generic Parameter definition of the where clause for some reason, which doesn't make a whole lot of sense to me. Is there a reason for this?

We worked around it by removing Q and just defining the parameter type inline. Just asking why fn isn't allowed inside the where clause.

Because fn isn't a trait. Fn is the trait.

3 Likes

There’s a difference between traits and types. Q: … means “Q implements the trait …”. What you would want if you wanted to write the fn type into the where clause, is a way of writing “Q is the type …”. E.g. this could look like Q == fn(T, &str) -> Fut. However Rust doesn’t support such “equality constraints” between types (yet), and it also would seem somewhat redundant to introduce a new generic variable Q for this anyways, which is why you must write the fn(T, &str) -> Fut type inline in the argument list.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.