- 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.
- Hands-On Machine Learning with scikit:learn and Scientific Python Toolkits
- Android Jetpack開發(fā):原理解析與應(yīng)用實(shí)戰(zhàn)
- Java系統(tǒng)分析與架構(gòu)設(shè)計(jì)
- Visual Basic 6.0程序設(shè)計(jì)計(jì)算機(jī)組裝與維修
- Spring Boot+Spring Cloud+Vue+Element項(xiàng)目實(shí)戰(zhàn):手把手教你開發(fā)權(quán)限管理系統(tǒng)
- Apache Kafka Quick Start Guide
- SQL Server與JSP動(dòng)態(tài)網(wǎng)站開發(fā)
- C#實(shí)踐教程(第2版)
- Selenium Testing Tools Cookbook(Second Edition)
- Learning Material Design
- Vue.js應(yīng)用測試
- JavaScript Unit Testing
- Building Microservices with Go
- Java Web 從入門到項(xiàng)目實(shí)踐(超值版)
- 鋁合金陽極氧化與表面處理技術(shù)(第三版)