Compile-time Macros - ทำงานตอน Compile
Macros ที่ทำงานตอน compile time ช่วยให้ได้ข้อมูลเกี่ยวกับ environment, source code location, configuration และอื่นๆ โดยไม่มี runtime cost เพราะค่าถูก evaluate ตอน compile
1. cfg! - Configuration Checking
Section titled “1. cfg! - Configuration Checking”cfg! macro ตรวจสอบ compile-time configuration flags และ return bool ใช้สำหรับ conditional logic ที่ขึ้นกับ platform หรือ build settings
ตรวจสอบ Debug/Release Mode
Section titled “ตรวจสอบ Debug/Release Mode”fn main() { // ตรวจ debug/release mode if cfg!(debug_assertions) { println!("Running in DEBUG mode"); println!("Extra validation enabled"); } else { println!("Running in RELEASE mode"); println!("Optimized for performance"); }
// ใช้กับ feature flags if cfg!(feature = "verbose") { println!("Verbose logging enabled"); }}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google