Implementing setters
The second part of this exercise will be to implement setters for our structure. Usually this is done by creating a set_{field}() function for each field. Moreover, it's common practice to use generics for it so that they can be used with many different types. For example, for a String field, it would be great if we didn't need to always use an actual String type and we could use a &str, a Cow<_, str>, or a Box<str>.
We only need to declare the input as Into<String>. This makes things a little bit more complex, but our API will look much better. To implement the new setters, it would only mean changing a little bit of what we saw earlier and most of the code would be duplicated.
To avoid that, we will use the strategy ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access