Rust toml parser - filling up a struct


So the toml crate provide nice parser functionality. However, looking at examples I see that you can not partially “fill” a struct, except that you make all fields Options. I was wondering (as I did not manage to dig up such example) if there is any direct way to pass a mutable already existing struct, and the toml parser just to overwrite any fields which are in the toml and use default ones after.


Hi! I suggest you check out using serde with toml, which will help you turn your toml file into Rust structs. You’ll need to add the serde feature to your toml dependency, which is described in the toml README.


I did that, but the docs follow the example of creating a new struct out of the toml string, while I want to pass one and just update the fields present in the toml, without writing it manually.


Why do you want to pass in a struct to be mutated? What does that original struct have that you want to keep? If it’s just to have a default value for fields which don’t exist instead of an optional, maybe this will help you? I believe we may be running into the XY problem here.


I think the serde default is what I was looking for. The problem on itself is that I have quite a few configs, which the user can tweek trough a toml file. However, I don’t want the user to have to specify all of them, so I want to parse the toml file and fill in everything the user has specified and using default values for the rest, without using the Option<T> solution.