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?
Asked
Active
Viewed 425 times
3

Lukas Kalbertodt
- 79,749
- 26
- 255
- 305
-
Well, at least starting with one underscore seems common practice for unused arguments. The prelude and lack of text-based macros may help considerably in cutting down the number of reserved identifiers. – Matthieu M. Aug 11 '17 at 10:24
1 Answers
3
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.

ljedrz
- 20,316
- 4
- 69
- 97