Keyword identifier collisions, eg `type`


#1

What do people do when they have a keyword collision? Eg, I have a struct that I’d like to give a type field:

pub enum Type {
    Battery,
    Mains,
}

pub struct PowerSupply {
    type: Type,
}

I can do a syntactic change like type_, or I can add more words like power_supply_type. The first is ugly, the second verbose and redundant. What do people do in this sort of situation?


#2

Sometimes a synonym is ok – perhaps kind instead of type.


#3

Rustc often abbreviates ‘type’ to ‘ty’.


#4

Or suffix it with an underscore, as in:

pub struct PowerSupply {
    type_: Type,
}

#5

ty for type and krate for crate.

The problem with this is that kind is already a term of art when talking about types…


#6

But what do I do if i need this for FFI? How do I call a C function void type(void)?


#7
extern {
    #[link_name = "type"]
    fn type_();
}