Files
100-exercises-to-learn-rust/book/src/05_ticket_v2/15_outro.md
Luca Palmieri 1aae615bb4 Automatically add exercise links to sections. (#52)
We use an mdbook preprocessor to automatically generate links to the relevant exercise for each section.
We remove all existing manual links and refactor the deploy process to push the rendered book to a branch.
2024-05-24 18:15:38 +02:00

1.1 KiB
Raw Blame History

Wrapping up

When it comes to domain modelling, the devil is in the details.
Rust offers a wide range of tools to help you represent the constraints of your domain directly in the type system, but it takes some practice to get it right and write code that looks idiomatic.

Lets close the chapter with one final refinement of our Ticket model.
Well introduce a new type for each of the fields in Ticket to encapsulate the respective constraints.
Every time someone accesses a Ticket field, theyll get back a value thats guaranteed to be valid—i.e. a TicketTitle instead of a String. They wont have to worry about the title being empty elsewhere in the code: as long as they have a TicketTitle, they know its valid by construction.

This is just an example of how you can use Rusts type system to make your code safer and more expressive.

Further reading