How to avoid a Heart that needs a default trait for a field that skips serialization?

I have this structure, which is created by deserialization using Serde, and I want to have in it a field of type Url that is not deserialized:

 #[derive(Deserialize, Debug)] pub struct Flow { #[serde(skip_deserializing)] pub source: Url, } 

Playground

Serd complains about Url not matching the Default attribute. I tried with and without getting Default . Am I the only option for me to implement the Default trait for Url myself?
+5
source share
2 answers

You can use #[serde(default = "path")] in the field to give a function with the signature fn() -> Url , which should be called if the field is absent.

+9
source

It is also possible to implement Deserialize and handle missing values ​​accordingly.

0
source

Source: https://habr.com/ru/post/1274922/


All Articles