<?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: johnnylarner</title>
    <description>The latest articles on DEV Community by johnnylarner (@johnnylarner).</description>
    <link>https://dev.to/johnnylarner</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%2F1010293%2Fa31f822e-2d52-4cd9-9e35-6863add6187f.png</url>
      <title>DEV Community: johnnylarner</title>
      <link>https://dev.to/johnnylarner</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/johnnylarner"/>
    <language>en</language>
    <item>
      <title>Thirty Days Of Agents: Motivation</title>
      <dc:creator>johnnylarner</dc:creator>
      <pubDate>Mon, 23 Mar 2026 21:10:07 +0000</pubDate>
      <link>https://dev.to/johnnylarner/thirty-days-of-agents-motivation-4679</link>
      <guid>https://dev.to/johnnylarner/thirty-days-of-agents-motivation-4679</guid>
      <description>&lt;h1&gt;
  
  
  Thirty days of agents: motivation
&lt;/h1&gt;

&lt;p&gt;Anyone who loves the beach must at some point in their life visit Rio de Janeiro. I have no doubt that Ipanema beach has something you want. Be it sun, surfable waves or refrigerated beverages. And let's not forget about the phat view. As the sun sets behind the Vidigal favela, you can take a moment to reflect on the day that has now passed. Your mind slips into a dream-like state and you feel grateful and warm until suddenly you are ripped out of lucidity by the fresh yet sterile glare of the beach floodlights. &lt;/p&gt;

&lt;p&gt;This transition of light - from the sun's natural rays to artificial, mechanical light - turned out to be a fitting metaphor for the theme of this blog series. After all, we software engineers are witnessing in real time how artisinal hand-written code is being replaced by probabilistic streams of tokens.&lt;/p&gt;

&lt;p&gt;When I found myself jogging under these bright lights in Rio a few weeks back I was listening to a podcast about Openclaw, the agentic development framework that has taken the tech world by storm. It was quite motivating to hear how the project's author Peter Steinberger had rediscovered his passion for coding whilst developing Openclaw as I too had been struggling to see the beauty in work I was doing. The last time I found myself in this situation was back in 2023 not long after the release of ChatGPT (wow - those years flew by!). Back then I was working as a data engineer in my first proper coding job since teaching myself to code during the covid pandemic. The antidote to my growing disaffection for my first language python was the discovery of my second language - rust. Having attended Pycon that year in Berlin I noticed that rust was becoming the language of choice for anyone who wanted to rewrite python libraries to make them faster and/or functionality better. One prime example was the evolution of the polars (rs for rust, you dummy) library which was starting to challenge the data wrangling hegemony that pandas had over python devs and data scientists. Back then it felt as if the future was rust and it was rewarding to find a way to ride that wave.&lt;/p&gt;

&lt;p&gt;Agents and frameworks to manage them represent a fundamental rewrite of that future I had imagined. One the one hand, they represent a "democratization" (to borrow the vernacular of tech leaders in the 2010s) of access to software. Even bros in Berghain will tell you how they developed an app with Claude. For experienced developers the offer huge productivity gains and bootstrapping companies has never been easier. But importantly they also pose a risk to the industry of software at large:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Large tech companies will be able to maintain monopolies with fewer engineers&lt;/li&gt;
&lt;li&gt;Engineers who aren't willing to embrace significant change will get left behind&lt;/li&gt;
&lt;li&gt;Private equity firms with big bets on pre-agentic enterprise software fear loss of market share (white collar jobs replaced by agents)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And so in many ways it is a bittersweet moment for me, accepting that I must and therefore &lt;em&gt;ought to want to change&lt;/em&gt;. I believe passion, interest and knowledge are not virtues born out of nothing. You have to work hard to cultivate them and that's why I want to share this journey with the wilder world. Our first stop is going to be getting Openclaw up and running. Watch this space.&lt;/p&gt;

</description>
      <category>software</category>
      <category>ai</category>
    </item>
    <item>
      <title>Dummy Software Post 1</title>
      <dc:creator>johnnylarner</dc:creator>
      <pubDate>Mon, 09 Mar 2026 19:35:36 +0000</pubDate>
      <link>https://dev.to/johnnylarner/dummy-software-post-1-3c92</link>
      <guid>https://dev.to/johnnylarner/dummy-software-post-1-3c92</guid>
      <description>&lt;p&gt;This is a dummy software post for manual UI testing.&lt;/p&gt;

</description>
      <category>software</category>
    </item>
    <item>
      <title>Dummy Software Post 3</title>
      <dc:creator>johnnylarner</dc:creator>
      <pubDate>Mon, 09 Mar 2026 19:35:35 +0000</pubDate>
      <link>https://dev.to/johnnylarner/dummy-software-post-3-2i0j</link>
      <guid>https://dev.to/johnnylarner/dummy-software-post-3-2i0j</guid>
      <description>&lt;p&gt;This is a third dummy software post for manual UI and publishing workflow testing.&lt;/p&gt;

</description>
      <category>software</category>
    </item>
    <item>
      <title>Dummy CI Publish Test</title>
      <dc:creator>johnnylarner</dc:creator>
      <pubDate>Mon, 09 Mar 2026 19:35:06 +0000</pubDate>
      <link>https://dev.to/johnnylarner/dummy-ci-publish-test-4734</link>
      <guid>https://dev.to/johnnylarner/dummy-ci-publish-test-4734</guid>
      <description>&lt;p&gt;This is a dummy article used to validate the publishing pipeline.&lt;/p&gt;

&lt;p&gt;If this post appears on both GitHub Pages and dev.to after merge, the workflow is working.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>ci</category>
    </item>
    <item>
      <title>Dummy CI Publish Test 2</title>
      <dc:creator>johnnylarner</dc:creator>
      <pubDate>Mon, 09 Mar 2026 19:35:05 +0000</pubDate>
      <link>https://dev.to/johnnylarner/dummy-ci-publish-test-2-1ndb</link>
      <guid>https://dev.to/johnnylarner/dummy-ci-publish-test-2-1ndb</guid>
      <description>&lt;p&gt;This is a second dummy article for validating publishing automation.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>ci</category>
    </item>
    <item>
      <title>How much do you nitpick other people's code?</title>
      <dc:creator>johnnylarner</dc:creator>
      <pubDate>Thu, 28 Sep 2023 19:24:16 +0000</pubDate>
      <link>https://dev.to/johnnylarner/how-much-do-you-nitpick-other-peoples-code-1ihn</link>
      <guid>https://dev.to/johnnylarner/how-much-do-you-nitpick-other-peoples-code-1ihn</guid>
      <description>&lt;p&gt;I recently watched a &lt;a href="https://www.youtube.com/watch?v=08NlhU4gzdY&amp;amp;pp=ygUVcHJpbWUgcmVhY3RzIG5pdCBwaWNr"&gt;video&lt;/a&gt; posted by Prime Reacts based on &lt;a href="https://blog.danlew.net/2021/02/23/stop-nitpicking-in-code-reviews/"&gt;this blog&lt;/a&gt; by Dan Lew on why nitpicking colleagues' code is bad. For those not familiar with the term, Dan Lew defines it as flagging small, insignificant problems in a code review that are not wrong but &lt;em&gt;suboptimal&lt;/em&gt;. He also argues that fixing the "problems" will make you, the grouchy reviewer, feel smug but won't benefit your code base or your team's opinion of you.&lt;/p&gt;

&lt;p&gt;Watching this video made me feel a petty criminal listening to a convict showing genuine remorse for a crime that I too had committed. Fortunately for me, I haven't been coding long enough to piss off hundreds of colleagues yet. But to those of you whose code I nitpicked - I apologise.&lt;/p&gt;

&lt;p&gt;Upon reflection I realised that my previous career paths before software engineering had baked into me a culture of nitpicking. First as an editor, my task was to locate and remove language inconsistencies as well as create a coherent tone across an article. Then, as a compliance analyst I was tasked with nitpicking colleagues' work for typos, document dates and other trivialities that make you question the point of life.&lt;/p&gt;

&lt;p&gt;But from here on out I'm a changed man. For the reasons clearly laid out in Dan's blog, I am turning a new leaf and accepting that code perfection does not make code better!&lt;/p&gt;

&lt;h2&gt;
  
  
  Now it's your turn
&lt;/h2&gt;

&lt;p&gt;The reason you probably opened this blog was because you sought an answer to the question in the title. I've collected 4 code snippets and for each of them provided one or multiple issues. For relative consistency, there are no possible other issues (though please leave a comment if you want). Select things you would seriously considering leaving as feedback in a code review. At the end the blog I'll leave my two scores: my old self versus my new self. See how you fare.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quiz
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Question 1
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_features&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trip_df&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zone_df&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s"&gt;"""Returns a pandas DataFrame containing
    all the features derived from NYC
    Taxi dataset to answer all business questions.
    """&lt;/span&gt;
    &lt;span class="c1"&gt;# Rename columns
&lt;/span&gt;    &lt;span class="n"&gt;trip_df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rename_columns_as_lowercase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trip_df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;zone_df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rename_columns_as_lowercase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;zone_df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;trip_df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;update_payment_type_as_string_values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trip_df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Add features
&lt;/span&gt;    &lt;span class="n"&gt;trip_df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;add_borough_and_zone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trip_df&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zone_df&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"pulocationid"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;trip_df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;add_borough_and_zone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trip_df&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zone_df&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"dolocationid"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;trip_df&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Possible issues
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Duplicated function calls can be refactored into loops&lt;/li&gt;
&lt;li&gt;Comments are redundant or misleading&lt;/li&gt;
&lt;li&gt;Function name is not descriptive&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Question 2
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;build_payload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;train_type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;train_age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;train_length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;train_is_diesel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;

    &lt;span class="n"&gt;input_parameters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="n"&gt;train_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;train_age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;train_length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;train_is_diesel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="n"&gt;payload_names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s"&gt;"train_type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"train_age"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"train_length"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"train_is_diesel"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="c1"&gt;# Set payload defaults
&lt;/span&gt;    &lt;span class="n"&gt;current_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"pipeline"&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p_name&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload_names&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;p_name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"train_is_diesel"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;train_is_diesel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"engine_type"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"diesel"&lt;/span&gt;

        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;p_name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;input_parameters&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Possible issues
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt; Declare default values as global values for transparency&lt;/li&gt;
&lt;li&gt; Zipping is a more intuitive way to iterate over the lists, so replace &lt;code&gt;enumerate&lt;/code&gt; with &lt;code&gt;zip&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt; Refactoring the boolean function argument would make the payload mapping static&lt;/li&gt;
&lt;li&gt; Current time makes the function indeterministic, it should be parameterised&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Question 3
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;write_df&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Select columns
&lt;/span&gt;    &lt;span class="n"&gt;small_df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"age"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"dob"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"gender"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Cache df
&lt;/span&gt;    &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"full_data.csv"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;small_df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"subset_data.csv"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Possible issues
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;This function should do something generic but lack of parameterisation makes it not possible to reuse&lt;/li&gt;
&lt;li&gt; Small df isn't an expressive name&lt;/li&gt;
&lt;li&gt;Comments don't give us useful information&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Question 4
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DataRowExtractor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;extract_rows&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="n"&gt;extracted_rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iterrows&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="n"&gt;extracted_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;extracted_rows&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;csv_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/working_dir"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"table_data.csv"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;write_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/working_dir"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"row_data.json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;csv_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;data_extractor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DataRowExtractor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;row_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data_extractor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extract_rows&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;write_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;json_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;row_data&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
        &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row_data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Possible issues
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt; OS Path module is verbose and should be replaced with the new pathlib Path API&lt;/li&gt;
&lt;li&gt;The RowExtractor doesn't add any real abstraction and overcomplicates the code&lt;/li&gt;
&lt;li&gt;Pandas has a built in method to create records from a dataframe&lt;/li&gt;
&lt;li&gt;Data is not a descriptive name&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Have I really changed?
&lt;/h2&gt;

&lt;p&gt;As promised, I'd give you my scores:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;question&lt;/th&gt;
&lt;th&gt;old_me&lt;/th&gt;
&lt;th&gt;new_me&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Total&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Now obviously I rigged the results to make my improvement look and feel real. Jokes aside, time (and colleagues) will tell whether I really improve at not nitpicking!&lt;/p&gt;

&lt;p&gt;One instructive aspect I tried to get across in the examples is that - perhaps in a production environment - the bugs in question 2 and 4 (good work if you found them) may have gone unseen by a reviewer focussed on scrutinising irrelevant banalities.&lt;/p&gt;

&lt;p&gt;This is particularly relevant for question two: if we focus on reviewing the design of the function as a whole, we see that by replacing a boolean argument with a string, we can simply return a dictionary or maybe even just drop the function all together. Simple design is less bug prone, and this case we'd avoid the bug in our nested if statement. &lt;/p&gt;

&lt;p&gt;Now we can see concretely why nitpicking is dangerous. But what if (like me) - despite having only picked out critical issues in a code review - you still found a bunch of nits want to pick ( I'm thinking here about code simplification through better library knowledge i.e. with &lt;code&gt;extract_rows&lt;/code&gt;) ? I spoke to a family friend with 25&amp;gt; years experience coding about this and he suggested offering your nits in a non-committal to your colleague (I found nits, if &lt;em&gt;you want&lt;/em&gt; to discuss them, we can). This makes the feedback loop something your colleague controls and could turn nits into valuable insights.&lt;/p&gt;

</description>
      <category>devjournal</category>
      <category>career</category>
    </item>
    <item>
      <title>30 Days of Rust - Day 30</title>
      <dc:creator>johnnylarner</dc:creator>
      <pubDate>Wed, 05 Jul 2023 06:00:00 +0000</pubDate>
      <link>https://dev.to/johnnylarner/30-days-of-rust-day-30-4dan</link>
      <guid>https://dev.to/johnnylarner/30-days-of-rust-day-30-4dan</guid>
      <description>&lt;p&gt;This is it, the final blog. It's long and arduous, so take some time to read it. Don't worry, I'll still be posting blogs, but probably not on the features of the Rust language - I think we all need a break from that for now.&lt;/p&gt;

&lt;h2&gt;
  
  
  Yesterday's questions answered
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;No questions to answer&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Today's open questions
&lt;/h2&gt;

&lt;p&gt;*No open questions&lt;/p&gt;

&lt;h2&gt;
  
  
  Associated types vs generics
&lt;/h2&gt;

&lt;p&gt;We've already looked at how generics are a way to improve reusability of functions or traits in Rust. Associated types offer a different approach to reusability. There may be instances where a certain trait need only be implemented once for a given type. This is when an associate type definition on a trait may be more useful. Consider this example from the Rust book:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;ops&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Debug,&lt;/span&gt; &lt;span class="nd"&gt;Copy,&lt;/span&gt; &lt;span class="nd"&gt;Clone,&lt;/span&gt; &lt;span class="nd"&gt;PartialEq)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nb"&gt;Add&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="py"&gt;.x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="py"&gt;.y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note how in the implementation of &lt;code&gt;Add&lt;/code&gt; we declare the type &lt;code&gt;Output&lt;/code&gt;. What's we're actually doing is defining the associate type of the &lt;code&gt;Add&lt;/code&gt; trait. Without this, our code won't compile. The associate type serves as a contract in this function. As per the trait definition, our associated type serves as the return type of our  &lt;code&gt;add&lt;/code&gt; function. Here's the trait declaration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nb"&gt;Add&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Rhs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;Self&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Rhs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's interesting about the &lt;code&gt;Add&lt;/code&gt; trait is that it also has &lt;em&gt;default&lt;/em&gt; associate type. This is represented by the &lt;code&gt;Rhs&lt;/code&gt; alias input argument.&lt;/p&gt;

&lt;p&gt;Default types in this context provide flexibility to library authors: often most users of a library will use a trait in one way. This typical usage pattern can be expressed through the default type of a trait. By making this a parameter, however, you can give power users the chance to use the library in a way you may not have foreseen.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fully qualified syntax for duplicated method and associated functions
&lt;/h2&gt;

&lt;p&gt;Rust, like Python, provides no limitations on defining multiple methods of the same name on an object. That's probably because no programming language intended on its users to do that (except when overloading methods, of course).&lt;/p&gt;

&lt;p&gt;In Rust, however, there is actually a relatively common situation where a &lt;code&gt;struct&lt;/code&gt; may have multiple methods or associated functions of the same name. When implementing a &lt;code&gt;trait&lt;/code&gt; from an external library, we may be forced to use a method name that we have already defined.&lt;/p&gt;

&lt;h3&gt;
  
  
  Syntax for method calls
&lt;/h3&gt;

&lt;p&gt;Remember back to when we talked about multithreading, we briefly talked about the &lt;code&gt;Send&lt;/code&gt; (and &lt;code&gt;Sync&lt;/code&gt;) trait. Imagine you want code that implements not only &lt;code&gt;Send&lt;/code&gt; from the standard library, but also from a third party library that instantiates threads in a different way. Suddenly you have two versions of the &lt;code&gt;send&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;When calling these methods, we have to use fully qualified syntax. This consists of the &lt;code&gt;trait&lt;/code&gt; and the &lt;code&gt;method name&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;thread_struct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;MyCustomThread&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nn"&gt;StdLibrarySendTrait&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;thread_struct&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nn"&gt;ExternalLibrarySendTrait&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;thread_struct&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Syntax for associated functions
&lt;/h3&gt;

&lt;p&gt;As a &lt;code&gt;struct&lt;/code&gt; method contains a reference to the &lt;code&gt;struct&lt;/code&gt; as its first argument, when we call the method using fully qualified syntax, Rust knows which implementation block is being called as it knows the type of &lt;code&gt;self&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;ExternalLibrarySendTrait&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;MyCustomThread&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Do something&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;ExternalLibrarySendTrait&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;SomeOtherType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Do something&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nn"&gt;ExternalLibrarySendTrait&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;thread_struct&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is different for associate functions which don't take &lt;code&gt;self&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;baby_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;baby_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Spot"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;baby_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"puppy"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Cat&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;baby_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"kitten"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="nn"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;baby_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// No problems&lt;/span&gt;
&lt;span class="nn"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;baby_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Cat or Dog?&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;You can see here how the fully qualified syntax for methods isn't enough here (also, the code won't compile). That's actually because we didn't use &lt;em&gt;fully&lt;/em&gt; fully qualified syntax. Before we could skip the type aliasing because it's implicit in the &lt;code&gt;&amp;amp;self&lt;/code&gt; argument. Here is how we use fully qualified syntax which will get the above code to compile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;baby_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// A puppy&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Cat&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;baby_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// A kitten&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could use this syntax for methods, too, but it's overly verbose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Supertraits: declaring trait dependencies
&lt;/h2&gt;

&lt;p&gt;When defining a &lt;code&gt;trait&lt;/code&gt; we can declare an optional &lt;code&gt;supertrait&lt;/code&gt; which must be implemented if that &lt;code&gt;trait&lt;/code&gt; is to work as expected. Using a &lt;code&gt;struct&lt;/code&gt; that does not implement the &lt;code&gt;supertrait&lt;/code&gt; will cause you code to not compile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;PrettyPrint&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Display&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Do something&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code declares a &lt;code&gt;trait&lt;/code&gt; that can only be implemented on &lt;code&gt;structs&lt;/code&gt; that implement &lt;code&gt;Display&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Supertraits can help reduce code duplication at the cost of increasing code coupling and complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Type wizardry
&lt;/h2&gt;

&lt;p&gt;Until now we've barely touched on the &lt;code&gt;type&lt;/code&gt; keyword. This keyword is used to declare type aliases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Kilometer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's important to understand is that &lt;code&gt;Kilometer&lt;/code&gt; is still an &lt;code&gt;i32&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Kilometer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;asserteq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Often we see the &lt;em&gt;newtype&lt;/em&gt; pattern in Rust. This is popular because of Rust's &lt;code&gt;trait&lt;/code&gt; implementation rule:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A trait can only be implemented for a type if the trait &lt;em&gt;or&lt;/em&gt; the type is local to your code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can implement the above code using the new type pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;Kilometer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Kilometer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;asserteq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code won't compile, because the &lt;code&gt;Kilometer&lt;/code&gt; type doesn't implement the &lt;code&gt;Add&lt;/code&gt; trait. &lt;code&gt;i32&lt;/code&gt; &lt;em&gt;does&lt;/em&gt; implement the trait, which is is proof that the &lt;code&gt;type&lt;/code&gt; alias really is an alias around &lt;code&gt;i32&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to alias and newtype?
&lt;/h3&gt;

&lt;p&gt;Aliasing makes sense when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have a long type (nested Result or Options, for example) that can be shortened&lt;/li&gt;
&lt;li&gt;A composite type where one part is always the same but the other should be treated as a generic&lt;/li&gt;
&lt;li&gt;Giving a meaningful name to an existing type (like &lt;code&gt;i32&lt;/code&gt;) can make your code easier to read.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Newtyping&lt;/em&gt; makes sense when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need to implement external traits&lt;/li&gt;
&lt;li&gt;You want to use a public API to control access to internal code&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The type you never knew about
&lt;/h3&gt;

&lt;p&gt;Functions that return  &lt;code&gt;!&lt;/code&gt; return the so-called &lt;em&gt;never&lt;/em&gt; type. The &lt;em&gt;never&lt;/em&gt; type is a Rust alias for the &lt;em&gt;empty&lt;/em&gt; type. The point of a never type is that it expresses a function that never &lt;em&gt;returns&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Functions that do this yield a result that can be coerced into any other type. This is useful when using &lt;code&gt;match&lt;/code&gt; statements, as these must always return a single type. Keywords and macros such as &lt;code&gt;continue&lt;/code&gt;, &lt;code&gt;panic!&lt;/code&gt; and &lt;code&gt;loop&lt;/code&gt; return the never type.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sized types
&lt;/h3&gt;

&lt;p&gt;A short note on sized types. Generic functions by default can only take &lt;em&gt;sized&lt;/em&gt; types. A type is &lt;em&gt;sized&lt;/em&gt; when its size is known at compile time. Sized types by default implement the &lt;code&gt;Sized&lt;/code&gt; trait.&lt;/p&gt;

&lt;p&gt;If you want to use a dynamically sized type with generics you need to use the &lt;code&gt;?Trait&lt;/code&gt; notation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;generic&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="nb"&gt;Sized&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we also expect to receive a reference to the dynamically sized type as these are also stored on the heap behind a pointer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Passing and return functions
&lt;/h2&gt;

&lt;p&gt;Functions can easily passed to functions using the &lt;code&gt;fn&lt;/code&gt; type. This is different from the &lt;code&gt;trait&lt;/code&gt; restraints used to indicate that a &lt;code&gt;closure&lt;/code&gt; should be passed as an argument. As something of type &lt;code&gt;fn&lt;/code&gt; implements all three &lt;code&gt;Fn&lt;/code&gt; traits, you can always pass functions where closures are accepted.&lt;/p&gt;

&lt;p&gt;If you want to return a &lt;code&gt;closure&lt;/code&gt; from a function, you have to wrap it in a &lt;code&gt;Box&lt;/code&gt; as a &lt;code&gt;closure&lt;/code&gt;'s size is determined at runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Last but not least: Macros
&lt;/h2&gt;

&lt;p&gt;The most advanced Rust feature is the &lt;code&gt;macro&lt;/code&gt;. These are functions that use Rust code to generate more Rust code. This pattern is generally known as &lt;em&gt;metaprogramming&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Rust has different kinds of macros. This have different implementation detail, and each type is suited to a different use case.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;macro_rules!&lt;/code&gt; macros
&lt;/h3&gt;

&lt;p&gt;The most common type of macro in Rust is the declarative macro. This is created by using the &lt;code&gt;macro_rules!&lt;/code&gt; macro. In a declarative macro, you can match Rust source code to patterns and generate different kind of code based on which kind of pattern is matched. This makes it possible for these kind of macros to take an undefined number of arguments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Procedural macros
&lt;/h3&gt;

&lt;p&gt;The remaining three types of macros are procedural. These macros must take Rust source code as an input and return it.&lt;/p&gt;

&lt;p&gt;The first of these is the &lt;em&gt;derive&lt;/em&gt; macro. To create a derive macro, you need to derive from the &lt;code&gt;proc_macro_derive&lt;/code&gt; macro of the standard library.  Derive macros &lt;strong&gt;only work&lt;/strong&gt; &lt;br&gt;
on &lt;code&gt;structs&lt;/code&gt; and &lt;code&gt;enums&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Derive macros have a number of restrictions around packaging. When developing such macros, developers are encouraged to include them as a dependency of the main library being developed. Then the macro can simply be reexported as part of the main library for users to import.&lt;/p&gt;

&lt;p&gt;Attribute macros are similar to derive macros, only that they are more flexible as they can be used for functions and other data types as well. They derive from the &lt;code&gt;proc_macro_attribute&lt;/code&gt; macro.&lt;/p&gt;

&lt;p&gt;Finally, you have function-like macros. Though procedural in nature, these macros can be used inline like a &lt;code&gt;macro_rules!&lt;/code&gt; macro and can take an unspecified number of arguments. They derive from &lt;code&gt;proc_macro&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>devjournal</category>
      <category>learning</category>
    </item>
    <item>
      <title>30 Days of Rust - Day 29</title>
      <dc:creator>johnnylarner</dc:creator>
      <pubDate>Tue, 04 Jul 2023 18:47:02 +0000</pubDate>
      <link>https://dev.to/johnnylarner/30-days-of-rust-day-29-1cb7</link>
      <guid>https://dev.to/johnnylarner/30-days-of-rust-day-29-1cb7</guid>
      <description>&lt;p&gt;Good evening folks,&lt;br&gt;
We're nearly there. Two posts left. I've got a final kick of energy to push through. Today we'll be zooming over some of Rust's object oriented, pattern matching and unsafe features.&lt;/p&gt;
&lt;h2&gt;
  
  
  Yesterday's questions answered
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;No questions to answer&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Today's open questions
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;No open questions&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Is Rust object-oriented?
&lt;/h2&gt;

&lt;p&gt;There are many ways to define whether a programming language is object-oriented or not. When it comes to Rust, a useful way to think about this is to ask yourself: what are OO languages designed to achieve and how do they do this?&lt;/p&gt;
&lt;h3&gt;
  
  
  Encapsulating logic
&lt;/h3&gt;

&lt;p&gt;Objects allow you to separate and encapsulate different parts of your code. Rust's features definitely support encapsulation. &lt;code&gt;Structs&lt;/code&gt; and &lt;code&gt;impl&lt;/code&gt; blocks allow programmers to write distinct code that is separated from other bits of code ✅&lt;/p&gt;
&lt;h3&gt;
  
  
  Objects as data storage
&lt;/h3&gt;

&lt;p&gt;Often object-oriented languages express human concepts through grouping data in objects. In Rust &lt;code&gt;enums&lt;/code&gt; and &lt;code&gt;structs&lt;/code&gt; are also a great way to group conceptually similar data in one place. ✅&lt;/p&gt;
&lt;h3&gt;
  
  
  Reusing and types through inheritance
&lt;/h3&gt;

&lt;p&gt;The most obvious missing aspect so far for new Rust developers is the absence of inheritance. In many programming languages, objects expressed as subclasses are able to inherit from parent classes. Inheritance can:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reduce code duplication by defining common code at the parent level&lt;/li&gt;
&lt;li&gt;Increase the breadth of composability as subclasses share their parent's type.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We know there are no classes in Rust, and that &lt;code&gt;structs&lt;/code&gt; cannot inherit from each other. But this doesn't mean Rust doesn't have mechanisms to tackle the issues noted above.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;trait&lt;/code&gt; objects provide a way of declaring default behaviour. If a &lt;code&gt;trait&lt;/code&gt; implements a method and a &lt;code&gt;struct&lt;/code&gt; implements that &lt;code&gt;trait&lt;/code&gt; , the &lt;code&gt;struct&lt;/code&gt; will take on the default implementation. &lt;/p&gt;

&lt;p&gt;What's more &lt;code&gt;traits&lt;/code&gt; also grant programmers access to typed interfaces across different structs. If a function or struct field has a &lt;code&gt;trait&lt;/code&gt; object as its type definition, it will accept any &lt;code&gt;struct&lt;/code&gt; that implements that trait. &lt;code&gt;trait&lt;/code&gt; objects incur a small runtime penalty. As we don't know at compile time what all the types could be (imagine you're writing a library that would allow users to make their own components), &lt;code&gt;trait&lt;/code&gt; objects are stored on the heap. This means we need to access them via a pointer at runtime to find the method we want to call. This is known as &lt;em&gt;dynamic dispatch&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Match, match, match
&lt;/h2&gt;

&lt;p&gt;When learning Rust, &lt;code&gt;match&lt;/code&gt; statements are one of the first novel features you come across. What's particularly cool about &lt;code&gt;match&lt;/code&gt; is that the compiler will often force you to exhaustively match all possible outcomes. This catches bugs before they even have a chance to compile.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;match&lt;/code&gt; arms require so-called &lt;em&gt;refutable&lt;/em&gt; patterns, patterns that can match or not match. &lt;em&gt;Irrefutable patterns&lt;/em&gt; are most commonly seen in variable assignment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Nested matches
&lt;/h3&gt;

&lt;p&gt;If you're working with nested &lt;code&gt;enums&lt;/code&gt; or &lt;code&gt;structs&lt;/code&gt;, you can use &lt;code&gt;match&lt;/code&gt; patterns beyond the root level of the data structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;MarriageStatus&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Single&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;MarriedTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;Divorced&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;TaxProfile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;marriage_status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MarriageStatus&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;profile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TaxProfile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Max Mustermann"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;marriage_status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;MarriageStatus&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;MarriedTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Jane Doe"&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;profile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;TaxProfile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;marriage_status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;MarriageStatus&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;MarriedTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spouse&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="o"&gt;..&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Congrats, you and {spouse} get a tax break."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Only married people are worthy of tax breaks!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we want to print a different message based on whether a given tax profile gets a tax break or not. This code demonstrates several more advanced features of the &lt;code&gt;match&lt;/code&gt; system:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;struct&lt;/code&gt; matching requires you to temporarily instantiate a &lt;code&gt;struct&lt;/code&gt; and compare the relevant fields&lt;/li&gt;
&lt;li&gt;Irrelevant fields can be ignored using the range operator &lt;code&gt;..&lt;/code&gt;. This is useful when we have more than one other field we want to ignore.&lt;/li&gt;
&lt;li&gt;We can also match based on the inner field's value. This can also be used in the result of the match arm.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Match guards and bindings
&lt;/h3&gt;

&lt;p&gt;In the above example the first arm of the &lt;code&gt;match&lt;/code&gt; statement matched to an enum. If we wanted to only match if your spouse has a specific name, we'd also have to introduce a match guard:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;profile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;TaxProfile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;marriage_status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;MarriageStatus&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;MarriedTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spouse&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="o"&gt;..&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;spouse&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"Angela Merkel"&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Congrats, you and Angie get a tax break."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Only married people are worthy of tax breaks!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, only the spouse of Angela Merkel would get a tax break. What if we wanted to give tax breaks to old people too? We could specify a range for the match arm and use a binding to extract the result. We can print this to the screen to give the user a personalised response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;profile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;TaxProfile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;marriage_status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;MarriageStatus&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;MarriedTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spouse&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="o"&gt;..&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;spouse&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"Angela Merkel"&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Congrats, you and Angie get a tax break."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;TaxProfile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;boomer_age&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="o"&gt;..=&lt;/span&gt;&lt;span class="mi"&gt;79&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="o"&gt;..&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Even at age {boomer_age} you matter to society, have a tax break."&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sorry, no tax break for you."&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is unsafe code?
&lt;/h2&gt;

&lt;p&gt;Until now in these blog posts we've only talked about safe Rust code. Safe Rust code is effectively any code that meets the Rust compiler's ownership, borrowing and type rules. We call the code safe because we know that this code will not suffer from invalid pointers, null values, unintended side effects or security issues posed by incorrect pointer addresses.&lt;/p&gt;

&lt;p&gt;Unsafe code is the inverse: we no longer get any guarantees from Rust that our program upon submission to the compiler won't have any of these issues at runtime. The trade off is that we no longer have to adhere to all of the compiler's rule. One example might be having more than one mutable reference in scope at any given time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;split_as_mut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="nf"&gt;.as_mut_ptr&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nd"&gt;assert!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nn"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_raw_parts_mut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="nn"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_raw_parts_mut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="nf"&gt;.add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see we use the &lt;code&gt;unsafe&lt;/code&gt; keyword to declare a block in which we can execute unsafe code. What makes this code unsafe is calling the associated function &lt;code&gt;from_raw_parts_mut&lt;/code&gt; as this function is declared as &lt;code&gt;unsafe&lt;/code&gt;. To make a function &lt;code&gt;unsafe&lt;/code&gt; you can prepend the function declaration with the &lt;code&gt;unsafe&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;Beyond the function being &lt;code&gt;unsafe&lt;/code&gt;, we can tell from our function's return type that both calls &lt;code&gt;from_raw_parts_mut&lt;/code&gt; a mutable reference for our array into scope. This is not permitted by the Rust compiler in a &lt;code&gt;safe&lt;/code&gt; context. But was the code is &lt;code&gt;unsafe&lt;/code&gt;, we don't have any issues when submitting our code.  &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;split_as_mut&lt;/code&gt; function is considered as an &lt;em&gt;acceptable&lt;/em&gt; example of using &lt;code&gt;unsafe Rust&lt;/code&gt; as:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;unsafe&lt;/code&gt; part of the code is isolated and wrapped in a &lt;code&gt;safe&lt;/code&gt; API. This makes calling the function &lt;code&gt;safe&lt;/code&gt; regardless of where you call it.&lt;/li&gt;
&lt;li&gt;We manually assert that the pointers we create will point to a valid location in memory for the data structure we're trying to access&lt;/li&gt;
&lt;li&gt;Both pointers access unique subsets of the array.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Multilingual Rust
&lt;/h2&gt;

&lt;p&gt;We can also declare Rust APIs for other language using the &lt;code&gt;extern&lt;/code&gt; keyword. Private &lt;code&gt;extern&lt;/code&gt; functions call functions from other languages, while public functions can be called in other languages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s"&gt;"C"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;#[no_mangle]&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s"&gt;"C"&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;call_from_c&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Just called a Rust function from C!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>rust</category>
      <category>devjournal</category>
      <category>learning</category>
    </item>
    <item>
      <title>30 Days of Rust - Day 28</title>
      <dc:creator>johnnylarner</dc:creator>
      <pubDate>Sat, 24 Jun 2023 06:32:50 +0000</pubDate>
      <link>https://dev.to/johnnylarner/30-days-of-rust-day-28-nbo</link>
      <guid>https://dev.to/johnnylarner/30-days-of-rust-day-28-nbo</guid>
      <description>&lt;p&gt;Hi folks, I've been really struggling to keep up the blog recently. This was in part to a change of scenery in Berlin. But als I've learned that writing continuously on the same subject for so long doesn't get kind of boring. That said, I'm gonna push through today and summarize the key points about threading in Rust.&lt;/p&gt;

&lt;h2&gt;
  
  
  Yesterday's questions answered
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;No questions to answer&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Today's open questions
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;No open questions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Threading 101
&lt;/h2&gt;

&lt;p&gt;A multi-threaded program can run code in a concurrent or parallelised way. This can increase the performance of your program at the cost of increasing its complexity. In older programming languages, threading also made debugging code more difficult: threading environments are hard to reproduce (as thread creation is usually managed by the OS) and understanding what a specific thread is doing can be impossible to tell without an advanced debugger.&lt;/p&gt;

&lt;p&gt;Rust aims to solve some of the complexities via compiler rules that will make thread unsafe code fail at the compile step. This is achieved through the use of ownership guards and atomic reference counters which we'll cover later.&lt;/p&gt;

&lt;h3&gt;
  
  
  The API
&lt;/h3&gt;

&lt;p&gt;We can create a thread really easily in Rust:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;time&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nn"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(||&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hi number {} from the spawned thread!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
            &lt;span class="nn"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_millis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt; 
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default the main thread will not wait for subthreads to exit before continuing. This is why we need to use a &lt;code&gt;join handle&lt;/code&gt; to force the main thread to wait. As this returns a &lt;code&gt;Result&lt;/code&gt;, we also unwrap it. Note that where you do this in your code will impact the runtime behaviour:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;time&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;handle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(||&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hi number {} from the spawned thread!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
            &lt;span class="nn"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_millis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt; 
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="nf"&gt;.join&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What do threads own?
&lt;/h2&gt;

&lt;p&gt;In the above example, we can see that threads take a closure as their first argument. If we think back, we'll remember that closures are special in Rust: they capture variables declared in their scope. Capturing actually means borrowing in ownership terms:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;time&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_vec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;handle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(||&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"my vec: {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_vec&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt; 
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="nf"&gt;.join&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler will reject this code as we don't know how long a thread's lifetime will be. This means that a thread could outlive the duration of &lt;code&gt;my_vec&lt;/code&gt;, leading to an invalid reference being used in a thread.&lt;/p&gt;

&lt;p&gt;We can &lt;code&gt;move&lt;/code&gt; the vector into the thread. However, we won't be able to reference the value anywhere after our instantiation of the thread, as it will no longer be in scope.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chatty threads
&lt;/h2&gt;

&lt;p&gt;Rust implements the concept of &lt;code&gt;channels&lt;/code&gt; as a way to pass messages between threads. This is considered more robust that manipulating shared state. A channel consists of at least one producer and one receiver:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;mpsc&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When sending messages between threads it's important to remember two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Sending stuff also moves ownership of the data to the receiving thread&lt;/li&gt;
&lt;li&gt;You can force a thread to wait for a message with the &lt;code&gt;recv&lt;/code&gt;. You can use &lt;code&gt;try_recv&lt;/code&gt; to check if a message is available or not.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first point is a great source of robustness for your Rust programs. Channels are a really safe way to pass around values as you know the compiler won't let you accidentally borrow data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sharing state
&lt;/h2&gt;

&lt;p&gt;So far our examples have only covered situations where we create one thread. If we want to create multiple threads, we also need to use more advanced tools to manage ownership of shared resources among them. To be able to &lt;code&gt;move&lt;/code&gt; ownership of data into multiple threads, we need to use combination of the &lt;code&gt;Arc&lt;/code&gt; and &lt;code&gt;Mutex&lt;/code&gt; type.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mutually exclusive
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;Mutex&lt;/code&gt; type is a was to guarantee singular access to a resource via a lock. When you aren't working with threads, &lt;code&gt;Mutex&lt;/code&gt; can be used to access a value that it wraps in a different scope where it isn't owned and manipulate that value.&lt;/p&gt;

&lt;p&gt;If we use a &lt;code&gt;Mutex&lt;/code&gt; with threading, things get a bit more complicated. As we have to &lt;code&gt;move&lt;/code&gt; values into a thread, the &lt;code&gt;Mutex&lt;/code&gt; would end up having multiple owners. In a previous blog we looked at the &lt;code&gt;Rc&lt;/code&gt; (reference counter) type. This isn't thread safe. &lt;code&gt;Rc&lt;/code&gt; are cheaper than the thread safe &lt;code&gt;Arc&lt;/code&gt; (atomic reference counter) type which will use here.&lt;/p&gt;

&lt;p&gt;By wrapping our &lt;code&gt;Mutex&lt;/code&gt; in an &lt;code&gt;Arc&lt;/code&gt;, we instruct Rust to track all the owners of the &lt;code&gt;Mutex&lt;/code&gt;. We can call the &lt;code&gt;Arc&lt;/code&gt;'s &lt;code&gt;clone&lt;/code&gt; method with a reference of our &lt;code&gt;Mutex&lt;/code&gt; to create multiple references to it. Deref coercion then helps us write clean code to dereference the reference and manipulate the underlying value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Send and Sync
&lt;/h2&gt;

&lt;p&gt;If you want data to be transferrable between threads, they have to implement the &lt;code&gt;Send&lt;/code&gt; and &lt;code&gt;Sync&lt;/code&gt; traits. These are the overheads I mentioned before. Implementing your own types with &lt;code&gt;Send&lt;/code&gt; and &lt;code&gt;Sync&lt;/code&gt; results in &lt;em&gt;unsafe&lt;/em&gt; Rust. This is a topic I'll touch on a later blog.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>devjournal</category>
      <category>learning</category>
    </item>
    <item>
      <title>30 Days of Rust - Day 27</title>
      <dc:creator>johnnylarner</dc:creator>
      <pubDate>Tue, 13 Jun 2023 18:48:53 +0000</pubDate>
      <link>https://dev.to/johnnylarner/30-days-of-rust-day-27-389l</link>
      <guid>https://dev.to/johnnylarner/30-days-of-rust-day-27-389l</guid>
      <description>&lt;p&gt;Good afternoon everyone, I'm entering the final blog sprint. I'm finishing my 30 days of learning this week. We'll be getting into some more advanced concepts over the next few days. The final sprint starts with a concept not totally unique to Rust: smart pointers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Yesterday's questions answered
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;No questions to answer&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Today's open questions
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;No open questions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the point (of reading my blog?)
&lt;/h2&gt;

&lt;p&gt;We already know that pointers provide an efficient way of tracking large data structures during a program's runtime. Rather than copying the data each time we do some operation on it, we can simply pass around a pointer that points to the memory location of the result. References are the most common types of pointer and are represented by the &lt;code&gt;&amp;amp;&lt;/code&gt; symbol.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Smart&lt;/em&gt; &lt;em&gt;pointers&lt;/em&gt; in Rust are more powerful than a mere reference. Smart pointers can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Help us efficiently allocate and track data on the heap&lt;/li&gt;
&lt;li&gt;Count the numbers of references so that we can have multiple ownership (i.e. multithreaded code)&lt;/li&gt;
&lt;li&gt;Enable us to run borrow rules at runtime instead of compile time&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Put your data in a Box&amp;lt;T&amp;gt;
&lt;/h2&gt;

&lt;p&gt;Most programs interact with data of an unknown quantity. The &lt;code&gt;Box&lt;/code&gt; API allows us to allocate that data directly to program's heap while maintaining a pointer on the stack. There are certain types of data structures where this is required in Rust. &lt;/p&gt;

&lt;p&gt;Any type that is of a recursive nature needs to be in a box. This includes binary trees and linked lists. The reason for this is that a type which declares one of its fields as being of its own type will set off a recursive chain. Consider a binary tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             a
           /   \
          b     c
         / \   / \
        e  -  f  -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could set a type for this structure if we knew that the root node would always have 2 children, and each of its children would have a left leaf node and no right leaf:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;RootNode&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ChildNode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ChildNode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;ChildNode&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;LeafNode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;LeafNode&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But as soon as that structure changes, its type would also have to change. When working with recursive structures, we know that the size of each instance is very, very likely to vary (otherwise we'd be using some other data structure). &lt;code&gt;Box&lt;/code&gt; is a way to tell this to the Rust compiler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are saying to the compiler: we will have a node which may have another node which may have another node and so on to infinity 🚀  &lt;/p&gt;

&lt;h3&gt;
  
  
  Getting inside your Box
&lt;/h3&gt;

&lt;p&gt;As a &lt;code&gt;Box&lt;/code&gt; is a reference to data, you need to &lt;em&gt;dereference&lt;/em&gt; a box if you want to access your data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Under the hood, the deference operator is calling &lt;code&gt;deref&lt;/code&gt; method of the &lt;code&gt;Deref&lt;/code&gt; trait of the &lt;code&gt;Box&lt;/code&gt; object and then dereferencing the result.  The verbosity of dereferencing is hidden from us as programmers.&lt;/p&gt;

&lt;p&gt;Rust is also able to coercively dereference smart pointers. This means you can get references to the underlying value simply by calling the &lt;code&gt;&amp;amp;&lt;/code&gt; operator. When you call this on a smart pointer, it will be implicitly dereferenced to yield the underlying value. As this behaviour is defined before compilation, Rust can determine the value of final result, which would be the last value to not implement the &lt;code&gt;Deref&lt;/code&gt; trait.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cleaning up your Box
&lt;/h3&gt;

&lt;p&gt;Smart pointers also implement the &lt;code&gt;Drop&lt;/code&gt; trait. This tells Rust what to do with a given resource when it goes out of scope. For Boxes, this will result in the pointer and the data on the heap being deallocated.&lt;/p&gt;

&lt;p&gt;As with the &lt;code&gt;Deref&lt;/code&gt; trait, Rust is actually calling the &lt;code&gt;drop&lt;/code&gt; method behind the scenes. The Rust compiler won't let you call this method in your code. This could lead to two attempts being made to clear memory, this is known as a double free error. Instead you can use: &lt;code&gt;std::mem::drop&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sharing is caring
&lt;/h2&gt;

&lt;p&gt;Above we talked about a case where want to express to the compiler that we don't know how many instances type may be nested in a structure: we didn't know the &lt;em&gt;number&lt;/em&gt; of instances.&lt;/p&gt;

&lt;p&gt;Another case for smart pointers is when we don't know how many &lt;em&gt;owners&lt;/em&gt; a piece of data will have at compile time. For example, if we're trying to express a graph structure where each node is a person that has edges which represent connections to that node. We want obviously want to hold on to that node until we know each edge has been removed. If a node represents an expensive EC2 instance and an edge an active inbound connection, we only want autoshutdown to kick in when no more connections are active. &lt;em&gt;Enter &lt;code&gt;Rc&amp;lt;T&amp;gt;&lt;/code&gt;...&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The reference counter smart pointer (&lt;code&gt;Rc&amp;lt;T&amp;gt;&lt;/code&gt;) is a way of expressing this situation in Rust. Data wrapped in a &lt;code&gt;Rc&lt;/code&gt; will only ever go out of scope once all reference owners themselves go out of scope.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Who cares what the compiler thinks!
&lt;/h2&gt;

&lt;p&gt;Throughout this series, we've seen how the Rust compiler pushes us towards writing safer programs. One example of this is the borrowing rules:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;There can be multiple immutable references in scope at one time if there are no mutable references in scope&lt;/li&gt;
&lt;li&gt;There can be only one mutable reference in scope at any time&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;But sometimes we may want to break these rules. This is what the &lt;code&gt;RefCell&lt;/code&gt; smart pointer does. Well strictly speaking, it doesn't break the rules, it defers validating the rules until &lt;em&gt;runtime&lt;/em&gt; (instead of compile time). &lt;code&gt;RefCell&lt;/code&gt; wraps data that might otherwise be immutable such that you can then mutate it at runtime if desired. A &lt;code&gt;RefCell&lt;/code&gt; has the &lt;code&gt;borrow&lt;/code&gt; and &lt;code&gt;borrow_mut&lt;/code&gt; methods that return references to the data it is wrapping. &lt;/p&gt;

&lt;p&gt;You may rightly ask: why not just use a mutable reference if you want to mutate data. Most of the time you probably should. But there are cases, particularly when mocking stuff for unit tests, where you may want to use mutability to validate the behaviour of your library. The &lt;a href="https://doc.rust-lang.org/book/ch15-05-interior-mutability.html#interior-mutability-a-mutable-borrow-to-an-immutable-value"&gt;Rust Book&lt;/a&gt; explains the case really well and I'll defer to that as this blog post is already getting pretty long...&lt;/p&gt;

&lt;h2&gt;
  
  
  Separating the weak from the strong
&lt;/h2&gt;

&lt;p&gt;Some data structures may want to contain references to each other. For example our binary tree we look at earlier. We may want a root node to have access to all its children. Each parent and grandparent node should also access their children. Thus each node except the root will have an unknown number of owners at compile time. This means each node should be wrapped in a reference counter type. &lt;/p&gt;

&lt;p&gt;But that means we aren't able to modify the hierarchy of our tree as reference counters return immutable references. We can wrap the reference counter in a &lt;code&gt;RefCell&lt;/code&gt; if we want to adjust the hierarchy at run time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;cell&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;RefCell&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;rc&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Rc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;RefCell&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Rc&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A child node may also want to have a reference to its parent. And as parents can have multiple children, we'd want an reference counter wrapper type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Rc&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;RefCell&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Rc&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But now we have an issue: the child and parent now have circular references to each other. Why is that an issue? Reference counter types are only dropped when they have 0 &lt;em&gt;strong&lt;/em&gt; references. If a &lt;code&gt;Rc&amp;lt;Node&amp;gt;&lt;/code&gt; has 2 children with this setup, it will have 2 references. If those children are dropped or go out of scope, the &lt;code&gt;Rc&amp;lt;Node&amp;gt;&lt;/code&gt; will have 0 references, meaning it will be dropped. This is probably not what we want to happen.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Weak&lt;/code&gt; smart pointer is designed to solve this: when you want to have multiple references that should not influence the ownership of a given value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Weak&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;RefCell&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Rc&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now when a node's children are dropped, the compiler will not forcibly drop the parent node 🙌&lt;/p&gt;

</description>
      <category>rust</category>
      <category>learning</category>
      <category>devjournal</category>
      <category>advanced</category>
    </item>
    <item>
      <title>30 Days of Rust - Day 26</title>
      <dc:creator>johnnylarner</dc:creator>
      <pubDate>Sun, 11 Jun 2023 20:03:03 +0000</pubDate>
      <link>https://dev.to/johnnylarner/30-days-of-rust-day-26-43n9</link>
      <guid>https://dev.to/johnnylarner/30-days-of-rust-day-26-43n9</guid>
      <description>&lt;p&gt;Good evening everyone, as promised here is my third and final blog this week. I had a lovely trip over the weekend to Brandenburg on my bicycle. The pain in my legs was worth it: we took glorious forest routes and camped at a lovely lake. Back to reality today though with a deeper look into packaging in Rust with &lt;code&gt;cargo&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Yesterday's questions answered
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;No questions to answer&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Today's open questions
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;No open questions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  We &amp;lt;3 cargo
&lt;/h2&gt;

&lt;p&gt;The keen reader may have noticed that we already looked at some features of &lt;code&gt;cargo&lt;/code&gt; in  &lt;a href="https://dev.to/johnnylarner/30-days-of-rust-day-1-54an"&gt;Day 1's blog &lt;/a&gt;. It has also featured in days &lt;a href="https://dev.to/johnnylarner/panics-and-projects-3aia"&gt;5&lt;/a&gt;, &lt;a href="https://dev.to/johnnylarner/30-days-of-rust-day-7-4fj1"&gt;7&lt;/a&gt;,  &lt;a href="https://dev.to/johnnylarner/30-days-of-rust-valley-of-despair-26kb"&gt;10&lt;/a&gt;, &lt;a href="https://dev.to/johnnylarner/30-day-of-rust-day-13-2hh7"&gt;13&lt;/a&gt;, &lt;a href="https://dev.to/johnnylarner/30-days-of-rust-day-14-23d1"&gt;14&lt;/a&gt;, &lt;a href="https://dev.to/johnnylarner/30-days-of-rust-day-19-53ap"&gt;19&lt;/a&gt; and &lt;a href="https://dev.to/johnnylarner/30-days-of-rust-day-23-4d1k"&gt;23&lt;/a&gt;. It's packed with functionality: the package manager helps with testing, project builds and publishing, documentation and many other things.&lt;/p&gt;

&lt;h2&gt;
  
  
  Powerful publishing with autodocs and markdown
&lt;/h2&gt;

&lt;p&gt;We already know that &lt;code&gt;cargo&lt;/code&gt; can generate the documentation for any rust project in a locally stored project. This is because HTML is generated from &lt;code&gt;documentation comments&lt;/code&gt; when a project is built using &lt;code&gt;cargo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;documentation comments&lt;/code&gt; begin with &lt;code&gt;///&lt;/code&gt;. This is different to the &lt;code&gt;//&lt;/code&gt; of a standard comment. What makes these comments powerful is that they support markdown notation. This means headings and code snippets can be designated for a given function or module which is then rendered into pretty HTML. Where Rust goes one step further is with the documentation testing. Any code snippets you write in your documentation that use an &lt;code&gt;assert&lt;/code&gt; macro are tested when you run &lt;code&gt;cargo test&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Metamarkdown
&lt;/h3&gt;

&lt;p&gt;There is a third type of comment in Rust: &lt;code&gt;//!&lt;/code&gt;.  This is known as a &lt;code&gt;style of doc&lt;/code&gt; comment. There can be used to edit the display HTML outside of the explicit API documentation.  For example, if you want to give your crate a meaningful introduction, you'd write &lt;code&gt;//!&lt;/code&gt;  followed by some text. This would be rendered at a different position to the usual API code which is nested in with the functions it describes.&lt;/p&gt;

&lt;h2&gt;
  
  
  User friendly project structure
&lt;/h2&gt;

&lt;p&gt;Often when you write code, the technical implementation can vary from the conceptual structure of the thing your are implementing. This can make your package harder to use: your public APIs are at locations that don't match that position they occupy in the conceptual domain. Also, this can lead to long import statements if the public interface to your code is deeply nested in some structure.&lt;/p&gt;

&lt;p&gt;To solve this problem, you can use &lt;code&gt;public re-exports&lt;/code&gt;. This is where you use the &lt;code&gt;use&lt;/code&gt; keyword followed by the name of the module you want to make public. This also impacts how developers see your code in the crates.io documentation.&lt;/p&gt;

&lt;p&gt;Another feature of &lt;code&gt;cargo&lt;/code&gt; are so-called workspaces. These offer a way to group multiple projects around a single set of dependencies. You can use the &lt;code&gt;[workspace]&lt;/code&gt; section of your Cargo.toml file to group subdirectories as subprojects. Each will have its own Cargo.toml, but the &lt;code&gt;lock&lt;/code&gt; file will be managed at the common parent level. &lt;/p&gt;

&lt;p&gt;The aim here is to ensure that projects you create that are mutually dependent on each other always contain the same, non-breaking dependencies. Nonetheless you have to explicitly add subprojects to another subproject's Cargo.toml for it to be marked as a dependency. &lt;/p&gt;

&lt;h2&gt;
  
  
  Things to remember about your crates
&lt;/h2&gt;

&lt;p&gt;There are a couple of rules you need to remember when publishing your projects to &lt;code&gt;crates.io&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can't delete old versions. The best you can do is &lt;code&gt;cargo yank&lt;/code&gt; them, but this will just prevent any external project from using that specific version.&lt;/li&gt;
&lt;li&gt;You must specify a licence or a licence file, you can have multiple licences per project.&lt;/li&gt;
&lt;li&gt;Your projects name must be unique on &lt;code&gt;crates.io&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>rust</category>
      <category>devjournal</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>30 Days of Rust - Day 25</title>
      <dc:creator>johnnylarner</dc:creator>
      <pubDate>Wed, 07 Jun 2023 18:14:46 +0000</pubDate>
      <link>https://dev.to/johnnylarner/30-days-of-rust-day-25-1ihe</link>
      <guid>https://dev.to/johnnylarner/30-days-of-rust-day-25-1ihe</guid>
      <description>&lt;p&gt;What is up world, I promised you I'd be back again this week. Today's blog flog will shed some light on Rust's self-proclaimed &lt;em&gt;functional&lt;/em&gt; aspects. I'm not really dedicated enough blog time to this topic, but I'm quite time-pressed this evening 🫣&lt;/p&gt;

&lt;h2&gt;
  
  
  Yesterday's questions answered
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;No questions to answer&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Today's open questions
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;No open questions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5 days left until closure
&lt;/h2&gt;

&lt;p&gt;Anonymous functions in Rust are known as closures. This feature of the language is particularly powerful due to the following fact:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Closures capture variables in the scope at which they are declared&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We know that variable scope is fundamental point of thought when designing Rust code. Closures are the mechanism to get around some of the restrictions this poses. One obvious example of where this makes sense is creating a thread. You can make a variable available in that thread's scope. &lt;/p&gt;

&lt;h3&gt;
  
  
  Fn Traits
&lt;/h3&gt;

&lt;p&gt;Closures (as well as normal functions) can implement so-called Fn Traits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;FnOnce -&amp;gt; Called once, moves captured data out when returning&lt;/li&gt;
&lt;li&gt;FnMut -&amp;gt; Called more than once, may mutate captured values but won't move them&lt;/li&gt;
&lt;li&gt;Fn -&amp;gt; Called more than once, no mutations, no captured values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These traits allow the Rust compiler to spot issues early. For example, if multiple threads call a closure that mutates a resource that all threads need access too. Similarly, if you move an item in a closure while its original owner is still active in scope, you'd encounter some nasty bugs (if the compiler let you).&lt;/p&gt;

&lt;h2&gt;
  
  
  Iterators are sexy
&lt;/h2&gt;

&lt;p&gt;Turning collections into iterators opens up a bunch of useful operation to use as a developer. The &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt; and &lt;code&gt;sum&lt;/code&gt;  methods provide a readable, high-level way of writing code. Consider these two code snippets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;contents&lt;/span&gt;
        &lt;span class="nf"&gt;.lines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;.filter&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="nf"&gt;.contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Vec&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="nf"&gt;.lines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="nf"&gt;.contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; 

    &lt;span class="n"&gt;results&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not only is the first example shorter, the code is more expressive of the procedural nature of the task. What's more, we don't have to worry about state management (of the vector) which could become challenging in a multi-threaded system.&lt;/p&gt;

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