{"id":3830,"date":"2022-12-16T14:12:59","date_gmt":"2022-12-16T14:12:59","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=3830"},"modified":"2023-08-31T10:25:49","modified_gmt":"2023-08-31T10:25:49","slug":"what-is-a-solidity-struct","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/what-is-a-solidity-struct\/","title":{"rendered":"What is a Solidity struct?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/what-is-a-solidity-struct\/#Main_purposes_that_a_Solidity_struct_serves\" title=\"Main purposes that a Solidity struct serves\">Main purposes that a Solidity struct serves<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/what-is-a-solidity-struct\/#1_Organizing_data\" title=\"1. Organizing data\">1. Organizing data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/what-is-a-solidity-struct\/#2_Encapsulating_data\" title=\"2. Encapsulating data\">2. Encapsulating data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/what-is-a-solidity-struct\/#3_Modeling_real-world_entities\" title=\"3. Modeling real-world entities\">3. Modeling real-world entities<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/what-is-a-solidity-struct\/#4_Improving_code_reuse\" title=\"4. Improving code reuse\">4. Improving code reuse<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/metaschool.so\/articles\/what-is-a-solidity-struct\/#How_to_define_a_struct_in_Solidity\" title=\"How to define a struct in Solidity\">How to define a struct in Solidity<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>In <a href=\"https:\/\/metaschool.so\/solidity?utm_campaign=smf&amp;utm_source=blog&amp;utm_medium=organic\" target=\"_blank\" rel=\"noreferrer noopener\">Solidity<\/a>, a struct is a user-defined type that represents a composite data structure, similar to a class in object-oriented programming. A struct can contain multiple fields of different types, and you can use it to define complex data structures that can be used to represent real-world entities.<\/p>\n\n\n\n<p>A Solidity struct is useful for organizing and encapsulating data in a <a href=\"https:\/\/metaschool.so\/courses\/writing-your-first-hello-world-contract-in-solidity?ref=Articles&amp;utm_source=Blog_Organic\" target=\"_blank\" rel=\"noreferrer noopener\">smart contract<\/a>. A struct can contain multiple fields of different types, allow you to define complex data structures that can be used to represent real-world entities, and they can help you write more readable and maintainable code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"main-purposes-that-a-solidity-struct-serves\"><span class=\"ez-toc-section\" id=\"Main_purposes_that_a_Solidity_struct_serves\"><\/span>Main purposes that a Solidity struct serves<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"organizing-data-structs-allow-you-to-group-related-data-fields-together-and-give-them-a-meaningful-name-this-can-make-your-code-more-readable-and-easier-to-understand-especially-if-you-are-working-with-a-large-contract-that-has-many-different-data-fields\"><span class=\"ez-toc-section\" id=\"1_Organizing_data\"><\/span>1. Organizing data<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p id=\"organizing-data-structs-allow-you-to-group-related-data-fields-together-and-give-them-a-meaningful-name-this-can-make-your-code-more-readable-and-easier-to-understand-especially-if-you-are-working-with-a-large-contract-that-has-many-different-data-fields\">Structs allow you to group related data fields together and give them a meaningful name. This can make your code more readable and easier to understand, especially if you are working with a large contract that has many different data fields.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-encapsulating-data\"><span class=\"ez-toc-section\" id=\"2_Encapsulating_data\"><\/span>2. Encapsulating data<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Structs can be used to create a logical separation between different parts of your data model. For example, you could define a struct to represent a user account, and then use that struct to store all of the data related to a user in one place. This can make it easier to manage your data and avoid conflicts between different parts of your contract.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-modeling-real-world-entities\"><span class=\"ez-toc-section\" id=\"3_Modeling_real-world_entities\"><\/span>3. Modeling real-world entities<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Structs can be used to represent real-world entities in your contract, such as users, products, or orders. This can make it easier to work with your data and understand the relationships between different entities in your contract.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-improving-code-reuse\"><span class=\"ez-toc-section\" id=\"4_Improving_code_reuse\"><\/span>4. Improving code reuse<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Structs can be used as building blocks for more complex data structures, such as arrays and mappings. This can allow you to reuse your struct definitions in multiple places in your contract, which can save you time and improve the maintainability of your code.<\/p>\n\n\n\n<p>Overall, structs are a powerful tool in Solidity that can help you organize and encapsulate your data, model real-world entities, and improve the reuse and maintainability of your code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-define-a-struct-in-solidity\"><span class=\"ez-toc-section\" id=\"How_to_define_a_struct_in_Solidity\"><\/span>How to define a struct in Solidity<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>with examples \ud83d\udc47\ud83c\udffc<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>struct Person {\n  string name;\n  uint age;\n}<\/code><\/pre>\n\n\n\n<p>In this example, the <code>Person<\/code> struct has two fields: <code>name<\/code>, which is a string, and <code>age<\/code>, which is an unsigned integer. You can create an instance of the <code>Person<\/code> struct and set its fields like this:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>Person alice;\nalice.name = \"Alice\";\nalice.age = 30;\n<\/code><\/pre>\n\n\n\n<p>You can also define a struct with an array field, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>struct Person {\n  string name;\n  uint age;\n  uint&#91;] favoriteNumbers;\n}\n<\/code><\/pre>\n\n\n\n<p>In this example, the <code>Person<\/code> struct has a field called <code>favoriteNumbers<\/code> that is an array of unsigned integers. You can access and modify the elements of the array just like you would with any other array in Solidity.<\/p>\n\n\n\n<p>&#8212; <\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":3835,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[31,46],"class_list":["post-3830","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-ethereum-blockchain","tag-solidity"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=3830"}],"version-history":[{"count":7,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3830\/revisions"}],"predecessor-version":[{"id":6820,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3830\/revisions\/6820"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/3835"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=3830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=3830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=3830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}