New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggest struct constructor instead of mutable object + field mod #568

Open
oli-obk opened this Issue Jan 20, 2016 · 0 comments

Comments

Projects
None yet
2 participants
@oli-obk
Collaborator

oli-obk commented Jan 20, 2016

If you have a struct where all fields are public, then you should never do let mut a = A::default(); a.x = 42; but instead use a constructor with .. A::default() at the end:

#[derive(Default)]
struct A {
    i: i32,
    j: i64,
}

fn main() {
    let mut a: A = Default::default();
    a.i = 42;
}

fn should_be() {
    let a = A {
        i: 42,
        .. Default::default()
    };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment