本文簡要介紹rust語言中 Macro core::include
的用法。
用法
macro_rules! include {
($file : expr $(,) ?) => { ... };
}
根據上下文將文件解析為表達式或項。
該文件相對於當前文件定位(類似於如何找到模塊)。提供的路徑在編譯時以特定於平台的方式進行解釋。因此,例如,使用包含反斜杠 \
的 Windows 路徑的調用將無法在 Unix 上正確編譯。
使用這個宏通常是個壞主意,因為如果文件被解析為表達式,它將不衛生地放置在周圍的代碼中。如果當前文件中存在同名的變量或函數,這可能會導致變量或函數與文件預期的不同。
例子
假設同一目錄下有兩個文件,內容如下:
文件'monkeys.in':
['🙈', '🙊', '🙉']
.iter()
.cycle()
.take(6)
.collect::<String>()
文件'main.rs':
fn main() {
let my_string = include!("monkeys.in");
assert_eq!("🙈🙊🙉🙈🙊🙉", my_string);
println!("{}", my_string);
}
編譯'main.rs' 並運行生成的二進製文件將打印“🙈🙊🙉🙈🙊🙉”。
相關用法
- Rust include_bytes用法及代碼示例
- Rust include_str用法及代碼示例
- Rust i64.overflowing_add_unsigned用法及代碼示例
- Rust i128.log2用法及代碼示例
- Rust i16.log10用法及代碼示例
- Rust i128.checked_div用法及代碼示例
- Rust i64.abs用法及代碼示例
- Rust i16.wrapping_add_unsigned用法及代碼示例
- Rust i128.checked_next_multiple_of用法及代碼示例
- Rust isize.unstable_div_floor用法及代碼示例
- Rust i32.saturating_mul用法及代碼示例
- Rust isize.swap_bytes用法及代碼示例
- Rust i128.overflowing_pow用法及代碼示例
- Rust i64.checked_add用法及代碼示例
- Rust i16.wrapping_sub用法及代碼示例
- Rust i128.overflowing_sub用法及代碼示例
- Rust i64.saturating_abs用法及代碼示例
- Rust isize.from_ne_bytes用法及代碼示例
- Rust i16.wrapping_shr用法及代碼示例
- Rust i128.overflowing_shr用法及代碼示例
- Rust i128.overflowing_shl用法及代碼示例
- Rust i16.unstable_div_floor用法及代碼示例
- Rust i16.wrapping_abs用法及代碼示例
- Rust i128.checked_sub_unsigned用法及代碼示例
- Rust i32.checked_log2用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 Macro core::include。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。