3

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?

1
  • 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. Commented Aug 11, 2017 at 10:24

1 Answer 1

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

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.