Attributes
Attributes ใน Rust คือ Metadata ที่แปะไว้บนโค้ด เพื่อบอก Compiler ว่า “เฮ้ ช่วยทำตรงนี้หน่อย” หน้าตาจะเป็น #[...] สำหรับ outer attributes หรือ #![...] สำหรับ inner attributes
ลองนึกภาพว่า Attributes เป็นเหมือน “ป้ายสั่งงาน” ที่แปะบนโค้ด บอก compiler ว่าจะทำอะไรพิเศษกับโค้ดตรงนี้
1. Outer Attributes - #[...]
Section titled “1. Outer Attributes - #[...]”Outer attributes แปะอยู่บนหัวของ function, struct หรือ item อื่นๆ มีผลกับ item ที่อยู่ถัดไป
1.1 derive - Auto-implement Traits
Section titled “1.1 derive - Auto-implement Traits”#[derive] ให้ compiler implement traits อัตโนมัติ เป็น attribute ที่ใช้บ่อยที่สุด
เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google