Tsallaka zuwa abun ciki
Justice Tech School

Derive and Common Traits

#[derive] generates implementations of Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord and Default.

Example
#[derive(Debug, Clone, PartialEq, Default)]
struct Point { x: i32, y: i32 }
fn main() {
    let a = Point { x: 1, y: 2 };
    let b = a.clone();
    println!("{:?} {}", a, a == b);
    println!("{:?}", Point::default());
}

Result

Point { x: 1, y: 2 } true
Point { x: 0, y: 0 }

Exercise

Auto implement Debug.

#[(Debug)]

Quiz Test

What does derive do?

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