The #[derive] attribute instructs the compiler to auto-generate trait implementations for a struct. Clone adds a .clone() method that produces a deep copy, while Debug adds .debug_str() for human-readable inspection.
// #[derive(Clone, Debug)]: clonar structs y obtener representación debug #[derive(Clone, Debug)] struct Point { x: int, y: int } #[derive(Clone, Debug)] struct Rectangle { origin: int, width: int, height: int } fn main() -> int { // Clonar un Point let p1: Point = Point { x: 10, y: 20 } let p2: Point = p1.clone() print("Original: x=" + int_to_string(p1.x) + " y=" + int_to_string(p1.y)) print("Clon: x=" + int_to_string(p2.x) + " y=" + int_to_string(p2.y)) // Debug string let ds: String = p1.debug_str() print("Debug: " + ds) // Clonar Rectangle let r1: Rectangle = Rectangle { origin: 0, width: 100, height: 50 } let r2: Rectangle = r1.clone() print("Rect original: w=" + int_to_string(r1.width)) print("Rect clon: w=" + int_to_string(r2.width)) return 0 }
Original: x=10 y=20
Clon: x=10 y=20
Debug: Point { x: 10, y: 20 }
Rect original: w=100
Rect clon: w=100How it works
Placing #[derive(Clone, Debug)] above a struct declaration tells the Nyx compiler to synthesize the corresponding trait implementations automatically. You get a fully working .clone() and .debug_str() without writing a single line of implementation code. This eliminates a whole category of repetitive boilerplate.
Clone performs a field-by-field copy of the struct. After let p2 = p1.clone(), modifying p2 does not affect p1 — each holds its own independent copy of the data. This is safe even for structs that contain heap-allocated fields, because the derive macro handles the deep copy logic.
Debug generates a .debug_str() method that formats the struct with its type name and field values in a readable form — useful for logging, tests, and development-time inspection. Multiple traits can be derived in a single annotation by listing them comma-separated inside the parentheses.