Rust中满地都是属性,对于这些,我们是需要有所了解,否则会感觉 到晕:

#[lang=“copy”] :表示Rust语言本身使用
#[lang =“drop”]
#[derive(Debug,Copy,Clone)]:自动实现这些trait
#![feature(never_type)] :标明是只能在nightly版本使用,此特性目前还是实验性质
#![feature(feature1)]:来源于rust函数,特性有实验、beta、稳定版本等不同阶段,如果打上这个标签,就是让尽管可能是非稳定版,编译器你帮我带上这个特性,开个绿灯,有我用到。
#![feature(async_await,await_macro,pin)]:标明是只能在nightly版本使用
#![feature(specialization)]:只能在nightly版本实现的特化规则
#[allow(dead_code)]: 对于没有用上的代码,不用提示warning
#![feature(try_trait)]:表明是实验特性
#![crate_type = “lib”] :说明一下,这个crate类型是lib
#![crate_name = “rary”]:说明一下,这个crate的名字为rary
#[fundamental]:告诉编译器不必遵守孤儿规则(Box,Fn,FnMut,FnOnce,Sized)
#[rustc_paren_sugar]:表示对括号语法特别解析:<F:Fn(i32,Vec,&str)->Option<&str>>,进行特别解析
#[must_use=“iterator adaptors are lazy…”]:提示开发者相关信息:比如该属性是惰性的
#[must_use]:对相关对象要进行处理,否则会报错,比如用在Result<T.E>上面,
#[macro_use]:导出相应模块中的宏;
#[macro_export]:表示宏定义的部分,对外面也是可见的。
#[fail(display=“IoErrorKind”)]:自动实现fail,并且所有的错误都需要实现Display
#[cause]:指定标准库中内置的基础错误类型
#[cfg(test)]:只有在执地cargo test时才编译下面的模块
#[cfg(target_arch =“x86”)]:条件编译属性,如果是x86架构的条件,则会进行编译
#[cfg(any(target_arch=“x86”,target_arch=“x86_64”))]:符合这两种的条件,则会进行编译
#[bench]:标注为基础测试,如函数
#[doc=“some desc”]
#[test]:测试函数
#[inline(always)]
#[no_mangle]:告诉编译器,兄弟,你别给我把签名整乱了,请保持原始签名。往往是在ffi时,需要与人交互,一一对应上。
#[repr©] :加上了这个标签的结构体,在内存中的布局和对齐就和 C 一样了,便于安全地传递给 C ABI。
#[link(name = “snappy”)] #[link(…)]属性用来指示链接器链接snappy库来解析符号。
#[cfg(feature =“pattern”)] : 表示当执行cargo build --feature "pattern“命令时,在cargo内部调用rust编译器rustc时,会传"pattern"标记,在输出中也会包含pattern模块。

#[foo]
struct Foo;mod bar {#![bar]
}

#[foo]作用于下一个项,在这就是struct声明。#![bar]作用于包含它的项,在这是mod声明。否则,它们是一样的。它们都以某种方式改变它们附加到的项的意义。

三类:container attribute、field attribute、variant attribute

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]  // <-- this is a container attribute
struct S {#[serde(default)]  // <-- this is a field attributef: i32,
}#[derive(Serialize, Deserialize)]
#[serde(rename = "e")]  // <-- this is also a container attribute
enum E {#[serde(rename = "a")]  // <-- this is a variant attributeA(String),
}

https://doc.rust-lang.org/1.24.1/reference/attributes.html

Attributes

Syntax
Attribute :InnerAttribute | OuterAttributeInnerAttribute :#![ MetaItem ]OuterAttribute :#[ MetaItem ]MetaItem :IDENTIFIER| IDENTIFIER = LITERAL| IDENTIFIER ( MetaSeq )| IDENTIFIER ( MetaSeq , )MetaSeq :EMPTY| MetaItem| MetaSeq , MetaItem

Any item declaration may have an attribute applied to it. Attributes in Rust are modeled on Attributes in ECMA-335, with the syntax coming from ECMA-334 (C#). An attribute is a general, free-form metadatum that is interpreted according to name, convention, and language and compiler version. Attributes may appear as any of:

A single identifier, the attribute name
An identifier followed by the equals sign '=' and a literal, providing a key/value pair
An identifier followed by a parenthesized list of sub-attribute arguments

Attributes with a bang ("!") after the hash ("#") apply to the item that the attribute is declared within. Attributes that do not have a bang after the hash apply to the item that follows the attribute.

An example of attributes:

// General metadata applied to the enclosing module or crate.
#![crate_type = "lib"]// A function marked as a unit test
#[test]
fn test_foo() {/* ... */
}// A conditionally-compiled module
#[cfg(target_os = "linux")]
mod bar {/* ... */
}// A lint attribute used to suppress a warning/error
#[allow(non_camel_case_types)]
type int8_t = i8;

Crate-only attributes

crate_name - specify the crate's crate name.
crate_type - see linkage.
no_builtins - disable optimizing certain code patterns to invocations of library functions that are assumed to exist
no_main - disable emitting the main symbol. Useful when some other object being linked to defines main.
no_start - disable linking to the native crate, which specifies the "start" language item.
no_std - disable linking to the std crate.
recursion_limit - Sets the maximum depth for potentially infinitely-recursive compile-time operations like auto-dereference or macro expansion. The default is #![recursion_limit="64"].
windows_subsystem - Indicates that when this crate is linked for a Windows target it will configure the resulting binary's subsystem via the linker. Valid values for this attribute are console and windows, corresponding to those two respective subsystems. More subsystems may be allowed in the future, and this attribute is ignored on non-Windows targets.

Module-only attributes

no_implicit_prelude - disable injecting use std::prelude:

Rust: 属性(attribute)的含义及文档大全相关推荐

  1. 连载三:Oracle升级文档大全

    (共33篇干货文章,建议收藏并在PC端打开) Oracle 11203->11204.6小版本物理升级方案 https://www.modb.pro/doc/190 OEM CC 12.1.0. ...

  2. 股票期货化数据文档大全覆盖国内6大易的历史数据和实时行情

    原 股票期货量化数据文档大全,覆盖国内6大交易所的历史数据和实时行情 一.基础数据 目前掘金支持上交所, 深交所的股票, 中金所, 上期所, 大商所, 郑商所的期货, 交易标的查询. 可使用get_i ...

  3. 股票期货量化数据文档大全覆盖国内6大交易的史数据和实时行情

    原 股票期货量化数据文档大全,覆盖国内6大交易所的历史数据和实时行情 一.基础数据 目前掘金支持上交所, 深交所的股票, 中金所, 上期所, 大商所, 郑商所的期货, 交易标的查询. 可使用get_i ...

  4. Rust语言圣经43 - 深度解读文档注释

    注释和文档 好的代码会说话,好的程序员不写注释,这些都是烂大街的"编程界俚语".但是,如果你真的遇到一个不写注释的项目或程序员,那一定会对它/他"刮目相看". ...

  5. 关于本地离线API文档大全-Zeal的下载以及使用

    目录 1.先进zeal官网进行下载对应的版本 2.进入点击edit -->preferents,在directory中设置存放文档的地址 3.进入下面的网址,按ctrl+f查找所需的语言后复制n ...

  6. 20个软件开发常用设计文档大全下载

    搜集了一些软件开发的常用文档,分享给大家 总下载地址: http://files.cnblogs.com/hsapphire/20%E4%B8%AA%E8%BD%AF%E4%BB%B6%E5%BC%8 ...

  7. 连载一:Oracle迁移文档大全

    (共33个干货文档,建议收藏并在PC端打开) Oracle 10g->11g XTTS迁移方案 https://www.modb.pro/doc/188 Oracle 9i至11g exp迁移  ...

  8. oracle dbua 升级,33篇Oracle升级文档大全(收藏版)

    Oracle 11203->11204.6小版本物理升级方案https://www.modb.pro/doc/190 OEM CC 12.1.0.2 -> 12.1.0.5 升级报告htt ...

  9. Zeal - 离线 API 文档大全

    之前学语言的时候需要下载API,很麻烦,一门一个API,后来学前端,乱七八糟的文档也是一堆,再后来听老师推荐了zeal,当然了今晚我捣鼓了一晚上. 提示一下,是英文的哦. 2021-9-26更新,我建 ...

  10. C语言中文文档、C语言文档大全(网址)

    中文文档 C语言标准库中文文档 英文文档 cppreference.com 注意:打开网页后滑最下面才是C 微软的C语言文档 C 文档 - 入门.教程.参考. | Microsoft Learn IE ...

最新文章

  1. python贴吧爬虫-Python 爬虫练习: 爬取百度贴吧中的图片
  2. 防sql注入 php代码,完美的php防sql注入代码
  3. Editplus PHP版
  4. 【译】10 years Blockchain. The Race is on: Blockchain vs. Tangle vs. Hashgraph
  5. NodeJS——模块全局安装路径配置以及关于supervisor的问题解释
  6. hadoop题目(一)
  7. 多台Linux服务器之间互相免密登陆
  8. 计算火车运行时间(pta)
  9. radiogroup多选_【RadioGroup】实现单选并获得所选项值 | 学步园
  10. ffmpeg把YUV转换为H264
  11. 【第158期】游戏策划:给@zhang的简历分析
  12. Xshell 外观配置
  13. Print Conductor中文版
  14. 企业信息化认知的四个误区
  15. profibus通讯快速入门_西门子S7-300/400PLC入门:硬件配置
  16. base64字符串转换为图片
  17. 通过 经纬度 获取 地理位置(Python、高德地图)
  18. ih5手机版怎么登录服务器未响应,ih5 与服务器链接教程
  19. Android apk 腾讯云-乐固的加固及签名
  20. MOBA类和“吃鸡”游戏为什么对网络延迟要求高?

热门文章

  1. 浏览器根对象window之caches
  2. DBA和开发同事的一些代沟(一)
  3. Python之迭代器(iterator)
  4. HttpClient 4.3学习笔记
  5. Hyper-V云解决方案-IT产业新变革
  6. ubuntu下切换到root用户
  7. 在Centos7上配置docker运行DotNetCore项目
  8. ubuntu 安装cuda 成功
  9. 创建.ignore文件
  10. 如何在开发时部署和运行前后端分离的JavaWeb项目