Search code examples
rusttraitsmethod-signaturetype-signature

What are the possible operators for traits in a where clause in Rust?


I'm learning Rust and got to the chapter on trait bounds. In that chapter, they give an example with the + (plus) operator to enumerate all required traits in a where clause.

What other operators are allowed on traits in Rust? I searched but I wasn't able to find any documentation about this. Does Rust support 'or' notation, brackets, negation?


Solution

  • 'or' notation

    No. Such a thing doesn't make sense to me — what would the code do if something could be A OR B?

    brackets

    No, unless you count the parenthesis in Fn(...) and friends, the use of angle brackets as generics (Vec<T>) or the use of square brackets in slices ([u8]), but these are parts of types.

    Again, such a request doesn't make sense to me, why would you need any kind of grouping?

    negation

    No. I believe this is due to the fact that handling negative logic makes things a lot more complicated.

    other operators

    There's the ? (question mark) bound.


    Source: I've written a parser of Rust code. While it's possible I've missed something, it parses all the ~37500 Rust files I've thrown at it, so it's a pretty good fit.