Structs - โครงสร้างข้อมูล
Rust มี structs ที่หลากหลายใน std library!
Structs คืออะไร?
Section titled “Structs คืออะไร?”Structs คือโครงสร้างข้อมูลที่รวมหลายค่าเข้าด้วยกัน:
- Custom types สร้างชนิดข้อมูลใหม่
- Encapsulation รวม data กับ methods
- Zero-cost abstractions ไม่มี overhead
::: best-practice
ใช้ Structs เพื่อสร้าง Type-safety
การสร้าง Struct ใหม่ (Newtype Pattern) ช่วยป้องกันความสับสนในการส่งค่าผิด เช่น struct UserId(u32) ดีกว่าใช้ u32 เฉยๆ
:::
เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google