Is there a faster/shorter way to initialize variables in a Rust struct?
@lebyy▲ 0 net votes
Problem: In the following example, I would much prefer to assign a value to each field in the struct in the declaration of the fields. Alternatively, it effectively takes one additional statement for each field to assign a value to the fields. All I want to be able… Fix (from the top answer): You can provide default values for your struct by implementing the Default trait. The default function would look like your current new function: impl Default for cParams { fn default() -> cParams { cParams { iInsertMax: -1, iUpdateMax: -1, iDeleteMax: -1, iInsta You can then instantiate the struct by giving only the…