RUST StructStructs are similar to tuples. The pieces of a struct can be different types. You’ll name each piece of data so it’s clear what the values mean. As a result of these names, structs are more flexible than tuples: you don’t have to rely on the order of the data to specify or access the values of an instance.
To define a struct, we enter the keyword struct and name the entire struct. A struct’s name should describe the significance of the pieces of data being grouped together. Then, inside curly brackets, we define the names and types of the pieces of data, which we call fields.
Example of RUST Struct
Use of RUST StructTo use a struct after we’ve defined it, we create an instance of that struct by specifying concrete values for each of the fields.
Accesing RUST Struct AttributeTo get a specific value from a struct, we can use dot notation. If we wanted just this user’s email address, we could use user1.email
Complete Example of RUST Struct
output of above code
My user email: firstname.lastname@example.org
In this page (written and validated by A. Gawali) you learned about Rust Struct . What's Next? If you are interested in completing Rust tutorial, your next topic will be learning about: Rust File handling - Read - Write.
Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: email@example.com.