You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When creating a component, there are often cases where the component can and should function without having an explicit value provided as a prop. While currently, props structs will provide a default value for a field if Default is implemented for it's type, this is not always the value that is desired or required by the component. For example, a bool will default to false and numerics will default to 0. In many sitiuations, a component will require alternative defaults than the type defaults.
#[derive(Deserialize,Debug)]structRequest{// Use the result of a function as the default if "resource" is// not included in the input.#[serde(default = "default_resource")]resource:String,}fndefault_resource() -> String{"/".to_string()}
A similar approach could be taken for property structs:
Context
When creating a component, there are often cases where the component can and should function without having an explicit value provided as a prop. While currently, props structs will provide a default value for a field if
Default
is implemented for it's type, this is not always the value that is desired or required by the component. For example, abool
will default tofalse
and numerics will default to0
. In many sitiuations, a component will require alternative defaults than the type defaults.Possibilities
With serde, default values can be provided using a syntax like
A similar approach could be taken for property structs:
The text was updated successfully, but these errors were encountered: