This commit is contained in:
nap.liu
2023-12-11 18:40:53 +08:00
parent d1b95f1096
commit 5dc4708fe2
79 changed files with 2269 additions and 39 deletions

7
13.Attributes/13.3 cfg/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg"
version = "0.1.0"

View File

@@ -0,0 +1,8 @@
[package]
name = "cfg"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@@ -0,0 +1,38 @@
//! `cfg` 属性和宏
//!
//! `cfg` 是一个条件属性,当条件成立的时候才会生效,常用的两种形式如下。
//!
//! - `cfg` 当做属性来使用:`#[cfg(...)]` 指定的位置
//! - `cfg` 当做条件计算来用:`cfg!(...)` 条件表达式
//!
//! 当做属性来使用的时候是在编译阶段执行的,当做条件表达式的时候是在运行时执行的。
//! 两种形式都接受相同的参数类型。
//!
//! `cfg!` 宏不像是 `#[cfg]`在编译阶段直接移除掉了条件为 `false` 的代码,
//! 而是保留了所有的代码在运行时执行判断。
//!
// 这个函数只会在 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!");
}
}