02-Derive

Derive — The Rust Reference

译文 · 基于 The Rust Reference

原文链接: https://doc.rust-lang.org/reference/attributes/derive.html

r[attributes.derive]

Derive

r[attributes.derive.intro] *derive [属性][attributes]*会调用一个或多个 derive 宏,从而为数据结构自动生成新的项。你可以通过过程宏创建 derive 宏。

r[attributes.derive.syntax] derive 属性使用 [MetaListPaths] 语法,指定要调用的 derive 宏路径列表。

r[attributes.derive.allowed-positions] derive 属性只能应用于[结构体][items.struct]、[枚举][items.enum]和[联合体][items.union]。

r[attributes.derive.duplicates] derive 属性可在同一项上使用任意次数。所有属性中列出的 derive 宏都会被调用。

r[attributes.derive.stdlib] derive 属性在标准库中导出为:

  • [core::derive]
  • [std::derive]
  • [core::prelude::v1::derive]
  • [std::prelude::v1::derive]

r[attributes.derive.built-in] 内置 derive 定义在[语言 prelude][names.preludes.lang] 中。内置 derive 列表如下:

  • [Clone]
  • [Copy]
  • [Debug]
  • [Default]
  • [Eq]
  • [Hash]
  • [Ord]
  • [PartialEq]
  • [PartialOrd]

r[attributes.derive.built-in-automatically_derived] 内置 derive 会在其生成的实现上包含 [automatically_derived 属性][attributes.derive.automatically_derived]。

r[attributes.derive.behavior] 在宏展开期间,对于 derive 列表中的每个元素,对应的 derive 宏会展开为零个或多个项。

r[attributes.derive.automatically_derived]

automatically_derived 属性

r[attributes.derive.automatically_derived.intro] *automatically_derived [属性][attributes]*用于标注某个实现,表明它是由 derive 宏自动创建的。它没有直接效果,但可供工具与诊断 lint 用来检测这些自动生成的实现。

r[attributes.derive.automatically_derived.syntax] automatically_derived 属性使用 [MetaWord] 语法。

r[attributes.derive.automatically_derived.allowed-positions] automatically_derived 属性只能应用于实现。

注意 rustc 会忽略其他位置上的使用,但会对其发出 lint。将来这可能变成错误。

r[attributes.derive.automatically_derived.duplicates] 在同一实现上多次使用 automatically_derived 与使用一次效果相同。

注意 rustc 会对第一次之后的使用发出 lint。

r[attributes.derive.automatically_derived.behavior] automatically_derived 属性没有行为。

最后修改 August 21, 2026: 更新 (76fc81a2e)