Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fields with reserved names #84

Closed
dbrgn opened this issue Jun 16, 2017 · 5 comments
Closed

Fields with reserved names #84

dbrgn opened this issue Jun 16, 2017 · 5 comments
Labels

Comments

@dbrgn
Copy link

dbrgn commented Jun 16, 2017

If I do serialization of the JSON object {"type": "foo", "count": 3}, the struct would have to look like that to match the field names:

struct Obj {
    type: String,
    count: u32,
}

The thing is that type is a reserved keyword, so it can't be used.

Of course I can use the Serde rename feature. But is there a convention on how to call the field instead, e.g. trailing slash (type_), leading slash (_type) or modification (typ or tipe)?

@joshtriplett
Copy link
Member

Leading underscore has a specific meaning in Rust, namely that it suppresses warnings about something going unused. So, I'd suggest trailing underscore.

@nrc
Copy link
Member

nrc commented Jun 17, 2017

Personally I prefer a trailing underscore. The convention in the compiler (which faces this issue a lot) is to modify the keyword (typ, krate are widely used).

We've shied away from naming conventions here, we prefer to concentrate on purely formatting issues. This does seem like a reasonable thing to cover in the naming section of the style guide though.

@gbutler69
Copy link

C# allows you to use, @reserved instead of reserved if you want to use a reserved word as an identifier. Isn't $ a valid character for identifiers in rust? How about $reserved or $$reserved for code generated identifiers that would otherwise use reserved words?

@nrc
Copy link
Member

nrc commented Sep 18, 2017

Isn't $ a valid character for identifiers in rust?

I don't think it is - $ is used to identify macro variables.

@mqudsi
Copy link

mqudsi commented Nov 19, 2017

Just to point out that a trailing underscore is also autocomplete-friendly for users expecting to find a function/member with a name that coincides with a reserved keyword.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants