`where` in `type` is unavailable. Whether it decision or error?


pub trait Target {}

pub type Action<T> = Box<FnOnce(&mut T)>
    where T: Target;


src\lib.rs:4:5: 4:10 error: expected one of `!`, `+`, `::`, or `;`, found `where` 
src\lib.rs:4     where T: Target;

I wonder is it correct or bug?

Rust currently doesn’t enforce trait bounds in type declarations. If you wrote that pub type Action<T: Target>, you’d get this warning, which explains it: https://doc.rust-lang.org/error-index.html#E0122

As far as I know this feature could be added some day, but right now it doesn’t exist.

But since it doesn’t do anything, there’s no reason to allow where clauses on type declarations right now.

I know that it emits a warning. It should allow where like in <>. I don’t see any reasons why it is forbidden.

1 Like

Its excluded because it doesn’t do anything and it was easy to exclude (<T: Trait> should probably have been excluded to, but it would have been a more complicated change to the grammar than excluding where clauses is). If/when this feature is added, it should be included, but I don’t see why rustc should maintain a grammatical form that is at best pointless and at worst very misleading.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.