Rust: Some important traits

std::fmt::Display

pub trait Display {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error>;
}

User-facing;

cannot be derived.

std::fmt::Debug

pub trait Debug {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error>;
}

Programmer-facing debug info;

can be used with #[derive] if all fields implement Debug

Debug: derived example from doc

#[derive(Debug)]
struct Point {
    x: i32,
    y: i32,
}

let origin = Point { x: 0, y: 0 };

println!("The origin is: {:?}", origin);

std::clone::Clone

Explicit duplication;

provide any type-specific behavior necessary to duplicate values safely.

pub trait Clone {
    fn clone(&self) -> Self;

    fn clone_from(&mut self, source: &Self) { ... }
}

std::marker::Copy

Implicit bit-wise duplication;

pub trait Copy: Clone { }

A type can implement Copy if all of its components implement Copy

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s