Tsallaka zuwa abun ciki
Justice Tech School

derive Attributes

#[derive(...)] adds Debug, Clone, PartialEq and more automatically.

Example
#[derive(Debug, Clone, PartialEq)]
struct P { x: i32, y: i32 }

fn main() {
    let a = P { x: 1, y: 2 };
    let b = a.clone();
    println!("{:?} {}", a, a == b);
}

Result

P { x: 1, y: 2 } true

Exercise

Debug derive.

#[derive()]

Quiz Test

What does derive(Debug) enable?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.