Building structs
In modern high-level dynamic languages, objects have been the bedrock for building big applications and solving complex problems, and for good reason. Objects enable us to encapsulate data, functionality, and behavior. In Rust, we do not have objects. However, we do have structs that can hold data in fields. We can then manage the functionality of these structs and group them together with traits. This is a powerful approach, and it gives us the benefits of objects without the high coupling, as highlighted in the following figure:
Figure 1.10 – Difference between Rust structs and objects
We will start with something basic by creating a Human
struct with the following code:
#[derive(Debug)] struct Human<'a> { name: &'a str, age: i8, current_thought: &'a str }
In the preceding code, we can see that our string literal fields have...