Attributes

Rust的Attribute

参考资料: Attribute

1
2
3
4
5
6
7
8
9
10
11
12
13
Syntax
InnerAttribute :
# ! [ Attr ]

OuterAttribute :
# [ Attr ]

Attr :
SimplePath AttrInput?

AttrInput :
DelimTokenTree
| = Expression
  • Inner attributes, written with a bang (!) after the hash (#), apply to the item that the attribute is declared within.
  • Outer attributes, written without the bang after the hash, apply to the thing that follows the attribute.

Attributes能够分类为以下几类:

1. Testing Attributes

参考资料: https://doc.rust-lang.org/reference/attributes/testing.html

The test attribute marks a function to be executed as a test.

Derive

Derive作用主要有两点:

  1. use the MetaListPaths to specify a list of traits to implement
  2. or paths to derive macro to process

简而言之, derive的作用一个是指定trait(可以认为是Java中接口的近亲), 另一个作用是指定derive macro宏.


Attributes
https://www.torch-fan.site/2022/07/20/Attribute/
作者
Torch-Fan
发布于
2022年7月20日
更新于
2022年11月15日
许可协议