Search code examples
functiongenericsrustwhere-clauseconst-generics

Define const generics in the where clause


Is there any way to define a const generic in the where clause, currently it doesn't seem to work. If there isn't, are there any plans of adding it in the future?

My Failed Attempts

fn foo<N>()
where
    const N:usize,
{}

fn foo<const N>()
where
    N:usize  
{}

Solution

  • That's not possible. As described in the reference a const generic always follows the pattern

    const IDENTIFIER : Type ( = Block | IDENTIFIER | -?LITERAL )?

    Seing as they are a completly different kind of generic from both lifetime and type parameters and the : Type isn't a bound like with them it's unlikely to ever be supported within where clauses as they are for bounds.