Is it possible to just use default partially?

Source is here https://github.com/pingcap/tikv/blob/master/src/raft/raft.rs#L230
There are many redundant assignments. Like 0, None, and Default::default.
Can i make this code cleaner by using default value partially? Thanks.

To use the default for all unspecified fields, you can write .. Default::default(). So your code can be rewritten like this:

    let mut r = Raft {
        id: c.id,
        raft_log: raft_log,
        max_inflight: c.max_inflight_msgs,
        max_msg_size: c.max_size_per_msg,
        prs: HashMap::with_capacity(peers.len()),
        state: StateRole::Follower,
        check_quorum: c.check_quorum,
        heartbeat_timeout: c.heartbeat_tick,
        election_timeout: c.election_tick,
        allow_step: true,
        tag: c.tag.to_owned(),
        .. Default::default()
    };
2 Likes