Struct
Explore how to create and use structs in Rust to combine primitive types into custom data structures. Understand how to declare structs, access their fields, and pass them as function parameters. This lesson also covers practical implementation and highlights Rust's ownership rules related to structs.
We'll cover the following...
A struct in Rust allows you to create a new data type made up of values from other data types. So far, we’ve worked with primitives–things built into the language—like an i32. But we can combine these primitives into larger, custom types. In this case, I want to define a new data type called Fruit that tells me how many apples and bananas I have. Let’s see what that looks like:
struct starts a declaration, similar to how fn starts a declaration. In the case of fn, we’re declaring a new function name. In the case of a struct, we’re declaring a new type name. In this case, our ...