Skip to content

Instantly share code, notes, and snippets.

@barseghyanartur
Created February 2, 2023 14:07
Show Gist options
  • Select an option

  • Save barseghyanartur/9f199c6a95bcd418679a0cc24a2c68f8 to your computer and use it in GitHub Desktop.

Select an option

Save barseghyanartur/9f199c6a95bcd418679a0cc24a2c68f8 to your computer and use it in GitHub Desktop.
Make a struct clonable in Rust

Make a struct clonable in Rust

Also, we make it debuggable

fn main() {
    let user = User {
        active: true,
        username: String::from("someusername123"),
        email: String::from("[email protected]"),
        sign_in_count: 1,
    };
    println!("{:?}", user);

    let user2 = User {
        email: String::from("[email protected]"),
        ..user.clone()
    };
    println!("{:?}", user2);
}

// This is where the magic happens!
#[derive(Clone, Debug)]
struct User {
    active: bool,
    username: String,
    email: String,
    sign_in_count: u64,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment