Skip to content

Instantly share code, notes, and snippets.

@barseghyanartur
Created February 2, 2023 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barseghyanartur/9f199c6a95bcd418679a0cc24a2c68f8 to your computer and use it in GitHub Desktop.
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("someone@example.com"),
        sign_in_count: 1,
    };
    println!("{:?}", user);

    let user2 = User {
        email: String::from("another@example.com"),
        ..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