Lukas Kalbertodt
Lukas Kalbertodt

Reputation: 88916

Are there any reserved identifiers (e.g. starting with underscore) in Rust apart from keywords?

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

Answers (1)

ljedrz
ljedrz

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

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

Related Questions