Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Syntax
Item
    OuterAttribute* ( VisItem | MacroItem )

VisItem
    Visibility?
    (
        Module
      | ExternCrate
      | UseDeclaration
      | Function
      | TypeAlias
      | Struct
      | Enumeration
      | Union
      | ConstantItem
      | StaticItem
      | Trait
      | Implementation
      | ExternBlock
    )

MacroItem
      MacroInvocationSemi
    | MacroRulesDefinition

(Item)是 crate 的组成部分。项通过嵌套的模块集合在 crate 内组织。每个 crate 都有一个“最外层“的匿名模块;crate 内的所有其他项在 crate 的模块树中都有路径

项在编译时完全确定,通常在执行期间保持固定,并且可能驻留在只读内存中。

有几种类型的项:

项可以在crate 的根模块块表达式中声明。

项的一个子集,称为关联项,可以在 trait实现中声明。

项的一个子集,称为外部项,可以在 extern中声明。

项可以按任何顺序定义,但 macro_rules 除外,它有自己的作用域行为。

项名称的名称解析允许项在模块或块中引用项的位置之前或之后定义。

有关项的作用域规则信息,请参阅项作用域