- Mastering Rust
- Rahul Sharma Vesa Kaihlavirta
- 283字
- 2021-07-02 13:35:24
Attributes
An attribute is an annotation on an item in Rust code. Items are top-level language constructs in a crate such as functions, modules, structs, enums, and constant declarations, and other things that are meant to be defined only at the crate root. Attributes are usually compiler built-ins, but can also be created by users through compiler plugins. They instruct the compiler to inject extra code or meaning for the item that appears below them, or for the module if they apply to a module. We'll cover more on these in Chapter 7, Advanced Concepts. For the sake of keeping things in scope, we will talk about two forms of attributes here:
- #[<name>]: This applies per item and usually appears above them in their definition. For example, test functions in Rust are annotated with the #[test] attribute. It signifies that the function is to be treated as part of the test harness.
- #![<name>]: This applies to the whole crate. Notice that it has an extra ! there. It usually goes at the very top of your crate root.
There are also other forms of attributes such as #[cfg(test)] that are used when writing tests within a module. This attribute is added on top of test modules to hint to the compiler to conditionally compile the module, but only when code is compiled in test mode. Attributes are not just limited to being used in testing code; they are widely used in Rust. We'll get to see more of them in upcoming chapters.
- Learning Microsoft Windows Server 2012 Dynamic Access Control
- Power Up Your PowToon Studio Project
- Python Game Programming By Example
- Learning Linux Binary Analysis
- AppInventor實踐教程:Android智能應(yīng)用開發(fā)前傳
- Learning Continuous Integration with TeamCity
- Python網(wǎng)絡(luò)爬蟲技術(shù)與應(yīng)用
- PHP程序設(shè)計高級教程
- Visual Basic程序設(shè)計
- HTML5+CSS3+JavaScript案例實戰(zhàn)
- 51單片機(jī)C語言程序設(shè)計經(jīng)典實例(第3版)
- Practical C Programming
- Python編程基礎(chǔ)與應(yīng)用
- Python程序設(shè)計基礎(chǔ)教程(慕課版)
- 響應(yīng)式編程實戰(zhàn):構(gòu)建彈性、可伸縮、事件驅(qū)動的分布式系統(tǒng)