《通过例子学Rust》第13章 属性

属性是应用于某些模块、crate 或项的元数据(metadata)。这元数据可以用来:

当属性作用于整个 crate 时,它们的语法为 #![crate_attribute],当它们用于模块或项时,语法为 #[item_attribute](注意少了感叹号 !)。

属性可以接受参数,有不同的语法形式:

  • #[attribute = "value"]
  • #[attribute(key = "value")]
  • #[attribute(value)]

属性可以多个值,它们可以分开到多行中:

复制代码
#[attribute(value, value2)]

#[attribute(value, value2, value3,
            value4, value5)]

13.1 死代码 dead_code

编译器提供了 dead_code(死代码,无效代码)lint,这会对未使用的函数产生警告。可以用一个属性来禁用这个 lint。

复制代码
// 13.1节 死代码dead_code
fn used_function() {
    println!("call used_function()");
}

// `#[allow(dead_code)]` 属性可以禁用 `dead_code` lint
#[allow(dead_code)]
fn unused_functin() {}

#[allow(dead_code)]
fn noisy_unused_function() {}
// 改正 ^ 增加一个属性来消除警告

fn main() {
    used_function();

    println!("Hello Rust"); 
}

// rustc main.rs  
// ./main

编译运行:

复制代码
PS F:\rustproject\rustbyexample\chapter13\example13_1> rustc main.rs
PS F:\rustproject\rustbyexample\chapter13\example13_1> ./main
call used_function()
Hello Rust
PS F:\rustproject\rustbyexample\chapter13\example13_1>

注意在实际程序中,需要将死代码清除掉。由于本书的例子是交互性的,因而其中需要允许一些死代码的出现。

13.2 crate

crate_type 属性可以告知编译器 crate 是一个二进制的可执行文件还是一个库(甚至是哪种类型的库),crate_name 属性可以设定 crate 的名称。

不过,一定要注意在使用 cargo 时,这两种类型时都没有 作用。由于大多数 Rust 工程都使用 cargo,这意味着 crate_typecrate_name 的作用事实上很有限。

复制代码
// 13.2节 crate
// 这个 crate 是一个库文件
#![crate_type = "lib"]
// 库的名称为 "rary"
#![crate_name = "rary"]

pub fn public_function() {
    println!("called rary's  'public_function()'");
}

fn private_function() {
    println!("called rary's 'private_function()'");
}

pub fn indirect_access() {
    print!("called rary's 'indirect_access()', that\n> ");
    private_function();
}

// rustc lib.rs 
// ls lib*

当用到 crate_type 属性时,就不再需要给 rustc 命令加上 --crate-type 标记。

复制代码
PS F:\rustproject\rustbyexample\chapter13\example13_2>  rustc lib.rs
PS F:\rustproject\rustbyexample\chapter13\example13_2>  ls lib*
library.rlib

13.3 cfg

条件编译可能通过两种不同的操作符实现:

  • cfg 属性:在属性位置中使用 #[cfg(...)]
  • cfg! 宏:在布尔表达式中使用 cfg!(...)

两种形式使用的参数语法都相同。

复制代码
// 13.3节 cfg
// 这个函数仅当目标系统是 Linux 的时候才会编译
#[cfg(target_os = "linux")]
fn are_you_on_linux() {
    println!("You are running linux!")
}

// 而这个函数仅当目标系统 **不是** Linux 时才会编译
#[cfg(not(target_os = "linux"))]
fn are_you_on_linux() {
    println!("You are *not* running linux!")
}

fn main() {
    are_you_on_linux();
    println!("Are you sure?");
    if cfg!(target_os = "linux") {
        println!("Yes. It's definitely linux!");
    } else {
        println!("Yes. It's definitely *not* linux!");
    }

    if cfg!(target_os = "windows") {
        println!("this is windows OS!");
    } else {
        println!("this not is windows OS!");
    }

    println!("Hello Rust"); 
}

// rustc main.rs  
// ./main

编译运行:

复制代码
PS F:\rustproject\rustbyexample\chapter13\example13_3> rustc main.rs
PS F:\rustproject\rustbyexample\chapter13\example13_3> ./main
You are *not* running linux!
Are you sure?
Yes. It's definitely *not* linux!
this is windows OS!
Hello Rust
PS F:\rustproject\rustbyexample\chapter13\example13_3>
参见:

引用, cfg!, 和 .

13.3.1 自定义条件

有部分条件如 target_os 是由 rustc 隐式地提供的,但是自定义条件必须使用 --cfg 标记来传给 rustc

复制代码
// 13.3.1节 自定义条件
#[cfg(some_condition)]
fn conditional_function() {
    println!("condition met!")
}

fn main() {
    conditional_function();
    println!("Hello Rust");
}

// rustc --cfg some_condition main.rs  
// ./main

试试不使用自定义的 cfg 标记会发生什么:

复制代码
PS F:\rustproject\rustbyexample\chapter13\example13_4> rustc main.rs
error[E0425]: cannot find function `conditional_function` in this scope
 --> main.rs:8:5
  |
8 |     conditional_function();
  |     ^^^^^^^^^^^^^^^^^^^^ not found in this scope
  |
note: found an item that was configured out
 --> main.rs:3:4
  |
2 | #[cfg(some_condition)]
  |       -------------- the item is gated here
3 | fn conditional_function() {
  |    ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0425`.
PS F:\rustproject\rustbyexample\chapter13\example13_4>

使用自定义的 cfg 标记:

复制代码
PS F:\rustproject\rustbyexample\chapter13\example13_4> rustc --cfg some_condition main.rs
PS F:\rustproject\rustbyexample\chapter13\example13_4> ./main
condition met!
Hello Rust
PS F:\rustproject\rustbyexample\chapter13\example13_4> 
相关推荐
魔力军2 小时前
Rust学习Day5:结构体介绍和使用
开发语言·学习·rust
好家伙VCC3 小时前
**发散创新:用 Rust构建多智能体系统,让分布式协作更高效**在人工智能快速演进的今天,**多智能体系统(
java·人工智能·分布式·python·rust
艾尔aier14 小时前
mini-shell成果展示
rust
班公湖里洗过脚17 小时前
《通过例子学Rust》第10章 模块
rust
魔力军17 小时前
Rust学习Day4: 所有权、引用和切片介绍
开发语言·学习·rust
Sheffi6619 小时前
Swift 所有权宏 `~Copyable` 深度解析:如何在 Swift 中实现类似 Rust 的内存安全模型?
rust·ssh·swift
Rust语言中文社区1 天前
【Rust日报】 confidential-ml-transport - 机密机器学习传输
开发语言·人工智能·后端·机器学习·rust
中国胖子风清扬1 天前
Rust 桌面应用开发的现代化 UI 组件库
java·后端·spring·ui·rust·kafka·web application
Rust语言中文社区1 天前
【Rust日报】 Rust 错误源追踪示例
开发语言·后端·rust