Why do I use generic in the where clause, but the compiler still reports it not used?

Viewed 202

Code:

struct A<F, T, R> where F: FnOnce(T) -> R {
    foo: F,
}

Error:

error[E0392]: parameter `T` is never used
error[E0392]: parameter `R` is never used

Why do I use generic in the where clause, but the compiler still reports it not used?

1 Answers

This is a limit of the type system which currently checks the type looks directly used, and not only as a condition between constraints.

A solution is to use phantom types, which have no runtime cost:

use std::marker::PhantomData;

struct A<F, T, R> where F: FnOnce(T) -> R {
    phantom: PhantomData<(T, R)>,
    foo: F,
}
Related