<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vennila Pugazhenthi on Medium]]></title>
        <description><![CDATA[Stories by Vennila Pugazhenthi on Medium]]></description>
        <link>https://medium.com/@vennilapugazhenthi?source=rss-b862a7388643------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GM49a1lPLquLpG_eQMgA8Q.jpeg</url>
            <title>Stories by Vennila Pugazhenthi on Medium</title>
            <link>https://medium.com/@vennilapugazhenthi?source=rss-b862a7388643------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 19:54:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vennilapugazhenthi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[TLS/SSL handshake and possible failures]]></title>
            <link>https://medium.com/@vennilapugazhenthi/tls-ssl-handshake-and-possible-failures-b8a9db0547cb?source=rss-b862a7388643------2</link>
            <guid isPermaLink="false">https://medium.com/p/b8a9db0547cb</guid>
            <category><![CDATA[tls]]></category>
            <category><![CDATA[sslhandshakeexception]]></category>
            <category><![CDATA[ssl]]></category>
            <category><![CDATA[ssl-handshake]]></category>
            <category><![CDATA[tls-handshake]]></category>
            <dc:creator><![CDATA[Vennila Pugazhenthi]]></dc:creator>
            <pubDate>Sat, 26 Oct 2024 17:57:11 GMT</pubDate>
            <atom:updated>2024-10-26T19:14:19.706Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/413/1*e44I4Lv8Db69HJnduI047w.png" /><figcaption>TLS v1.2</figcaption></figure><p>A TLS or SSL handshake is a cryptographic protocol that occurs between two parties, typically a client (such as a browser) and a server, to establish a secure and encrypted communication channel. This article will focus on TLS version 1.2.</p><h4>Why is TLS important?</h4><p>The TLS handshake is critical because it provides three essential functions:</p><ol><li><strong>Authentication</strong>: Confirms the legitimacy of the server and verifies its identity, ensuring it is who it claims to be.</li><li><strong>Encryption</strong>: Secures the data transmitted between the client and server through encryption, preventing unauthorized parties from intercepting or accessing the communication.</li><li><strong>Data Integrity</strong>: Guarantees that the transmitted data has not been altered or tampered with during the communication process.</li></ol><h4>TLS/SSL handshake</h4><p>To provide an overview, the handshake begins with asymmetric encryption (involving public and private keys) for the secure key exchange and then transitions to symmetric encryption to protect the data transmitted throughout the session. The TLS/SSL handshake involves two round-trip exchanges between the client and the server to successfully establish a secure connection. We will explore the details in the following steps. Additionally, we will examine potential causes of TLS/SSL handshake failures, which commonly occur during step 2 and/or step 3 of the handshake.</p><h4>Step 1: ClientHello</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VdX2H1sYoiqiYvbumUwh7Q.jpeg" /><figcaption>ClientHello</figcaption></figure><p>The handshake begins with the client sending the <strong>ClientHello</strong> message. This message contains several key pieces of information:</p><ul><li>The <strong>TLS version</strong> that the client supports.</li><li>A <strong>random value</strong> called the “Client Random”, which will later be used in the handshake to help calculate a shared session key.</li><li><strong>Cipher suites </strong>are a list of cryptographic algorithms the client supports for key exchange, authentication, encryption, and hashing.</li></ul><h4>Step 2: ServerHello</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s0Hp6JsSgCCIzXTPuinSIQ.jpeg" /><figcaption>ServerHello</figcaption></figure><p>In response to the <strong>ClientHello</strong> message, the server sends the <strong>ServerHello</strong> message. This message includes parameters similar to those in the ClientHello, such as the supported TLS version, the server’s random value, and the selected cipher suite (which is chosen from the list provided by the client).</p><p>Additionally, the server provides its TLS/SSL certificate, which contains the server’s public key and information regarding the server’s identity, signed by a trusted Certificate Authority (CA). The ServerHello message also includes the parameters necessary for the server key exchange required for the asymmetric key exchange process.</p><p>In this step, depending on the selected cipher suite, if a Diffie-Hellman-based key exchange is used, the server’s SSL certificate private key is utilized to sign the server’s key exchange parameters. However, in the case of an RSA-based key exchange, the key exchange parameters are not signed.</p><p>The SSL certificate provided by the server enables the client to <strong>authenticate </strong>the server and verify its legitimacy, safeguarding against man-in-the-middle attacks and ensuring a secure connection. The key exchange parameters facilitate the calculation of the symmetric key, which is subsequently used for <strong>encryption</strong>. If the key exchange parameters are signed, they offer additional proof of the server’s identity.</p><p><strong>Causes of TLS/SSL handshake errors in Step 2:</strong></p><ul><li><strong>Incompatible TLS versions between the client and server</strong>: If the client’s browser only supports older TLS versions and the server requires a newer version, the SSL connection will be rejected.</li><li><strong>Incompatible cipher suites</strong>: The server and client must agree on a common cipher suite to proceed with the handshake. If no compatible suite is found, the handshake will fail.</li></ul><h4><strong>Step 3: Client key exchange and generate the master secret</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8RstghFv8qB7N8BzLR-CuA.jpeg" /><figcaption>Step 3</figcaption></figure><p><strong>Certificate Verification - </strong>Once the client receives the server’s SSL certificate, it performs a series of checks to validate its authenticity:</p><ul><li><strong>Trusted Certificate Authority (CA):</strong> The client verifies that the certificate is signed by a trusted <strong>Certificate Authority</strong> (CA). A CA is a trusted third party that vouches for the legitimacy of the certificate. The client has a list of trusted CAs (pre-installed in its operating system or browser). If the certificate is signed by one of these CAs, it is considered valid.<br><strong>Example:</strong> Let’s say you are accessing <a href="https://example.com/">https://example.com</a>. The server provides its certificate, which is signed by <strong>DigiCert</strong> (a trusted CA). The client checks whether <strong>DigiCert</strong> is in its list of trusted authorities. If it is, the certificate passes this check.</li><li><strong>Certificate Validity:</strong> The client checks whether the certificate is still within its valid period. Every SSL certificate has a start and end date, and it should not be used outside of this range. If the certificate is expired, the client will not trust it.<br><strong>Example:</strong> If the certificate for https://example.com was issued on January 1, 2023, and is valid until January 1, 2024, the client will ensure that today’s date falls within this period. If the date is beyond January 1, 2024, the certificate is considered expired.</li><li><strong>Domain Name Match:</strong> The client verifies that the domain name (the URL being accessed) matches the domain listed in the certificate’s <strong>Common Name (CN)</strong> or <strong>Subject Alternative Name (SAN)</strong> fields. This ensures that the certificate is being used by the correct website.<br><strong>Example:</strong> If you visit https://example.com, the client will check if the certificate is issued to example.com in the CN or SAN fields. If the certificate is for example.net instead, the client will flag this as a mismatch and may block the connection.</li><li><strong>Revocation Status:</strong> The client checks if the certificate has been revoked by the CA. Certificates can be revoked if they are compromised or no longer trusted. This is done by querying the <strong>Certificate Revocation List (CRL)</strong> or using the <strong>Online Certificate Status Protocol (OCSP)</strong>.<br><strong>Example:</strong> If the certificate for example.com was compromised and the CA has revoked it, the client would query the OCSP or check the CRL to confirm its status. If revoked, the client will not trust the certificate.</li></ul><p><strong>Generation of pre-master secret </strong>— The <strong>pre-master secret</strong> is generated as part of the key exchange process to derive the session keys that will be used for symmetric encryption. The method for generating the pre-master secret depends on the type of key exchange algorithm used (e.g., RSA or Diffie-Hellman).</p><p><strong>Client Key Generation/Exchange — </strong>After validating the server’s certificate, the client initiates key exchange based on the selected algorithm:</p><ul><li><strong>RSA Key Exchange:</strong><br> The client generates a <strong>pre-master secret</strong>, encrypts it with the server’s public key (which is derived from the SSL certificate), and sends it in the <strong>ClientKeyExchange</strong> message. Only the server can decrypt it using its private key, and both parties then derive the session keys from this shared secret.</li><li><strong>Diffie-Hellman (DH) or Elliptic Curve Diffie-Hellman (ECDH) Key Exchange:</strong><br> In DH or ECDHE, the client and server jointly generate the pre-master secret. The client sends its DH public key, and the server combines it with its own to compute the shared secret.</li></ul><p><strong>Pre-Master Secret to Session Keys — </strong>Regardless of the key exchange method (RSA or Diffie-Hellman), once the pre-master secret is established, both the client and server use it to derive <strong>session keys</strong> through a process called the <strong>TLS PRF (Pseudo-Random Function)</strong>. These session keys are used for symmetric encryption and message authentication during the rest of the session.</p><p><strong>Change Cipher Spec — </strong>The ChangeCipherSpec message is sent by the client to inform the server that future messages from this point onward will be encrypted using the newly negotiated session keys and cryptographic algorithms (cipher suite). Essentially, it marks the switch from the handshake process to secure, encrypted communication.</p><p><strong>Finished — </strong>After sending the ChangeCipherSpec, the client immediately sends a <strong>Finished</strong> message. This is the first message encrypted with the newly established session keys. The Finished message contains a hash of all the handshake messages exchanged so far, ensuring the <strong>integrity </strong>of the entire handshake process.</p><p><strong>Causes of SSL Handshake Errors in Step 3:</strong></p><ul><li><strong>TLS/SSL Certificate Issues:</strong> SSL handshake errors can occur if the server’s certificate is expired, revoked, or issued by an untrusted Certificate Authority (CA).</li><li><strong>Incorrect Client Time Settings:</strong> Since the client verifies the validity of the SSL certificate, an incorrect time setting on the client can cause the certificate to appear invalid (outside its validity period), leading to a handshake failure.</li><li><strong>Missing or Incorrect Private Key:</strong> If the server’s SSL private key is missing or incorrect, it cannot be used to sign the certificate or decrypt the pre-master secret, resulting in an SSL handshake error.</li><li><strong>Unauthorized Plugins or Extensions:</strong> SSL handshake errors may occur if third-party plugins or extensions on the client side intercept the connection, as this could compromise the integrity of the handshake messages, particularly the Finished message, which verifies message integrity.</li></ul><p><strong>Step 4: Server’s Finished Message</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MtIjtzQ_FVrlkdVrKQ05sw.jpeg" /><figcaption>Step 4</figcaption></figure><p>After receiving the client’s Finished message (which is encrypted using the negotiated session keys), the server responds with its own ChangeCipherSpec and Finished messages.</p><p>The <strong>Finished</strong> message guarantees the integrity of the handshake. Once both parties have exchanged and successfully verified these messages, the handshake is complete, and all subsequent application data is encrypted using the session keys.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b8a9db0547cb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What does the Question Mark (?) operator do in Rust?]]></title>
            <link>https://medium.com/@vennilapugazhenthi/what-does-the-question-mark-operator-do-in-rust-581fe7bc4b0e?source=rss-b862a7388643------2</link>
            <guid isPermaLink="false">https://medium.com/p/581fe7bc4b0e</guid>
            <category><![CDATA[error-handling]]></category>
            <category><![CDATA[rust]]></category>
            <dc:creator><![CDATA[Vennila Pugazhenthi]]></dc:creator>
            <pubDate>Sun, 13 Aug 2023 18:40:45 GMT</pubDate>
            <atom:updated>2023-08-13T18:40:45.739Z</atom:updated>
            <content:encoded><![CDATA[<p>Rust&#39;s question mark (?) operator is used for propagating errors.</p><p>Let’s say there are three functions: func1(), func2(), and func3() where func1() calls func2() and func2() in turn calls func3(). If there’s an error returned from func3() to func2(), func2() can either handle the error or it can return the error to func1().</p><p>Returning the error to the caller function (in this case func1()) is known as the propagation of error and this is where the question mark (?) operator is helpful. Propagation of error is used when the caller function has more context and placing the logic of error handling in the caller function makes sense.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mtDLmFChAjlNe0JcCfU_gg.jpeg" /><figcaption>Propagation of error</figcaption></figure><p>Let’s take a look at a code example. Let’s say you are writing a function to read a username from a file.</p><p>To read a username from a file, we need to do the following:</p><p>1. Open the file</p><p>2. Read the content of the file into a variable</p><p>3. Return the variable</p><p>We also need to think about possible failures or errors in these steps. Opening a file that is not present can throw an error or trying to read a file without having access to it can also throw an error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I175YMLm4FtRn6s7Ym1H7Q.jpeg" /><figcaption>Possible Errors</figcaption></figure><p>Here, we will be propagating any errors, that is to return any errors. Since the function will either return the username of type ‘String’ or return errors, the return type in the function signature will be ‘Result&lt;String, io::Error&gt;’.</p><pre>fn read_username_from_file() -&gt; Result&lt;String, io::Error&gt; {}</pre><p>Note: Result&lt;T, E&gt; is an enum with two variants: Ok(T) and Err(E). Ok(T) represents success and contains a value and Err(E) represents error and contains an error value</p><pre>use std::fs::File;<br>use std::io::{self, Read};<br><br>fn read_username_from_file() -&gt; Result&lt;String, io::Error&gt; {<br>    let username_file_result = File::open(&quot;hello.txt&quot;);<br><br>    let mut username_file = match username_file_result {<br>        Ok(file) =&gt; file,<br>        Err(e) =&gt; return Err(e),<br>    };<br><br>    let mut username = String::new();<br><br>    match username_file.read_to_string(&amp;mut username) {<br>        Ok(_) =&gt; Ok(username),<br>        Err(e) =&gt; Err(e),<br>    }<br>}</pre><p>Opening a file and reading a file to a string can either succeed or fail. So, we use ‘match’ to handle both cases. If encountered an error, it returns the error early out of the function. If it succeeds, it returns Ok(username).</p><p>We can rewrite the same function using the ‘?’ operator.</p><pre>use std::fs::File;<br>use std::io::{self, Read};<br><br>fn read_username_from_file() -&gt; Result&lt;String, io::Error&gt; {<br>    let mut username_file = File::open(&quot;hello.txt&quot;)?;<br>    let mut username = String::new();<br>    username_file.read_to_string(&amp;mut username)?;<br>    Ok(username)<br>}</pre><p>The ‘?’ operator works in a similar way as ‘match’ to handle the ‘Result’ enum.</p><ul><li>If the ‘Result’ has an ‘Ok(T)’ variant, then the ‘?’ operator will return the value inside the ‘Ok’ to the expression and the program will continue.</li><li>If the ‘Result’ has an ‘Err(E)’ variant, then the ‘?’ operator will return the error ‘Err(E)’ to the caller function.</li></ul><p>The ‘?’ operator allows chaining of operation. We can rewrite the same function as:</p><pre>use std::fs::File;<br>use std::io::{self, Read};<br><br>fn read_username_from_file() -&gt; Result&lt;String, io::Error&gt; {<br>    let mut username = String::new();<br><br>    File::open(&quot;hello.txt&quot;)?.read_to_string(&amp;mut username)?;<br><br>    Ok(username)<br>}</pre><p>The functionality is the same. The ‘?’ allows you to write more ergonomic code.</p><p>So far, we have used the ‘?’ operator with the ‘Result&lt;T, E&gt;’ type to propagate errors. Rust also allows you to use the ‘?’ operator with ‘Option&lt;T&gt;’.</p><p>Option&lt;T&gt; is an enum with two variants: ‘Some(T)’ contains a value and ‘None’ doesn’t contain a value. This is useful in cases where the value could be present or not.</p><ul><li>If the ‘Option&lt;T&gt;’ has a ‘Some(T)’ variant, then the ‘?’ operator will return the value inside the ‘Some(T)’ to the expression, and the program will continue.</li><li>If the ‘Option&lt;T&gt;’ has a ‘None’ variant, then the ‘?’ operator will return the ‘None’ to the caller functions.</li></ul><p>Note: The ‘?’ operator can be used on the ‘Result’ type in a function that returns ‘Result’. Likewise, the ‘?’ operator can be used on the ‘Option’ type in a function that returns ‘Option’. However, the ‘?’ operator cannot be mixed with both the ‘Result’ and ‘Option’ types.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=581fe7bc4b0e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rust Ownership — Explained for Beginners]]></title>
            <link>https://medium.com/@vennilapugazhenthi/rust-ownership-explained-for-beginners-de70de16b099?source=rss-b862a7388643------2</link>
            <guid isPermaLink="false">https://medium.com/p/de70de16b099</guid>
            <category><![CDATA[rust-ownership-beginners]]></category>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[ownership]]></category>
            <dc:creator><![CDATA[Vennila Pugazhenthi]]></dc:creator>
            <pubDate>Sun, 30 Jul 2023 01:00:32 GMT</pubDate>
            <atom:updated>2023-07-31T13:41:05.603Z</atom:updated>
            <content:encoded><![CDATA[<h3>Rust Ownership — Explained for Beginners</h3><p>Memory management in Rust is done through a set of rules known as <em>Ownership</em>.</p><h3>Ownership Rules:</h3><ul><li>Each value in Rust has an <em>owner</em>.</li><li>There can only be one owner at a time.</li><li>When the owner goes out of scope, the value will be dropped.</li></ul><h3>Types of Memory:</h3><p>Since Rust is a systems programming language, whether a value is present in a stack or heap affects how the code is written.</p><h3>Stack:</h3><figure><img alt="Value gets pushed into a stack" src="https://cdn-images-1.medium.com/max/1024/1*MnKZvzDsJ3DkOPo3v98kKg.jpeg" /><figcaption>Representation of value getting pushed into a stack</figcaption></figure><p>All data whose size is fixed and known at compile time will be stored in the stack. For example, an integer variable ‘x’ has a value of 5. Its size is fixed and known at compile time, so it gets stored in the stack.</p><h3>Heap:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TkfPkt9raJp7zxmndn2uaA.jpeg" /><figcaption>Representation of data in the heap</figcaption></figure><p>The data whose size is unknown at compile time or if it might change gets stored in the heap. The memory allocator finds a big enough empty spot to store the data and returns a pointer. The pointer contains the memory address of the data.</p><h3>Ownership (Stack):</h3><pre>let x = 5;<br>let y = x;</pre><p>In this example, the data is of integer type. So, the data reside in a stack. When ‘y’ is initialized with ‘x’, the data get <strong>copied</strong> to ‘y’. However, the ownership of ‘x’ doesn’t change, and ‘y’ gets ownership of a new copy of ‘5’.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_5gzpr9_iAi8WZ-6AKCVsA.jpeg" /><figcaption>Representation of stack data getting copied</figcaption></figure><p>Rule: All the scalar data types in Rust have the ‘Copy’ trait implemented, which will have the same behavior as above. Some scalar datatypes with the ‘Copy’ trait are Integer, Boolean, Floating-point, Character, and Tuples if they only contain types that also implement ‘Copy’.</p><h3>Ownership (Heap):</h3><h3>Move:</h3><pre>    let x = String::from(&quot;hello&quot;);<br>    let y = x;</pre><p>In this example, ‘x’ is of String type and it’s stored in the heap. Initializing a variable with another variable with data in the heap will result in data <strong>moving</strong> to a new variable. Here, “hello” will move to ‘y’, and ‘y’ will gain ownership. ‘x’ will lose ownership and we can no longer use it in the subsequent code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iBLC6whMYGPMee3u8SQ5vA.jpeg" /><figcaption>Representation of data move in the heap</figcaption></figure><pre>    let x = String::from(&quot;hello&quot;);<br>    let y = x;<br><br>    println!(&quot;{}, world!&quot;, x); // throws an error</pre><p>If you try to use ‘x’ after it lost its ownership, then the program will give you an error.</p><h3>Clone:</h3><p>If you want to make a copy of the value of ‘x’ without ‘x’ losing its ownership, you can use ‘clone()’. This will make a deep copy of the data, which means both the data in the heap and the pointer will be replicated.</p><pre>    let x = String::from(&quot;hello&quot;);<br>    let y = s1.clone();<br><br>    println!(&quot;x = {}, y = {}&quot;, x, y);</pre><p>Here, both the ‘x’ &amp; ‘y’ will have ownership over their own copy of data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mSceVeapCTlZBOWEigdYcA.jpeg" /><figcaption>Representation of data clone in the heap</figcaption></figure><h3>Ownership in Functions:</h3><p>So far, we went through three concepts:</p><p>1. For data in stack, the value gets <strong>copied</strong> and doesn’t lose it ownership.</p><p>2. For data in the heap, the value gets <strong>moved</strong> and it loses ownership.</p><p>3. For data in heap and ‘clone()’ is used, then the value gets <strong>cloned </strong>and it doesn’t lose its ownership.</p><p>Now, let&#39;s see how these concepts come into action when used in a function.</p><pre>fn main() {<br>  let s = String::from(&quot;hello&quot;);  // s comes into scope<br><br>  takes_ownership(s);             // s&#39;s value moves into the function...<br>                                  // ... and so is no longer valid here<br><br>}<br><br>fn takes_ownership(some_string: String) { // some_string comes into scope<br>  println!(&quot;{}&quot;, some_string);<br>} // Here, some_string goes out of scope and `drop` is called. The backing<br>  // memory is freed.    </pre><p>Here since ‘s’ is of type “String” and is stored in the heap, the value <strong>moves,</strong> and ‘s’ loses its ownership. If ‘s’ was of type Integer, then the value would have been <strong>copied, </strong>and ‘s’ won’t have lost its ownership.</p><h3>References:</h3><p>If you want to use a value without dealing with ownership, you can create a reference.</p><pre>fn main() {<br>    let s1 = String::from(&quot;hello&quot;);<br><br>    let len = calculate_length(&amp;s1);<br><br>    println!(&quot;The length of &#39;{}&#39; is {}.&quot;, s1, len);<br>}<br><br>fn calculate_length(s: &amp;String) -&gt; usize {<br>    s.len()<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oDhuyLrLsgKT_Ep9HJpNrg.jpeg" /><figcaption>Representation of reference of data in the heap</figcaption></figure><p>Here, ‘s’ doesn’t have ownership of “hello” but it can access the value. The process of creating a reference is known as borrowing. By default, references are immutable, and ‘s’ cannot modify the value “hello”.</p><p>Another point to note is that the reference ‘s’ is only valid as long as ‘s1’ is valid. When ‘s1’ goes out of the scope and it loses its ownership, you cannot create a reference for it.</p><h3>Mutable References:</h3><p>If you want to modify the value that the reference is pointing to, then you need to explicitly add ‘mut’ in the variable declaration. With mutable references, you can modify or edit the value without having ownership of it.</p><pre>fn main() {<br>    let mut s = String::from(&quot;hello&quot;);<br><br>    change(&amp;mut s);<br>}<br><br>fn change(some_string: &amp;mut String) {<br>    some_string.push_str(&quot;, world&quot;);<br>}</pre><p>There’s one big restriction with using mutable reference: if you have a mutable reference of a value, then you cannot any other reference in the same scope. This doesn’t mean that you cannot have a mutable reference and an immutable reference of the same value within the program. Just that, it shouldn’t be within the same scope of the program like within the same function. This condition helps to prevent any data races.</p><h3>String Slices:</h3><p>A string slice is a reference that points to part of a String without no ownership of the data.</p><pre>    let s = String::from(&quot;hello world&quot;);<br><br>    let world = &amp;s[6..11];</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FtCjLEQzFINx_7m5a7ckyQ.jpeg" /><figcaption>Representation of string slice</figcaption></figure><p>The String Slice is of type ‘&amp;str’ which is different from ‘&amp;String’. The difference is ‘&amp;str’ is a reference to part of the String while ‘&amp;String’ is a reference to the whole value of the String.</p><p>As string slice points to a part of the string data, it’s an immutable reference and it can never be a mutable reference.</p><p>String slice is specific to String datatype, but you can have a slice of other collections. For example, below is a slice of an integer array of type ‘&amp;[i32]’</p><pre>let a = [1, 2, 3, 4, 5];<br><br>let slice = &amp;a[1..3];</pre><h3>Conclusion:</h3><p>Understanding how ownership works differently depending on whether data is on stack or heap and understanding how references work is crucial for writing code in Rust. Hope this article gave you a foundation for understanding how memory works in Rust.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de70de16b099" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>