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 ...

Get Rust High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.