Core Traits
Fundamental traits ที่เป็นพื้นฐานของ Rust
Copy และ Clone
Section titled “Copy และ Clone”fn main() { // Copy - implicit bitwise copy let x: i32 = 42; let y = x; // Copy println!("x: {}, y: {}", x, y); // Both valid
// Clone - explicit duplication let s1 = String::from("hello"); let s2 = s1.clone(); // Clone println!("s1: {}, s2: {}", s1, s2); // Both valid
// Without clone - move let s3 = String::from("world"); let s4 = s3; // Move // println!("{}", s3); // Error: s3 moved println!("s4: {}", s4);
// Copy types println!("\n=== Copy Types ==="); println!("Integers: i8, i16, i32, i64, i128, u*"); println!("Floats: f32, f64"); println!("bool, char"); println!("References: &T"); println!("Tuples of Copy types"); println!("Arrays of Copy types");
// Derive Copy (requires Clone) #[derive(Copy, Clone, Debug)] struct Point { x: i32, y: i32 }
let p1 = Point { x: 1, y: 2 }; let p2 = p1; // Copy println!("\np1: {:?}, p2: {:?}", p1, p2);}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google