Merging multiple structs

⚓ Rust    📅 2025-11-06    👤 surdeus    👁️ 6      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Merging multiple structs

Hi folks,

I have two or more structs (same type) that I need to merge into one in a given direction (c -> b -> a)... The merging simply replaces values in "a" by values in "b" unless it is a None option.

It is easy to write a function for that, but the problem is that it is a struct with nested structs, and it'll take a lot of time / a lot of code to do that.

I thought about a clever way and realised that I can serialise both structs via serde_json and write a recursive function that loops over key/values and overrides "a" by "b" unless the "b" value is Value::Null.

It is clever but not really, since serialising and deserialising just for this... Do you have any better ideas?

This probably can be done by macro, but that would be some giant, super complicated macro....

Thank you.

2 posts - 2 participants

Read full topic

🏷️ Rust_feed