Reputation: 88916
In C++, certain identifiers starting with underscores are reserved to be used by the compiler or the standard library. Are there similar rules for identifiers in Rust? Of course, keywords (like if
) are not allowed as an identifier, but apart from that: may I use any identifier I want?
Upvotes: 3
Views: 476
Reputation: 22273
According to the Rust Reference, identifiers may start with an underscore and there don't seem to be restrictions other than the length (not just an underscore) and keywords:
An identifier is any nonempty Unicode (see note) string of the following form:
Either
- The first character has property XID_start
- The remaining characters have property XID_continue
Or
- The first character is _
- The identifier is more than one character, _ alone is not an identifier
- The remaining characters have property XID_continue
that does not occur in the set of keywords.
note: Non-ASCII characters in identifiers are currently feature gated.
XID_start and XID_continue are properties of Unicode code points; digits, for example (and most notably), do not have the XID_start property and are thus not valid as first characters of identifiers.
Upvotes: 3