How is the Index/Add/Deref/etc traits able to enable new syntax?


#1

Sorry if I am a bit dull, but what is happening to enable traits to relate to syntax, such as index notation? (v[0])

I assume that the same mechanism is enabling dereferencing via the unary * and addition the binary + operator.


#2

These traits are defined in the standard library, and have a magical #[lang] attribute applied: https://github.com/rust-lang/rust/blob/master/src/libcore/ops.rs#L243.

This attribute tells the compiler that this trait should use to desugar certain syntax into method calls.


#3

Wow, that was quick. Thanks!


#4

Just in case anyone is looking for an answer to “so, which lang attributes are currently supported?”, the Language Items section of the Rust Reference is probably a fairly good place to look.

At the moment, it doesn’t look like there’s anything definitive:

The set of language items is currently considered unstable. A complete list of the built-in language items will be added in the future.


#5

There is a project going on at

This includes filling out the reference. Help is definitely welcome.


#6

Lang items explained: http://manishearth.github.io/blog/2017/01/11/rust-tidbits-what-is-a-lang-item/