<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bharam</title>
    <description>The latest articles on DEV Community by Bharam (@bharamappa).</description>
    <link>https://dev.to/bharamappa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F339546%2F2f23c585-c7cf-40fc-bae7-51bd66a92a1c.png</url>
      <title>DEV Community: Bharam</title>
      <link>https://dev.to/bharamappa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bharamappa"/>
    <language>en</language>
    <item>
      <title>Structs in rust</title>
      <dc:creator>Bharam</dc:creator>
      <pubDate>Thu, 11 Apr 2024 12:14:34 +0000</pubDate>
      <link>https://dev.to/bharamappa/structs-in-rust-523l</link>
      <guid>https://dev.to/bharamappa/structs-in-rust-523l</guid>
      <description>&lt;h2&gt;
  
  
  Structs
&lt;/h2&gt;

&lt;p&gt;struct is custom data type that lets you package together and name multiple related values that make up a meaningfull group.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining Structs
&lt;/h2&gt;

&lt;p&gt;structs are defining in the following way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct User {
    active: bool,
    username: String,
    email: String,
    sign_in_count: u64,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Instantiating Structs
&lt;/h2&gt;

&lt;p&gt;Structs are instantiated by creating an instance of the struct with the concrete values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let user1 = User {
    active: true,
    username: String::from("sherlock"),
    email: Sting::from("sherlock@yahoo.com"),
    sign_in_count: 1,
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Accessing Struct Values
&lt;/h2&gt;

&lt;p&gt;We use dot notation to access the specific value from the struct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user1.email // sherlock@yahoo.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Modifying The Struct
&lt;/h2&gt;

&lt;p&gt;To change the value of struct first we need make user instance as mutable. &lt;code&gt;let mut user1 = User {...}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;we use the same dot notion to mutate the struct.&lt;br&gt;
&lt;code&gt;user1.email = String::from("jhon@yahoo.com")&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Field Init Shorthand
&lt;/h2&gt;

&lt;p&gt;When function parameters are passed down to the struct defnition there posiblitly repeating field names and function paramters. let say we have&lt;br&gt;
funciton:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function build_user(username: String, email: string) -&amp;gt; User {
    //This is called implicit return.
    User {
        active: true,
        username: username,
        email: email,
        sign_in_count: 1,
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;AS u see we are repeating paramters and fields here.&lt;br&gt;
With help of Field Init Shorthand we can write as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function build_user(username: String, email: string) -&amp;gt; User {
    //This is called implicit return.
    User {
        active: true,
        username,
        email,
        sign_in_count: 1,
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating new instances from other instance with Struct Update Syntax(Spread syntax if you know JS).
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let user2 = User {
    email: String::from("holmes@yahoo.com");
    ...user1
};
It will have same the values of user1 and but email will be different.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Structs can be defined without the name and value fields.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Structs can also be defined unit-like without any field or types.
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;struct AlwaysEqual;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  When to use structs
&lt;/h2&gt;

&lt;p&gt;We use struct when we want to convey the meaning of our data in our code.&lt;/p&gt;

&lt;p&gt;for example:&lt;br&gt;
let say i have tuple like this&lt;br&gt;
&lt;br&gt;
  &lt;code&gt;let rect1 = (30, 50)&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 which does not convey the what the values represent. In this case we can use struct to define our values with right structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let rect1 = Rectangle {
    width: 30,
    height: 50,
};
and the struct is 
struct Rectangle {
    width: u32,
    height: u32,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>rust</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
