【问题标题】:Are there any reserved identifiers (e.g. starting with underscore) in Rust apart from keywords?除了关键字之外,Rust 中是否有任何保留标识符(例如以下划线开头)?
【发布时间】:2017-09-03 21:44:44
【问题描述】:

在 C++ 中,certain identifiers starting with underscores are reserved to be used by the compiler or the standard library。 Rust 中的标识符有类似的规则吗?当然,关键字(如if)不允许作为标识符,但除此之外:我可以使用任何我想要的标识符吗?

【问题讨论】:

  • 好吧,至少以一个下划线开头似乎是未使用参数的常见做法。基于文本的宏的前奏和缺乏可能有助于减少保留标识符的数量。

标签: rust language-lawyer


【解决方案1】:

根据Rust Reference,标识符可以以下划线开头,除了长度(不仅仅是下划线)和关键字之外似乎没有限制:

标识符是以下形式的任何非空 Unicode(见注)字符串:

要么

或者

  • 第一个字符是_
  • 标识符多于一个字符,单独_不是标识符
  • 其余字符具有属性XID_continue

这不会出现在keywords 的集合中。

注意:标识符中的非 ASCII 字符目前是功能门控的。

XID_start 和 XID_continue 是 Unicode code pointsproperties;例如,数字(最值得注意的是)没有 XID_start 属性,因此不能作为标识符的第一个字符。

【讨论】:

    猜你喜欢
    • 2012-10-07
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多