In Rust, field init shorthand syntax is a more concise way to define fields in structs. It was introduced to make struct initialization more
readable and expressive.
In the shorthand syntax, if a variable exists in the scope with the same name as the struct field you’re defining, you can omit the field name and
just write the variable name. The compiler will automatically understand that the field and the variable are linked.
Using field init shorthand syntax can make your code cleaner and easier to read. It can also reduce the chance of making errors, as you don’t have
to repeat yourself by writing the variable name twice.
let a = 1;
struct MyStruct {
a: i32,
}
let my_struct = MyStruct {
a: a, // Noncompliant
};
You can omit the field name and the colon if it is the same as the local variable name.
let a = 1;
struct MyStruct {
a: i32,
}
let my_struct = MyStruct {
a,
};