Updating the fields of a struct
While working with structs, sometimes we might need to update specific fields of a struct in order to change the existing or default value that a field holds. V has certain specifications when it comes to updating struct fields. In this section, we will see different approaches to updating the fields of structs and understand all the prerequisites that are needed in order to make changes to the values of struct fields.
All the fields of a struct are immutable by default. They can only be initialized once. To change the value of a field of a struct, it needs to be specified under the section marked with mut:
. All the fields defined in a line below the mut:
will be mutable fields.
We will change the struct Note
so that the message
field can be mutable:
struct Note { id int mut: message string }
Now, we will declare...