2
Which code style to initialize structs?
(lemmy.ml)
I prefer to encapsulate a mutable reference to the instance in a scope.
let post_form = {
let mut post_form = PostInsertForm::new(
// your constructor arguments
);
post_form.some_mutating_method(
// mutation arguments
);
post_form
};
This way you're left with an immutable instance and you encapsulate all of the logic needed to setup the instance in one place.
Definitely the second one.
Neither.
new()
give you a fully valid and usable struct value.Maybe you should also use substructs that hold some of the info.
100% the second one. It's the idiomatic way to do this in Rust, and it leaves you with an immutable object.
I personally like to move the short declarations together (i.e. body down with language_id (or both at the top)) but that's a minor quibble.