13. Attributes¶
Syntax
InnerAttributeOrDoc::=InnerAttribute|InnerBuiltinAttribute|InnerBlockDoc|InnerLineDocInnerAttribute::= #![AttributeContent]OuterAttributeOrDoc::=OuterAttribute|OuterBuiltinAttribute|OuterBlockDoc|OuterLineDocOuterAttribute::= #[AttributeContent]AttributeContent::=SimplePathAttributeInput?AttributeInput::= (TokenTree* ) | =ExpressionAttributeContentList::=AttributeContent(,AttributeContent)* ,?
Legality Rules
13:1 An attribute is a general, free-form metadatum that is interpreted based on its name, convention, language, and tool.
13:2 An inner attribute is an attribute that applies to an enclosing item.
13:3 An outer attribute is an attribute that applies to a subsequent item.
13:4 An attribute content is a construct that provides the content of an attribute.
Examples
#[cfg(target_os = "linux")]
fn linux_only_function() {
#![allow(unused_variables)]
let unused = ();
}
13.1. Attribute Properties¶
Legality Rules
13.1:1 An active attribute is an attribute that is removed from the item it decorates.
13.1:2 An inert attribute is an attribute that remains with the item it decorates.
13.1:3 The following attributes are active attributes:
13.1:4 Attribute macros.
13.1:7
Attribute test is an inert attribute when compiling for testing
purposes, otherwise it is an active attribute.
13.1:8 All remaining attributes are inert attributes.
13.2. Built-in Attributes¶
Syntax
InnerBuiltinAttribute::= #![BuiltinAttributeContent]OuterBuiltinAttribute::= #[BuiltinAttributeContent]BuiltinAttributeContent::=AutomaticallyDerivedContent|CfgAttrContent|CfgContent|CollapseDebuginfoContent|ColdContent|CrateNameContent|CrateTypeContent|DeriveContent|DocContent|ExportNameContent|GlobalAllocatorContent|InlineContent|IgnoreContent|LinkContent|LinkNameContent|LinkSectionContent|LinkOrdinalContent|MacroExportContent|MacroUseContent|NakedContent|NoBuiltinsContent|NoImplicitPreludeContent|NoLinkContent|NoMainContent|NoMangleContent|NonExhaustiveContent|NoStdContent|PanicHandlerContent|PathContent|ProcMacroAttributeContent|ProcMacroContent|ProcMacroDeriveContent|RecursionLimitContent|ReprContent|ShouldPanicContent|TargetFeatureContent|TestContent|TrackCallerContent|TypeLengthLimitContent|UnsafeContent|UsedContent|WindowsSubsystemContent
Legality Rules
13.2:1 A built-in attribute is a language-defined attribute.
13.2:2 The following built-in attributes are code generation attributes:
13.2:6 Attribute
no_builtins.13.2:7 Attribute
target_feature.13.2:8 Attribute
track_caller.
13.2:9 The following built-in attributes are conditional compilation attributes:
13.2:12 The following built-in attributes are derivation attributes:
13.2:13 Attribute
automatically_derived.
13.2:15 The following built-in attributes are diagnostics attributes:
13.2:16 Attribute
allow.13.2:17 Attribute
deny.13.2:18 Attribute
deprecated.13.2:19 Attribute
expect.13.2:20 Attribute
forbid.13.2:21 Attribute
must_use.13.2:22 Attribute
unsafe.13.2:23 Attribute
warn.
13.2:24 The following built-in attributes are documentation attributes:
13.2:26 The following built-in attributes are foreign function interface attributes:
13.2:27 Attribute
crate_name.13.2:28 Attribute
crate_type.13.2:29 Attribute
export_name.13.2:32 Attribute
link_section.
13.2:38 The following built-in attributes are limits attributes:
13.2:39 Attribute
recursion_limit.13.2:40 Attribute
type_length_limit.
13.2:41 The following built-in attributes are macro attributes:
13.2:42 Attribute
macro_export.13.2:44 Attribute
proc_macro.13.2:45 Attribute
proc_macro_attribute.13.2:46 Attribute
proc_macro_derive.
13.2:47 The following built-in attributes are modules attributes:
13.2:49 The following built-in attributes are prelude attributes:
13.2:50 Attribute
no_implicit_prelude.
13.2:52 The following built-in attributes are runtime attributes:
13.2:53 Attribute
global_allocator.13.2:54 Attribute
panic_handler.13.2:55 Attribute
windows_subsystem.
13.2:56 The following built-in attributes are testing attributes:
13.2:60 The following built-in attributes are type attributes:
13.2:61 Attribute
non_exhaustive.
13.2.1. Code Generation Attributes¶
13.2.1.1. Attribute cold¶
Syntax
ColdContent ::=
cold
Legality Rules
13.2.1.1:1
Attribute cold shall apply to functions.
13.2.1.1:2
Attribute cold indicates that its related function is unlikely
to be called.
Examples
#[cold]
fn rarely_called_function () {}
13.2.1.2. Attribute inline¶
Syntax
InlineContent::= inlineInlineHint?InlineHint::= ( (always | never) )
Legality Rules
13.2.1.2:1
Attribute inline shall apply to closure expressions and
functions.
13.2.1.2:2
Attribute inline marks its related function as inlined.
The process of replacing a call expression to an inlined function
with the function body is referred to as inlining.
13.2.1.2:3
Attribute inline without an InlineHint suggests to a tool that
inlining should be performed.
13.2.1.2:4
Attribute inline with InlineHint always suggests to a tool
that inlining should always be performed.
13.2.1.2:5
Attribute inline with InlineHint never suggests to a tool
that inlining should never be performed.
13.2.1.2:6 A tool is not obliged to perform inlining.
Examples
#[inline]
fn suggests_inlining() {}
#[inline(always)]
fn requests_consistent_inlining() {}
#[inline(never)]
fn requests_suppressed_inlining() {}
13.2.1.3. Attribute no_builtins¶
Syntax
NoBuiltinsContent ::=
no_builtins
Legality Rules
13.2.1.3:1
Attribute no_builtins shall apply to the crate root module.
13.2.1.3:2
Attribute no_builtins prevents the tool from replacing certain code
patterns with calls to intrinsic functions.
Examples
#![no_builtins]
13.2.1.4. Attribute target_feature¶
Syntax
TargetFeatureContent::= target_feature ( enable = "FeatureList" )FeatureList::=Feature(,Feature)*
13.2.1.4:1
A Feature is any of the strings listed in Feature column in any of the tables in the sub-sections below.
Legality Rules
13.2.1.4:2
Safe functions that are annotated with target_features
can only be called without an unsafe block by a caller that is within a function
that enables all the target_features that the callee enables.
13.2.1.4:3
Attribute target_feature enables target architecture features for its
related function.
Undefined Behavior
13.2.1.4:4 It is undefined behavior to execute a program compiled with target architecture features that are not supported.
Examples
#[target_feature(enable="bmi1,sse4.1")]
fn requires_target_architecture_features () {}
13.2.1.4.1. Attribute target_feature: AArch64¶
13.2.1.4.1:1 |
Feature |
Implicitly enables |
Description |
13.2.1.4.1:2 |
aes |
neon |
FEAT_AES & FEAT_PMULL — Advanced <abbr title=”Single Instruction Multiple Data”>SIMD</abbr> AES & PMULL instructions |
13.2.1.4.1:3 |
bf16 |
FEAT_BF16 — BFloat16 instructions |
|
13.2.1.4.1:4 |
bti |
FEAT_BTI — Branch Target Identification |
|
13.2.1.4.1:5 |
crc |
FEAT_CRC — CRC32 checksum instructions |
|
13.2.1.4.1:6 |
dit |
FEAT_DIT — Data Independent Timing instructions |
|
13.2.1.4.1:7 |
dotprod |
neon |
FEAT_DotProd — Advanced SIMD Int8 dot product instructions |
13.2.1.4.1:8 |
dpb |
FEAT_DPB — Data cache clean to point of persistence |
|
13.2.1.4.1:9 |
dpb2 |
dpb |
FEAT_DPB2 — Data cache clean to point of deep persistence |
13.2.1.4.1:10 |
f32mm |
sve |
FEAT_F32MM — SVE single-precision FP matrix multiply instruction |
13.2.1.4.1:11 |
f64mm |
sve |
FEAT_F64MM — SVE double-precision FP matrix multiply instruction |
13.2.1.4.1:12 |
fcma |
neon |
FEAT_FCMA — Floating point complex number support |
13.2.1.4.1:13 |
fhm |
fp16 |
FEAT_FHM — Half-precision FP FMLAL instructions |
13.2.1.4.1:14 |
flagm |
FEAT_FLAGM — Conditional flag manipulation |
|
13.2.1.4.1:15 |
fp16 |
neon |
FEAT_FP16 — Half-precision FP data processing |
13.2.1.4.1:16 |
frintts |
FEAT_FRINTTS — Floating-point to int helper instructions |
|
13.2.1.4.1:17 |
i8mm |
FEAT_I8MM — Int8 Matrix Multiplication |
|
13.2.1.4.1:18 |
jsconv |
neon |
FEAT_JSCVT — JavaScript conversion instruction |
13.2.1.4.1:19 |
lor |
FEAT_LOR — Limited Ordering Regions extension |
|
13.2.1.4.1:20 |
lse |
FEAT_LSE — Large System Extensions |
|
13.2.1.4.1:21 |
mte |
FEAT_MTE & FEAT_MTE2 — Memory Tagging Extension |
|
13.2.1.4.1:22 |
neon |
FEAT_AdvSimd & FEAT_FP — Floating Point and Advanced SIMD extension |
|
13.2.1.4.1:23 |
paca |
FEAT_PAUTH — Pointer Authentication (address authentication) |
|
13.2.1.4.1:24 |
pacg |
FEAT_PAUTH — Pointer Authentication (generic authentication) |
|
13.2.1.4.1:25 |
pan |
FEAT_PAN — Privileged Access-Never extension |
|
13.2.1.4.1:26 |
pmuv3 |
FEAT_PMUv3 — Performance Monitors extension (v3) |
|
13.2.1.4.1:27 |
rand |
FEAT_RNG — Random Number Generator |
|
13.2.1.4.1:28 |
ras |
FEAT_RAS & FEAT_RASv1p1 — Reliability, Availability and Serviceability extension |
|
13.2.1.4.1:29 |
rcpc |
FEAT_LRCPC — Release consistent Processor Consistent |
|
13.2.1.4.1:30 |
rcpc2 |
rcpc |
FEAT_LRCPC2 — RcPc with immediate offsets |
13.2.1.4.1:31 |
rdm |
neon |
FEAT_RDM — Rounding Double Multiply accumulate |
13.2.1.4.1:32 |
sb |
FEAT_SB — Speculation Barrier |
|
13.2.1.4.1:33 |
sha2 |
neon |
FEAT_SHA1 & FEAT_SHA256 — Advanced SIMD SHA instructions |
13.2.1.4.1:34 |
sha3 |
sha2 |
FEAT_SHA512 & FEAT_SHA3 — Advanced SIMD SHA instructions |
13.2.1.4.1:35 |
sm4 |
neon |
FEAT_SM3 & FEAT_SM4 — Advanced SIMD SM3/4 instructions |
13.2.1.4.1:36 |
spe |
FEAT_SPE — Statistical Profiling Extension |
|
13.2.1.4.1:37 |
ssbs |
FEAT_SSBS & FEAT_SSBS2 — Speculative Store Bypass Safe |
|
13.2.1.4.1:38 |
sve |
neon, fp16 |
FEAT_SVE — Scalable Vector Extension |
13.2.1.4.1:39 |
sve2 |
sve |
FEAT_SVE2 — Scalable Vector Extension 2 |
13.2.1.4.1:40 |
sve2-aes |
sve2, aes |
FEAT_SVE_AES & FEAT_SVE_PMULL128 — SVE AES instructions |
13.2.1.4.1:41 |
sve2-bitperm |
sve2 |
FEAT_SVE2_BitPerm — SVE Bit Permute |
13.2.1.4.1:42 |
sve2-sha3 |
sve2, sha3 |
FEAT_SVE2_SHA3 — SVE SHA3 instructions |
13.2.1.4.1:43 |
sve2-sm4 |
sve2, sm4 |
FEAT_SVE2_SM4 — SVE SM4 instructions |
13.2.1.4.1:44 |
tme |
FEAT_TME — Transactional Memory Extension |
|
13.2.1.4.1:45 |
vh |
FEAT_VHE — Virtualization Host Extensions |
13.2.1.4.2. Attribute target_feature: RISC-V¶
13.2.1.4.2:1 |
Feature |
implicitly enables |
Description |
13.2.1.4.2:2 |
a |
zaamo, zalrsc |
A — Atomic instructions |
13.2.1.4.2:3 |
b |
zba, zbb, zbs |
B — Bit Manipulation instructions |
13.2.1.4.2:4 |
c |
zca |
C — Compressed instructions |
13.2.1.4.2:5 |
d |
f |
D — Double-Precision Floating-Point |
13.2.1.4.2:6 |
f |
zicsr |
F — Single-Precision Floating-Point |
13.2.1.4.2:7 |
m |
M — Integer Multiplication and Division instructions |
|
13.2.1.4.2:8 |
za64rs |
za128rs |
Za64rs — Platform Behavior: Naturally aligned Reservation sets with ≦ 64 Bytes |
13.2.1.4.2:9 |
za128rs |
Za128rs — Platform Behavior: Naturally aligned Reservation sets with ≦ 128 Bytes |
|
13.2.1.4.2:10 |
zaamo |
Zaamo — Atomic Memory Operation instructions |
|
13.2.1.4.2:11 |
zabha |
zaamo |
Zabha — Byte and Halfword Atomic Memory Operation instructions |
13.2.1.4.2:12 |
zacas |
zaamo |
Zacas — Atomic Compare-and-Swap (CAS) instructions |
13.2.1.4.2:13 |
zalrsc |
Zalrsc — Load-Reserved/Store-Conditional instructions |
|
13.2.1.4.2:14 |
zama16b |
Zama16b — Platform Behavior: Misaligned loads, stores, and AMOs to main memory regions that do not cross a naturally aligned 16-byte boundary are atomic |
|
13.2.1.4.2:15 |
zawrs |
Zawrs — Wait-on-Reservation-Set instructions |
|
13.2.1.4.2:16 |
zba |
Zba — Address Generation instructions |
|
13.2.1.4.2:17 |
zbb |
Zbb — Basic bit-manipulation |
|
13.2.1.4.2:18 |
zbc |
zbkc |
Zbc — Carry-less multiplication |
13.2.1.4.2:19 |
zbkb |
Zbkb — Bit Manipulation Instructions for Cryptography |
|
13.2.1.4.2:20 |
zbkc |
Zbkc — Carry-less multiplication for Cryptography |
|
13.2.1.4.2:21 |
zbkx |
Zbkx — Crossbar permutations |
|
13.2.1.4.2:22 |
zbs |
Zbs — Single-bit instructions |
|
13.2.1.4.2:23 |
zca |
Zca — Compressed instructions: integer part subset |
|
13.2.1.4.2:24 |
zcb |
zca |
Zcb — Simple Code-size Saving Compressed instructions |
13.2.1.4.2:25 |
zcmop |
zca |
Zcmop — Compressed May-Be-Operations |
13.2.1.4.2:26 |
zic64b |
Zic64b — Platform Behavior: Naturally aligned 64 byte Cache blocks |
|
13.2.1.4.2:27 |
zicbom |
Zicbom — Cache-Block Management instructions |
|
13.2.1.4.2:28 |
zicbop |
Zicbop — Cache-Block Prefetch Hint instructions |
|
13.2.1.4.2:29 |
zicboz |
Zicboz — Cache-Block Zero instruction |
|
13.2.1.4.2:30 |
ziccamoa |
Ziccamoa — Platform Behavior: Cacheable and Coherent Main memory supports all basic atomic operations |
|
13.2.1.4.2:31 |
ziccif |
Ziccif — Platform Behavior: Cacheable and Coherent Main memory supports instruction fetch and fetches of naturally aligned power-of-2 sizes up to min(ILEN,XLEN) are atomic |
|
13.2.1.4.2:32 |
zicclsm |
Zicclsm — Platform Behavior: Cacheable and Coherent Main memory supports misaligned load/store accesses |
|
13.2.1.4.2:33 |
ziccrse |
Ziccrse — Platform Behavior: Cacheable and Coherent Main memory guarantees eventual success on LR/SC sequences |
|
13.2.1.4.2:34 |
zicntr |
zicsr |
Zicntr — Base Counters and Timers |
13.2.1.4.2:35 |
zicond |
Zicond — Integer Conditional Operation instructions |
|
13.2.1.4.2:36 |
zicsr |
Zicsr — Control and Status Register (CSR) instructions |
|
13.2.1.4.2:37 |
zifencei |
Zifencei — Instruction-Fetch Fence instruction |
|
13.2.1.4.2:38 |
zihintntl |
Zihintntl — Non-Temporal Locality Hint instructions |
|
13.2.1.4.2:39 |
zihintpause |
Zihintpause — Pause Hint instruction |
|
13.2.1.4.2:40 |
zihpm |
zicsr |
Zihpm — Hardware Performance Counters |
13.2.1.4.2:41 |
zimop |
Zimop — May-Be-Operations |
|
13.2.1.4.2:42 |
zk |
zkn, zkr, zkt |
Zk — Scalar Cryptography |
13.2.1.4.2:43 |
zkn |
zknd, zkne, zknh, zbkb, zbkc, zbkx |
Zkn — NIST Algorithm suite extension |
13.2.1.4.2:44 |
zknd |
Zknd — NIST Suite: AES Decryption |
|
13.2.1.4.2:45 |
zkne |
Zkne — NIST Suite: AES Encryption |
|
13.2.1.4.2:46 |
zknh |
Zknh — NIST Suite: Hash Function Instructions |
|
13.2.1.4.2:47 |
zkr |
Zkr — Entropy Source Extension |
|
13.2.1.4.2:48 |
zks |
zksed, zksh, zbkb, zbkc, zkbx |
Zks — ShangMi Algorithm Suite |
13.2.1.4.2:49 |
zksed |
Zksed — ShangMi Suite: SM4 Block Cipher Instructions |
|
13.2.1.4.2:50 |
zksh |
Zksh — ShangMi Suite: SM3 Hash Function Instructions |
|
13.2.1.4.2:51 |
zkt |
Zkt — Data Independent Execution Latency Subset |
|
13.2.1.4.2:52 |
ztso |
Ztso — Total Store Ordering |
13.2.1.4.3. Attribute target_feature: x86¶
13.2.1.4.3:1 |
Feature |
implicitly enables |
Description |
13.2.1.4.3:2 |
adx |
Intel Multi-Precision Add-Cary Instruction Extensions |
|
13.2.1.4.3:3 |
aes |
sse2 |
Advanced Encryption Standard |
13.2.1.4.3:4 |
avx |
sse4.2 |
Advanced Vector Extensions |
13.2.1.4.3:5 |
avx2 |
avx |
Advanced Vector Extensions 2 |
13.2.1.4.3:6 |
avx512bf16 |
avx512bw |
Advanced Vector Extensions 512-bit - Bfloat16 Extensions |
13.2.1.4.3:7 |
vx512bitalg |
avx512bw |
Advanced Vector Extensions 512-bit - Bit Algorithms |
13.2.1.4.3:8 |
avx512bw |
avx512f |
Advanced Vector Extensions 512-bit - Byte and Word Instructions |
13.2.1.4.3:9 |
avx512cd |
avx512f |
Advanced Vector Extensions 512-bit - Conflict Detection Instructions |
13.2.1.4.3:10 |
avx512dq |
avx512f |
Advanced Vector Extensions 512-bit - Doubleword and Quadword Instructions |
13.2.1.4.3:11 |
avx512f |
avx2, fma, f16c |
Advanced Vector Extensions 512-bit - Foundation |
13.2.1.4.3:12 |
avx512fp16 |
avx512bw |
Advanced Vector Extensions 512-bit - Float16 Extensions |
13.2.1.4.3:13 |
avx512ifma |
avx512f |
Advanced Vector Extensions 512-bit - Integer Fused Multiply Add |
13.2.1.4.3:14 |
avx512vbmi |
avx512bw |
Advanced Vector Extensions 512-bit - Vector Byte Manipulation Instructions |
13.2.1.4.3:15 |
avx512vbmi2 |
avx512bw |
Advanced Vector Extensions 512-bit - Vector Byte Manipulation Instructions 2 |
13.2.1.4.3:16 |
avx512vl |
avx512f |
Advanced Vector Extensions 512-bit - Vector Length Extensions |
13.2.1.4.3:17 |
avx512vnni |
avx512f |
Advanced Vector Extensions 512-bit - Vector Neural Network Instructions |
13.2.1.4.3:18 |
avx512vp2intersect |
avx512f |
Advanced Vector Extensions 512-bit - Vector Pair Intersection to a Pair of Mask Registers |
13.2.1.4.3:19 |
avx512vpopcntdq |
avx512f |
Advanced Vector Extensions 512-bit - Vector Population Count Instruction |
13.2.1.4.3:20 |
avxifma |
avx2 |
Advanced Vector Extensions - Integer Fused Multiply Add |
13.2.1.4.3:21 |
avxneconvert |
avx2 |
Advanced Vector Extensions - No-Exception Floating-Point conversion Instructions |
13.2.1.4.3:22 |
avxvnni |
avx2 |
Advanced Vector Extensions - Vector Neural Network Instructions |
13.2.1.4.3:23 |
avxvnniint16 |
avx2 |
Advanced Vector Extensions - Vector Neural Network Instructions with 16-bit Integers |
13.2.1.4.3:24 |
avxvnniint8 |
avx2 |
Advanced Vector Extensions - Vector Neural Network Instructions with 8-bit Integers |
13.2.1.4.3:25 |
bmi1 |
Bit Manipulation Instruction Sets |
|
13.2.1.4.3:26 |
bmi2 |
Bit Manipulation Instruction Sets 2 |
|
13.2.1.4.3:27 |
fma |
avx |
Three-operand fused multiply-add |
13.2.1.4.3:28 |
fxsr |
Save and restore x87 FPU, MMX technology, and SSE state |
|
13.2.1.4.3:29 |
gfni |
sse2 |
Galois Field New Instructions |
13.2.1.4.3:30 |
kl |
sse2 |
Intel Key Locker Instructions |
13.2.1.4.3:31 |
lzcnt |
Leading zeros count |
|
13.2.1.4.3:32 |
pclmulqdq |
sse2 |
Packed carry-less multiplication quadword |
13.2.1.4.3:33 |
popcnt |
Count of bits set to 1 |
|
13.2.1.4.3:34 |
rdrand |
Read random number |
|
13.2.1.4.3:35 |
rdseed |
Read random seed |
|
13.2.1.4.3:36 |
sha |
sse2 |
Secure Hash Algorithm |
13.2.1.4.3:37 |
sha512 |
avx2 |
Secure Hash Algorithm with 512-bit digest |
13.2.1.4.3:38 |
sm3 |
avx |
ShangMi 3 Hash Algorithm |
13.2.1.4.3:39 |
sm4 |
avx2 |
ShangMi 4 Hash Algorithm |
13.2.1.4.3:40 |
sse |
Streaming SIMD Extensions |
|
13.2.1.4.3:41 |
sse2 |
sse |
Streaming SIMD Extensions 2 |
13.2.1.4.3:42 |
sse3 |
sse2 |
Streaming SIMD Extensions 3 |
13.2.1.4.3:43 |
sse4.1 |
sse3 |
Streaming SIMD Extensions 4.1 |
13.2.1.4.3:44 |
sse4.2 |
sse4.1 |
Streaming SIMD Extensions 4.2 |
13.2.1.4.3:45 |
sse4a |
sse3 |
Streaming SIMD Extensions 4a |
13.2.1.4.3:46 |
ssse3 |
sse3 |
Supplemental Streaming SIMD Extensions 3 |
13.2.1.4.3:47 |
tbm |
Trailing Bit Manipulation |
|
13.2.1.4.3:48 |
vaes |
avx2, aes |
Vector AES Instructions |
13.2.1.4.3:49 |
vpclmulqdq |
avx, pclmulqdq |
Vector Carry-less multiplication of Quadwords |
13.2.1.4.3:50 |
widekl |
kl |
Intel Wide Keylocker Instructions |
13.2.1.4.3:51 |
xsave |
Save processor extended status |
|
13.2.1.4.3:52 |
xsavec |
Save processor extended status with compaction |
|
13.2.1.4.3:53 |
xsaveopt |
Save processor extended states optimized |
|
13.2.1.4.3:54 |
xsaves |
Save processor extended states supervizor |
13.2.1.5. Attribute track_caller¶
Syntax
TrackCallerContent ::=
track_caller
Legality Rules
13.2.1.5:1
Attribute track_caller shall apply to non-main functions with
ABI “Rust”.
13.2.1.5:2
Attribute track_caller allows the function body of its
related function to obtain a core::panic::Location which indicates
the topmost untracked caller that ultimately led to the invocation of the
function.
13.2.1.5:3 A tool is not required to implement this indication in an effective manner.
13.2.1.5:4
When applied to an associated trait function, attribute
track_caller applies to all associated functions of the
implemented trait. If the associated trait function has a default
implementation, then the attribute applies to all overriding
functions.
Undefined Behavior
13.2.1.5:5
It is undefined behavior when attribute track_caller applies to an external block function but does not apply to all linked implementations.
13.2.1.5:6
It is undefined behavior when attribute track_caller is applied to an exported function but the external block function it links to is missing the attribute.
Examples
#[track_caller]
fn who_called_me () {}
13.2.1.6. Attribute naked¶
Syntax
NakedContent ::=
naked
Legality Rules
13.2.1.6:1
Attribute naked shall apply to functions.
13.2.1.6:2
Attribute naked ensures that a function prologue and epilogue are not generated.
13.2.1.6:3
Attribute naked function body must consist of exactly one
macro core::arch::naked_asm call.
13.2.1.6:4
Attribute naked cannot be used together with testing functions.
13.2.1.6:5
Attribute naked cannot be used together with attribute inline.
13.2.1.6:6
Attribute naked cannot be used together with attribute track_caller.
13.2.1.6:7
A naked function is a function subject to attribute naked.
13.2.1.6:8 A naked function that specifies a variadic part shall only use the ABIs permitted on a variadic external block function.
Undefined Behavior
13.2.1.6:9 It is a safety invariant for the function body to respect the ABI of the function.
13.2.1.6:10 It is a safety invariant for the function body to diverge.
13.2.2. Conditional Compilation Attributes¶
13.2.2.1. Attribute cfg¶
Syntax
CfgContent::= cfg (ConfigurationPredicate)ConfigurationPredicate::=ConfigurationOption|ConfigurationPredicateAll|ConfigurationPredicateAny|ConfigurationPredicateNot| true | falseConfigurationOption::=ConfigurationOptionNameConfigurationOptionValue?ConfigurationOptionName::=IdentifierConfigurationOptionValue::= =StringLiteralConfigurationPredicateAll::= all (ConfigurationPredicateList? )ConfigurationPredicateAny::= any (ConfigurationPredicateList? )ConfigurationPredicateNot::= not (ConfigurationPredicate)ConfigurationPredicateList::=ConfigurationPredicate(,ConfigurationPredicate)* ,?
Legality Rules
13.2.2.1:1
Attribute cfg enables conditional compilation.
13.2.2.1:2
A configuration predicate is a construct that evaluates statically
to either true or false, and controls conditional compilation.
13.2.2.1:3 An all configuration predicate is a configuration predicate that models existential quantifier ALL.
13.2.2.1:4
An all configuration predicate evaluates statically to true when either
all nested configuration predicates evaluate to true, or there are no nested
configuration predicates.
13.2.2.1:5 An any configuration predicate is a configuration predicate that models existential quantifier ANY.
13.2.2.1:6
An any configuration predicate evaluates statically to true when any
nested configuration predicate evaluates to true.
13.2.2.1:7 A not configuration predicate is a configuration predicate that negates the Boolean value of its nested configuration predicate.
13.2.2.1:8
A not configuration predicate evaluates statically to true when its
nested configuration predicate evaluates to false.
13.2.2.1:9
Configuration predicate true always evaluates statically to true.
13.2.2.1:10
Configuration predicate false always evaluates statically to false.
13.2.2.1:11 The evaluation of a configuration option is tool-defined.
Examples
#[cfg(all(unix, target_pointer_width = "32"))]
fn on_32bit_unix() {}
13.2.2.2. Attribute cfg_attr¶
Syntax
CfgAttrContent::= cfg_attr (ConfigurationPredicate,AttributeContentList)
Legality Rules
13.2.2.2:1
Attribute cfg_attr enables conditional compilation.
13.2.2.2:2
An attribute cfg_attr where the related
configuration predicate evaluates to true is replaced with a new
attribute for each AttributeContent enumerated in the
attribute's AttributeContentList.
Examples
#[cfg_attr(windows, path="windows.rs")]
mod os;
13.2.3. Derivation Attributes¶
13.2.3.1. Attribute automatically_derived¶
Syntax
AutomaticallyDerivedContent ::=
automatically_derived
Legality Rules
13.2.3.1:1
Attribute automatically_derived is automatically added to
implementations that are created by attribute derive for
built-in traits.
13.2.3.2. Attribute derive¶
Syntax
DeriveContent::= derive (SimplePathList? )
Legality Rules
13.2.3.2:1
Attribute derive shall apply to abstract data types.
13.2.3.2:2
Attribute derive lists derive macros for automatic
implementation by a tool.
Examples
#[derive(PartialEq)]
struct S<T> {
field: T
}
13.2.3.2:3
Attribute derive causes trait core::cmp::PartialEq to be
automatically implemented for struct S<T> as follows:
impl<T: core::cmp::PartialEq> core::cmp::PartialEq for S<T> {
fn eq(&self, other: &S<T>) -> bool {
self.field == other.field
}
fn ne(&self, other: &S<T>) -> bool {
self.field != other.field
}
}
13.2.4. Diagnostics Attributes¶
Note
The contents of this section are informational.
13.2.4:1 Diagnostic attributes are related to linting, and are not defined in this document.
13.2.5. Documentation Attributes¶
13.2.5.1. Attribute doc¶
Syntax
DocContent::= docDocInputDocInput::= =MacroInvocation| =StringLiteral| (TokenTree* )
Legality Rules
13.2.5.1:1
Attribute doc associates documentation with a construct.
Examples
#![doc = "This is a doc comment written as an attribute."]
13.2.6. Foreign Function Interface Attributes¶
13.2.6.1. Attribute crate_name¶
Syntax
CrateNameContent::= crate_name =StringLiteral
Legality Rules
13.2.6.1:1
Attribute crate_name shall apply to the crate root module.
13.2.6.1:2
Attribute crate_name shall specify the name of the related
crate.
Examples
#![crate_name = "factories"]
13.2.6.2. Attribute crate_type¶
Syntax
CrateTypeContent::= crate_type = "CrateType"CrateType::= bin | cdylib | dylib | lib | proc-macro | rlib | staticlib
Legality Rules
13.2.6.2:1
Attribute crate_type shall apply to the crate root module.
13.2.6.2:2
Attribute crate_type shall specify the linkage type of the
crate it appears in.
Examples
#![crate_type = "cdylib"]
13.2.6.3. Attribute export_name¶
Syntax
ExportNameContent::= export_name =StringLiteral
Legality Rules
13.2.6.3:1
Attribute export_name shall apply to functions and
statics.
13.2.6.3:2
Attribute export_name shall specify the exported symbol of the
related function or static.
Examples
#[export_name = "exported_symbol"]
pub fn rust_name() {}
13.2.6.4. Attribute link¶
Syntax
LinkContent::= link (LinkOption)LinkOption::=NativeLibraryName|NativeLibraryNameWithKind|WebAssemblyModuleNameNativeLibraryName::= name =StringLiteralNativeLibraryNameWithKind::=NativeLibraryName,NativeLibraryKindWebAssemblyModuleName::= wasm_import_module =StringLiteralNativeLibraryKind::= kind = "NativeLibraryKindType"NativeLibraryKindType::= dylib | raw-dylib | framework | static
Legality Rules
13.2.6.4:1
Attribute link shall apply to external blocks.
13.2.6.4:2
Attribute link shall specify the name of a native library that a tool
should link with.
13.2.6.4:3 The following native library kinds are available:
13.2.6.4:4 |
Native Library Kind |
Description |
13.2.6.4:5 |
dylib |
Dynamic library |
13.2.6.4:6 |
raw-dylib |
Dynamic library |
13.2.6.4:7 |
framework |
macOS framework |
13.2.6.4:8 |
static |
Static library |
13.2.6.4:9
When attribute link appears without a native library kind, its native
library kind defaults to dylib. Native library kind framework is only valid
on macOS targets.
13.2.6.4:10
If attribute link appears without a WebAssembly module name, then the
WebAssembly module name defaults to env.
13.2.6.4:11
When attribute link appears with raw-dylib as the native library
kind, the supplied name must include the file extension.
13.2.6.4:12
When attribute link appears with raw-dylib as the native library kind, the external block functions and external statics of the related external block shall not be subject to attribute no_mangle.
Examples
#[link(name = "CoreFoundation", kind = "framework")]
extern {}
13.2.6.5. Attribute link_name¶
Syntax
LinkNameContent::= link_name =StringLiteral
Legality Rules
13.2.6.5:1
Attribute link_name shall apply to external block functions and external statics.
13.2.6.5:2
Attribute link_name shall specify the linking symbol of the related external block function or external static.
13.2.6.5:3
Attribute link_name shall not be applied to external block functions and external statics that are annotated with Attribute link_ordinal.
Examples
extern {
#[link_name = "linking_symbol"]
pub fn rust_name() {}
}
13.2.6.6. Attribute link_ordinal¶
Syntax
LinkOrdinalContent::= link_ordinal (DecimalLiteral)
Legality Rules
13.2.6.6:1
Attribute link_ordinal shall apply to external block functions and external statics.
13.2.6.6:2
The related external block of the external block function or external static shall be subject to attribute link with raw-dylib as the native library kind.
13.2.6.6:3
Attribute link_ordinal shall specify the linking symbol of the related external block function or external static by ordinal number.
Examples
#[link(name = "lib.dll", kind = "raw-dylib")]
extern "system" {
#[link_ordinal(16)]
fn function();
}
13.2.6.7. Attribute link_section¶
Syntax
LinkSectionContent::= link_section =StringLiteral
Legality Rules
13.2.6.7:1
Attribute link_section shall apply to functions and
statics.
13.2.6.7:2
Attribute link_section specifies the object file section where the
symbol of the related function or static will be placed.
Examples
#[link_section = ".example_section"]
pub static THE_ANSWER: u32 = 42;
13.2.6.8. Attribute no_link¶
Syntax
NoLinkContent ::=
no_link
Legality Rules
13.2.6.8:1
Attribute no_link shall apply to crate imports.
13.2.6.8:2
Attribute no_link indicates that the imported external crate
will not be linked into the resulting binary or library.
Examples
#[no_link]
extern crate do_not_link;
13.2.6.9. Attribute no_main¶
Syntax
NoMainContent ::=
no_main
Legality Rules
13.2.6.9:1
Attribute no_main shall apply to the crate root module.
13.2.6.9:2
Attribute no_main indicates that the symbols of the
program entry point will not be present in a binary.
Examples
#![no_main]
13.2.6.10. Attribute no_mangle¶
Syntax
NoMangleContent ::=
no_mangle
Legality Rules
13.2.6.10:1
Attribute no_mangle has no effect on constructs other than
functions and statics.
13.2.6.10:2
Attribute no_mangle indicates that the name of the related
function or static will be used as the symbol for that function
or static.
13.2.6.10:3
Attribute no_mangle causes the related function or static to
be publicly exported from the produced library or object file.
13.2.6.10:4
An exported function is a function subject to attribute
no_mangle.
13.2.6.10:5
An exported static is a static subject to attribute
no_mangle.
Examples
#[no_mangle]
pub fn symbol_name() {}
13.2.6.11. Attribute repr¶
Syntax
ReprContent::= repr (RepresentationList)RepresentationList::=Representation(,Representation)* ,?Representation::=RepresentationKind|AlignmentRepresentationKind::=PrimitiveRepresentation| C | Rust | transparentAlignment::=AlignmentDecrease|AlignmentIncreasePrimitiveRepresentation::= i8 | i16 | i32 | i64 | i128 | isize | u8 | u16 | u32 | u64 | u128 | usizeAlignmentDecrease::= packedAlignmentValue?AlignmentIncrease::= alignAlignmentValueAlignmentValue::= (DecimalLiteral)
Legality Rules
13.2.6.11:1
Attribute repr shall apply to abstract data types.
13.2.6.11:2
Attribute repr shall indicate the type representation of the
related type.
Examples
#[repr(C, align(8))]
struct c_struct {
first_field: i16,
second_field: i8
}
13.2.6.12. Attribute unsafe¶
Syntax
UnsafeContent::= unsafe (BuiltinAttributeContent)
Legality Rules
13.2.6.12:1
Attribute unsafe is used to indicate that an attribute is unsafe.
13.2.6.12:2
Attribute unsafe shall apply to what the contained attribute applies to.
13.2.6.12:3
Attribute unsafe does not affect the behavior of the contained attribute.
13.2.6.12:4 The contained attribute shall be one of the following:
13.2.6.12:6 Attribute
export_name13.2.6.12:7 Attribute
link_section
Examples
#[unsafe(no_mangle)]
pub fn clear_name() {}
13.2.6.13. Attribute used¶
Syntax
UsedContent ::=
used
Legality Rules
13.2.6.13:1
Attribute used shall apply to statics.
13.2.6.13:2
Attribute used forces a tool to keep the related static in the
output object file even if the static is not used or referenced by other
items in the crate.
Examples
#[used]
pub static THE_ANSWER: u32 = 42;
13.2.7. Limits Attributes¶
13.2.7.1. Attribute recursion_limit¶
Syntax
RecursionLimitContent::= recursion_limit = "DecimalLiteral"
Legality Rules
13.2.7.1:1
Attribute recursion_limit shall apply to the crate root module.
13.2.7.1:2
Attribute recursion_limit sets the maximum depth of
macro expansion and auto-dereferencing.
Examples
#![recursion_limit = "42"]
13.2.7.2. Attribute type_length_limit¶
Syntax
TypeLengthLimitContent::= type_length_limit = "DecimalLiteral"
Legality Rules
13.2.7.2:1
Attribute type_length_limit shall apply to the
crate root module.
13.2.7.2:2
Attribute type_length_limit sets the maximum number of
generic substitutions for type parameters when constructing a
concrete type.
Examples
#![type_length_limit = "42"]
13.2.8. Macros Attributes¶
13.2.8.1. Attribute collapse_debuginfo¶
Syntax
CollapseDebuginfoContent::= collapse_debuginfo (CollapseDebuginfoKind)CollapseDebuginfoKind::= no | external | yes
Legality Rules
13.2.8.1:1
Attribute collapse_debuginfo shall apply to declarative macros.
13.2.8.1:2
Attribute collapse_debuginfo changes the debug location information
for the expanded code of the declarative macro to its invocation site. This
repeats recursively to the top most expansion of a declarative macro that
is not annotated with attribute collapse_debuginfo.
Examples
#[collapse_debuginfo(yes)]
macro_rules! m {
() => {};
}
13.2.8.2. Attribute macro_export¶
Syntax
MacroExportContent ::=
macro_export
Legality Rules
13.2.8.2:1
Attribute macro_export shall apply to declarative macros.
13.2.8.2:2
Attribute macro_export changes the visibility of the related
declarative macro to public visibility and introduces the name
of the declarative macro into the item scope of the
crate root module.
Examples
#[macro_export]
macro_rules! m {
() => {};
}
13.2.8.3. Attribute macro_use¶
Syntax
MacroUseContent::= macro_useImportedMacroList?ImportedMacroList::= (IdentifierList)
Legality Rules
13.2.8.3:1
Attribute macro_use shall apply to crate imports and
modules.
13.2.8.3:2
An ImportedMacroList enumerates macros-to-import. A macro-to-import shall
be subject to attribute macro_export.
13.2.8.3:3
When applied to a crate import, attribute macro_use
imports from the related crate either:
13.2.8.3:4 The enumerated macros-to-import, or
13.2.8.3:5 If no macros-to-import have been specified, all macros subject to attribute
macro_export.
13.2.8.3:6
When applied to a module, attribute macro_use extends the
textual macro scope of the related macro.
Examples
#[macro_use(first_macro, second_macro)]
extern crate macros;
#[macro_use]
mod module {}
13.2.8.4. Attribute proc_macro¶
Syntax
ProcMacroContent ::=
proc_macro
Legality Rules
13.2.8.4:1
Attribute proc_macro shall apply to public functions in the
crate root module of a proc-macro crate.
13.2.8.4:2
Attribute proc_macro turns the related function into a
function-like macro.
Examples
#[proc_macro]
pub fn make_answer_to_life(_items: TokenStream) -> TokenStream {
"fn answer_to_life() -> u32 { 42 }".parse().unwrap()
}
13.2.8.5. Attribute proc_macro_attribute¶
ProcMacroAttributeContent ::=
proc_macro_attribute
Legality Rules
13.2.8.5:1
Attribute proc_macro_attribute shall apply to public functions
in the crate root module of a proc-macro crate.
13.2.8.5:2
Attribute proc_macro_attribute turns the related function into
an attribute macro.
Examples
#[proc_macro_attribute]
pub fn output_and_return_item
(attr: TokenStream, item: TokenStream) -> TokenStream
{
println!("attr: \"{}\"", attr.to_string());
println!("item: \"{}\"", item.to_string());
item
}
13.2.8.6. Attribute proc_macro_derive¶
Syntax
ProcMacroDeriveContent::= proc_macro_derive (DeriveName(,HelperAttributeList)? )DeriveName::=NameHelperAttributeList::= attributes (IdentifierList)
Legality Rules
13.2.8.6:1
Attribute proc_macro_derive shall apply to public functions in
the crate root module of a proc-macro crate.
13.2.8.6:2
Attribute proc_macro_derive turns the related function into a
derive macro, where DeriveName defines the name of the
derive macro available to attribute derive.
13.2.8.6:3
The HelperAttributeList declares the derive helper attributes of
the derive macro.
Examples
#[proc_macro_derive(Answer, attributes(marker))]
pub fn derive_answer_to_life(_items: TokenStream) -> TokenStream {
"fn answer_to_life() -> u32 { 42 }".parse().unwrap()
}
#[derive(Answer)]
struct S {
#[marker] field: ()
}
13.2.9. Modules Attributes¶
13.2.9.1. Attribute path¶
Syntax
PathContent::= path =StringLiteral
Legality Rules
13.2.9.1:1
Attribute path shall apply to modules.
13.2.9.1:2
Attribute path specifies the module path of the respective
module as a string literal.
13.2.9.1:3 A tool is free to define the format of a module path.
Examples
#[path = "path/to/inline_module"]
mod inline_module {
#[path = "path/to/outline_module"]
mod outline_module;
}
13.2.10. Prelude Attributes¶
13.2.10.1. Attribute no_implicit_prelude¶
Syntax
NoImplicitPreludeContent ::=
no_implicit_prelude
Legality Rules
13.2.10.1:1
The inner attribute version of attribute no_implicit_prelude
shall apply to the crate root module or to modules.
13.2.10.1:2
The outer attribute version of attribute no_implicit_prelude
shall apply to modules.
13.2.10.1:3
Attribute no_implicit_prelude prevents the import of the
external prelude, the standard library prelude, and the tool
prelude.
Examples
#[no_implicit_prelude]
mod module {}
13.2.10.2. Attribute no_std¶
Syntax
NoStdContent ::=
no_std
Legality Rules
13.2.10.2:1
Attribute no_std shall apply to the crate root module.
13.2.10.2:2
Attribute no_std has the following effects:
13.2.10.2:3 Prevents the import of the standard library prelude.
13.2.10.2:4 Imports the core prelude.
13.2.10.2:5 Imports all declarative macros subject to attribute
macro_exportof thecorecrate into the macro_use prelude.
Examples
#![no_std]
13.2.11. Runtime Attributes¶
13.2.11.1. Attribute global_allocator¶
Syntax
GlobalAllocatorContent ::=
global_allocator
Legality Rules
13.2.11.1:1
Attribute global_allocator shall apply to statics whose
types implement the core::alloc::GlobalAlloc trait.
13.2.11.1:2
Attribute global_allocator sets the global allocator to the related
static.
Examples
#[global_allocator]
pub static THE_ANSWER: u32 = 42;
13.2.11.2. Attribute panic_handler¶
Syntax
PanicHandlerContent ::=
panic_handler
Legality Rules
13.2.11.2:1
Attribute panic_handler shall apply to functions with the
following restrictions:
13.2.11.2:2 It lacks function qualifiers,
13.2.11.2:3 Its ABI is Rust,
13.2.11.2:4 It lacks generic parameters,
13.2.11.2:5 It has a single function parameter whose type is
&core::panic::PanicInfo,13.2.11.2:6 It has the never type as its return type,
13.2.11.2:7 It lacks a where clause,
13.2.11.2:8 It has a function body.
13.2.11.2:9
Attribute panic_handler indicates that its related function
defines the behavior of panics.
13.2.11.2:10
A crate graph shall contain exactly one function subject to
attribute panic_handler.
Examples
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {}
13.2.11.3. Attribute windows_subsystem¶
Syntax
WindowsSubsystemContent::= windows_subsystem = "SubsystemKind"SubsystemKind::= console | windows
Legality Rules
13.2.11.3:1
Attribute windows_subsystem shall apply to the crate root module
of a binary crate.
13.2.11.3:2
Attribute windows_subsystem specifies the subsystem on Windows.
13.2.11.3:3
If attribute windows_subsystem is missing, the subsystem of the
related binary crate defaults to console.
Examples
#![windows_subsystem = "console"]
13.2.12. Testing Attributes¶
13.2.12.1. Attribute ignore¶
Syntax
IgnoreContent::= ignoreIgnoreReason?IgnoreReason::= =StringLiteral
Legality Rules
13.2.12.1:1
Attribute ignore shall apply to testing functions.
13.2.12.1:2
Attribute ignore prevents the execution of its related
testing function.
Examples
#[test]
#[ignore = "not implemented yet"]
fn unit_testing_function() {}
13.2.12.2. Attribute should_panic¶
Syntax
ShouldPanicContent::= should_panicExpectedPanicMessage?ExpectedPanicMessage::= ( expected =StringLiteral)
Legality Rules
13.2.12.2:1
Attribute should_panic shall apply to testing functions.
13.2.12.2:2
Attribute should_panic indicates that for the related
testing function to pass, it should panic.
13.2.12.2:3
If ExpectedPanicMessage is specified, then the related
testing function passes only when the panic message contains the
ExpectedPanicMessage.
Examples
#[test]
#[should_panic(expected = "did not get meaning of life")]
fn test_meaning_of_life() {
assert_eq!(meaning_of_life(), 42, "did not get meaning of life");
}
13.2.12.3. Attribute test¶
Syntax
TestContent ::=
test
Legality Rules
13.2.12.3:1
Attribute test shall apply to functions that:
13.2.12.3:2 Is not an async function, and
13.2.12.3:3 Is not an unsafe function, and
13.2.12.3:4 Lacks generic parameters, and
13.2.12.3:5 Lacks function parameters, and
13.2.12.3:6
Attribute test indicates that the respective function is a
testing function.
13.2.12.3:7 A testing function that returns the unit type passes when it terminates and does not panic.
13.2.12.3:8
A testing function that returns core::result::Result<(), E> passes when
it returns core::result::Result::Ok(()).
13.2.12.3:9 A testing function that does not terminate shall pass and fail according to the tool.
Examples
#[test]
fn test_the_thing() -> core::result::Result<()> {
let input = create_input()?;
do_the_thing(&input)?;
core::result::Result::Ok(());
}
13.2.13. Type Attributes¶
13.2.13.1. Attribute non_exhaustive¶
Syntax
NonExhaustiveContent ::=
non_exhaustive
Legality Rules
13.2.13.1:1
Attribute non_exhaustive shall apply to enum types,
enum variants, and struct types.
13.2.13.1:2
Attribute non_exhaustive indicates that the related
abstract data type or enum variant may have more fields or
enum variants added in the future. A type subject to attribute
non_exhaustive is referred to as a non-exhaustive type.
13.2.13.1:3 A non-exhaustive type shall not be constructed outside of its defining crate.
13.2.13.1:4
An enum variant subject to attribute non_exhaustive is referred
to as a non-exhaustive variant.
13.2.13.1:5 A non-exhaustive variant shall not be constructed outside of its defining crate.
13.2.13.1:6 Pattern matching a non-exhaustive variant shall require a struct pattern with a rest pattern outside its defining crate.
13.2.13.1:7 Pattern matching a non-exhaustive variant does not contribute towards the exhaustiveness of match arms.
Examples
#[non_exhaustive]
enum enum_with_future_variants {
Variant
}
enum enum_variants_with_future_fields {
#[non_exhaustive] Send { from: u32, to: u32 },
#[non_exhaustive] Quit
}
#[non_exhaustive]
struct struct_with_future_fields {
field: u32
}