struct_derive
StructUpdate is a derive macro in Rust that automatically generates an update_struct method for your struct. This method transforms all fields of type String in your struct to SCREAMING_SNAKE_CASE (all uppercase with underscores between words).
Usage
- First, you need to add the
struct_derivedependency in yourCargo.tomlfile.
[]
= "0.2.1"
- Then, you can use the
#[derive(StructUpdate)]annotation on your struct. Also, you need to use the#[update_struct(with(ty = String, func = "to_screaming_snake_case"))]annotation to specify which type of fields need to be updated and what function to use to update these fields.
use StructUpdate;
- Now, your struct has an
update_structmethod. You can call this method to update the fields in the struct.
In the example above, the update_struct method transforms the username, first_name, and last_name fields to SCREAMING_SNAKE_CASE, but does not change the age field, as the age field is not of type String.