<?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 amol khatri on Medium]]></title>
        <description><![CDATA[Stories by amol khatri on Medium]]></description>
        <link>https://medium.com/@khatri_amol?source=rss-1807cdf00ca8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*zPZuT2IZSJTpzwKc.jpeg</url>
            <title>Stories by amol khatri on Medium</title>
            <link>https://medium.com/@khatri_amol?source=rss-1807cdf00ca8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 23:04:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@khatri_amol/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[Handling Absence Gracefully: A Dive into Rust’s Option Type]]></title>
            <link>https://medium.com/@khatri_amol/handling-absence-gracefully-a-dive-into-rusts-option-type-4a2f535b684e?source=rss-1807cdf00ca8------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a2f535b684e</guid>
            <dc:creator><![CDATA[amol khatri]]></dc:creator>
            <pubDate>Sun, 15 Oct 2023 09:39:51 GMT</pubDate>
            <atom:updated>2023-10-15T09:42:49.825Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zQqH1rDZcSn8pnbq6tjkyw.png" /></figure><p>At times, when coding, you might encounter situations where a function’s return value is uncertain. Take, for instance, a basic division function that may or may not produce a result.</p><pre>fn div(a: i32, b: i32) -&gt; Option&lt;f32&gt; {<br>    if b == 0 {<br>        None<br>    }else{<br>        let result = (a  as f32) / (b as f32);<br>        Some(result)<br>    }<br>    <br>}</pre><p>In essence, a `div` function might return a result or might not. If the divisor is zero, it yields `None`; otherwise, the outcome is wrapped in `Some`. This approach uses the `None` variant to signify the absence of a value. <br>Given that the return value of the `div` function is encapsulated within the `Option` construct, special handling is required to retrieve the actual value from it.”</p><p>Here are some ways to do that:</p><ol><li>Using <strong><em>match:</em></strong></li></ol><p>You can use match expression to handle gracefully existence and non existence of value</p><pre>let num1 = 10;<br>let num2 = 5;<br>let mut result:f32 = 0.0;<br>match div(num1, num2) {<br>    Some(value) =&gt; result = value,<br>    None =&gt; result = 0.0<br>};</pre><p>2. Using <strong><em>unwrap</em></strong></p><p>If you are certain about the return value of the function, you can use unwrap function.</p><pre>  let num1 = 10;<br>  let num2 = 5;<br>  let result:f32 = div(num1, num2).unwrap();<br>  println!(&quot;Result is : {} &quot;, result)</pre><blockquote>3. Using <strong>unwrap_or</strong>:</blockquote><p>If you want to provide a default value in case the Option is None, you can use the unwrap_or method:</p><pre>let num1 = 10;<br>let num2 = 0;<br>let result:f32 = div(num1, num2).unwrap_or(0.0);<br>println!(&quot;Result is : {} &quot;, result)</pre><p>4. Using<strong><em> if let</em></strong></p><p>The if let syntax enables you to extract values from the Some variant and execute a specific block of code. It can also be employed to gracefully handle the None variant.</p><pre>let num1 = 10;<br>let num2 = 5;<br>let result = div(num1, num2);<br><br>if let Some(value) = result {<br>    println!(&quot;Result is : {}&quot;, value);<br>}else{<br>    print!(&quot;Result is : {}&quot;, 0); // None<br>}</pre><p>I hope this helps.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a2f535b684e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Array vs Vector: Rust Iteration]]></title>
            <link>https://medium.com/@khatri_amol/array-vs-vector-rust-iteration-ce96feb76905?source=rss-1807cdf00ca8------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce96feb76905</guid>
            <dc:creator><![CDATA[amol khatri]]></dc:creator>
            <pubDate>Tue, 13 Jun 2023 11:39:37 GMT</pubDate>
            <atom:updated>2023-06-13T11:39:37.773Z</atom:updated>
            <content:encoded><![CDATA[<p>Rust has this concept called ownership and borrowing. It can be confusing, especially if you’re familiar with languages like Javascript.</p><p><strong>Discovering the Difference: Iterating over Array vs. Vector in Rust.</strong></p><p>In the code snippet below, we’re exploring the process of iterating over an array. Since the array implements the copy trait, its values are copied to ‘i’ during iteration. This allows us to access array ‘a’ even after the loop..</p><pre>let a = [1,2,3,4,5]; // implements copy trait<br><br>for i in a { // ownership of a is not transferred here because fixed size array implements copy trait<br>  println!(&quot;{}&quot;, i); <br>}<br><br>println!(&quot;{:?}&quot;, a); // [1,2,3,4,5]</pre><p>Alright, let’s dive into what occurs when we iterate over a vector. In the code snippet below, we’re looping through vector ‘v’. However, since the vector doesn’t implement the copy trait, its value is fully moved during the loop. Consequently, we can’t access vector ‘v’ once the loop is done.</p><pre>let v = vec![1,2,3,4,5];<br><br>for i in v { // This will move the ownership of the vector<br>   println!(&quot;Element is {}&quot;, i);<br>}<br><br>println!(&quot;{:?}&quot;, v) // `v` moved due to this implicit call to `.into_iter()`<br>This was quite confusing for me when I was learning Rust, no doubt about it.<br></pre><p>This was quite confusing for me when I was learning Rust, no doubt about it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce96feb76905" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>