Skip to content
เข้าสู่ระบบ

Memory Traits

Traits สำหรับ memory management

struct Resource {
name: String,
}
impl Drop for Resource {
fn drop(&mut self) {
println!("Dropping: {}", self.name);
}
}
fn main() {
println!("=== Drop Order ===");
{
let a = Resource { name: "A".to_string() };
let b = Resource { name: "B".to_string() };
println!("Created A and B");
} // B drops first, then A (LIFO)
println!("\n=== Manual Drop ===");
let c = Resource { name: "C".to_string() };
println!("Before drop");
drop(c); // Explicit drop
println!("After drop\n");
// Tuple/struct field order
println!("=== Field Order ===");
struct Container {
first: Resource,
second: Resource,
}
let _container = Container {
first: Resource { name: "First".to_string() },
second: Resource { name: "Second".to_string() },
};
// Fields drop in declaration order
println!("End of main");
}

เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม

ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด