<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Brian on Things</title>
    <subtitle>Practical guides to Python, Rust and backend systems by software engineer Brian Obot, plus deep dives into low-level internals, physics and maths.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://brianobot.tech/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://brianobot.tech"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-12-01T00:00:00+00:00</updated>
    <id>https://brianobot.tech/atom.xml</id>
    <entry xml:lang="en">
        <title>Advent of Code Day One - 2026</title>
        <published>2026-12-01T00:00:00+00:00</published>
        <updated>2026-12-01T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/advent-of-code-2026-day-one/"/>
        <id>https://brianobot.tech/blog/advent-of-code-2026-day-one/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/advent-of-code-2026-day-one/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;This post is part of my &lt;a href=&quot;https://brianobot.tech/blog/advent-of-code-2026/&quot;&gt;Advent of Code 2026 series&lt;/a&gt;. Before committing to this article, I hope you, the reader, have solved or attempted the &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://adventofcode.com/2026/day/1&quot;&gt;puzzle&lt;/a&gt; and are using this article as additional learning material to see how I went about solving mine. Whatever the case, Happy New Month 🎉&lt;/p&gt;
&lt;p&gt;I can&#39;t help but express how much child-like excitement I got reading the prose for the first puzzle. The writing style and
choice of words do a lot towards setting the tone for the season and the puzzle&#39;s setting. Thingamabobs and Thingamajobs were new words for me!&lt;/p&gt;
&lt;h2 id=&quot;the-puzzle-in-plain-words&quot;&gt;The puzzle - In plain words&lt;/h2&gt;
&lt;p&gt;The goal of the puzzle is to find the 3 most common numbers (IDs) in a list of numbers and multiply them together for part 1, then do the same thing for the 3 least common numbers for part 2.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;3&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;From the list of numbers above, the most common numbers are &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;5&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt;, appearing 3, 2 and 2 times respectively, so their product is 1 × 5 × 3 = &lt;strong&gt;15&lt;/strong&gt;. The least common numbers are &lt;code&gt;4&lt;/code&gt;, &lt;code&gt;7&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt;, appearing once each, so their product is 4 × 7 × 2 = &lt;strong&gt;56&lt;/strong&gt;. The puzzle provides a very long list of numbers, and the goal is the same, but now with programming logic.&lt;/p&gt;
&lt;p&gt;What if two numbers are tied for 3rd place? The puzzle doesn&#39;t say how to break a tie, because its answer only makes sense when the top 3 is clear-cut. In the puzzle&#39;s own example, the counts are 6, 5, 4 and then 3, so there&#39;s a clear gap after 3rd place. My example works the same way: &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;5&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt; are the only numbers that appear more than once, and exactly three numbers appear only once.&lt;/p&gt;
&lt;h2 id=&quot;the-solution-in-pseudocode&quot;&gt;The solution: In pseudocode&lt;/h2&gt;
&lt;p&gt;I structure my solution scripts so that they read from a file that contains the input and then perform the analysis on that input.&lt;/p&gt;
&lt;h3 id=&quot;step-1-read-input-from-a-file&quot;&gt;Step 1: Read input from a file&lt;/h3&gt;
&lt;p&gt;Here is a cut section of the input for the puzzle:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;213&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;317&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;772&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;444&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;...more lines of IDs&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For step one, I read that input into a list of numbers. Notice that by default, values from a file are read as strings in most programming languages, so this is the step to do data cleaning and convert the IDs to integer values.&lt;/p&gt;
&lt;h3 id=&quot;step-2-implement-a-function-that-returns-the-3-most-common-items-from-a-collection&quot;&gt;Step 2: Implement a function that returns the 3 most common items from a collection&lt;/h3&gt;
&lt;h4 id=&quot;built-in-utility&quot;&gt;Built-in utility&lt;/h4&gt;
&lt;p&gt;Most programming languages provide some utility that takes in a collection of items and returns a data structure that describes the
count of each element in the original collection. For Python, there is the &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://docs.python.org/3/library/collections.html#collections.Counter&quot;&gt;&lt;code&gt;Counter&lt;/code&gt;&lt;/a&gt; class from the &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://docs.python.org/3/library/collections.html&quot;&gt;&lt;code&gt;collections&lt;/code&gt;&lt;/a&gt; module, which does exactly this for you. It even has a &lt;code&gt;most_common()&lt;/code&gt; method that returns the elements sorted by count, which covers Step 3 as well:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; collections&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; Counter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;counts&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; Counter&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;ids&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;top_three&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; counts&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;most_common&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;            #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; [(1, 3), (5, 2), (3, 2)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;bottom_three&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; counts&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;most_common&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; the 3 least common&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For the programming languages that do not have a ready-made utility, it is straightforward to implement this logic yourself, which is the route I took for my solution in Rust.&lt;/p&gt;
&lt;h4 id=&quot;custom-implementation&quot;&gt;Custom implementation&lt;/h4&gt;
&lt;p&gt;Given a collection of numbers, the most straightforward way to count the number of times each number appears is to loop over the collection and use a hashmap or dictionary to keep track of the count, with the number as the key and the count as the value. On the first encounter of a number, you add it to the hashmap, set its value to 0, and then increment by 1.&lt;/p&gt;
&lt;p&gt;Pseudocode implementation:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;collection = collection_from_step_one()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;hashmap = new empty_HashMap()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;for each_element in collection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    if each_element is not a key in hashmap already:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       add each_element to the hashmap and set its value to 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    increment the count (value) for each_element key in the hashmap&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At the end of this process, the hashmap should contain all the elements from the input collection as keys, and their values would be the number of times each element appeared in the collection.&lt;/p&gt;
&lt;p&gt;Our example list would produce this table:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Key&lt;/th&gt;&lt;th&gt;Count&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;3&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;4&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;/tr&gt;
&lt;tr&gt;&lt;td&gt;7&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;1&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;step-3-sort-the-entries-and-return-the-top-or-bottom-3&quot;&gt;Step 3: Sort the entries and return the top or bottom 3&lt;/h3&gt;
&lt;p&gt;A hashmap has no order of its own, so take its key/count pairs, sort them by count, and return the top 3 or bottom 3 keys for the next and final step.&lt;/p&gt;
&lt;p&gt;One gotcha in Rust: &lt;code&gt;HashMap&lt;/code&gt; iterates in a random order that changes from run to run. If two numbers ever did tie at the cutoff and you sorted by count alone, your answer could change between runs. Adding a tie-breaker on the key makes the sort deterministic:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mut&lt;/span&gt;&lt;span&gt; entries&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Vec&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;u64&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; u64&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; counts&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;into_iter&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; highest count first; equal counts fall back to the smaller ID&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;entries&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;sort_by&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;|&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;|&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;step-4-compute-the-product&quot;&gt;Step 4: Compute the product&lt;/h3&gt;
&lt;p&gt;For the final step, calculate and return the product of the elements from the previous step. In my own implementation, I avoided a loop at this stage and went with a functional programming approach, reducing the list of the top or bottom 3 elements to a single value. In Python that is &lt;code&gt;math.prod(n for n, _ in top_three)&lt;/code&gt;, and in Rust it is &lt;code&gt;top_three.iter().product::&amp;lt;u64&amp;gt;()&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;It&#39;s another Advent of Code season and it&#39;s the Christmas season, so I am excited. The puzzle for the first day is, unsurprisingly, very easy and straightforward. I look forward to solving those of the coming days with the same excitement and passion. I did revisit a few concepts I had not used in a while. If you want to see my actual code implementation, it is available &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://github.com/brianobot/advent-of-code-solutions/tree/master/_2026&quot;&gt;here&lt;/a&gt; on GitHub.&lt;/p&gt;
&lt;p&gt;Happy New Month once again 🎉✨&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>My Plans for Advent of Code - 2026</title>
        <published>2026-11-23T00:00:00+00:00</published>
        <updated>2026-11-23T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/advent-of-code-2026/"/>
        <id>https://brianobot.tech/blog/advent-of-code-2026/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/advent-of-code-2026/">&lt;h2 id=&quot;what-is-it&quot;&gt;What is it?&lt;/h2&gt;
&lt;p&gt;&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://adventofcode.com/&quot;&gt;Advent of Code&lt;/a&gt; is an annual set of Christmas-themed computer programming challenges that follow the story of the North Pole elves making necessary preparations for smooth festivity operations, unraveled day by day akin to how an Advent calendar is opened. It has been running since 2015. It was created by &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://was.tl/&quot;&gt;Eric Wastl&lt;/a&gt; (via Wikipedia).&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot;&gt;How it works&lt;/h2&gt;
&lt;p&gt;For each day from the first day of December up until the 12th day, two puzzles are dropped, the goal being to solve those puzzles with any programming language of your choice in order to obtain the stars linked to that day&#39;s puzzles. The second puzzle for each day does not unlock until the first is solved.&lt;/p&gt;
&lt;p&gt;There is no actual code upload for the solutions. You&#39;re only required to upload the actual answers you get from solving the puzzle.&lt;/p&gt;
&lt;p&gt;The puzzles cover a wild variety of skill levels and are a fun way to put your problem-solving skills and programming experience to the test. I first came across Advent of Code in December 2024. I haven&#39;t completed the puzzle set for any year so far, but regardless I look forward to doing so this year.&lt;/p&gt;
&lt;h2 id=&quot;2026&quot;&gt;2026&lt;/h2&gt;
&lt;p&gt;The puzzles begin on December 1st. I plan to write about my experience solving the puzzles and document the many exciting things I will learn along the way here on my blog. The website does have a leaderboard feature, where you can create a private leaderboard and invite your friends to join. At the same time, it discourages overly publicizing your leaderboard. These events are meant to be fun and cheerful, and a large leaderboard creates a competitive tension that strains that.&lt;/p&gt;
&lt;p&gt;As with most things recently in the programming space, human efforts are slowly taking a back seat to AI-assisted or generated coding. I personally think it robs you of the ability to genuinely enjoy and learn from the puzzles, so I will not be using AI-generated code for my solutions.&lt;/p&gt;
&lt;p&gt;I have a public GitHub repository where I keep my solutions for all the years, and you&#39;re welcome to go through it and learn how I approach the problems &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://github.com/brianobot/advent-of-code-solutions&quot;&gt;here&lt;/a&gt;. I implement my solutions in Python, Rust and &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://github.com/brianobot/py-lox&quot;&gt;Pylox&lt;/a&gt;, an experimental interpreted language I built while following the book &lt;em&gt;Crafting Interpreters&lt;/em&gt; by Robert Nystrom.&lt;/p&gt;
&lt;h2 id=&quot;where-to-go-from-here&quot;&gt;Where to go from here&lt;/h2&gt;
&lt;p&gt;There is an incredibly active community on &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://www.reddit.com/r/adventofcode/&quot;&gt;Reddit&lt;/a&gt; where users post guides, explainers and, most impressive to me, animated forms of their solutions to the puzzles. I will write about each day and its solution here on my blog. I really look forward to having fun and completing my first year of Advent of Code. If you would like to join my private leaderboard, you can send me a message on any of my socials.&lt;/p&gt;
&lt;p&gt;Create an account and be ready - &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://adventofcode.com/&quot;&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Merry Christmas ✨ in advance.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Entanglement: The Experiment That Proved Einstein Wrong</title>
        <published>2026-11-08T00:00:00+00:00</published>
        <updated>2026-11-08T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/entanglement-and-bells-theorem-the-experiment-that-proved-einstein-wrong/"/>
        <id>https://brianobot.tech/blog/entanglement-and-bells-theorem-the-experiment-that-proved-einstein-wrong/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/entanglement-and-bells-theorem-the-experiment-that-proved-einstein-wrong/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Einstein never accepted quantum mechanics as the final word. He was happy with its predictions; what
he refused was the idea that a particle doesn&#39;t have definite properties until it&#39;s measured, the idea
at the heart of the &lt;a href=&quot;https://brianobot.tech/blog/the-uncertainty-principle-is-not-about-clumsy-measurements/&quot;&gt;uncertainty principle&lt;/a&gt;. In 1935,
with Boris Podolsky and Nathan Rosen, he wrote a paper arguing that quantum mechanics must be
&lt;strong&gt;incomplete&lt;/strong&gt;: there had to be hidden details it was leaving out.&lt;/p&gt;
&lt;p&gt;For about thirty years this looked like a philosophical argument that could never be settled. Then in
1964, John Bell found a way to turn it into an &lt;strong&gt;experiment&lt;/strong&gt;. The experiment has since been done,
over and over, with steadily fewer loopholes. Einstein&#39;s picture lost. The 2022 Nobel Prize in Physics
went to three of the people who did those experiments.&lt;/p&gt;
&lt;p&gt;What I love about Bell&#39;s theorem is that the core of it is just counting. You can check it with a
simulation and a bit of arithmetic, which is exactly what I&#39;ll do in this post.&lt;/p&gt;
&lt;h2 id=&quot;tl-dr&quot;&gt;TL;DR&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Two &lt;strong&gt;entangled&lt;/strong&gt; particles have perfectly linked measurement results, however far apart they are.&lt;/li&gt;
&lt;li&gt;Einstein argued the results must be &lt;strong&gt;pre-set&lt;/strong&gt; at the source, like a pair of gloves split into two
boxes. That&#39;s a &lt;strong&gt;local hidden variable&lt;/strong&gt; theory.&lt;/li&gt;
&lt;li&gt;Bell showed that &lt;strong&gt;any&lt;/strong&gt; such theory obeys a limit on how strongly the results can be correlated.
In the CHSH version, a number called &lt;code&gt;S&lt;/code&gt; can be at most &lt;strong&gt;2&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Quantum mechanics predicts &lt;code&gt;S&lt;/code&gt; up to &lt;strong&gt;2√2 ≈ 2.83&lt;/strong&gt;, and experiments agree. So the results can&#39;t
have been pre-set by anything local.&lt;/li&gt;
&lt;li&gt;It still &lt;strong&gt;can&#39;t be used to send messages faster than light&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;entanglement-in-one-paragraph&quot;&gt;Entanglement in one paragraph&lt;/h2&gt;
&lt;p&gt;Two particles can be prepared in a joint quantum state where neither has a definite value on its own,
but their values are linked. A standard example is a pair of electrons in the &lt;strong&gt;singlet&lt;/strong&gt; state: if you
measure the spin of both along the same axis, you always get opposite answers. One up, one down. Which
one is up is completely random, 50/50, but they&#39;re always opposite. This holds whether the electrons
are a millimetre apart or on different continents.&lt;/p&gt;
&lt;h2 id=&quot;the-epr-argument-gloves-in-boxes&quot;&gt;The EPR argument: gloves in boxes&lt;/h2&gt;
&lt;p&gt;At first glance, perfect correlation isn&#39;t spooky at all. Take a pair of gloves, put one in each of two
boxes without looking, and post one to Lagos and one to Tokyo. Open the box in Lagos, find a left glove,
and you instantly know Tokyo has the right one. Nothing travelled between the boxes. The answer was
decided when the gloves were packed.&lt;/p&gt;
&lt;p&gt;Einstein, Podolsky and Rosen argued that entangled particles must work like this. If you can predict
the result at Tokyo with certainty, without touching it, then that result must correspond to some
&lt;strong&gt;element of reality&lt;/strong&gt; that was there all along. Quantum mechanics doesn&#39;t include these pre-set
values in its description, so it must be incomplete.&lt;/p&gt;
&lt;p&gt;This view has two ingredients:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Realism&lt;/strong&gt;: the measurement results are determined in advance by properties the particles carry.
Physicists call these &lt;strong&gt;hidden variables&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Locality&lt;/strong&gt;: what happens to one particle can&#39;t instantly affect the other. Nothing travels faster
than light.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Together they make a &lt;strong&gt;local hidden variable theory&lt;/strong&gt;. It sounds like common sense. Bell showed it makes
a testable prediction, and that prediction is wrong.&lt;/p&gt;
&lt;h2 id=&quot;where-the-gloves-analogy-breaks-choosing-the-angle&quot;&gt;Where the gloves analogy breaks: choosing the angle&lt;/h2&gt;
&lt;p&gt;The twist is that spin can be measured along &lt;strong&gt;any direction&lt;/strong&gt;, and the measurer chooses.&lt;/p&gt;
&lt;p&gt;Measure both electrons along the same axis and the results are always opposite. Measure them along
axes at 90° and the results are completely uncorrelated. In between, quantum mechanics predicts the
correlation follows a smooth cosine:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;E(a, b) = −cos(a − b)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where &lt;code&gt;E&lt;/code&gt; is the average of the product of the two results (+1 or −1 each), and &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt; are the
two measurement angles. &lt;code&gt;E = −1&lt;/code&gt; means always opposite, &lt;code&gt;0&lt;/code&gt; means no relation, &lt;code&gt;+1&lt;/code&gt; means always the same.&lt;/p&gt;
&lt;p&gt;A local hidden variable model can try to reproduce this. Each pair could carry a little instruction
sheet saying what to answer for every possible angle. The question Bell asked is whether &lt;em&gt;any&lt;/em&gt;
instruction sheet can match that cosine. The answer is no.&lt;/p&gt;
&lt;h2 id=&quot;the-chsh-inequality&quot;&gt;The CHSH inequality&lt;/h2&gt;
&lt;p&gt;The most practical version of Bell&#39;s result came in 1969 from John Clauser, Michael Horne, Abner
Shimony and Richard Holt, and it&#39;s known by their initials, &lt;strong&gt;CHSH&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Alice and Bob each have two measurement settings: Alice chooses &lt;code&gt;a&lt;/code&gt; or &lt;code&gt;a′&lt;/code&gt;, Bob chooses &lt;code&gt;b&lt;/code&gt; or &lt;code&gt;b′&lt;/code&gt;.
They measure lots of pairs, choosing settings at random, and compute:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;S = E(a, b) − E(a, b′) + E(a′, b) + E(a′, b′)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here&#39;s why local hidden variables can&#39;t push &lt;code&gt;|S|&lt;/code&gt; past 2. Suppose each pair carries pre-set answers
&lt;code&gt;A, A′&lt;/code&gt; for Alice and &lt;code&gt;B, B′&lt;/code&gt; for Bob, all ±1. For one pair:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;A·B − A·B′ + A′·B + A′·B′  =  A·(B − B′) + A′·(B + B′)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;B&lt;/code&gt; and &lt;code&gt;B′&lt;/code&gt; are each ±1, so either they&#39;re equal, and &lt;code&gt;B − B′ = 0&lt;/code&gt; while &lt;code&gt;B + B′ = ±2&lt;/code&gt;, or they&#39;re
opposite, and it&#39;s the other way round. Either way, one bracket is 0 and the other is ±2, so the whole
thing is exactly &lt;strong&gt;±2&lt;/strong&gt;. Average over any mix of pairs and you can&#39;t get beyond 2.&lt;/p&gt;
&lt;p&gt;That&#39;s the whole proof. No quantum mechanics involved, only the assumption that the answers existed
before the measurement and that Alice&#39;s choice can&#39;t change Bob&#39;s answer.&lt;/p&gt;
&lt;p&gt;Quantum mechanics, with angles &lt;code&gt;a = 0°&lt;/code&gt;, &lt;code&gt;a′ = 90°&lt;/code&gt;, &lt;code&gt;b = 45°&lt;/code&gt;, &lt;code&gt;b′ = 135°&lt;/code&gt;, predicts:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|S| = 4 × cos(45°) = 2√2 ≈ 2.83&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It breaks the limit. And 2√2 is the most any quantum system can reach, a bound found by Boris Tsirelson
in 1980.&lt;/p&gt;
&lt;h2 id=&quot;simulating-it-in-python&quot;&gt;Simulating it in Python&lt;/h2&gt;
&lt;p&gt;Let&#39;s check both claims. The first source is a reasonable local hidden variable model: each pair leaves
the source carrying a hidden angle, and each detector answers based only on its own setting and that
angle. The second samples outcomes from quantum mechanics&#39; predicted probabilities.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; random&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; local_pair&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; tuple&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Local hidden variables: each pair leaves the source carrying a hidden angle.&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    hidden&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; random&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;uniform&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;pi&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    alice&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; if&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span&gt; hidden&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; else&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    bob&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; if&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span&gt; hidden&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; else&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; alice&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; bob&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; quantum_pair&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; tuple&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Spin singlet: outcomes sampled from quantum mechanics&amp;#39; predicted probabilities.&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    alice&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; random&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    opposite&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; random&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; **&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    bob&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span&gt;alice&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; if&lt;/span&gt;&lt;span&gt; opposite&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; else&lt;/span&gt;&lt;span&gt; alice&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; alice&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; bob&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; correlation&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; trials&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 200_000&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    total&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    for&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; range&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;trials&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        alice&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; bob&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; source&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        total&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +=&lt;/span&gt;&lt;span&gt; alice&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; bob&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; total&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span&gt; trials&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; chsh&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    a&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; a2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0.0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;radians&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;90&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    b&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; b2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;radians&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;radians&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;135&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;correlation&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;source&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; correlation&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;source&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; b2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;            +&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; correlation&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;source&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; a2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; correlation&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;source&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; a2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; b2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;random&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Local hidden variables: |S| = &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;chsh&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;local_pair&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:.3f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Quantum mechanics:      |S| = &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;chsh&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;quantum_pair&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:.3f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Bell&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;s limit: 2, quantum maximum: 2√2 = &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:.3f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Local hidden variables: |S| = 1.997&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Quantum mechanics:      |S| = 2.826&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Bell&amp;#39;s limit: 2, quantum maximum: 2√2 = 2.828&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The hidden-angle model gets perfect anti-correlation at equal angles, just like the real thing. It
even sits right &lt;em&gt;at&lt;/em&gt; the limit of 2. But no matter how you tweak it, you can&#39;t get it past 2. You&#39;re
welcome to try: change &lt;code&gt;local_pair&lt;/code&gt; however you like, as long as Alice&#39;s answer only uses &lt;code&gt;a&lt;/code&gt; and
&lt;code&gt;hidden&lt;/code&gt;, and Bob&#39;s only uses &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;hidden&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Notice the cheat in &lt;code&gt;quantum_pair&lt;/code&gt;: Bob&#39;s result is computed using &lt;strong&gt;both&lt;/strong&gt; angles, &lt;code&gt;a − b&lt;/code&gt;. That&#39;s
the only way a classical program can reproduce quantum statistics. To fake entanglement classically,
one side has to know the other side&#39;s setting.&lt;/p&gt;
&lt;h2 id=&quot;the-experiments&quot;&gt;The experiments&lt;/h2&gt;
&lt;p&gt;The prediction is clear. Testing it took decades of increasingly clever experiments, usually with
pairs of polarisation-entangled photons rather than electrons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;1972&lt;/strong&gt;: John Clauser and Stuart Freedman at Berkeley ran the first test and found a violation of
Bell&#39;s inequality, in agreement with quantum mechanics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;1982&lt;/strong&gt;: Alain Aspect&#39;s team in Paris switched the measurement settings &lt;strong&gt;while the photons were in
flight&lt;/strong&gt;, so no signal at light speed could tell one detector what the other had chosen.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;1998 onwards&lt;/strong&gt;: Anton Zeilinger&#39;s group in Innsbruck and Vienna used random number generators and
longer distances to close more gaps, and later used entanglement for quantum teleportation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Sceptics pointed to &lt;strong&gt;loopholes&lt;/strong&gt;. If detectors miss many photons, maybe the ones they catch are a
biased sample (the detection loophole). If the detectors are close enough, maybe a signal could pass
between them (the locality loophole). In &lt;strong&gt;2015&lt;/strong&gt;, three groups, at Delft, Vienna and NIST in
Colorado, closed both loopholes in the same experiments. The Delft team used electrons in diamonds
1.3 km apart. The violation held.&lt;/p&gt;
&lt;p&gt;In 2022, Aspect, Clauser and Zeilinger shared the Nobel Prize in Physics &quot;for experiments with
entangled photons, establishing the violation of Bell inequalities and pioneering quantum information
science.&quot;&lt;/p&gt;
&lt;h2 id=&quot;what-it-does-and-doesn-t-mean&quot;&gt;What it does and doesn&#39;t mean&lt;/h2&gt;
&lt;p&gt;Bell&#39;s theorem rules out &lt;strong&gt;local&lt;/strong&gt; hidden variables. At least one of Einstein&#39;s two ingredients has to
go:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Give up &lt;strong&gt;realism&lt;/strong&gt;: the results really don&#39;t exist until measured. This is the mainstream reading.&lt;/li&gt;
&lt;li&gt;Give up &lt;strong&gt;locality&lt;/strong&gt;: there are hidden variables, but they&#39;re nonlocal, as in the pilot-wave
interpretation, where the guiding wave connects distant particles instantly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What it &lt;strong&gt;doesn&#39;t&lt;/strong&gt; allow is faster-than-light communication. Look at Alice&#39;s results on their own:
they&#39;re a random 50/50 sequence no matter what Bob does. The correlation only appears when Alice and
Bob bring their records together and compare, which needs an ordinary, light-speed-limited channel.
This is called the &lt;strong&gt;no-signalling&lt;/strong&gt; theorem. Entanglement is a correlation, not a telephone.&lt;/p&gt;
&lt;h2 id=&quot;why-it-matters-now&quot;&gt;Why it matters now&lt;/h2&gt;
&lt;p&gt;What started as an argument about the nature of reality has become engineering:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Quantum key distribution&lt;/strong&gt;: Artur Ekert&#39;s 1991 protocol uses Bell tests to check that nobody has
intercepted a key. If an eavesdropper interfered, the correlation would drop below the quantum value.
China&#39;s Micius satellite has distributed entangled photons between ground stations over 1,000 km
apart.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Quantum computing&lt;/strong&gt;: entanglement between qubits is the resource that lets quantum algorithms
outperform classical ones on some problems. The 2025 Nobel Prize went to John Clarke, Michel Devoret
and John Martinis for the 1980s experiments showing quantum behaviour in superconducting circuits, the
same kind of circuits many of today&#39;s quantum computers are built from.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Device-independent security&lt;/strong&gt;: because a Bell violation can&#39;t be faked by local means, you can
certify randomness or security without trusting the devices you bought.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Bell&#39;s theorem is one of the rare cases where a philosophical debate got settled by experiment:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Entangled particles show &lt;strong&gt;perfect correlations&lt;/strong&gt;, which on their own could be explained by pre-set
answers, like gloves in boxes.&lt;/li&gt;
&lt;li&gt;But with &lt;strong&gt;changeable measurement angles&lt;/strong&gt;, any local pre-set-answer theory obeys &lt;code&gt;|S| ≤ 2&lt;/code&gt;, and the
proof is a few lines of algebra.&lt;/li&gt;
&lt;li&gt;Quantum mechanics predicts and &lt;strong&gt;experiments confirm&lt;/strong&gt; &lt;code&gt;|S| ≈ 2√2&lt;/code&gt;, with every major loophole closed
since 2015.&lt;/li&gt;
&lt;li&gt;Nature is not locally realistic, and yet still can&#39;t be used to &lt;strong&gt;signal faster than light&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Einstein was right that something about quantum mechanics is strange. He was wrong about which part.
The strangeness isn&#39;t a gap in the theory; it&#39;s the way the world actually works.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Introduction to Bytes in Rust. Part 1</title>
        <published>2026-11-02T00:00:00+00:00</published>
        <updated>2026-11-02T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/working-with-bytes-in-rust/"/>
        <id>https://brianobot.tech/blog/working-with-bytes-in-rust/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/working-with-bytes-in-rust/">&lt;h2 id=&quot;why-you-have-probably-never-had-to-think-about-bytes&quot;&gt;Why you have probably never had to think about bytes&lt;/h2&gt;
&lt;p&gt;As a backend developer, most of my work sits at a high level of abstraction. I write handlers that take in structs, strings, vectors and the occasional custom enum, and return types that implement a particular trait.&lt;/p&gt;
&lt;p&gt;Here is a handler from an Axum project:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;Serialize&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Message&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    message&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; handler&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Json&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;    Json&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        message&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Hello World!&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;No bytes in sight. You define a struct, derive a trait, return it, and you&#39;re done. Somewhere further down, that &lt;code&gt;Json&amp;lt;Message&amp;gt;&lt;/code&gt; becomes a sequence of bytes on a network socket, but someone else already did that work for you.&lt;/p&gt;
&lt;p&gt;That is true for most backend projects: the framework has built the plumbing, and your job is to combine those building blocks to solve business problems.&lt;/p&gt;
&lt;p&gt;Things change when you go below that line and start building the blocks yourself: parsing a binary protocol, writing a file format, computing a checksum. There, understanding bytes stops being optional. I&#39;ve been learning the fundamentals of bytes in Rust, and this post is my attempt to explain what I&#39;ve understood to someone else. Explaining it is also the best test of whether I understand it.&lt;/p&gt;
&lt;h2 id=&quot;bits-bytes-and-the-rest-of-the-gang&quot;&gt;Bits, bytes and the rest of the gang&lt;/h2&gt;
&lt;p&gt;When you send a friend an AI-generated meme, then at the lowest level that picture is nothing but a stream of &lt;strong&gt;bits&lt;/strong&gt; travelling over a wire, a radio wave or a fibre-optic cable.&lt;/p&gt;
&lt;p&gt;&quot;Bit&quot; is short for &lt;strong&gt;binary digit&lt;/strong&gt;, and binary is a way of counting that uses only two digits: &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt;. In decimal, we count &lt;code&gt;0, 1, 2, ... 9&lt;/code&gt; and then add a new digit to go higher (&lt;code&gt;10&lt;/code&gt;). Binary works the same way, except it runs out of digits after &lt;code&gt;1&lt;/code&gt;, so it adds a new digit much sooner:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Decimal&lt;/th&gt;&lt;th&gt;Binary&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&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;10&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;11&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;100&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;101&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;110&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;111&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;1000&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Each position in a binary number is worth double the one to its right (1, 2, 4, 8, ...), just as decimal positions are worth 1, 10, 100, and so on. So &lt;code&gt;101&lt;/code&gt; is &lt;code&gt;1×4 + 0×2 + 1×1 = 5&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;bytes&quot;&gt;Bytes&lt;/h3&gt;
&lt;p&gt;Think about how written language works. Letters are the basic symbols, but a single letter rarely means much on its own. Arrange them into words, then sentences, then paragraphs, and meaning appears.&lt;/p&gt;
&lt;p&gt;Bits work similarly. A single bit carries very little, so we group them. A group of &lt;strong&gt;8 bits&lt;/strong&gt; is called a &lt;strong&gt;byte&lt;/strong&gt;, and the byte is the basic unit that computers read, write and address in memory.&lt;/p&gt;
&lt;p&gt;Eight bits can form 2⁸ = 256 different patterns, so a byte can represent any number from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;255&lt;/code&gt;. (Not every piece of information is exactly 8 bits wide: a flag might use a single bit, and some formats pack fields across byte boundaries. But the byte is the unit everything is built on.)&lt;/p&gt;
&lt;h3 id=&quot;hexadecimal&quot;&gt;Hexadecimal&lt;/h3&gt;
&lt;p&gt;Writing &lt;code&gt;11111111&lt;/code&gt; every time you mean 255 is tedious and easy to get wrong, which is why we use &lt;strong&gt;hexadecimal&lt;/strong&gt;, or &quot;hex&quot;.&lt;/p&gt;
&lt;p&gt;Hex is a base-16 counting system. It needs 16 digits, so after &lt;code&gt;0-9&lt;/code&gt; it borrows the letters &lt;code&gt;A-F&lt;/code&gt; (where &lt;code&gt;A&lt;/code&gt; is 10 and &lt;code&gt;F&lt;/code&gt; is 15). The useful part is that &lt;strong&gt;one hex digit corresponds to exactly 4 bits&lt;/strong&gt;, so a byte is always exactly &lt;strong&gt;two hex digits&lt;/strong&gt;:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Binary&lt;/th&gt;&lt;th&gt;Hex&lt;/th&gt;&lt;th&gt;Decimal&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;0000 0001&lt;/td&gt;&lt;td&gt;&lt;code&gt;0x01&lt;/code&gt;&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;0100 0001&lt;/td&gt;&lt;td&gt;&lt;code&gt;0x41&lt;/code&gt;&lt;/td&gt;&lt;td&gt;65&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1111 1111&lt;/td&gt;&lt;td&gt;&lt;code&gt;0xFF&lt;/code&gt;&lt;/td&gt;&lt;td&gt;255&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The &lt;code&gt;0x&lt;/code&gt; prefix is just a marker meaning &quot;this number is in hex&quot;. You&#39;ll see these constantly in low-level code. For example, &lt;code&gt;0x41&lt;/code&gt; is 65, which happens to be the code for the letter &lt;code&gt;A&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;why-would-anyone-leave-decimal-for-binary&quot;&gt;Why would anyone leave decimal for binary?&lt;/h2&gt;
&lt;p&gt;It does feel like unnecessary work. We already count comfortably in base 10, so why make computers use something harder to read?&lt;/p&gt;
&lt;p&gt;The answer is that computers aren&#39;t built to be read by humans. They are built from electronic circuits, which work with physical signals. Telling apart &lt;strong&gt;two&lt;/strong&gt; states, a high voltage and a low voltage, is easy and reliable. Telling apart ten distinct voltage levels is hard, because electrical noise is always present, and a small disturbance could turn a 7 into an 8.&lt;/p&gt;
&lt;p&gt;Two states leave a wide margin for error, so circuits can be simple, fast and dependable. Switch between those two states billions of times a second, and you can express any information at all, as long as you agree on what the patterns mean. (Quantum computers work differently, but that&#39;s a story for another day.)&lt;/p&gt;
&lt;h2 id=&quot;bringing-it-down-to-rust&quot;&gt;Bringing it down to Rust&lt;/h2&gt;
&lt;p&gt;Rust is a strongly typed language: every value has a type, and the compiler knows it at compile time. Often you write the type yourself as an annotation, but the compiler is smart enough to infer it in many cases.&lt;/p&gt;
&lt;p&gt;The type for a single byte is &lt;strong&gt;&lt;code&gt;u8&lt;/code&gt;&lt;/strong&gt;: an &lt;strong&gt;u&lt;/strong&gt;nsigned &lt;strong&gt;8&lt;/strong&gt;-bit integer, holding values from 0 to 255.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; u8&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 65&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; u8&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0x41&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;         //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; hex literal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; u8&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0b0100_0001&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; binary literal (underscores are just for readability)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; u8&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;         //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; byte literal: the ASCII code of &amp;#39;A&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;assert_eq!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;assert_eq!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;assert_eq!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All four are the same value, written four different ways. A &lt;code&gt;u8&lt;/code&gt; can&#39;t hold 256 or more: &lt;code&gt;let x: u8 = 256;&lt;/code&gt; fails to compile, and arithmetic that overflows (like &lt;code&gt;255u8 + 1&lt;/code&gt;) panics in debug builds.&lt;/p&gt;
&lt;h3 id=&quot;collections-of-bytes&quot;&gt;Collections of bytes&lt;/h3&gt;
&lt;p&gt;You&#39;ll mostly work with bytes in groups, and Rust has three common shapes for that:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span&gt; fixed&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;u8&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;0x89&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0x50&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0x4E&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0x47&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; array: size fixed at compile time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mut&lt;/span&gt;&lt;span&gt; growable&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Vec&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;u8&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Vec&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;         //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Vec: grows as needed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;growable&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;extend_from_slice&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;fixed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span&gt; view&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;u8&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;growable&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;              //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; slice: a borrowed view into bytes&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That array is the start of every PNG file, the format your meme was probably sent in. The &lt;code&gt;0x50 0x4E 0x47&lt;/code&gt; part spells &lt;code&gt;PNG&lt;/code&gt; in ASCII.&lt;/p&gt;
&lt;h3 id=&quot;strings-are-bytes-too&quot;&gt;Strings are bytes too&lt;/h3&gt;
&lt;p&gt;A Rust &lt;code&gt;String&lt;/code&gt; or &lt;code&gt;&amp;amp;str&lt;/code&gt; is really a sequence of bytes holding UTF-8 text, and you can look at them directly:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Hi!&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span&gt; bytes&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;u8&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;as_bytes&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;:?&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; bytes&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;   //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; [72, 105, 33]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;é&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note the last line: &lt;code&gt;&quot;é&quot;&lt;/code&gt; is one character but &lt;strong&gt;two&lt;/strong&gt; bytes, because UTF-8 uses more than one byte for characters outside basic ASCII. That&#39;s why &lt;code&gt;len()&lt;/code&gt; on a string counts bytes, not characters.&lt;/p&gt;
&lt;p&gt;To print numbers in hex or binary, use format specifiers:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;:#04x&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;:08b&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 65&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;u8&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 65&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;u8&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; 0x41 01000001&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;what-s-next&quot;&gt;What&#39;s next&lt;/h3&gt;
&lt;p&gt;A &lt;code&gt;u8&lt;/code&gt; is easy, but most real data is bigger than one byte. A &lt;code&gt;u32&lt;/code&gt; takes four bytes, and then you face a question: which byte goes first?&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; u32&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0x1234_5678&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;:02x?&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;to_be_bytes&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; [12, 34, 56, 78]  big-endian&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;:02x?&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;to_le_bytes&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; [78, 56, 34, 12]  little-endian&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That&#39;s called &lt;strong&gt;endianness&lt;/strong&gt;, and it&#39;s where I&#39;ll pick up in the next post.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>What I Learned (and Wish I&#39;d Done Differently) at TechCabal&#39;s Moonshot</title>
        <published>2026-11-01T00:00:00+00:00</published>
        <updated>2026-11-01T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/what-i-learned-at-techcabals-moonshot-2026/"/>
        <id>https://brianobot.tech/blog/what-i-learned-at-techcabals-moonshot-2026/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/what-i-learned-at-techcabals-moonshot-2026/">&lt;h2 id=&quot;why-i-was-there&quot;&gt;Why I Was There&lt;/h2&gt;
&lt;p&gt;A few months ago, I moved from Port Harcourt to Lagos with one mission: to tap into the city&#39;s tech ecosystem by attending events, meeting top talent, and getting the kind of exposure that could change my life.&lt;/p&gt;
&lt;p&gt;Then, honestly, not much happened. My first months in Lagos were quiet. No events, no big introductions, just me, a laptop, and a plan that hadn&#39;t started yet.&lt;/p&gt;
&lt;p&gt;That changed with TechCabal&#39;s Moonshot, my first tech event since the move. I loved it, but I also left with a list of things I wish I had done differently. This post covers both.&lt;/p&gt;
&lt;h2 id=&quot;arrival&quot;&gt;Arrival&lt;/h2&gt;
&lt;p&gt;I was so excited that I got to the &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://en.wikipedia.org/wiki/National_Theatre,_Nigeria&quot;&gt;National Theatre&lt;/a&gt; at about 8:30am, an hour and a half before the 10am start. It didn&#39;t matter. The place was already swarming with people, and from the energy around me, they were just as excited as I was.&lt;/p&gt;
&lt;p&gt;Getting in was painless. I scanned my ticket, collected my tag, and walked in. No queues!&lt;/p&gt;
&lt;p&gt;The hard part came after. From the start, I found the central hall confusing to navigate, and I spent my first stretch of the day just trying to work out where everything was.&lt;/p&gt;
&lt;h2 id=&quot;the-sessions&quot;&gt;The Sessions&lt;/h2&gt;
&lt;p&gt;The sessions are where Moonshot&#39;s tech focus really showed. This year&#39;s theme was &quot;Courage &amp;amp; Conviction: Building for a New World,&quot; and three threads ran through the day for me: AI, the future of money, and building companies that last.&lt;/p&gt;
&lt;p&gt;With talks running on several stages at once, I couldn&#39;t be everywhere, so I followed the topics closest to what I care about.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The State of AI in Nigeria:&lt;/strong&gt; A panel on where Nigeria actually stands with AI, moderated by Dr. Olubayo Adekanmbi on the Big Tech and Enterprise Stage. It made me see AI as a data and infrastructure problem as much as a model problem. Without good local data, reliable compute, and trained people, the technology stays a demo. As someone with a data engineering background, that was the part of the stack I felt closest to.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stablecoins &amp;amp; Monetary Sovereignty:&lt;/strong&gt; Seun Lanlege&#39;s talk on the Future of Money Stage asked how stablecoins fit into a country&#39;s control over its own money. It changed how I think about payment rails. The technology can move value in seconds, but the harder questions are trust, regulation, and who gets to set the rules.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Forty Years A Tech Bro: What It Takes To Build Something That Lasts:&lt;/strong&gt; Sir Demola Aladekomo, in conversation with Fu&#39;ad Lawal, spoke about building across decades instead of funding cycles. It was a useful counterweight to the speed-obsessed side of tech: lasting companies are built on patience and fundamentals more than hype.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Building Africa&#39;s Workforce for the Global Economy:&lt;/strong&gt; Joel Ogunsola&#39;s talk on the Workshop Stage was about preparing African talent to compete globally. It spoke directly to why I moved to Lagos. Opportunity tends to find people who put themselves in the room, and skills count for more when the right people can see them.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Hearing these ideas in one place made the ecosystem feel far more real than reading about it online ever had.&lt;/p&gt;
&lt;h2 id=&quot;walking-the-exhibition-floor&quot;&gt;Walking the Exhibition Floor&lt;/h2&gt;
&lt;p&gt;If the sessions gave me ideas, the exhibition tour gave me exposure. Every company had a booth with banners, merchandise, and someone ready to explain what made their product different.&lt;/p&gt;
&lt;p&gt;My favorite stop was &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://www.cloudflare.com/&quot;&gt;Cloudflare&lt;/a&gt;. I&#39;d been wondering whether they plan to support VPS-style machine provisioning for people who deploy on their cloud, so I asked. The answer was no. It wasn&#39;t what I&#39;d hoped for, but getting a straight answer from the people who would know was worth the walk.&lt;/p&gt;
&lt;p&gt;I also spoke with &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://liners.com/&quot;&gt;Liners&lt;/a&gt;, a software company that offers reviews and SEO boosting for African software products, and with a sales representative from &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://www.rexafrica.com/&quot;&gt;Rexx&lt;/a&gt;, which describes itself as Africa&#39;s first AI-native banking solution. Other booths on my route included &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://grey.co/&quot;&gt;Grey&lt;/a&gt;, a fintech platform for cross-border payments, &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://www.gopaddi.com/en&quot;&gt;GoPaddi&lt;/a&gt;, an AI-powered platform for travel, tourism, hospitality, and business management, and &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://www.zikoko.com/&quot;&gt;Zikoko&lt;/a&gt;, a Nigerian digital and entertainment platform.&lt;/p&gt;
&lt;h2 id=&quot;the-people&quot;&gt;The People&lt;/h2&gt;
&lt;p&gt;Networking was the reason I moved, so I made a point of talking to people from different corners of tech. These are some of the new friends I made:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://x.com/id_inyang_&quot;&gt;ID Inyang&lt;/a&gt; is a software engineer who builds practical applications for real-world problems. He specializes in backend development and system design, with a focus on software that is scalable, reliable, and efficient. Backend and system design is where I want to go deeper, so he&#39;s someone I plan to keep learning from.&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://www.linkedin.com/in/brian-ibekwe&quot;&gt;Brian Ibekwe&lt;/a&gt; is a product manager at &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://readpulse.app&quot;&gt;ReadPulse&lt;/a&gt;, where he works on building, improving, and growing the platform. Talking to someone who thinks in terms of users and growth gave me a useful contrast to my own more technical view of products.&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://ayobamiraheemportfolio.my.canva.site/&quot;&gt;Ninie&lt;/a&gt;: is a mobile videographer/content creator that is passionate about working with brands and individuals to bring their stories to life through engaging visual content&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://x.com/obiabo_immanuel&quot;&gt;Emmanuel Obiabo&lt;/a&gt; works mostly with TypeScript and Rust, contributes to open source, and writes about whatever catches his interest. Much of his open-source work has been on blockchain projects in the Stellar ecosystem, and he is a long-time Linux user. You can read more about him &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://www.obiabo.dev/about&quot;&gt;here&lt;/a&gt;. His path through open source is one I&#39;m curious to follow.&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://www.linkedin.com/in/bryanbenibo/&quot;&gt;Bryan Benibo&lt;/a&gt; is a Founding Designer who helps startups build brands and intuitive digital products from scratch, blending creativity with strategy so the design drives real growth and customer engagement.&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://marilynedem.framer.website/&quot;&gt;Marilyn&lt;/a&gt; is a UI/UX designer. Her portfolio includes TideReader, an AI-powered adaptive reading platform, Delve, a gamified language-learning platform, and Bubblr, a safe social media app for kids. Her case studies are a reminder that products live or die on design, and I&#39;d love to work on something where engineering and design are in the same room.&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://x.com/The_mauteen&quot;&gt;Mauteen&lt;/a&gt; is a product design engineer who works where design meets engineering. That overlap is exactly what I want to understand better, so I&#39;m glad we connected.&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://github.com/Ealfred1&quot;&gt;Eric&lt;/a&gt; is a creative software developer who builds mobile apps, backend systems, and web platforms end to end. He&#39;s a fellow builder, and the kind of person I&#39;d happily build with.&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://maryadejumo.tiiny.site/&quot;&gt;Mary Adejumo&lt;/a&gt;is product manager at Rex Microfinance Bank, where she owns the mobile app and website of the digital bank. She also built FindRyd, an AI-powered navigation app that gives delivery riders directions in landmarks instead of metres, taking it from 85 rider interviews to a tested prototype. A member of the Best Product Discovery Team in the 2026 PLA APM Fellowship, she writes things down until a team can build from them and tests the riskiest assumption first, which is a habit I want to pick up.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-i-wish-i-had-done-differently&quot;&gt;What I Wish I Had Done Differently&lt;/h2&gt;
&lt;p&gt;I&#39;d call these small preparations, not big mistakes. But each one would have made the day easier.&lt;/p&gt;
&lt;h3 id=&quot;1-carry-a-personal-card&quot;&gt;1. Carry a personal card&lt;/h3&gt;
&lt;p&gt;Over and over, I did the same thing: pulled out my phone, found someone&#39;s LinkedIn or X handle, and typed mine in return. It was slow, it interrupted the conversation, and it only worked if both of us had the patience for it.&lt;/p&gt;
&lt;p&gt;A personal card would have turned that into a two-second exchange. It doesn&#39;t have to be fancy: my name, what I do, and a QR code to my LinkedIn or GitHub. It would also give people something to remember me by when they leave a crowded venue with a bag of merchandise and a head full of names.&lt;/p&gt;
&lt;h3 id=&quot;2-wear-something-that-starts-conversations&quot;&gt;2. Wear something that starts conversations&lt;/h3&gt;
&lt;p&gt;Moonshot was a sea of similar tags and similar outfits, and it was hard to stand out. Branded wear, like a shirt or hoodie with my name, my role, or something I&#39;m building, would have done some of the introducing for me. People would have had a reason to ask about it, and I wouldn&#39;t have had to open every conversation cold. It would also have made me easier to find again after a first meeting.&lt;/p&gt;
&lt;h3 id=&quot;3-plan-my-route-through-the-day&quot;&gt;3. Plan my route through the day&lt;/h3&gt;
&lt;p&gt;I arrived early and still got lost in the halls. With sessions running on several stages at once, it&#39;s easy to drift toward whatever is closest instead of what matters. Next time, I&#39;ll bookmark my sessions the night before, note which stage each one is on, and leave gaps for the exhibition floor, so I&#39;m choosing my time instead of just reacting to the crowd.&lt;/p&gt;
&lt;h2 id=&quot;final-thoughts&quot;&gt;Final Thoughts&lt;/h2&gt;
&lt;p&gt;I moved to Lagos for moments like this one. Moonshot gave me sessions that sharpened my thinking, a floor full of companies to talk to, and a handful of people I&#39;m glad to know. Next time, I&#39;ll show up with a card, an outfit that speaks for me, and a plan for the day. If you were at Moonshot, I&#39;d love to hear what you&#39;d do differently. And if this post was useful, share it with someone heading to their first tech event.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Learning Technical Skills in the Age of SI</title>
        <published>2026-10-17T00:00:00+00:00</published>
        <updated>2026-10-17T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/learning-technical-skills-in-the-age-of-si/"/>
        <id>https://brianobot.tech/blog/learning-technical-skills-in-the-age-of-si/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/learning-technical-skills-in-the-age-of-si/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Like many technical or skilled persons in the world at the moment, in the age of SI&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-si-1&quot;&gt;&lt;a href=&quot;https://brianobot.tech/blog/learning-technical-skills-in-the-age-of-si/#fn-si&quot;&gt;[1]&lt;/a&gt;&lt;/sup&gt; it&#39;s becoming increasingly hard to feel special about the technical skill you possess, especially a skill like software mastery or software engineering. In recent times, I have had to come face to face with the reality that what made me special (with the hands in the air, defensively), in terms of my technical skills and
experience in software programming, is largely not special anymore.&lt;/p&gt;
&lt;p&gt;Now I have a real issue: Why should I invest real time like was done in the past, to gain mastery of topics that now feels very ephemeral? Why should I commit my weekend to understanding how Python&#39;s dictionary actually works under the hood and care about the details linked to open addressing, when I can generate more features far beyond my control in a product others could just as well vibe code?&lt;/p&gt;
&lt;p&gt;At my most recent workplace (&lt;small&gt;sad memories of my employed self&lt;/small&gt;), apart from the issues linked with lack of a concrete vision for the company&#39;s moat, I basically clocked out at some point, and just became a feature sprouting organism, attending demo after demo to wow &lt;em&gt;always almost-at-the-verge-of-paying-clients&lt;/em&gt;. This was sad for me, because outside making a living with the skills I had acquired, I do genuinely love solving problems with the tools in my arsenal that actually ended up being used by people.&lt;/p&gt;
&lt;h2 id=&quot;what-was-it-really-about-for-you&quot;&gt;What was it really about for you?&lt;/h2&gt;
&lt;p&gt;The answer to this question would probably justify most people&#39;s stance on whether to bother committing to learning certain technical skills, if it was solely about implementing a feature, I guess you&#39;re better off moving fast and generating one with an LLM or some agent of sorts, but if it was really about mastering systems, understanding principles and unconsciously or consciously transferring those skills to other unrelated domains then the fear of being replaced should be less (notice how I said less, it doesn&#39;t go away). This does not invalidate the fear of losing out on a race of doing so much more with limited time and capital compared to any other time in history.&lt;/p&gt;
&lt;h2 id=&quot;my-personal-initial-motivation&quot;&gt;My Personal initial motivation&lt;/h2&gt;
&lt;p&gt;For me, it was always about mastery, I mean that mastery made my work easier, but I always loved the bits and bytes of it all, the quirks you had to get used to, and understand why they were, the rationales, the processes, it made committing to mastery very rewarding. All these contributed to building one&#39;s problem solving muscles across domains. For example I do this thing in my real life activities, where I try to do a lot of things concurrently in a beautifully scheduled way, mimicking asynchronous programming style: Like how I try to hack my chores and wait times on other activity by using those wait times to attend to other pending activities and I do it consciously and get excitement from being my own asynchronous runtime.&lt;/p&gt;
&lt;h2 id=&quot;is-history-repeating-itself&quot;&gt;Is History Repeating itself?&lt;/h2&gt;
&lt;p&gt;I am sure a certain set of skilled person experienced things like this in the past, take for example, In the chess world, computers have practically perfected playing chess, and have since surpassed humans, but people still study it, and deep understanding separates good players from great ones. And that mastery is not any less valuable because it&#39;s beneath a computer&#39;s.&lt;/p&gt;
&lt;p&gt;I think the same argument can be made for what is happening to software engineering and maths at this point in history, of course everyone can spin up a React App and get a well documented API powering it, but I believe it&#39;s a matter of time, before the mass hysteria dies off (this is actual wishful thinking)
and people get bored of the AI hype. Is there a place of SI in work? of course, but I have accepted that mastery is personal and it does in fact reward more even outside of the specific domain of application.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I could be terribly wrong, man! I don&#39;t know the future, but this sounds quite reasonable, so if you&#39;re reading this from some distant day in the future and you laugh at my sheer misdirection on how things were going to go, I want you to know I too was open to the possibility that things happened in ways I could not have predicted. Soooo!! Do whatever makes you happy, and if that includes mastery of the internals of Fortran or memorizing factorials of a very large number, by all means go crazy. I am actively looking for a new role as a Software Engineer at the moment. Here is my &lt;a href=&quot;https://brianobot.tech/blog/learning-technical-skills-in-the-age-of-si/Brian_Obot_Resume.pdf&quot;&gt;CV&lt;/a&gt;&lt;/p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-si&quot;&gt;
&lt;p&gt;SI: Super Intelligence - President Donald Trump signed an Executive Order on September 29, 2026, officially directing federal agencies to replace the terms &quot;Artificial Intelligence&quot; (AI) and &quot;AI&quot; with &quot;Super Intelligence&quot; (SI) in all official correspondence, public communications, and non-statutory documents. &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://www.whitehouse.gov/presidential-actions/2026/09/inaugurating-the-era-of-super-intelligence/&quot;&gt;Read more&lt;/a&gt; &lt;a href=&quot;https://brianobot.tech/blog/learning-technical-skills-in-the-age-of-si/#fr-si-1&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>The Uncertainty Principle Is Not About Clumsy Measurements</title>
        <published>2026-03-07T00:00:00+00:00</published>
        <updated>2026-03-07T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/the-uncertainty-principle-is-not-about-clumsy-measurements/"/>
        <id>https://brianobot.tech/blog/the-uncertainty-principle-is-not-about-clumsy-measurements/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/the-uncertainty-principle-is-not-about-clumsy-measurements/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Ask most people what Heisenberg&#39;s uncertainty principle says and you&#39;ll hear something like: &quot;you
can&#39;t measure a particle without disturbing it.&quot; To see an electron you have to bounce light off it,
the light kicks it, and so you lose track of where it was going.&lt;/p&gt;
&lt;p&gt;That explanation is intuitive, and Heisenberg himself used a version of it in 1927. It&#39;s also
misleading. The uncertainty principle isn&#39;t a limit on our instruments. It&#39;s a statement about what
a particle &lt;strong&gt;is&lt;/strong&gt;. Even with perfect, gentle, impossible measuring tools, a particle simply does not
have a precise position and a precise momentum at the same time.&lt;/p&gt;
&lt;p&gt;In this post I want to make that idea feel obvious rather than mysterious. The trick is to think
about waves. Then I&#39;ll use the principle to do something I find delightful: work out the size of a
hydrogen atom in about ten lines of Python.&lt;/p&gt;
&lt;h2 id=&quot;tl-dr&quot;&gt;TL;DR&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Quantum particles are described by waves, and &lt;strong&gt;momentum is the wavelength&lt;/strong&gt; (&lt;code&gt;p = h/λ&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;A wave with a precise wavelength has to be spread out. A wave squeezed into a small region has to
mix many wavelengths. That&#39;s not physics, it&#39;s a property of all waves.&lt;/li&gt;
&lt;li&gt;So &lt;strong&gt;Δx · Δp ≥ ħ/2&lt;/strong&gt;: the spread in position times the spread in momentum can&#39;t go below a fixed
limit. No measurement required.&lt;/li&gt;
&lt;li&gt;The same idea explains why electrons don&#39;t spiral into the nucleus, why there&#39;s &lt;strong&gt;zero-point
energy&lt;/strong&gt;, and why atoms are the size they are.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;start-with-waves-not-particles&quot;&gt;Start with waves, not particles&lt;/h2&gt;
&lt;p&gt;Forget quantum mechanics for a moment and think about sound.&lt;/p&gt;
&lt;p&gt;A pure musical note, a single perfect frequency, is a sine wave that goes on and on. To be &lt;em&gt;exactly&lt;/em&gt;
one frequency, it has to last forever. If you cut it short, say a quick tap on a drum, the sound is
localised in time, but it no longer has one pitch. A short click contains a whole spread of
frequencies.&lt;/p&gt;
&lt;p&gt;This is a mathematical fact about waves, usually expressed through the &lt;strong&gt;Fourier transform&lt;/strong&gt;: any
pulse can be built by adding together sine waves of different frequencies, and the shorter the
pulse, the wider the range of frequencies you need. Engineers know this as the &lt;strong&gt;time–bandwidth
product&lt;/strong&gt;. It&#39;s why a radio signal that carries fast-changing data needs a wide frequency band, and
it&#39;s something I first met in signals and systems classes, long before I connected it to quantum
physics.&lt;/p&gt;
&lt;p&gt;The same is true in space instead of time. A wave with a single, precise &lt;strong&gt;wavelength&lt;/strong&gt; has to
stretch across all of space. A wave packet confined to a small region has to be built from many
different wavelengths.&lt;/p&gt;
&lt;h2 id=&quot;now-add-quantum-mechanics&quot;&gt;Now add quantum mechanics&lt;/h2&gt;
&lt;p&gt;Here&#39;s the quantum ingredient, due to Louis de Broglie: a particle is described by a wave, and its
&lt;strong&gt;momentum is tied to its wavelength&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;p = h / λ&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Put the two ideas together:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A particle with a &lt;strong&gt;precise momentum&lt;/strong&gt; has a precise wavelength, so its wave is spread across all of
space. Its position is completely undefined.&lt;/li&gt;
&lt;li&gt;A particle with a &lt;strong&gt;precise position&lt;/strong&gt; has a wave squeezed into a tiny region, which requires many
wavelengths mixed together. Its momentum is completely undefined.&lt;/li&gt;
&lt;li&gt;Anything in between is a trade-off.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That&#39;s the uncertainty principle. It&#39;s not that the particle has a definite position and momentum
that we can&#39;t quite pin down. It&#39;s that a wave &lt;strong&gt;can&#39;t have&lt;/strong&gt; both a sharp location and a sharp
wavelength, and a quantum particle is that wave.&lt;/p&gt;
&lt;h2 id=&quot;the-formula&quot;&gt;The formula&lt;/h2&gt;
&lt;p&gt;Made precise, the relationship is:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Δx · Δp ≥ ħ / 2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Δx&lt;/code&gt; is the &lt;strong&gt;spread&lt;/strong&gt; (standard deviation) of position.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Δp&lt;/code&gt; is the spread of momentum.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ħ&lt;/code&gt; (&quot;h-bar&quot;) is the reduced Planck constant, &lt;code&gt;h / 2π ≈ 1.055 × 10⁻³⁴ J·s&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The exact form with &lt;code&gt;ħ/2&lt;/code&gt; was proved in 1927 by Earle Kennard, shortly after Heisenberg&#39;s paper. The
equality is reached only by one shape of wave packet: a &lt;strong&gt;Gaussian&lt;/strong&gt;, the bell curve. Every other
shape is more spread out.&lt;/p&gt;
&lt;p&gt;Two notes worth making.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The spreads are about many repeated experiments.&lt;/strong&gt; Prepare a million electrons in the same state.
Measure position on half of them, momentum on the other half. The spreads in those two sets of
results always obey the inequality. Each individual measurement can be as precise as your equipment
allows. What you can&#39;t do is prepare a state where both sets of results come out sharp.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ħ is tiny.&lt;/strong&gt; For a 1 gram marble whose position is known to within a micrometre, the minimum
uncertainty in its velocity is about &lt;code&gt;5 × 10⁻²⁶&lt;/code&gt; m/s. Your marble would take longer than the age of
the universe to drift a millimetre. That&#39;s why you never notice this in everyday life. For an
electron, with a mass about 10²⁷ times smaller, the effect is everything.&lt;/p&gt;
&lt;h2 id=&quot;why-measurement-disturbs-it-is-the-wrong-picture&quot;&gt;Why &quot;measurement disturbs it&quot; is the wrong picture&lt;/h2&gt;
&lt;p&gt;Heisenberg&#39;s original argument was the &lt;strong&gt;gamma-ray microscope&lt;/strong&gt;: to locate an electron precisely,
you need short-wavelength light, which carries a big momentum kick, which disturbs the electron&#39;s
momentum.&lt;/p&gt;
&lt;p&gt;That&#39;s a real effect, but it suggests the electron &lt;em&gt;had&lt;/em&gt; a precise position and momentum before you
interfered with it. Experiments say otherwise. The famous tests of
&lt;a href=&quot;https://brianobot.tech/blog/entanglement-and-bells-theorem-the-experiment-that-proved-einstein-wrong/&quot;&gt;Bell&#39;s theorem&lt;/a&gt; rule out any theory in which particles carry definite, pre-existing values for
all their properties, if those values only depend on local influences. The uncertainty is in the
particle, not in our clumsiness.&lt;/p&gt;
&lt;p&gt;There&#39;s also an elegant way to see it from the
&lt;a href=&quot;https://brianobot.tech/blog/the-double-slit-experiment-how-one-particle-interferes-with-itself/&quot;&gt;double-slit experiment&lt;/a&gt;. Send a particle through a
single narrow slit. Narrowing the slit pins down its sideways position (small &lt;code&gt;Δx&lt;/code&gt;), and the
particle&#39;s landing spot on the screen spreads out wider (large &lt;code&gt;Δp&lt;/code&gt;). That&#39;s &lt;strong&gt;diffraction&lt;/strong&gt;. No one
is measuring anything at the slit. It&#39;s just a wave going through a gap.&lt;/p&gt;
&lt;h2 id=&quot;why-atoms-don-t-collapse&quot;&gt;Why atoms don&#39;t collapse&lt;/h2&gt;
&lt;p&gt;This is the part that made the uncertainty principle click for me.&lt;/p&gt;
&lt;p&gt;Classical physics has an embarrassing problem with atoms. An electron orbiting a proton is an
accelerating charge, and accelerating charges radiate. It should lose energy and spiral into the
nucleus in about a hundred-billionth of a second. Every atom in the universe should have collapsed.&lt;/p&gt;
&lt;p&gt;The uncertainty principle stops it. If the electron gets squeezed into a smaller region of size &lt;code&gt;r&lt;/code&gt;,
its momentum spread grows to roughly &lt;code&gt;ħ / r&lt;/code&gt;, which means more kinetic energy, roughly &lt;code&gt;ħ²/(2mr²)&lt;/code&gt;.
Meanwhile its electric potential energy falls as &lt;code&gt;-ke²/r&lt;/code&gt;. The kinetic term grows faster as &lt;code&gt;r&lt;/code&gt;
shrinks, so there&#39;s a size where the total energy is lowest. That&#39;s the atom.&lt;/p&gt;
&lt;p&gt;Let&#39;s find it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;HBAR&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1.054_571_8e-34&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;     #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; reduced Planck constant, J·s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;M_E&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 9.109_383_7e-31&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;      #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; electron mass, kg&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;KE2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2.307_077_6e-28&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;      #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; e² / (4πε₀), J·m&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EV&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1.602_176_6e-19&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;       #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; joules per electron-volt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; energy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Total energy of an electron squeezed into a region of size r.&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    kinetic&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; HBAR&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; **&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; M_E&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; **&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;   #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; from Δp ≈ ħ / r&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    potential&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span&gt;KE2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;                       #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; attraction to the proton&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; kinetic&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span&gt; potential&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Scan sizes from 0.01 to 5 ångström and keep the one with the lowest energy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sizes&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1e-13&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; range&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 50_000&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;best&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; min&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;sizes&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; key&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span&gt;energy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Most stable size: &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;best&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1e10&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:.3f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; Å&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Energy there:     &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;energy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;best&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span&gt; EV&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:.2f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; eV&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;for&lt;/span&gt;&lt;span&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;0.1e-10&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0.25e-10&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; best&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2e-10&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;  r = &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1e10&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:5.2f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; Å -&amp;gt; E = &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;energy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span&gt; EV&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:8.2f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; eV&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Most stable size: 0.529 Å&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Energy there:     -13.61 eV&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  r =  0.10 Å -&amp;gt; E =   237.00 eV&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  r =  0.25 Å -&amp;gt; E =     3.36 eV&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  r =  0.53 Å -&amp;gt; E =   -13.61 eV&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  r =  2.00 Å -&amp;gt; E =    -6.25 eV&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;0.529 ångström is the &lt;strong&gt;Bohr radius&lt;/strong&gt;, the textbook size of a hydrogen atom. And −13.6 eV is
&lt;strong&gt;exactly&lt;/strong&gt; the measured ground-state energy of hydrogen: the energy it takes to ionise it.&lt;/p&gt;
&lt;p&gt;I&#39;ll confess that landing on both numbers exactly is partly luck: &lt;code&gt;Δp ≈ ħ/r&lt;/code&gt; is a rough estimate,
and a different choice of constant would shift the answer by a factor of order one. But the point
stands. Squeeze the electron and its kinetic energy blows up (look at the 237 eV at 0.1 Å). The
uncertainty principle is literally what gives matter its size. It&#39;s the reason you don&#39;t fall through
the floor.&lt;/p&gt;
&lt;h2 id=&quot;zero-point-energy&quot;&gt;Zero-point energy&lt;/h2&gt;
&lt;p&gt;The same argument says a quantum system can never be perfectly still. A particle sitting motionless
at the bottom of a valley would have precise position &lt;em&gt;and&lt;/em&gt; zero momentum, which is forbidden. So
even at absolute zero, there&#39;s some unavoidable jiggling, called &lt;strong&gt;zero-point energy&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;It has real consequences. It&#39;s why &lt;strong&gt;helium never freezes&lt;/strong&gt; at normal pressure, even at absolute
zero: helium atoms are so light and so weakly attracted to each other that their zero-point motion
is enough to stop them locking into a crystal. You have to squeeze it to about 25 atmospheres to
make solid helium.&lt;/p&gt;
&lt;h2 id=&quot;other-pairs&quot;&gt;Other pairs&lt;/h2&gt;
&lt;p&gt;Position and momentum aren&#39;t the only pair with this trade-off. In general, any two quantities whose
quantum operators don&#39;t commute have one. The best known others:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Spin&lt;/strong&gt; along different axes. An electron can&#39;t have a definite spin both up–down and
left–right. Measure one and the other becomes completely random.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Energy and time&lt;/strong&gt;, in a slightly different sense: a state that only lasts a short time has a
spread of energies. That&#39;s why short-lived excited states emit light with a broader range of
colours, and why unstable particles have a &quot;width&quot; in their mass.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;The uncertainty principle is more natural, and more fundamental, than its popular description
suggests:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It comes from &lt;strong&gt;waves&lt;/strong&gt;: a sharp position and a sharp wavelength can&#39;t coexist, and in quantum
mechanics wavelength &lt;em&gt;is&lt;/em&gt; momentum.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Δx · Δp ≥ ħ/2&lt;/code&gt; is about the &lt;strong&gt;state of the particle&lt;/strong&gt;, not the quality of the measuring tools.&lt;/li&gt;
&lt;li&gt;It&#39;s not a nuisance at the edge of physics. It sets the &lt;strong&gt;size of atoms&lt;/strong&gt;, gives every system a
&lt;strong&gt;zero-point energy&lt;/strong&gt;, and keeps helium liquid at absolute zero.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The next question is the one that troubled Einstein most: if a particle doesn&#39;t have definite
properties until they&#39;re measured, what happens when two particles share a state and are measured
far apart? That&#39;s
&lt;a href=&quot;https://brianobot.tech/blog/entanglement-and-bells-theorem-the-experiment-that-proved-einstein-wrong/&quot;&gt;entanglement&lt;/a&gt;, and it gets its own post.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Nuclear Fusion: Why Building a Star on Earth Is So Hard</title>
        <published>2026-02-14T00:00:00+00:00</published>
        <updated>2026-02-14T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/nuclear-fusion-why-building-a-star-on-earth-is-so-hard/"/>
        <id>https://brianobot.tech/blog/nuclear-fusion-why-building-a-star-on-earth-is-so-hard/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/nuclear-fusion-why-building-a-star-on-earth-is-so-hard/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;There is an old joke that fusion power is thirty years away, and always will be. It&#39;s been told for
so long that it&#39;s become part of the subject. And yet the physics is not in doubt: every second,
the Sun fuses around 600 million tonnes of hydrogen, and has been doing it for 4.6 billion years.
Fusion obviously works. The question is why it&#39;s so devilishly hard to do it here.&lt;/p&gt;
&lt;p&gt;This is the third post in a small series. In the first, I explained the
&lt;a href=&quot;https://brianobot.tech/blog/binding-energy-why-iron-is-the-end-of-the-line/&quot;&gt;binding energy curve&lt;/a&gt;, and in the
second, how &lt;a href=&quot;https://brianobot.tech/blog/nuclear-fission-how-splitting-an-atom-powers-a-city/&quot;&gt;fission&lt;/a&gt; climbs it
from the heavy side. Fusion climbs it from the light side, and it&#39;s the bigger prize. You don&#39;t
need to have read either post to follow this one.&lt;/p&gt;
&lt;h2 id=&quot;tl-dr&quot;&gt;TL;DR&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Fusing light nuclei, like hydrogen isotopes into helium, releases energy because helium is far
more tightly bound. Per kilogram, it beats fission by about &lt;strong&gt;four times&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The obstacle is the &lt;strong&gt;Coulomb barrier&lt;/strong&gt;: nuclei are positively charged and repel each other, so
you need them extremely hot (~150 million °C) to get close enough.&lt;/li&gt;
&lt;li&gt;Even then, they only fuse thanks to &lt;strong&gt;quantum tunnelling&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A power plant needs hot enough plasma, dense enough, held for long enough: the &lt;strong&gt;Lawson
criterion&lt;/strong&gt;. Magnetic confinement (tokamaks, stellarators) and inertial confinement (lasers) are
two different bets on how to get there.&lt;/li&gt;
&lt;li&gt;In late 2022, the US National Ignition Facility got more fusion energy out of a target than the
lasers put in, a first, and it has since improved that. Nobody has yet produced net
&lt;strong&gt;electricity&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-reaction-everyone-is-chasing&quot;&gt;The reaction everyone is chasing&lt;/h2&gt;
&lt;p&gt;The Sun fuses ordinary hydrogen, but slowly (more on that below). Every serious fusion power design
on Earth uses a faster reaction between two heavier hydrogen isotopes, &lt;strong&gt;deuterium&lt;/strong&gt; (one proton,
one neutron) and &lt;strong&gt;tritium&lt;/strong&gt; (one proton, two neutrons):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;²H + ³H  →  ⁴He (3.5 MeV) + n (14.1 MeV)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Total energy: &lt;strong&gt;17.6 MeV&lt;/strong&gt; per reaction. The helium nucleus is the star of the binding energy curve,
unusually tightly bound for its size, and dropping into that state releases a lot of energy.&lt;/p&gt;
&lt;p&gt;Per reaction, 17.6 MeV is less than the ~200 MeV of a uranium fission. But only five nucleons are
involved instead of 236. Per kilogram of fuel, D-T fusion releases about &lt;strong&gt;340 terajoules&lt;/strong&gt;, roughly
four times more than fission and around &lt;strong&gt;ten million times&lt;/strong&gt; more than burning coal.&lt;/p&gt;
&lt;p&gt;Notice that four-fifths of the energy leaves with the &lt;strong&gt;neutron&lt;/strong&gt;. Neutrons have no charge, so no
magnetic field can hold them. They fly straight out of the plasma and into the walls. That fact
quietly shapes almost every fusion engineering problem, and I&#39;ll come back to it.&lt;/p&gt;
&lt;h2 id=&quot;the-real-enemy-the-coulomb-barrier&quot;&gt;The real enemy: the Coulomb barrier&lt;/h2&gt;
&lt;p&gt;Here is the core difficulty. The strong nuclear force that makes fusion possible only reaches about
a femtometre or two. To feel it, two nuclei have to almost touch. But both nuclei are positively
charged, and the electric repulsion between them grows stronger the closer they get.&lt;/p&gt;
&lt;p&gt;For deuterium and tritium, the repulsion peaks at around &lt;strong&gt;0.4 MeV&lt;/strong&gt; just before the strong force
takes over. That&#39;s the &lt;strong&gt;Coulomb barrier&lt;/strong&gt;: a hill of electric potential energy the nuclei have to
climb.&lt;/p&gt;
&lt;p&gt;The obvious way to give particles energy is to heat them. In a gas, temperature is a measure of
the particles&#39; average kinetic energy. So how hot would deuterium and tritium need to be to have an
average energy of 0.4 MeV? About &lt;strong&gt;4.6 billion kelvin&lt;/strong&gt;. The core of the Sun is about 15 million
kelvin, three hundred times cooler. By classical physics alone, the Sun shouldn&#39;t shine.&lt;/p&gt;
&lt;h2 id=&quot;quantum-tunnelling-to-the-rescue&quot;&gt;Quantum tunnelling to the rescue&lt;/h2&gt;
&lt;p&gt;Classical physics says a particle without enough energy to get over a hill simply can&#39;t. Quantum
mechanics disagrees. A particle is described by a wave, and that wave doesn&#39;t stop dead at a
barrier. It decays inside it, and if the barrier is thin enough, some of the wave leaks out the
other side. The particle has a small but real probability of appearing beyond a barrier it couldn&#39;t
classically climb. This is &lt;strong&gt;quantum tunnelling&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The tunnelling probability rises steeply with energy: faster nuclei face a thinner, lower part of
the barrier. Meanwhile, in a hot gas, very few particles are fast. Their energies follow a
Maxwell–Boltzmann distribution, which drops off exponentially at high energy.&lt;/p&gt;
&lt;p&gt;Multiply these two together, &quot;plenty of particles but bad tunnelling&quot; at low energy and &quot;great
tunnelling but almost no particles&quot; at high energy, and you get a narrow window where most fusion
actually happens. This is the &lt;strong&gt;Gamow peak&lt;/strong&gt;, named after George Gamow who first worked out
tunnelling for nuclear decay in 1928.&lt;/p&gt;
&lt;p&gt;Here&#39;s a small script that computes where that peak sits:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ALPHA&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 137.036&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;          #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; fine-structure constant&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;U_TO_KEV&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 931_494.0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;         #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; atomic mass unit in keV/c^2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;K_B&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 8.617e-8&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;               #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Boltzmann constant in keV per kelvin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; gamow_peak&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;Z1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; Z2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; m1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; m2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; temperature_k&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Return (Gamow energy, thermal energy kT, most likely fusion energy), all in keV.&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    reduced_mass&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; m1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; m2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span&gt;m1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span&gt; m2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; U_TO_KEV&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    e_gamow&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; reduced_mass&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span&gt;math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;pi&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; ALPHA&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; Z1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; Z2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; **&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    kT&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; K_B&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; temperature_k&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    peak&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span&gt;e_gamow&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; kT&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; **&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; **&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; e_gamow&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; kT&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; peak&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cases&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;p + p in the Sun&amp;#39;s core (15.7 million K)&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1.00728&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1.00728&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 15.7e6&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;D + T in a reactor (150 million K)&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2.01355&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 3.01550&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 150e6&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;for&lt;/span&gt;&lt;span&gt; label&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span&gt;Z1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; Z2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; m1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; m2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; T&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; cases&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    e_g&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; kT&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; peak&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; gamow_peak&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;Z1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; Z2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; m1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; m2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; T&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;label&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;  kT = &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;kT&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:.2f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; keV, Gamow peak = &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;peak&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:.1f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; keV (&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;peak&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span&gt; kT&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:.1f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; x kT)&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;p + p in the Sun&amp;#39;s core (15.7 million K)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  kT = 1.35 keV, Gamow peak = 6.1 keV (4.5 x kT)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;D + T in a reactor (150 million K)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  kT = 12.93 keV, Gamow peak = 36.7 keV (2.8 x kT)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Even in a fusion reactor, the nuclei that actually fuse carry around 37 keV: a tenth of the height
of the barrier. Every single fusion reaction, in the Sun and in our machines, is a tunnelling
event.&lt;/p&gt;
&lt;h2 id=&quot;why-the-sun-gets-away-with-it-and-we-can-t&quot;&gt;Why the Sun gets away with it, and we can&#39;t&lt;/h2&gt;
&lt;p&gt;The Sun has two huge advantages. It has &lt;strong&gt;gravity&lt;/strong&gt; to hold its fuel together for free, and it has
&lt;strong&gt;time&lt;/strong&gt;. Its core is so vast and dense that even a pitifully slow reaction rate adds up to an
enormous output.&lt;/p&gt;
&lt;p&gt;And it really is slow. The first step of the Sun&#39;s proton–proton chain needs one proton to turn into
a neutron mid-collision, via the weak nuclear force, which almost never happens. A given proton in
the Sun&#39;s core waits, on average, billions of years before it fuses. The power density in the
solar core is only around &lt;strong&gt;275 watts per cubic metre&lt;/strong&gt;, comparable to a compost heap. The Sun is
bright because it is big, not because it is intense.&lt;/p&gt;
&lt;p&gt;On Earth we have neither the gravity nor the size. So we compensate in two ways:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Use a much easier reaction.&lt;/strong&gt; D-T needs no weak-force miracle. Its cross-section is around
10²⁴ times larger than proton–proton fusion at similar energies.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Run much hotter than the Sun.&lt;/strong&gt; Around &lt;strong&gt;150 million kelvin&lt;/strong&gt;, ten times the temperature of the
solar core, which is where D-T&#39;s reaction rate is near its best per unit of pressure.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;At those temperatures, electrons are stripped from their nuclei and the fuel becomes a &lt;strong&gt;plasma&lt;/strong&gt;, a
soup of charged particles. No material can touch it. Hold it in a container and the plasma cools
instantly while the container vaporises.&lt;/p&gt;
&lt;h2 id=&quot;the-lawson-criterion&quot;&gt;The Lawson criterion&lt;/h2&gt;
&lt;p&gt;In 1955, the engineer John Lawson asked the question in its most practical form: when does a fusion
plasma produce more energy than it loses? The modern version for D-T comes down to the &lt;strong&gt;triple
product&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;n × T × τ_E  ≳  3 × 10²¹  keV·s/m³&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;n&lt;/code&gt;: the &lt;strong&gt;density&lt;/strong&gt; of fuel ions. More particles, more collisions.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;T&lt;/code&gt;: the &lt;strong&gt;temperature&lt;/strong&gt;. Hot enough to sit near D-T&#39;s sweet spot.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;τ_E&lt;/code&gt;: the &lt;strong&gt;energy confinement time&lt;/strong&gt;. How long the plasma holds its heat before losing it.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If the triple product clears that bar, the energy from the helium nuclei (which, being charged,
stay in the plasma) is enough to keep the plasma hot without any outside heating. That&#39;s
&lt;strong&gt;ignition&lt;/strong&gt;: a self-sustaining burn, like a fire that no longer needs a match.&lt;/p&gt;
&lt;p&gt;Every fusion approach is a strategy for maximising that product, and they fall into two camps.&lt;/p&gt;
&lt;h2 id=&quot;two-ways-to-bottle-a-star&quot;&gt;Two ways to bottle a star&lt;/h2&gt;
&lt;h3 id=&quot;magnetic-confinement-low-density-long-time&quot;&gt;Magnetic confinement: low density, long time&lt;/h3&gt;
&lt;p&gt;Charged particles in a magnetic field spiral along the field lines. Bend the field lines into a
closed loop, and in principle the particles go round forever without touching a wall.&lt;/p&gt;
&lt;p&gt;A simple ring of field doesn&#39;t quite work. The field is stronger on the inside of the ring than the
outside, and particles drift out. The fix is to &lt;strong&gt;twist&lt;/strong&gt; the field lines as they go round, so each
particle samples both sides and the drifts cancel. There are two ways to do the twisting:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;tokamak&lt;/strong&gt;, a Soviet design from the 1950s, drives a large electric current through the
plasma itself, and the current&#39;s own magnetic field supplies the twist. It&#39;s the most advanced
design, but that current is a source of instabilities. The worst are &lt;strong&gt;disruptions&lt;/strong&gt;, where the
plasma loses confinement in milliseconds and dumps its energy into the walls.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;stellarator&lt;/strong&gt; builds the twist into the external magnets, which are wound into bizarre
three-dimensional shapes. No plasma current is needed, so it&#39;s naturally steady and calmer, but
the magnets are much harder to design and build. Germany&#39;s Wendelstein 7-X is the flagship.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Magnetic confinement works with thin plasma, around a millionth of the density of air, held for
seconds or longer. The European JET tokamak in the UK set the record for total fusion energy in a
single pulse, 69 megajoules over about five seconds, in its final campaign in 2023 before it shut
down. &lt;strong&gt;ITER&lt;/strong&gt;, the enormous international tokamak under construction in southern France, is meant
to produce ten times more fusion power than the heating it receives. Its schedule has slipped
repeatedly; under its 2024 baseline, deuterium–tritium operation isn&#39;t due until the late 2030s.&lt;/p&gt;
&lt;p&gt;The most interesting recent shift is in &lt;strong&gt;magnets&lt;/strong&gt;. A tokamak&#39;s performance rises steeply with
magnetic field strength. New &lt;strong&gt;high-temperature superconducting&lt;/strong&gt; tape (REBCO) can produce much
stronger fields than the superconductors ITER uses, which means a reactor could get the same
performance in a far smaller machine. Commonwealth Fusion Systems demonstrated a 20-tesla magnet
built this way in 2021 and is building a compact tokamak called &lt;strong&gt;SPARC&lt;/strong&gt; around it, aiming to show
net energy gain in the plasma later this decade.&lt;/p&gt;
&lt;h3 id=&quot;inertial-confinement-high-density-tiny-time&quot;&gt;Inertial confinement: high density, tiny time&lt;/h3&gt;
&lt;p&gt;The other camp turns the problem inside out. Instead of holding a thin plasma for a long time, crush
a speck of fuel to enormous density and let it burn in the instant before it flies apart. The fuel&#39;s
own inertia is the &quot;container&quot;.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;National Ignition Facility (NIF)&lt;/strong&gt; in California does this with 192 lasers. They fire into a
small gold cylinder called a &lt;strong&gt;hohlraum&lt;/strong&gt;, about the size of a pencil eraser, turning the laser light
into X-rays. Those X-rays blast the surface off a peppercorn-sized capsule of frozen D-T, and the
recoil compresses the fuel inward like a rocket in reverse. For a few billionths of a second the
centre reaches over 100 million kelvin and densities around a hundred times that of lead.&lt;/p&gt;
&lt;p&gt;On 5 December 2022, NIF delivered 2.05 MJ of laser energy to the target and got &lt;strong&gt;3.15 MJ&lt;/strong&gt; of
fusion energy back. For the first time, a controlled fusion experiment produced more energy than
was put into the fuel. NIF has repeated and beaten that since, reaching yields of around 8.6 MJ in
2025.&lt;/p&gt;
&lt;h2 id=&quot;gain-the-fine-print&quot;&gt;Gain: the fine print&lt;/h2&gt;
&lt;p&gt;Headlines about &quot;net energy&quot; need careful reading, and this is where my engineering side wants to
be pedantic. There are several different ratios people call &lt;strong&gt;Q&lt;/strong&gt;:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Measure&lt;/th&gt;&lt;th&gt;What it compares&lt;/th&gt;&lt;th&gt;Status&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Scientific gain&lt;/td&gt;&lt;td&gt;Fusion energy out ÷ energy delivered to the fuel&lt;/td&gt;&lt;td&gt;Above 1 at NIF since 2022&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Facility gain&lt;/td&gt;&lt;td&gt;Fusion energy out ÷ energy drawn from the grid to run the machine&lt;/td&gt;&lt;td&gt;Far below 1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Engineering gain&lt;/td&gt;&lt;td&gt;Electricity sold ÷ electricity used&lt;/td&gt;&lt;td&gt;Not yet attempted&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;NIF&#39;s lasers draw around 300 MJ from the grid to deliver those 2 MJ of laser light, because the
lasers are about 1% efficient (they were built in the 1990s for weapons research, not efficiency).
And a power plant would then need to convert fusion heat to electricity at maybe 40%, and fire
several shots &lt;strong&gt;per second&lt;/strong&gt;, not one a day. The 2022 result was a genuine scientific milestone.
It&#39;s not yet a power plant, nor close to one.&lt;/p&gt;
&lt;h2 id=&quot;the-problems-after-ignition&quot;&gt;The problems after ignition&lt;/h2&gt;
&lt;p&gt;Getting the plasma to burn is the famous problem. Turning that into a power station brings a second
list, and these are mostly materials and engineering problems, not physics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The neutrons.&lt;/strong&gt; Those 14.1 MeV neutrons carry 80% of the energy and hammer the walls, knocking
atoms out of place and making the structure brittle and mildly radioactive over time. We still
need materials that can survive years of it. A dedicated test facility (IFMIF-DONES in Spain) is
being built specifically for this.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tritium.&lt;/strong&gt; Tritium is radioactive with a half-life of 12.3 years, so there&#39;s almost none in
nature, and the world&#39;s civilian stockpile is only a few tens of kilograms. A reactor would have
to &lt;strong&gt;breed&lt;/strong&gt; its own by surrounding the plasma with a lithium &lt;strong&gt;blanket&lt;/strong&gt;, where neutrons do
&lt;code&gt;⁶Li + n → ³H + ⁴He&lt;/code&gt;. Breeding more tritium than you burn, reliably, has never been demonstrated.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The exhaust.&lt;/strong&gt; Heat leaving the plasma is funnelled onto a component called the &lt;strong&gt;divertor&lt;/strong&gt;,
which faces heat fluxes comparable to a spacecraft re-entering the atmosphere, continuously.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Staying on.&lt;/strong&gt; A power plant needs to run steadily for months. Tokamaks have to manage
instabilities, and laser plants would need to make, place and hit hundreds of thousands of
precision targets every day, cheaply.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;why-bother&quot;&gt;Why bother?&lt;/h2&gt;
&lt;p&gt;Because the payoff is extraordinary, and the downsides compared with fission are small:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Fuel.&lt;/strong&gt; Deuterium can be extracted from seawater, about one in every 6,400 hydrogen atoms.
Lithium for breeding tritium is plentiful. There is fuel for millions of years.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No runaway.&lt;/strong&gt; There&#39;s no chain reaction. A fusion plasma is so hard to sustain that any fault
makes it go out. The total fuel in the chamber at any moment is around a gram.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No long-lived waste.&lt;/strong&gt; The helium product is harmless. The reactor structure becomes
radioactive from the neutrons, but with the right materials that decays over decades to a century,
not millennia.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No carbon&lt;/strong&gt;, and a power station that runs regardless of weather, unlike wind and solar.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Fusion is hard for one reason that sits on top of everything else: positively charged nuclei don&#39;t
want to get close to each other, and the only way past that is to make them so hot no material
can hold them. The rest of the story follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;D-T fusion&lt;/strong&gt; releases 17.6 MeV per reaction and about four times more energy per kilogram than
fission.&lt;/li&gt;
&lt;li&gt;Nuclei only fuse by &lt;strong&gt;quantum tunnelling&lt;/strong&gt; through the Coulomb barrier, even at 150 million kelvin.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lawson&#39;s triple product&lt;/strong&gt;, density × temperature × confinement time, is the scorecard every
machine is judged by.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scientific breakeven has happened&lt;/strong&gt;, at NIF in 2022, but engineering breakeven, with real
electricity, hasn&#39;t.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I&#39;ve come away from writing this series more optimistic than I expected. The 30-year joke used to be
funny because nobody had crossed a single real milestone. That&#39;s no longer true. There&#39;s still a
long engineering road between &quot;it works&quot; and &quot;it&#39;s on the grid&quot;, but for the first time, it&#39;s an
engineering road.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Common Gotchas When Working with Rust as a Python Developer</title>
        <published>2026-02-02T00:00:00+00:00</published>
        <updated>2026-02-02T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/common-gotchas-when-working-with-rust-as-a-python-developer/"/>
        <id>https://brianobot.tech/blog/common-gotchas-when-working-with-rust-as-a-python-developer/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/common-gotchas-when-working-with-rust-as-a-python-developer/">&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/common-gotchas-when-working-with-rust-as-a-python-developer/cover.webp&quot; alt=&quot;Snow-capped mountain peak against a clear blue sky above dark forested hills&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Transitioning from Python to Rust can present a myriad of unexpected challenges for developers. By identifying these common gotchas and understanding the nuances of Rust’s design philosophy, you can streamline your learning curve and develop more efficient code. In this guide, I’ll detail my experiences with the Rust programming language and provide illustrative examples to help ease your journey.&lt;/p&gt;
&lt;h2 id=&quot;tl-dr&quot;&gt;TL;DR;&lt;/h2&gt;
&lt;p&gt;Be mindful of specific Rust behaviors, such as explicit flushing of standard output, understanding ownership and borrowing, and mastering error handling.&lt;/p&gt;
&lt;h2 id=&quot;table-of-contents&quot;&gt;Table of Contents&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;1. Explicit Call to Flush Standard Output&lt;/li&gt;
&lt;li&gt;2. Understanding Ownership and Borrowing&lt;/li&gt;
&lt;li&gt;3. Error Handling: Comparisons between Rust and Python&lt;/li&gt;
&lt;li&gt;4. Differences in Concurrency Models&lt;/li&gt;
&lt;li&gt;5. Memory Management: Safe and Unsafe Rust&lt;/li&gt;
&lt;li&gt;6. Performance Challenges and Optimization Tips&lt;/li&gt;
&lt;li&gt;7. Conclusion&lt;/li&gt;
&lt;li&gt;8. Technical Deep-Dive Sections&lt;/li&gt;
&lt;li&gt;8.1 Advanced Ownership and Borrowing&lt;/li&gt;
&lt;li&gt;8.2 In-Depth Error Handling Techniques&lt;/li&gt;
&lt;li&gt;8.3 Concurrency Control in Depth&lt;/li&gt;
&lt;li&gt;9. Summary and Further Reading&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;1-explicit-call-to-flush-standard-output&quot;&gt;1. Explicit Call to Flush Standard Output&lt;/h2&gt;
&lt;p&gt;When creating command-line applications, developers used to Python’s straightforward &lt;code&gt;input&lt;/code&gt; function may face puzzling output behavior in Rust. The simple Python command below is familiar for prompting user input:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;user_input&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; input&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Enter your name: &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This line effectively handles output and input seamlessly. However, when converting this to Rust, you may encounter unexpected behavior:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mut&lt;/span&gt;&lt;span&gt; user_input&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;print!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Enter your name: &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;stdin&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;read_line&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;mut&lt;/span&gt;&lt;span&gt; user_input&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Upon execution, the prompt may not display until after the user enters their input, making it appear unresponsive. This behavior stems from Rust’s output buffering in the context of the &lt;code&gt;print!&lt;/code&gt; macro.&lt;/p&gt;
&lt;h3 id=&quot;what-s-missing&quot;&gt;What’s Missing&lt;/h3&gt;
&lt;p&gt;The oversight here is that Rust does not automatically flush standard output when using the &lt;code&gt;print!&lt;/code&gt; macro. For immediate visibility, developers must incorporate an explicit flush:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mut&lt;/span&gt;&lt;span&gt; user_input&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;print!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Enter your name: &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;flush&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Flush the output to show the prompt immediately&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;stdin&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;read_line&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;mut&lt;/span&gt;&lt;span&gt; user_input&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;practical-tips&quot;&gt;Practical Tips:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Always remember to include &lt;code&gt;use std::io::{self, Write};&lt;/code&gt; at the top of your Rust file if you use flushing methods.&lt;/li&gt;
&lt;li&gt;Create a reusable helper function to wrap &lt;code&gt;print!&lt;/code&gt; and flush calls to maintain consistency and improve code readability.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;2-understanding-ownership-and-borrowing&quot;&gt;2. Understanding Ownership and Borrowing&lt;/h2&gt;
&lt;p&gt;Rust’s ownership model, rooted in ensuring memory safety without a garbage collector, can be one of the most challenging paradigm shifts for Python developers. In Python, it’s common to pass around variables without strict ownership rules. In Rust, however, understanding these principles can significantly affect your code’s safety and performance.&lt;/p&gt;
&lt;h3 id=&quot;ownership-rules&quot;&gt;Ownership Rules&lt;/h3&gt;
&lt;p&gt;The essence of Rust’s ownership system includes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;One Owner at a Time: Each value in Rust has a single owner at any point.&lt;/li&gt;
&lt;li&gt;Dropping Values: When the owner goes out of scope, Rust automatically calls the &lt;code&gt;drop&lt;/code&gt; method to handle memory cleanup, thus preventing memory leaks.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Consider this Rust example demonstrating ownership transfer:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; s1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Hello&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; s2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; s1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; s1’s ownership is transferred to s2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; println!(&amp;quot;{}&amp;quot;, s1); // This will NOT compile; s1 is now invalid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;borrowing-safe-and-mutable-references&quot;&gt;Borrowing: Safe and Mutable References&lt;/h3&gt;
&lt;p&gt;To enable functions to access data without taking ownership, Rust introduces borrowing. There are two forms of borrowing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Immutable References: Denoted by &lt;code&gt;&amp;amp;value&lt;/code&gt;, allowing multiple references simultaneously.&lt;/li&gt;
&lt;li&gt;Mutable References: Denoted by &lt;code&gt;&amp;amp;mut value&lt;/code&gt;, permitting only one mutable reference at a time—thereby preventing data races.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;example-borrowing&quot;&gt;Example: Borrowing&lt;/h3&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; s1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Hello&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; len&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; calculate_length&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;s1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Borrowing s1 immutably&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;    println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Length of &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39; is &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; s1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; len&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; calculate_length&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; usize&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    s&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Borrowing does not take ownership&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This code illustrates how ownership and borrowing ensure safe access to memory, eliminating dangling pointers and data races.&lt;/p&gt;
&lt;h3 id=&quot;practical-tips-1&quot;&gt;Practical Tips:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Use ownership transfers judiciously to maintain performance; passing by reference often helps avoid excessive copying of data.&lt;/li&gt;
&lt;li&gt;Familiarize yourself with Rust’s lifetime annotations to guide the borrowing system in complex scenarios—especially when dealing with multiple references.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;3-error-handling-comparisons-between-rust-and-python&quot;&gt;3. Error Handling: Comparisons between Rust and Python&lt;/h2&gt;
&lt;p&gt;Rust’s approach to error handling is fundamentally different from Python’s exception system. Developers coming from Python, used to catching errors via &lt;code&gt;try&lt;/code&gt; and &lt;code&gt;except&lt;/code&gt;, may find Rust&#39;s explicit error handling both enlightening and demanding.&lt;/p&gt;
&lt;h3 id=&quot;rust-s-result-type&quot;&gt;Rust’s Result Type&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;Result&amp;lt;T, E&amp;gt;&lt;/code&gt; enum signifies outcomes that may succeed (type &lt;code&gt;T&lt;/code&gt;) or fail (type &lt;code&gt;E&lt;/code&gt;). This encourages handling errors explicitly at compile time:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-12&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Read&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Result&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; io&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Opening a non-existent file will trigger an error&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mut&lt;/span&gt;&lt;span&gt; file&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; File&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;hello.txt&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Propagates error&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mut&lt;/span&gt;&lt;span&gt; content&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    file&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;read_to_string&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;mut&lt;/span&gt;&lt;span&gt; content&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;    Ok&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using the &lt;code&gt;?&lt;/code&gt; operator provides a concise mechanism for error propagation; if an error occurs, it exits the function immediately.&lt;/p&gt;
&lt;h3 id=&quot;comparing-with-python&quot;&gt;Comparing with Python&lt;/h3&gt;
&lt;p&gt;In Python:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    with&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; open&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;hello.txt&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; as&lt;/span&gt;&lt;span&gt; f&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        data&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; f&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;except&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; FileNotFoundError&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; as&lt;/span&gt;&lt;span&gt; e&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;File not found:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; e&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This utilizes exception handling elegantly, enabling clearer control flow, but potentially obscuring some error states.&lt;/p&gt;
&lt;h3 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h3&gt;
&lt;p&gt;Rust compels developers to handle errors, enhancing robustness but injecting verbosity compared to Python’s simpler exception handling approach. Each potential error must be considered in Rust, leading to clearer, more maintainable code.&lt;/p&gt;
&lt;h3 id=&quot;practical-tips-2&quot;&gt;Practical Tips:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Leverage crates like &lt;code&gt;anyhow&lt;/code&gt; for error handling that simplifies error definitions and captures more context on errors.&lt;/li&gt;
&lt;li&gt;Establish custom error types with the &lt;code&gt;thiserror&lt;/code&gt; crate to enhance clarity when logging and debugging.&lt;/li&gt;
&lt;li&gt;Write unit tests to cover various error scenarios, which functions can produce, reinforcing the significance of explicit error handling.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;4-differences-in-concurrency-models&quot;&gt;4. Differences in Concurrency Models&lt;/h2&gt;
&lt;p&gt;Rust introduces a unique model for concurrency that guarantees the absence of data races at compile time, diverging significantly from Python’s Global Interpreter Lock (GIL).&lt;/p&gt;
&lt;h3 id=&quot;safe-concurrency-in-rust&quot;&gt;Safe Concurrency in Rust&lt;/h3&gt;
&lt;p&gt;Using &lt;code&gt;Mutex&lt;/code&gt;, &lt;code&gt;Arc&lt;/code&gt;, and other synchronization primitives, Rust allows for safe data access across threads. Here&#39;s an example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt;sync&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;Arc&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Mutex&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span&gt;thread&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; counter&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Arc&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt;Mutex&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Shared mutable state initialization&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mut&lt;/span&gt;&lt;span&gt; handles&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; vec!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    for&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        let&lt;/span&gt;&lt;span&gt; counter_clone&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Arc&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;clone&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;counter&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        let&lt;/span&gt;&lt;span&gt; handle&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt; thread&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;spawn&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;            let&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mut&lt;/span&gt;&lt;span&gt; num&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; counter_clone&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;lock&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Safely lock the mutex&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;            *&lt;/span&gt;&lt;span&gt;num&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Mutable access&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        handles&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;handle&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    for&lt;/span&gt;&lt;span&gt; handle&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; handles&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        handle&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Wait for threads to complete&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;    println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Result: &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt;counter&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;lock&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Final count&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;python-s-gil&quot;&gt;Python’s GIL&lt;/h3&gt;
&lt;p&gt;In Python, threading is limited by the GIL, allowing only one thread to execute bytecode at a time. This presents challenges for CPU-bound applications, limiting developers when attempting to achieve better scalability.&lt;/p&gt;
&lt;h3 id=&quot;practical-tips-3&quot;&gt;Practical Tips:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Utilize Rust’s concurrency features, such as &lt;code&gt;rayon&lt;/code&gt;, for more straightforward parallel data processing without manual thread management.&lt;/li&gt;
&lt;li&gt;When translating Python code to Rust, assess if operations can be concurrent and leverage Rust’s concurrency abstractions for efficiency.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;5-memory-management-safe-and-unsafe-rust&quot;&gt;5. Memory Management: Safe and Unsafe Rust&lt;/h2&gt;
&lt;p&gt;Memory safety and management are core aspects of Rust’s design philosophy, emphasizing prevention of the common pitfalls found in many programming languages, including Python.&lt;/p&gt;
&lt;h3 id=&quot;safe-rust&quot;&gt;Safe Rust&lt;/h3&gt;
&lt;p&gt;The majority of Rust code adheres to “safe” coding practices, where the compiler enforces ownership rules and ensures no data is accessed incorrectly. This minimizes issues such as dereferencing null pointers and ensuring memory leaks do not occur.&lt;/p&gt;
&lt;h3 id=&quot;unsafe-rust&quot;&gt;Unsafe Rust&lt;/h3&gt;
&lt;p&gt;For certain performance-sensitive or system-level development, developers may employ &lt;code&gt;unsafe&lt;/code&gt; code blocks. This provides flexibility to opt out of Rust&#39;s safety checks when necessary:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span&gt; r&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; i32&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Creating a raw pointer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;unsafe&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;    println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Dereferencing a raw pointer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Utilizing unsafe features allows manipulation of memory in powerful ways; however, employing unsafe code should always be approached with caution.&lt;/p&gt;
&lt;h3 id=&quot;practical-tips-4&quot;&gt;Practical Tips:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Review crates such as &lt;code&gt;unsafe-code-guidelines&lt;/code&gt; to further your understanding of best practices when working with unsafe code.&lt;/li&gt;
&lt;li&gt;Regularly evaluate whether your code can accomplish needs without unsafe blocks; strive to keep unsafe code isolated and manageable.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;6-performance-challenges-and-optimization-tips&quot;&gt;6. Performance Challenges and Optimization Tips&lt;/h2&gt;
&lt;p&gt;Rust’s capabilities are often celebrated for their performance benefits, yet to fully realize these advantages, a comprehensive approach to optimization is essential.&lt;/p&gt;
&lt;h3 id=&quot;performance-optimization-tips&quot;&gt;Performance Optimization Tips:&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Avoid Unnecessary Cloning: Instead of relying on cloning structures, utilize references whenever feasible. This reduces overhead and enhances performance in tight loops.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; process_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;Vec&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Efficient handling of data without cloning&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Perform operations directly on the data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;Profile Your Code: Utilize profiling tools such as &lt;code&gt;cargo flamegraph&lt;/code&gt; or &lt;code&gt;cargo bench&lt;/code&gt; for pinpointing performance bottlenecks. Profiling provides insights into where optimizations can save processing time.&lt;/li&gt;
&lt;li&gt;Utilize Smart Pointers: Use smart pointers, including &lt;code&gt;Rc&lt;/code&gt;, &lt;code&gt;Arc&lt;/code&gt;, and &lt;code&gt;RefCell&lt;/code&gt;, to manage complex data ownership situations. For straightforward heap allocations, &lt;code&gt;Box&lt;/code&gt; is a great choice.&lt;/li&gt;
&lt;li&gt;Opt For Idiomatic Rust Before Optimizing: Start with writing intuitive and maintainable code. Only engage in optimization when profiling reveals specific performance concerns to address.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;example-of-optimized-code&quot;&gt;Example of Optimized Code:&lt;/h3&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;HashMap&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; count_word_frequencies&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;Vec&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; HashMap&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; i32&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mut&lt;/span&gt;&lt;span&gt; frequency_map&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; HashMap&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; i32&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    for&lt;/span&gt;&lt;span&gt; word&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; words&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;        *&lt;/span&gt;&lt;span&gt;frequency_map&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;entry&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;word&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;clone&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;or_insert&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Increment frequency count&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    frequency_map&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;7-technical-deep-dive-sections&quot;&gt;7. Technical Deep-Dive Sections&lt;/h2&gt;
&lt;h2 id=&quot;7-1-advanced-ownership-and-borrowing&quot;&gt;7.1 Advanced Ownership and Borrowing&lt;/h2&gt;
&lt;p&gt;Ownership and borrowing mechanics bear deeper investigation, particularly when applied to advanced data structures, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Complex Ownership Scenarios: The interactions between ownership in structs and enums and how lifetime annotations help maintain correctness.&lt;/li&gt;
&lt;li&gt;Dynamic Borrowing: Secrets of borrowing across function calls, especially within closures, and the implications of mutable borrowing with respect to data access.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;7-2-in-depth-error-handling-techniques&quot;&gt;7.2 In-Depth Error Handling Techniques&lt;/h2&gt;
&lt;p&gt;Delve deeper into Rust’s error processing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Creating Custom Error Types: Instruction for establishing distinct error types with enums and using the &lt;code&gt;thiserror&lt;/code&gt; crate for ease of error propagation.&lt;/li&gt;
&lt;li&gt;Handling Errors Gracefully: Strategies for managing complex error chains and ensuring the implementation remains user-friendly.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;7-3-concurrency-control-in-depth&quot;&gt;7.3 Concurrency Control in Depth&lt;/h2&gt;
&lt;p&gt;Concurrency in Rust is vital for building efficient applications:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Advanced Synchronization Primitives: Discover the uses of &lt;code&gt;RwLock&lt;/code&gt; and &lt;code&gt;Condvar&lt;/code&gt;, the contexts for their application, and trade-offs between performance and safety.&lt;/li&gt;
&lt;li&gt;Async Programming in Rust: Exploration of asynchronous paradigms, including &lt;code&gt;async/await&lt;/code&gt;, futures, and how to integrate them into the existing Rust infrastructure seamlessly.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;8-summary-and-further-reading&quot;&gt;8. Summary and Further Reading&lt;/h2&gt;
&lt;p&gt;This guide presents insights into common challenges developers face when migrating from Python to Rust, accentuating the differences in ownership, error management, and concurrency.&lt;/p&gt;
&lt;p&gt;To extend your understanding, consider these resources:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://doc.rust-lang.org/book/&quot;&gt;Rust Programming Language Book&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Articles detailing Rust’s &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html&quot;&gt;ownership model&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Discussions focused on &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://rust-lang.github.io/async-book/&quot;&gt;Asynchronous Programming&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Stop Writing Manual Validation: Master Pydantic</title>
        <published>2026-02-02T00:00:00+00:00</published>
        <updated>2026-02-02T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/stop-writing-manual-validation-master-pydantic/"/>
        <id>https://brianobot.tech/blog/stop-writing-manual-validation-master-pydantic/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/stop-writing-manual-validation-master-pydantic/">&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/stop-writing-manual-validation-master-pydantic/cover.webp&quot; alt=&quot;Snow-covered mountain face and glacier under a dark blue sky&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Validation is a crucial part of any software that collects data from users or other external sources. This ensures that the data used in your program or application remains correct and well-structured. Serialization, which is the process of converting data from one format to another for transmission or storage, is also essential to ensure that we use the best data format for the right scenario (for example, using JSON for transmission and objects for storage in a database).&lt;/p&gt;
&lt;p&gt;Pydantic is a library in the Python ecosystem that elegantly solves both use cases. To benefit greatly from the content of this article, it is assumed that you have familiarity with Python programming and a basic understanding of Object-Oriented Programming in Python.&lt;/p&gt;
&lt;h2 id=&quot;why-pydantic&quot;&gt;Why Pydantic&lt;/h2&gt;
&lt;h2 id=&quot;understanding-data-validation-in-traditional-python&quot;&gt;Understanding Data Validation in Traditional Python&lt;/h2&gt;
&lt;p&gt;Before diving into Pydantic code, let’s explore how data validation can be handled for a practical scenario in plain, regular Python. Assume we have a software application for students in a university to register their courses for the semester.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; A class to store student data; this can be an instance of a record in the database&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Student&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-1 z-l-i z-d-3 z-d-i&quot;&gt; __init__&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-12 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; year&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-12 z-d-i&quot;&gt;        self&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-12 z-d-i&quot;&gt;        self&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; email&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-12 z-d-i&quot;&gt;        self&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;year&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; year&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-12 z-d-i&quot;&gt;        self&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;courses&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; set&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; add_course&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-12 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; course&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-12 z-d-i&quot;&gt;        self&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;courses&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;course&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; remove_course&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-12 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; course&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-12 z-d-i&quot;&gt;        self&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;courses&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;course&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Function to handle student registration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; register_student&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; year&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; Student&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; year&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a simple way to represent a student record in our application, allowing for the storage and manipulation of student data through methods like adding or removing courses. However, validation for attributes like &lt;code&gt;year&lt;/code&gt; can become intricate and verbose.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Adding validation with an updated function version&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; register_student_v2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; year&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    if&lt;/span&gt;&lt;span&gt; year&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; not&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; range&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; from 1 to 6, 7 is not included&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        raise&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; ValueError&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Student Year must be between 1 and 6 years&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; Student&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; year&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    james&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; register_student_v2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;James Okon&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;james@mail.com&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; year&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    grace&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; register_student_v2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Grace Mabel&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;grace@gmail.com&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; year&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    brian&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; register_student_v2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Brian Obot&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;brianobot9@gmail.com&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; year&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;except&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; ValueError&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; as&lt;/span&gt;&lt;span&gt; err&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Failed to register student because: &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;err&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This illustrates how we incorporate validation for &lt;code&gt;year&lt;/code&gt;, ensuring it meets predetermined requirements. However, adding validation checks for other properties requires additional verbose code, leading to repetitive structures.&lt;/p&gt;
&lt;h2 id=&quot;why-to-use-pydantic&quot;&gt;Why To Use Pydantic?&lt;/h2&gt;
&lt;p&gt;Enter Pydantic. Here are some compelling reasons why Pydantic is an excellent option for validating and serializing data:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Performance: Pydantic’s core validation logic is implemented in Rust; as a result, it stands out as one of the fastest data validation libraries for Python.&lt;/li&gt;
&lt;li&gt;Type Hints Powered Schema Validation: The schema that Pydantic uses to validate data is generally defined using Python type annotations/hints.&lt;/li&gt;
&lt;li&gt;Serialization: Pydantic excels at data serialization, crucial when converting data for network transfer or file storage.&lt;/li&gt;
&lt;li&gt;Strict Mode and Data Coercion: Pydantic attempts to coerce incorrect data into the right type by default, converting entries like numeric strings to integers as appropriate.&lt;/li&gt;
&lt;li&gt;Customization: Custom validation logic fits unique use cases seamlessly within Pydantic’s framework.&lt;/li&gt;
&lt;li&gt;Extensive Use in Big Projects: Many significant projects, especially in AI, leverage Pydantic, making it an essential skill for a Python developer.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;introduction-to-pydantic&quot;&gt;Introduction to Pydantic&lt;/h2&gt;
&lt;p&gt;Pydantic is the most widely used data validation library for Python. It allows users to define validation and serialization rules through type annotations, enabling robust data management practices.&lt;/p&gt;
&lt;h2 id=&quot;basic-usage-example&quot;&gt;Basic Usage Example&lt;/h2&gt;
&lt;p&gt;Let’s revisit the student registration example but utilize Pydantic for validation.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; typing&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; Annotated&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; pydantic&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; Field&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; EmailStr&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; ValidationError&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Student&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; EmailStr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    year&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; Annotated&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; Field&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;ge&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; le&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    james&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; Student&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;James Okon&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;james@mail.com&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; year&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    grace&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; Student&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Grace Mabel&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;grace@gmail.com&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; year&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    brian&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; Student&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Brian Obot&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;brianobot9@gmail.com&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; year&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;except&lt;/span&gt;&lt;span&gt; ValidationError&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; as&lt;/span&gt;&lt;span&gt; err&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Failed to register student because: &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;err&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;james&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;grace&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;brian&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In just a few lines, we leverage Pydantic to define validations, resulting in cleaner and more maintainable code. The validation error messages also provide immediate feedback, making debugging simpler.&lt;/p&gt;
&lt;h2 id=&quot;key-features-of-pydantic&quot;&gt;Key Features of Pydantic&lt;/h2&gt;
&lt;h2 id=&quot;type-annotation-the-power-of-conventions&quot;&gt;Type Annotation: The Power of Conventions&lt;/h2&gt;
&lt;p&gt;Pydantic’s expressive validation derives from Python’s type annotation system, allowing developers to explicitly define variable types. Here’s how type annotation works:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Bruce Wayne&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;age&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 43&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;height_in_feets&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 6.4&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This flexibility enables you to define types without runtime enforcement. However, Pydantic utilizes annotations to enforce these checks when models are instantiated.&lt;/p&gt;
&lt;h2 id=&quot;common-type-annotations-in-pydantic&quot;&gt;Common Type Annotations in Pydantic&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;str: Represents any string type, useful for names and addresses.&lt;/li&gt;
&lt;li&gt;int: Represents whole numbers.&lt;/li&gt;
&lt;li&gt;float: Represents floating-point numbers.&lt;/li&gt;
&lt;li&gt;EmailStr: A specialized type to validate email formats right out of the box.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;data-coercion-with-pydantic&quot;&gt;Data Coercion with Pydantic&lt;/h2&gt;
&lt;p&gt;Pydantic performs automatic data coercion, allowing types to be converted when possible to match defined schemas. This feature lessens the need for redundant conversion checks.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Example&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    age&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;example&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; Example&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;example&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;age&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Output: 25&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this instance, Pydantic auto-converts the string &lt;code&gt;&#39;25&#39;&lt;/code&gt; to the integer &lt;code&gt;25&lt;/code&gt;, enhancing code robustness and reducing potential user error.&lt;/p&gt;
&lt;h2 id=&quot;technical-deep-dive-sections&quot;&gt;Technical Deep-Dive Sections&lt;/h2&gt;
&lt;h2 id=&quot;advanced-validation-techniques&quot;&gt;Advanced Validation Techniques&lt;/h2&gt;
&lt;h3 id=&quot;custom-validators&quot;&gt;Custom Validators&lt;/h3&gt;
&lt;p&gt;Pydantic supports validators that can enforce complex rules or conditions for model attributes. This offers control over how data is validated and transformed.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; pydantic&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; validator&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Student&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; EmailStr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    year&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    @&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;validator&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;year&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; validate_year&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        if&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; or&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;            raise&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; ValueError&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Year must be between 1 and 6&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        return&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The custom validator in the &lt;code&gt;Student&lt;/code&gt; model ensures that the &lt;code&gt;year&lt;/code&gt; is validated against the specified criteria.&lt;/p&gt;
&lt;h3 id=&quot;built-in-and-user-defined-validations&quot;&gt;Built-in and User-defined Validations&lt;/h3&gt;
&lt;p&gt;Utilizing built-in validators can tackle most common cases. However, when more complex rules are necessary, user-defined validation methods can introduce needed logic.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; User-defined validators for format and criteria&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; User&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    username&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    age&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    @&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;validator&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; username_must_not_contain_spaces&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;            raise&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; ValueError&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Username must not contain spaces&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        return&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;User-defined validators provide high customization based on specific app needs, thereby maintaining code clarity.&lt;/p&gt;
&lt;h2 id=&quot;serialization-and-deserialization-with-pydantic&quot;&gt;Serialization and Deserialization with Pydantic&lt;/h2&gt;
&lt;h3 id=&quot;transforming-data-formats&quot;&gt;Transforming Data Formats&lt;/h3&gt;
&lt;p&gt;Serialization allows Python objects to be transformed into formats such as JSON. Pydantic simplifies this process, allowing for straightforward conversion, ensuring that complex data types are adequately formatted.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;student&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; Student&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;James Okon&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;james@mail.com&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; year&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;student_json&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; student&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;student_json&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This snippet serializes the &lt;code&gt;Student&lt;/code&gt; instance into JSON for efficient transmission across web services or for storage.&lt;/p&gt;
&lt;h3 id=&quot;deserializing-data&quot;&gt;Deserializing Data&lt;/h3&gt;
&lt;p&gt;Pydantic also parses JSON strings into Python objects, automatically inferring types and validating data upon model construction.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;data&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;{&amp;quot;name&amp;quot;: &amp;quot;James Okon&amp;quot;, &amp;quot;email&amp;quot;: &amp;quot;james@mail.com&amp;quot;, &amp;quot;year&amp;quot;: 2}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;student_instance&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; Student&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;parse_raw&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;student_instance&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This example illustrates how incoming data can be efficiently parsed and validated, streamlining data intake processes.&lt;/p&gt;
&lt;h2 id=&quot;working-with-nested-models&quot;&gt;Working with Nested Models&lt;/h2&gt;
&lt;h3 id=&quot;creating-hierarchical-data-structures&quot;&gt;Creating Hierarchical Data Structures&lt;/h3&gt;
&lt;p&gt;Pydantic’s design allows for the representation of nested data structures through multiple models. This design is particularly useful in RESTful APIs where data returned often contains multiple layers.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; typing&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; List&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Course&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    title&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    credits&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; StudentWithCourses&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; EmailStr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    courses&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; List&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;Course&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This model shows how Pydantic handles complex hierarchies and encapsulates related data naturally.&lt;/p&gt;
&lt;h3 id=&quot;handling-lists-and-dicts&quot;&gt;Handling Lists and Dicts&lt;/h3&gt;
&lt;p&gt;When collections like lists or dictionaries come into play, Pydantic validates and parses items effectively, facilitating robust data handling strategies.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; GroupedStudents&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    group_name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    students&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; List&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;group&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; GroupedStudents&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;group_name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Freshmen&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; students&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span&gt;james&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; grace&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;group&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, Pydantic allows seamless handling of multi-part data constructs, making it a powerful tool for managing related datasets.&lt;/p&gt;
&lt;h2 id=&quot;edge-cases-with-pydantic&quot;&gt;Edge Cases with Pydantic&lt;/h2&gt;
&lt;h3 id=&quot;handling-optional-fields&quot;&gt;Handling Optional Fields&lt;/h3&gt;
&lt;p&gt;Often, datasets contain optional fields. Pydantic allows these fields to be declared using &lt;code&gt;Optional&lt;/code&gt; and will validate their presence and data type correctly.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; typing&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; Optional&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; StudentWithOptional&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; EmailStr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    year&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    phone&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; Optional&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; None&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; This field is optional&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;student&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; StudentWithOptional&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Mary Jane&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;mary.jane@mail.com&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; year&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;student&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;phone&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Output: None because no phone number was provided&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this case, the &lt;code&gt;phone&lt;/code&gt; field can either have a value or be absent, which Pydantic handles gracefully.&lt;/p&gt;
&lt;h3 id=&quot;complex-nested-validations&quot;&gt;Complex Nested Validations&lt;/h3&gt;
&lt;p&gt;Pydantic models can also contain nested models, allowing for validations that span multiple levels of data structures.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Teacher&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; EmailStr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; School&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    teachers&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; List&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;Teacher&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;school_data&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Springfield High&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;teachers&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        {&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Mr. Smith&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;smith@school.com&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        {&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Ms. Johnson&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;johnson@school.com&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;school&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; School&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;**&lt;/span&gt;&lt;span&gt;school_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;school&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This snippet demonstrates how complex data structures can be validated all at once, thanks to Pydantic’s capabilities.&lt;/p&gt;
&lt;h2 id=&quot;real-world-applications-of-pydantic&quot;&gt;Real-World Applications of Pydantic&lt;/h2&gt;
&lt;p&gt;In practical deployment, Pydantic’s utility shines. Many web applications and APIs leverage Pydantic for data validation and serialization, contributing immensely to reliability and clarity of data handling.&lt;/p&gt;
&lt;h2 id=&quot;applications-in-fastapi&quot;&gt;Applications in FastAPI&lt;/h2&gt;
&lt;p&gt;One of the most compelling showcases of Pydantic’s capabilities arises in the context of FastAPI, a modern web framework that utilizes Pydantic heavily for request validation and response serialization.&lt;/p&gt;
&lt;p&gt;Example FastAPI integration with Pydantic is illustrated below:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; FastAPI&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; pydantic&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; BaseModel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;app&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; FastAPI&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Item&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    description&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    price&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    tax&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/items/&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; create_item&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; Item&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; item&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this FastAPI app, Pydantic is instrumental in defining and enforcing expected input formats for the endpoint &lt;code&gt;/items/&lt;/code&gt;, facilitating straightforward integration and error management.&lt;/p&gt;
&lt;h2 id=&quot;applications-in-machine-learning&quot;&gt;Applications in Machine Learning&lt;/h2&gt;
&lt;p&gt;Data validation and processing pipelines in machine learning applications are crucial for managing incoming data. Pydantic ensures that datasets conform to expected formats and types, simplifying preprocessing and reducing errors in training models.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; pydantic&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; BaseModel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; UserProfile&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    age&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; EmailStr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    preferences&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; List&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Processing dimensionality reduction&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; validate_user_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;user_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; dict&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; UserProfile&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; UserProfile&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;**&lt;/span&gt;&lt;span&gt;user_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this snippet, incoming user profiles are validated to ensure the data integrity required for further processing steps, improving model reliability.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In conclusion, Pydantic emerges as an extremely efficient tool for validating data in Python applications. By mastering its core features—including type annotations, built-in serialization, custom validations, and handling complex data structures—Python developers can significantly improve their data validation strategies. Pydantic’s versatility paves the way for easily creating scalable and maintainable codebases that uphold high standards of data integrity.&lt;/p&gt;
&lt;p&gt;As you continue your journey with Pydantic, consider how its features can be applied to enhance the functionality and reliability of your applications. Mastering Pydantic not only enriches your skill set but also positions you favorably for tackling complex challenges in Python software development.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;For more expansive insights and practical examples, refer to the &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://docs.pydantic.dev/latest/&quot;&gt;Pydantic documentation&lt;/a&gt;.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>I Finally Understand References and Lifetimes in Rust</title>
        <published>2026-01-31T00:00:00+00:00</published>
        <updated>2026-01-31T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/i-finally-understand-references-and-lifetimes-in-rust/"/>
        <id>https://brianobot.tech/blog/i-finally-understand-references-and-lifetimes-in-rust/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/i-finally-understand-references-and-lifetimes-in-rust/">&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/i-finally-understand-references-and-lifetimes-in-rust/cover.webp&quot; alt=&quot;Snowy mountain ridge under a deep blue sky&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I have been learning and working with Rust for over a year now, and for most of that time, I felt as if I understood references and how to use them. However, for lifetimes, I made a conscious effort not to get my life entangled with that concept. It wasn’t until recently that I gained a new perspective on them that they finally made sense. Understanding these concepts has been pivotal not just in writing correct code, but also in grasping why Rust’s memory guarantees provide safety and performance.&lt;/p&gt;
&lt;h2 id=&quot;understanding-references-in-rust&quot;&gt;Understanding References in Rust&lt;/h2&gt;
&lt;p&gt;References are pointers to a value or part of data; they don’t store the data themselves, just the pointer to where the data is stored in memory (they store the address of another item in memory). This distinction is crucial for understanding how to manage memory safely in Rust.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/i-finally-understand-references-and-lifetimes-in-rust/image-2.webp&quot; alt=&quot;Diagram: a Rust reference created with let reference_val = &amp;amp;val points to the memory address holding val in a table of addresses and values&quot; /&gt;&lt;/p&gt;
&lt;p&gt;From the image above, val is a variable that holds the value 1. Taking a reference to that variable stores the address of val in another variable.&lt;/p&gt;
&lt;h2 id=&quot;validity-of-references&quot;&gt;Validity of References&lt;/h2&gt;
&lt;p&gt;When you create a reference to a value, that reference should only be valid until that value is freed or changed. When the freeing of the value occurs, it is dropped from memory, and that memory address is no longer valid for that value. Consequently, that reference is also invalidated.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Attempting to use a reference after its associated value has been dropped leads to a compiler error.&lt;/li&gt;
&lt;li&gt;Furthermore, if the value is changed, the memory address of that value can also change.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is especially common for values stored on the Heap, where memory management is dynamic and subject to change. Rust’s compiler enforces these rules, ensuring that all references remain valid during their lifetime.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/i-finally-understand-references-and-lifetimes-in-rust/image-3.webp&quot; alt=&quot;Diagram: after drop(val) frees its memory address, the reference points to an empty slot, so using reference_val would be an error&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;lock-on-behavior-of-references&quot;&gt;Lock-on Behavior of References&lt;/h2&gt;
&lt;p&gt;Valid references can be thought of as lock-on values that, when acquired, ensure that the underlying value is accessed only in read-only mode.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you want to modify that value, the original reference must be mutable, enabling you to change the underlying data.&lt;/li&gt;
&lt;li&gt;Moreover, if the memory address of the original value is changed—either due to reallocation or modification—the reference will become invalid. Accessing it afterwards will result in a compiler error.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This behavior reinforces Rust’s objectives of safe concurrency by preventing dangling references—references that point to a memory location that has already been freed.&lt;/p&gt;
&lt;h2 id=&quot;technical-deep-dive-the-ownership-model-in-rust&quot;&gt;Technical Deep-Dive: The Ownership Model in Rust&lt;/h2&gt;
&lt;h2 id=&quot;key-concepts-of-ownership&quot;&gt;Key Concepts of Ownership&lt;/h2&gt;
&lt;p&gt;Rust employs an ownership model that is central to its design and memory safety guarantees.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Each Value has an Owner: Each value in Rust is associated with a variable that acts as its owner.&lt;/li&gt;
&lt;li&gt;Single Ownership: There can only be one owner of a value at any given time. This guarantees that when the owner goes out of scope, the value is dropped, thereby preventing memory leaks.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;ownership-and-memory-management&quot;&gt;Ownership and Memory Management&lt;/h2&gt;
&lt;p&gt;This unique model fosters straightforward memory management without the need for a garbage collector. The compiler and ownership rules ensure that memory is reclaimed as soon as it is no longer needed, leading to high performance and zero overhead in most cases.&lt;/p&gt;
&lt;h3 id=&quot;example-code-ownership-and-scoping&quot;&gt;Example Code: Ownership and Scoping&lt;/h3&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; string_value&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;      let&lt;/span&gt;&lt;span&gt; greetings&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Hello there!&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; greetings is valid in this scop&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; greetings is no longer valid once we exit this scope.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mut&lt;/span&gt;&lt;span&gt; name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Brian&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;push_str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; David&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;    println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Name: &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, &lt;code&gt;greetings&lt;/code&gt; is a temporary variable that goes out of scope, thereby freeing its associated memory. Meanwhile, the &lt;code&gt;name&lt;/code&gt; variable retains ownership of its data until it goes out of scope.&lt;/p&gt;
&lt;h2 id=&quot;technical-deep-dive-references-lifetimes&quot;&gt;Technical Deep-Dive: References &amp;amp; Lifetimes&lt;/h2&gt;
&lt;h2 id=&quot;what-are-lifetimes&quot;&gt;What are Lifetimes?&lt;/h2&gt;
&lt;p&gt;Lifetimes in Rust ensure that references are valid for as long as they need to be. Every reference in Rust has a lifetime, signifying the scope within which that reference is valid.&lt;/p&gt;
&lt;h3 id=&quot;rules-of-lifetimes&quot;&gt;Rules of Lifetimes:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Each Reference has a Lifetime: This signifies how long the reference is valid in relation to the data it points to.&lt;/li&gt;
&lt;li&gt;No Dangling References: The Rust compiler checks that all references remain valid at compile time, thus preventing any accidental use of dangling references.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;using-lifetimes-in-function-signatures&quot;&gt;Using Lifetimes in Function Signatures&lt;/h2&gt;
&lt;p&gt;When functions take references as parameters, it is crucial to indicate the relationship between the lifetimes of the input and output references using lifetime annotations.&lt;/p&gt;
&lt;h3 id=&quot;example-code-function-with-lifetime-annotations&quot;&gt;Example Code: Function with Lifetime Annotations&lt;/h3&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; longest&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; s2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    if&lt;/span&gt;&lt;span&gt; s1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt; s2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        s1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; else&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        s2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, &lt;code&gt;&#39;a&lt;/code&gt; indicates that both input references must live at least as long as the returned reference. These annotations allow the Rust compiler to enforce correct memory management rules.&lt;/p&gt;
&lt;h2 id=&quot;advanced-concepts-lifetime-elision&quot;&gt;Advanced Concepts: Lifetime Elision&lt;/h2&gt;
&lt;p&gt;Rust also employs lifetime elision, which simplifies writing functions by allowing the compiler to infer lifetimes based on specific patterns. This means you can often omit lifetime annotations in straightforward cases, allowing for cleaner and more concise code.&lt;/p&gt;
&lt;h3 id=&quot;example-code-lifetime-elision-in-practice&quot;&gt;Example Code: Lifetime Elision in Practice&lt;/h3&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; first_word&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; bytes&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;as_bytes&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    for&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;item&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; bytes&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        if&lt;/span&gt;&lt;span&gt; item&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;            return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;..&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Return a slice of the string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;    &amp;amp;&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Return the entire string if no space was found&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the above code, the compiler can infer that the returned reference has the same lifetime as the input &lt;code&gt;s&lt;/code&gt;, thereby avoiding the need for explicit annotations.&lt;/p&gt;
&lt;h2 id=&quot;references-and-lifetimes-in-structs&quot;&gt;References and Lifetimes in Structs&lt;/h2&gt;
&lt;p&gt;Using references dynamically in structs can complicate things, especially when you want to ensure that the struct does not outlive the references it holds.&lt;/p&gt;
&lt;h3 id=&quot;example-code-struct-with-lifetimes&quot;&gt;Example Code: Struct with Lifetimes&lt;/h3&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Book&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    title&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    author&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; title&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Rust Programming&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; author&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Jane Doe&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; book&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Book&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        title&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;title&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        author&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;author&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;    println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Book Title: &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;, Author: &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; book&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span&gt;title&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; book&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span&gt;author&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, the &lt;code&gt;Book&lt;/code&gt; struct contains references to &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;author&lt;/code&gt;. The lifetimes specified through the struct ensure that these references live at least as long as the &lt;code&gt;Book&lt;/code&gt; instance itself.&lt;/p&gt;
&lt;h2 id=&quot;borrowing-mutably-vs-immutably&quot;&gt;Borrowing Mutably vs. Immutably&lt;/h2&gt;
&lt;p&gt;In Rust, when you borrow a value, you can choose to do so immutably or mutably, and this choice has significant implications for lifetimes and references.&lt;/p&gt;
&lt;h3 id=&quot;example-code-immutable-and-mutable-borrowing&quot;&gt;Example Code: Immutable and Mutable Borrowing&lt;/h3&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mut&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 10&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; ref1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Immutable borrow&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; let ref2 = &amp;amp;mut value; // Error: cannot borrow `value` as mutable because it is also borrowed as immutable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;    println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Immutable Reference: &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; ref1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; ref2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;mut&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Mutable borrow after immutable borrow ends&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;    *&lt;/span&gt;&lt;span&gt;ref2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Modifying the value through mutable reference&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;    println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Modified Value: &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; ref2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this case, you can see how immutably borrowing &lt;code&gt;value&lt;/code&gt; prevents you from getting a mutable reference until the immutable reference goes out of scope.&lt;/p&gt;
&lt;h2 id=&quot;technical-deep-dive-common-pitfalls-when-working-with-references-and-lifetimes&quot;&gt;Technical Deep-Dive: Common Pitfalls When Working with References and Lifetimes&lt;/h2&gt;
&lt;p&gt;Working with references and lifetimes can lead to some common issues, particularly for those new to Rust. It’s essential to be aware of these pitfalls to effectively manage memory and prevent compiler errors.&lt;/p&gt;
&lt;h2 id=&quot;dangling-references&quot;&gt;Dangling References&lt;/h2&gt;
&lt;p&gt;Creating a reference to a local variable can cause dangling references. When the local variable goes out of scope, the reference to that variable remains, leading to potential undefined behavior if accessed.&lt;/p&gt;
&lt;h3 id=&quot;example-code-dangling-reference&quot;&gt;Example Code: Dangling Reference&lt;/h3&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; create_dangling_reference&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Dangling Reference&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;    &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Error: return of reference to local variable `str`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The above code will raise a compile-time error because &lt;code&gt;str&lt;/code&gt; is dropped at the end of the function, leaving the returned reference dangling.&lt;/p&gt;
&lt;h2 id=&quot;lifetimes-mismatch&quot;&gt;Lifetimes Mismatch&lt;/h2&gt;
&lt;p&gt;Another common issue arises when the lifetimes of references provided to a function do not match the expected lifetimes defined in function signatures. This can lead to confusion and compiler errors.&lt;/p&gt;
&lt;h3 id=&quot;example-code-lifetime-mismatch&quot;&gt;Example Code: Lifetime Mismatch&lt;/h3&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; incorrect_lifetime&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; s2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    if&lt;/span&gt;&lt;span&gt; s1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt; s2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        s1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; else&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        s2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Error: the returned reference may not outlive `s2`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this case, the function attempts to return a reference from &lt;code&gt;s1&lt;/code&gt;, tied to a specific lifetime &lt;code&gt;&#39;a&lt;/code&gt;, while &lt;code&gt;s2&lt;/code&gt; does not have that constraint.&lt;/p&gt;
&lt;h2 id=&quot;borrow-checker-confusion&quot;&gt;Borrow Checker Confusion&lt;/h2&gt;
&lt;p&gt;Rust’s borrow checker enforces strict rules for borrowing, which can lead to misunderstandings for new programmers. Understanding when ownership transfers and when immutability or mutability can be used is critical.&lt;/p&gt;
&lt;h3 id=&quot;example-code-borrow-checker-issues&quot;&gt;Example Code: Borrow Checker Issues&lt;/h3&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mut&lt;/span&gt;&lt;span&gt; number&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 10&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; reference1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;number&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Immutable borrow&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; reference2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;mut&lt;/span&gt;&lt;span&gt; number&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Error: cannot borrow `number` as mutable due to previous immutable borrow&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;    println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Number: &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; reference1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The compiler will generate an error when trying to create a mutable reference while an immutable reference still exists.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Navigating through references and lifetimes in Rust can seem overwhelming at first. However, the language’s design promotes safety and concurrency without a garbage collector. Understanding the ownership model, the nature of references, and how lifetimes work paves the way for better coding practices in Rust.&lt;/p&gt;
&lt;p&gt;By learning these concepts, my confidence in leveraging Rust’s memory guarantees has greatly improved. Rust encourages developers to think critically about memory usage, leading to highly efficient programs that are safe from unexpected behavior.&lt;/p&gt;
&lt;p&gt;If you want deeper insights into Rust’s memory model, I recommend checking out the official Rust documentation and exploring the various use cases of references and lifetimes. For practical Rust examples, you can refer to the Rust Book for a comprehensive understanding and practical implementation.&lt;/p&gt;
&lt;p&gt;The combination of references, lifetimes, and the ownership model forms the backbone of Rust’s memory safety and performance. While challenging at first, mastering these concepts leads to robust and efficient Rust code that stands the test of time, makes concurrent programming easier, and ultimately, enhances the developer experience.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Mastering Python Performance: The Small Tweaks That Make Big Speed</title>
        <published>2026-01-30T00:00:00+00:00</published>
        <updated>2026-01-30T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/mastering-python-performance-small-tweaks-big-speed/"/>
        <id>https://brianobot.tech/blog/mastering-python-performance-small-tweaks-big-speed/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/mastering-python-performance-small-tweaks-big-speed/">&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/mastering-python-performance-small-tweaks-big-speed/cover.webp&quot; alt=&quot;Jagged snow-covered mountain summit against a clear blue sky&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;In the realm of software development, writing functional code is often just the beginning. The delicate balance between efficiency, execution duration, and code complexity can make or break your software, particularly in performance-critical applications. For instance, in algorithmic trading, microsecond differences can determine profitable entries. In this guide, you will acquire a comprehensive mental model for optimizing your Python code and learn how to extend this knowledge to enhance your applications’ overall performance.&lt;/p&gt;
&lt;h2 id=&quot;tl-dr&quot;&gt;TL;DR&lt;/h2&gt;
&lt;p&gt;If you are looking for quick pointers to boost your Python code efficiency, check out the key topics below or jump to specific sections:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Favor constants over variables&lt;/li&gt;
&lt;li&gt;Utilize sets for efficient look-ups&lt;/li&gt;
&lt;li&gt;Leverage built-in constructs for optimization&lt;/li&gt;
&lt;li&gt;Explore memory management techniques&lt;/li&gt;
&lt;li&gt;Understand algorithm complexity&lt;/li&gt;
&lt;li&gt;Optimize I/O-bound operations&lt;/li&gt;
&lt;li&gt;Employ multi-threading and parallel processing&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;core-performance-principles&quot;&gt;Core Performance Principles&lt;/h2&gt;
&lt;h3 id=&quot;favor-constants-over-variables&quot;&gt;Favor Constants Over Variables&lt;/h3&gt;
&lt;p&gt;While variables may enhance code readability, every time a variable is accessed, the Python interpreter must load the value from memory. Constants, in contrast, can be accessed directly, which introduces a speed advantage.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; dis&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; add_const&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 10&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; add_var&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Bytecode for Add Const&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;dis&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;add_const&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Bytecode for Add Var&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;dis&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;add_var&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/mastering-python-performance-small-tweaks-big-speed/image-2.webp&quot; alt=&quot;Python dis output: adding constants compiles to a single RETURN_CONST, while adding variables needs LOAD_FAST twice, BINARY_OP and RETURN_VALUE&quot; /&gt;&lt;/p&gt;
&lt;p&gt;In this simplified example, even though the efficiency gains by using constants may seem trivial, understanding these nuances can significantly influence code structure. The key lies in systematically using constants where appropriate while ensuring readability is not compromised. Constants reduce the overhead associated with variable lookups, thereby contributing to more efficient execution times.&lt;/p&gt;
&lt;h3 id=&quot;use-sets-for-lookups&quot;&gt;Use Sets for Lookups&lt;/h3&gt;
&lt;p&gt;When your code requires frequent membership tests (checking if a value exists in a collection), sets outpace lists and tuples significantly due to their underlying hash table implementation.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Using a list for lookups&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;my_list&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; list&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;100000000000&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2000000&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; my_list&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Found!&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Using a set for lookups&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;my_set&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; set&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;100000000000&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;if&lt;/span&gt;&lt;span&gt; 2000000in&lt;/span&gt;&lt;span&gt; my_set&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Found!&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sets deliver O(1) average time complexity for look-up operations, making them the ideal choice for performance-critical applications. By using sets, you can avoid the linear time complexity associated with lists. This efficiency can make a substantial difference in applications where repeated lookups occur in a large dataset.&lt;/p&gt;
&lt;h3 id=&quot;leverage-built-in-constructs-for-optimization&quot;&gt;Leverage Built-In Constructs for Optimization&lt;/h3&gt;
&lt;p&gt;Another crucial aspect of writing efficient Python code involves leveraging built-in constructs that Python provides out of the box. Python’s standard libraries, including functools, itertools, and collections, contain optimized implementations that can enhance performance.&lt;/p&gt;
&lt;h3 id=&quot;using-functools-lru-cache-for-caching&quot;&gt;Using &lt;code&gt;functools.lru_cache&lt;/code&gt; for Caching&lt;/h3&gt;
&lt;p&gt;One of the simplest yet highly effective optimizations involves caching results of function calls that are expensive to compute using &lt;code&gt;lru_cache&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; functools&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; lru_cache&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;lru_cache&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;maxsize&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; fibonacci&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    if&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        return&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; fibonacci&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; fibonacci&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;fibonacci&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Efficiently computed using caching&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By applying caching, you avoid redundant computations, which can save considerable time, particularly in recursive algorithms.&lt;/p&gt;
&lt;h3 id=&quot;technical-deep-dive-sections&quot;&gt;Technical Deep-Dive Sections&lt;/h3&gt;
&lt;h3 id=&quot;deep-dive-section-1-understanding-python-s-memory-management&quot;&gt;Deep-Dive Section 1: Understanding Python’s Memory Management&lt;/h3&gt;
&lt;h3 id=&quot;introduction-to-memory-management&quot;&gt;Introduction to Memory Management&lt;/h3&gt;
&lt;p&gt;Python employs dynamic memory management through its object allocator. Understanding how Python handles memory allocation and garbage collection can equip you with strategies to write more efficient code. Memory management techniques are crucial for optimizing resource usage and minimizing lag.&lt;/p&gt;
&lt;h3 id=&quot;the-role-of-the-global-interpreter-lock-gil&quot;&gt;The Role of the Global Interpreter Lock (GIL)&lt;/h3&gt;
&lt;p&gt;Python’s Global Interpreter Lock (GIL) prevents multiple native threads from executing Python bytecodes at once. This design choice simplifies memory management but can also restrict performance in CPU-bound programs. To circumvent this, consider concurrent or parallel processing techniques, such as using the multiprocessing module to create separate memory spaces.&lt;/p&gt;
&lt;h3 id=&quot;strategies-for-efficient-memory-use&quot;&gt;Strategies for Efficient Memory Use&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Use Generators: Instead of returning large data sets, use generators to yield items one at a time, conserving memory.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; count_down&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt;     &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    while&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt;         &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;      yield&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span&gt;         &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Pool Resources: Utilize &lt;code&gt;ThreadPoolExecutor&lt;/code&gt; or &lt;code&gt;ProcessPoolExecutor&lt;/code&gt; from the &lt;code&gt;concurrent.futures&lt;/code&gt; library to manage thread and process pools efficiently. This strategy can lead to improved responsiveness in your applications.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; concurrent&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;futures&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; ThreadPoolExecutor&lt;/span&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt;     &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; ThreadPoolExecutor&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;max_workers&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; as&lt;/span&gt;&lt;span&gt; executor&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt;     &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    results&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; executor&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; range&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These strategies aim to optimize memory access and allocation, reducing the overhead of data processing in Python applications.&lt;/p&gt;
&lt;h3 id=&quot;deep-dive-section-2-algorithm-complexity-analysis&quot;&gt;Deep-Dive Section 2: Algorithm Complexity Analysis&lt;/h3&gt;
&lt;h3 id=&quot;introduction-to-big-o-notation&quot;&gt;Introduction to Big O Notation&lt;/h3&gt;
&lt;p&gt;Understanding how operations scale with data input sizes is crucial for optimizing performance. Big O notation provides a high-level understanding of an algorithm’s efficiency. This notation helps developers categorize algorithms based on their time complexity characteristics.&lt;/p&gt;
&lt;h3 id=&quot;analyzing-common-python-algorithms&quot;&gt;Analyzing Common Python Algorithms&lt;/h3&gt;
&lt;p&gt;Analyze the time and space complexity of common operations, explaining distinctions between lists, sets, and dictionaries. Consider the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Sorting Lists: O(n log n)&lt;/li&gt;
&lt;li&gt;Searching through Sets/Dictionaries: O(1) average&lt;/li&gt;
&lt;li&gt;Iterating through Lists: O(n)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is another practical example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; find_duplicates&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    seen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; set&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    duplicates&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    for&lt;/span&gt;&lt;span&gt; number&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        if&lt;/span&gt;&lt;span&gt; number&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; seen&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            duplicates&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;number&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        seen&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;number&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; duplicates&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this code, using a set for lookups maintains O(1) complexity for checking the existence of previously-seen numbers, showcasing the speed advantage of using appropriate data structures.&lt;/p&gt;
&lt;h3 id=&quot;practical-examples&quot;&gt;Practical Examples&lt;/h3&gt;
&lt;p&gt;Providing code snippets illustrating computational complexities in realistic scenarios solidifies understanding. Here’s an example of an efficient search function:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; binary_search&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; target&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    low&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; high&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; len&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;arr&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    while&lt;/span&gt;&lt;span&gt; low&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;lt;=&lt;/span&gt;&lt;span&gt; high&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        mid&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span&gt;low&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span&gt; high&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; arr&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;mid&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; target&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            low&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; mid&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        elif&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; arr&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;mid&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span&gt; target&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            high&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; mid&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        else&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;            return&lt;/span&gt;&lt;span&gt; mid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The binary search algorithm operates in O(log n) time, making it significantly more efficient for large, sorted datasets compared to the linear search.&lt;/p&gt;
&lt;h3 id=&quot;deep-dive-section-3-optimizing-i-o-bound-operations&quot;&gt;Deep-Dive Section 3: Optimizing I/O-Bound Operations&lt;/h3&gt;
&lt;h3 id=&quot;exploring-i-o-bound-performance-challenges&quot;&gt;Exploring I/O-Bound Performance Challenges&lt;/h3&gt;
&lt;p&gt;In many applications, I/O operations (like reading and writing to disk, or network requests) can often become bottlenecks. When designing your software, consider asynchronous programming models that allow for overlapping I/O operations to enhance overall speed.&lt;/p&gt;
&lt;h3 id=&quot;employing-asyncio-for-non-blocking-i-o&quot;&gt;Employing Asyncio for Non-Blocking I/O&lt;/h3&gt;
&lt;p&gt;By utilizing the &lt;code&gt;asyncio&lt;/code&gt; module, Python provides a way to manage I/O-bound operations more efficiently. This can prevent blocking during data retrieval or processing, which is crucial for applications that require responsiveness.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; asyncio&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; fetch_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    await&lt;/span&gt;&lt;span&gt; asyncio&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Simulate a network call&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;data fetched!&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    data&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; fetch_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;asyncio&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With &lt;code&gt;asyncio&lt;/code&gt;, you can perform I/O operations without stalling other processes, which can considerably improve efficiency, especially in web applications and services.&lt;/p&gt;
&lt;hr /&gt;
&lt;h3 id=&quot;multi-threading-and-parallel-processing&quot;&gt;Multi-threading and Parallel Processing&lt;/h3&gt;
&lt;h3 id=&quot;benefits-of-multi-threading&quot;&gt;Benefits of Multi-threading&lt;/h3&gt;
&lt;p&gt;When dealing with I/O-bound tasks, consider applying multi-threading alongside asynchronous I/O. Python’s &lt;code&gt;threading&lt;/code&gt; module allows for concurrent execution of code, which can improve responsiveness in applications that require simultaneous operations.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; threading&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; worker&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Worker thread is executing.&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;threads&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; range&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    thread&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; threading&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span&gt;worker&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    threads&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;thread&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    thread&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;for&lt;/span&gt;&lt;span&gt; thread&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; threads&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    thread&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By launching multiple threads, you can manage several tasks in parallel, thereby reducing the total execution time for operations such as web scraping or downloading multiple files.&lt;/p&gt;
&lt;h3 id=&quot;exploring-multi-processing&quot;&gt;Exploring Multi-processing&lt;/h3&gt;
&lt;p&gt;For CPU-bound tasks, consider using the &lt;code&gt;multiprocessing&lt;/code&gt; module, which allows you to create processes that run in their own memory space. This approach can bypass the limitations imposed by the GIL and effectively utilize multiple CPU cores.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; multiprocessing&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; Process&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Process &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; is executing.&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;processes&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; range&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    process&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; Process&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span&gt;task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; args&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    processes&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;process&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    process&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;for&lt;/span&gt;&lt;span&gt; process&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; processes&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    process&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The multi-processing module instantiates separate processes for each task, leveraging the full capabilities of your CPU. This technique is especially beneficial when you require intensive computations, such as numerical simulations or data processing.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In summary, while functionality is paramount, considering performance in your code design is essential for ensuring efficient execution in any application. By leveraging constants, optimizing data structures, understanding algorithm complexity, and employing techniques for asynchronous and parallel processing, you can refine your codebase for better performance.&lt;/p&gt;
&lt;p&gt;The strategies covered herein offer foundational principles that you can apply across various projects, contributing to a culture of performance-driven development. Continue to learn and adapt these methodologies as you advance in your Python journey to create more efficient and robust applications.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;The importance of writing performant code cannot be overstressed in modern software development. By implementing small but impactful tweaks, you can drastically improve the speed and efficiency of your Python applications. Continue to explore threading, asynchronous I/O, and memory management concepts to enhance your skills further. Sharing your discoveries with your peers can foster collaboration and collective growth in programming quality.&lt;/p&gt;
&lt;p&gt;Should you find additional optimizations, document your findings, and do not hesitate to contribute back to the Python community. Happy coding!&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Mastering the Crate and Module System in Rust</title>
        <published>2026-01-30T00:00:00+00:00</published>
        <updated>2026-01-30T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/mastering-the-crate-and-module-system-in-rust/"/>
        <id>https://brianobot.tech/blog/mastering-the-crate-and-module-system-in-rust/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/mastering-the-crate-and-module-system-in-rust/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Once your program or project starts to grow, the need to introduce more files and organize your
project&#39;s structure becomes crucial to maintaining sanity and ensuring a productive coding
environment. Rust offers the concepts of &lt;strong&gt;crates&lt;/strong&gt; and &lt;strong&gt;modules&lt;/strong&gt;, and mastering these can help
you shape your next project in a systematic way that enhances understanding and contributions to
larger Rust projects. This piece will dive into the intricacies of the crate and module system,
providing clarity on their roles, differences, and the best practices around them.&lt;/p&gt;
&lt;h2 id=&quot;packages-are-not-crates-and-vice-versa&quot;&gt;Packages are not Crates, and Vice Versa&lt;/h2&gt;
&lt;p&gt;This has often been the stumbling block for many developers new to Rust. Common materials assert
that crates are the building blocks of Rust projects and, by extension, the smallest unit of
compilation. This leads to the misconception that when a new project is created, that project is,
in itself, a crate. However, this is not true.&lt;/p&gt;
&lt;h3 id=&quot;creating-a-new-project&quot;&gt;Creating a New Project&lt;/h3&gt;
&lt;p&gt;When you start a new project using Cargo with the command:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; cargo&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; rust-projects&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The output indicates that a binary application package has been created:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Created binary (application) `rust-projects` package&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This initializes your project directory and generates several files. The hierarchical structure of
the generated files appears as follows:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; ls&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; rust-projects&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;Cargo.toml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;src&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; ls&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; rust-projects/src&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;main.rs&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Every package contains a &lt;code&gt;Cargo.toml&lt;/code&gt; file and a &lt;code&gt;src&lt;/code&gt; directory with a &lt;code&gt;main.rs&lt;/code&gt; file (in the
binary case), but the package itself is not the crate. It is technically correct to refer to a
package in Rust as a folder containing a &lt;code&gt;Cargo.toml&lt;/code&gt; file — you can make a regular directory a
package by adding a valid &lt;code&gt;Cargo.toml&lt;/code&gt; file.&lt;/p&gt;
&lt;h3 id=&quot;crates-explained&quot;&gt;Crates Explained&lt;/h3&gt;
&lt;p&gt;When a package is created, Rust automatically generates a binary crate rooted at &lt;code&gt;src/main.rs&lt;/code&gt;. If
you wish to create a library crate, you can use:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; cargo&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;-lib&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;lib-package-nam&lt;/span&gt;&lt;span&gt;e&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This generates a standard Cargo package, but houses a &lt;code&gt;lib.rs&lt;/code&gt; file instead of &lt;code&gt;main.rs&lt;/code&gt;. The
distinction between files like &lt;code&gt;main.rs&lt;/code&gt; and &lt;code&gt;lib.rs&lt;/code&gt; is crucial, as it indicates whether a package
is a binary or a library.&lt;/p&gt;
&lt;h3 id=&quot;key-definitions&quot;&gt;Key Definitions&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Package:&lt;/strong&gt; A housing unit for crates. A package contains at least one crate rooted at
&lt;code&gt;src/main.rs&lt;/code&gt; or &lt;code&gt;src/lib.rs&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Crate:&lt;/strong&gt; The compiled piece of code, which can either be a binary or a library. Each package
allows for multiple binary crates (and can also include dependencies).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By defining packages and crates in this way, it becomes easier to grasp syntax such as
&lt;code&gt;crate::&amp;lt;path&amp;gt;&lt;/code&gt;. This directly translates to the &lt;code&gt;&amp;lt;path&amp;gt;&lt;/code&gt; item located in the &lt;code&gt;src/main.rs&lt;/code&gt; file,
which is the crate root for binary packages, and &lt;code&gt;src/lib.rs&lt;/code&gt; for library packages.&lt;/p&gt;
&lt;h2 id=&quot;modules-the-organizational-structure&quot;&gt;Modules: The Organizational Structure&lt;/h2&gt;
&lt;p&gt;While packages help you group crates at a project level, modules provide a structured organization
for different features or parts of your code. Think of your package as a house filled with various
items sourced from various brands — some you create and others you buy. It&#39;s your responsibility to
organize these items in a way that makes sense for your desired house setting.&lt;/p&gt;
&lt;h3 id=&quot;the-importance-of-modules&quot;&gt;The Importance of Modules&lt;/h3&gt;
&lt;p&gt;Consider a house where all items are crammed together. This leads to confusion and makes it
difficult to find the necessary items. The clearer the organization within your project or code,
the more efficient you can be. This concept of organization is the primary benefit modules provide
— not just in Rust, but in every programming language.&lt;/p&gt;
&lt;h3 id=&quot;basic-usage-of-modules&quot;&gt;Basic Usage of Modules&lt;/h3&gt;
&lt;p&gt;To define a module in Rust, the &lt;code&gt;mod&lt;/code&gt; keyword is utilized. For instance:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; src/main.rs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; kitchen&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    struct&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Cooker&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    struct&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Heater&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    struct&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Table&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;    println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Hello World!&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, a module named &lt;code&gt;kitchen&lt;/code&gt; is created, containing three structs: &lt;code&gt;Cooker&lt;/code&gt;, &lt;code&gt;Heater&lt;/code&gt;, and
&lt;code&gt;Table&lt;/code&gt;. To utilize an item from this module, the path to the item must be specified at the point
where the item is needed.&lt;/p&gt;
&lt;h3 id=&quot;path-to-items-inside-modules&quot;&gt;Path to Items Inside Modules&lt;/h3&gt;
&lt;p&gt;If you need to access an item from this module:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; inside the main function&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;let&lt;/span&gt;&lt;span&gt; cooker&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt; kitchen&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;Cooker&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Pressure Cooker&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;to_string&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This statement effectively directs Rust to navigate to the &lt;code&gt;kitchen&lt;/code&gt; module to access the &lt;code&gt;Cooker&lt;/code&gt;
item.&lt;/p&gt;
&lt;h3 id=&quot;visibility-in-modules&quot;&gt;Visibility in Modules&lt;/h3&gt;
&lt;p&gt;Items within modules are private by default, which means they are not accessible from outside
unless designated as public using the &lt;code&gt;pub&lt;/code&gt; keyword. For example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; kitchen&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Cooker&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; the String item inside the Cooker is private&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Heater&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; so is the String item inside the Heater&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    struct&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Table&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Still private&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Even if &lt;code&gt;Cooker&lt;/code&gt; is public, its contents remain private unless stated explicitly. This ensures
encapsulation and keeps the module organized and maintainable.&lt;/p&gt;
&lt;h2 id=&quot;large-codebase-management-in-rust&quot;&gt;Large Codebase Management in Rust&lt;/h2&gt;
&lt;p&gt;For projects evolving into larger codebases, managing dependencies and organization becomes
critical. This section covers best practices for structuring large Rust projects.&lt;/p&gt;
&lt;h3 id=&quot;dependency-management&quot;&gt;Dependency Management&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Cargo.toml&lt;/code&gt;:&lt;/strong&gt; This central configuration file plays a crucial role in managing dependencies.
You must specify the dependencies your crate is using here, enabling straightforward integration
and updates. By allowing version control directly within this file, Rust ensures compatibility with
the modules or packages you rely on.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;toml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span&gt;dependencies&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;rand&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;0.8&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;serde&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span&gt; version&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; features&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Using external crates:&lt;/strong&gt; You can include external libraries available from crates.io, effectively
reducing development time and enhancing functional capabilities.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;toml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Adding an external crate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span&gt;dependencies&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;log&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;0.4&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Your project can then use external libraries seamlessly in your codebase, allowing for rich
functionality with little overhead.&lt;/p&gt;
&lt;h3 id=&quot;module-organization-strategies&quot;&gt;Module Organization Strategies&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Logical grouping:&lt;/strong&gt; Organizing functionality into logically cohesive modules reduces complexity.
For instance, if you have functionality related to user management, implement that in a &lt;code&gt;user.rs&lt;/code&gt;
module, while maintaining a separate &lt;code&gt;product.rs&lt;/code&gt; for product-related functionality. This keeps
separation of concerns clear.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Namespacing:&lt;/strong&gt; The use of prefixes in module names can help maintain clarity, especially when
dealing with numerous modules. By structuring your modules distinctly, you can easily avoid naming
collisions that would lead to conflicts at compile time.&lt;/p&gt;
&lt;h2 id=&quot;advanced-module-usage&quot;&gt;Advanced Module Usage&lt;/h2&gt;
&lt;p&gt;Modules in Rust offer a breadth of functionality that can enhance code clarity and reusability.
This section dives into advanced techniques for modules.&lt;/p&gt;
&lt;h3 id=&quot;nested-modules&quot;&gt;Nested Modules&lt;/h3&gt;
&lt;p&gt;Nested modules allow you to create more complex hierarchical structures, which can help in
organizing similar functionality together. Here&#39;s an example of how to define nested modules:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; server&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mod&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; routes&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        pub&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; get_user&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;            println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Getting user&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mod&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; handlers&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        pub&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; handle_request&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;            println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Handling request&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To access these nested modules, the syntax becomes a bit more complex. You need to specify the full
path:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt;    server&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;get_user&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt;    server&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt;handlers&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;handle_request&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This organization not only keeps your project tidy but also clearly delineates functionality,
making it easier for others to understand the codebase.&lt;/p&gt;
&lt;h3 id=&quot;public-interfaces-and-privacy&quot;&gt;Public Interfaces and Privacy&lt;/h3&gt;
&lt;p&gt;Understanding visibility is crucial for maintaining a clean boundary between different parts of
your code. Any item marked as &lt;code&gt;pub&lt;/code&gt; can be accessed from the outside, while those without this
modifier are private. Here&#39;s how to correctly declare visibility:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; kitchen&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Cooker&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Both the struct and its field are public.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Heater&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Struct is public, but field remains private.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    impl&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Heater&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; This method can be public if needed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        pub&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Heater&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;            Heater&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This approach allows for a flexible API while maintaining internal encapsulation where necessary.&lt;/p&gt;
&lt;h2 id=&quot;performance-optimization-with-modules&quot;&gt;Performance Optimization with Modules&lt;/h2&gt;
&lt;p&gt;Understanding how to structure your modules and leverage Rust&#39;s compilation process can lead to
optimized performance.&lt;/p&gt;
&lt;h3 id=&quot;optimizing-for-compilation&quot;&gt;Optimizing for Compilation&lt;/h3&gt;
&lt;p&gt;When you split your code into smaller modules, Rust&#39;s incremental compilation can speed up the
build process. When only one module is changed, only that particular module needs to be recompiled
instead of the entire project. This can be a significant advantage in large applications.&lt;/p&gt;
&lt;h3 id=&quot;memory-usage&quot;&gt;Memory Usage&lt;/h3&gt;
&lt;p&gt;Using modules wisely can allow Rust to optimize memory usage at runtime. By compartmentalizing
functionality, unnecessary data can often be discarded if it&#39;s only used within a particular
context:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;inline&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;always&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; expensive_function&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Result&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Hypothetical expensive operation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;    Ok&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;to_string&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By marking functions inline, you can reduce the overhead of function calls and streamline
performance in high-frequency usage scenarios.&lt;/p&gt;
&lt;h2 id=&quot;working-with-crates-dependency-management-and-structuring&quot;&gt;Working with Crates: Dependency Management and Structuring&lt;/h2&gt;
&lt;p&gt;Understanding how to manage and structure crates within Rust applications is key to building robust
applications.&lt;/p&gt;
&lt;h3 id=&quot;cargo-toml-the-heartbeat-of-dependency-management&quot;&gt;&lt;code&gt;Cargo.toml&lt;/code&gt;: The Heartbeat of Dependency Management&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;Cargo.toml&lt;/code&gt; file is crucial for managing dependencies and specifying package metadata. Here is
how you can structure it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;toml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span&gt;package&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;my_project&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;version&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;0.1.0&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;edition&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;2018&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span&gt;dependencies&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;serde&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span&gt; version&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; features&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;chrono&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;0.4&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By keeping dependencies well-organized and up to date, you ensure your project remains stable and
compatible with other libraries.&lt;/p&gt;
&lt;h3 id=&quot;case-study-real-world-application-structure&quot;&gt;Case Study: Real-world Application Structure&lt;/h3&gt;
&lt;p&gt;Consider a Rust web application structured for a blog. The project can be divided into several
parts: a main entry point for the server, one for handling database interactions, one for
authentication, and another for utility functions.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;my_blog/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;├── Cargo.toml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;└── src/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── main.rs   # Main entry point.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── db.rs     # Database interactions.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── auth.rs   # Authentication logic.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    └── utils.rs  # Utility functions.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each module can be further divided upon reaching significant complexity, forming yet another layer
of hierarchy.&lt;/p&gt;
&lt;h2 id=&quot;practical-examples-and-use-cases&quot;&gt;Practical Examples and Use Cases&lt;/h2&gt;
&lt;p&gt;To solidify understanding, let&#39;s run through practical examples that demonstrate these concepts in
action.&lt;/p&gt;
&lt;h3 id=&quot;1-basic-module-usage&quot;&gt;1. Basic Module Usage&lt;/h3&gt;
&lt;p&gt;Imagine a library to handle mathematical operations:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; src/math.rs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; mod&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; operations&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; add&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; i32&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; i32&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; i32&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    pub&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; multiply&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; i32&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; i32&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; i32&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can call it directly within your main program:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; src/main.rs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; sum&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt;operations&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; product&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt;operations&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;    println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Sum: &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;, Product: &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; sum&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; product&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;2-using-external-crates&quot;&gt;2. Using External Crates&lt;/h3&gt;
&lt;p&gt;Let&#39;s include the popular &lt;code&gt;serde&lt;/code&gt; crate for serialization and deserialization. Here&#39;s how to use
it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;toml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Cargo.toml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span&gt;dependencies&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;serde&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span&gt; version&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; features&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;serde_json&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; src/user.rs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;Serialize&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Deserialize&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;Serialize&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Deserialize&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; User&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    pub&lt;/span&gt;&lt;span&gt; username&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    pub&lt;/span&gt;&lt;span&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; src/main.rs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; user&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt; user&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; user&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; User&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        username&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;john_doe&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;john.doe@example.com&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    let&lt;/span&gt;&lt;span&gt; serialized_user&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-15&quot;&gt; serde_json&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;to_string&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;user&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;    println!&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Serialized User: &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; serialized_user&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, we&#39;re leveraging an external crate to efficiently handle data in our application.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Mastering the crate and module system in Rust is essential for building maintainable, scalable
applications. Understanding the differences between packages and crates, along with the
organizational capabilities offered by modules, allows you to create more efficient and effective
codebases. By adopting the best practices highlighted throughout this article, you can elevate your
programming skills and foster clean code structures that accommodate growth and complexity. Embrace
these tools and watch your Rust projects flourish, ready to tackle broader functionality and larger
codebases.&lt;/p&gt;
&lt;h2 id=&quot;technical-verdict&quot;&gt;Technical Verdict&lt;/h2&gt;
&lt;p&gt;In conclusion, a profound understanding of crates and modules not only enhances your capability to
write efficient Rust code but also empowers you to navigate complex projects confidently. With
careful management of packages, robust dependency handling, and organized module structures, Rust
developers can cultivate a prolific development environment conducive to both individual and
collaborative growth.&lt;/p&gt;
&lt;p&gt;By continuously expanding your knowledge and adapting your practices to leverage the power of
crates and modules, you prepare yourself to create the next generation of Rust applications
efficiently.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>The Double-Slit Experiment: How One Particle Interferes With Itself</title>
        <published>2026-01-10T00:00:00+00:00</published>
        <updated>2026-01-10T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/the-double-slit-experiment-how-one-particle-interferes-with-itself/"/>
        <id>https://brianobot.tech/blog/the-double-slit-experiment-how-one-particle-interferes-with-itself/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/the-double-slit-experiment-how-one-particle-interferes-with-itself/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Richard Feynman once said the double-slit experiment contains &quot;the only mystery&quot; of quantum
mechanics. Everything else that&#39;s weird about the quantum world, he argued, is some version of what
happens here.&lt;/p&gt;
&lt;p&gt;2025 was the International Year of Quantum Science and Technology, marking a hundred years since
Heisenberg&#39;s 1925 paper that founded quantum mechanics. I spent part of it going back to the basics
I had skimmed through in university physics, and the double-slit experiment was the one that kept
pulling me back. It&#39;s simple enough to describe in a paragraph, and nobody has fully made peace with
it in a century.&lt;/p&gt;
&lt;p&gt;This post walks through it in three acts: with light, with particles, and with a detector watching.
No maths beyond a couple of ratios, plus a short Python simulation you can run yourself.&lt;/p&gt;
&lt;h2 id=&quot;tl-dr&quot;&gt;TL;DR&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Light through two narrow slits makes a pattern of bright and dark &lt;strong&gt;stripes&lt;/strong&gt; (fringes). That&#39;s
&lt;strong&gt;interference&lt;/strong&gt;, the signature of a wave.&lt;/li&gt;
&lt;li&gt;Send &lt;strong&gt;electrons&lt;/strong&gt; through, &lt;strong&gt;one at a time&lt;/strong&gt;, and each lands as a single dot. But the dots
gradually build up the &lt;strong&gt;same striped pattern&lt;/strong&gt;. Each electron seems to interfere with itself.&lt;/li&gt;
&lt;li&gt;Put a detector at the slits to find out which slit each electron used, and the stripes
&lt;strong&gt;vanish&lt;/strong&gt;. You get two plain bands, as if they were bullets.&lt;/li&gt;
&lt;li&gt;Quantum mechanics describes this with a &lt;strong&gt;wavefunction&lt;/strong&gt; that passes through both slits, and the
&lt;strong&gt;Born rule&lt;/strong&gt;: the wave tells you only the &lt;strong&gt;probability&lt;/strong&gt; of where a particle will be found.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;act-one-light-is-a-wave&quot;&gt;Act one: light is a wave&lt;/h2&gt;
&lt;p&gt;In the early 1800s, Thomas Young shone light through two narrow, closely spaced slits onto a screen.
If light were a stream of tiny particles, as Newton had argued, you&#39;d expect two bright bands, one
behind each slit. Instead Young saw a row of bright and dark stripes.&lt;/p&gt;
&lt;p&gt;The explanation is &lt;strong&gt;interference&lt;/strong&gt;, the same thing you see when two ripples cross on a pond. Light
spreads out from each slit as a wave. At some points on the screen, the waves from the two slits
arrive in step, crest on crest, and add up to a bright fringe. At others they arrive exactly out of
step, crest on trough, and cancel to darkness.&lt;/p&gt;
&lt;p&gt;Whether they arrive in step depends on the difference in distance from each slit. That gives a neat
formula for the spacing between bright fringes:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fringe spacing = wavelength × screen distance / slit separation&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Green light (500 nm) through slits 50 µm apart onto a screen 1 metre away gives fringes 1 cm apart.
That&#39;s how 19th-century physicists measured the wavelength of light with nothing more than a ruler.&lt;/p&gt;
&lt;p&gt;The crucial point: &lt;strong&gt;darkness appears in places that would be lit if only one slit were open.&lt;/strong&gt; Open
a second slit, and some spots on the screen get &lt;em&gt;less&lt;/em&gt; light. Particles can&#39;t do that. Waves can.&lt;/p&gt;
&lt;p&gt;So by about 1850 the question was settled: light is a wave. Then Einstein unsettled it in 1905 by
showing light also arrives in discrete lumps of energy, &lt;strong&gt;photons&lt;/strong&gt;. And in 1924, Louis de Broglie
proposed the reverse: if waves can act like particles, maybe particles act like waves, with a
wavelength set by their momentum:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;λ = h / p&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where &lt;code&gt;h&lt;/code&gt; is Planck&#39;s constant. For anything you can see, that wavelength is absurdly small. For an
electron, it&#39;s comparable to the spacing between atoms, which is why electron microscopes can see
things light microscopes can&#39;t.&lt;/p&gt;
&lt;h2 id=&quot;act-two-one-electron-at-a-time&quot;&gt;Act two: one electron at a time&lt;/h2&gt;
&lt;p&gt;Now the strange part. Replace the light with a beam of electrons, and turn the beam down so low that
only &lt;strong&gt;one electron&lt;/strong&gt; is in the apparatus at any moment. The screen is a detector that records
exactly where each electron lands.&lt;/p&gt;
&lt;p&gt;The famous version of this was done by Akira Tonomura&#39;s team at Hitachi in 1989, using an electron
biprism (a fine charged wire that does the same job as two slits). Here&#39;s what they saw:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Each electron arrives as a &lt;strong&gt;single dot&lt;/strong&gt;, at a single point. Not smeared out. Not half here, half
there. Particle-like.&lt;/li&gt;
&lt;li&gt;The positions look &lt;strong&gt;random&lt;/strong&gt;. You can&#39;t predict where the next one will land.&lt;/li&gt;
&lt;li&gt;As thousands of dots accumulate, &lt;strong&gt;stripes appear&lt;/strong&gt;. The same interference pattern as with light.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You can watch this happen with a short simulation. Each particle lands randomly, but the chance of
landing at each point follows the brightness of the two-slit pattern:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; random&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;WAVELENGTH&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 500e-9&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;   #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; metres (green light)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;SLIT_GAP&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 50e-6&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;      #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; distance between the slits, metres&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;SCREEN&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1.0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;          #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; distance from slits to screen, metres&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;WIDTH&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0.03&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;          #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; half-width of the screen we look at, metres&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; intensity&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Relative brightness of the two-slit pattern at height y on the screen.&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    phase&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;pi&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; SLIT_GAP&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span&gt;WAVELENGTH&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; SCREEN&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;phase&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; **&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; detect_one_particle&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Pick where a single particle lands, with probability set by intensity.&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    while&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; True&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        y&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; random&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;uniform&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;-&lt;/span&gt;&lt;span&gt;WIDTH&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; WIDTH&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        if&lt;/span&gt;&lt;span&gt; random&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; intensity&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;            return&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; show&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;hits&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; bins&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 60&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; None&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    counts&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; bins&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    for&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; range&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;hits&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        y&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; detect_one_particle&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        counts&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt; WIDTH&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt; WIDTH&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt; bins&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt; bins&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    peak&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; max&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;counts&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    shades&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; .:-=+*#%@&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    row&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;shades&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt; peak&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;shades&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; for&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; counts&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;hits&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:&amp;gt;6&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; particles |&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;row&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;random&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;for&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 100&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1_000&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 10_000&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 100_000&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;    show&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    10 particles | @   =           =           =         =     =    @   =     |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   100 particles | **   ---%* : :  :**@--:    :-@- :    =-%=:    =-*--=-  :-**|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  1000 particles |#==.  .-%#*=-:  --*##==.  .:=*%*+.  .++*%+=.   -+#@+=:.. -#%|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 10000 particles |%*=:  :=#@%#=:  :=##%*+:  :=*@%#=:  :=*@%#=:  :=*@%*=:  :=#@|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;100000 particles |@#=:  :=#@@#+:  :=#@@#+:  :=#@@#+:  :=#@@#=:  :=#@@#+:  :+#@|&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At ten particles it&#39;s noise. By ten thousand, the fringes are unmistakable. (To be clear, this
script &lt;em&gt;assumes&lt;/em&gt; the interference pattern; it shows what the build-up looks like, not why it
happens. The real experiment is the evidence.)&lt;/p&gt;
&lt;p&gt;Now sit with what this means. There is only one electron in the apparatus at a time. There&#39;s no
other electron for it to interfere with. Yet the dark fringes are places where electrons almost never
land, places they &lt;em&gt;would&lt;/em&gt; land if only one slit were open. Somehow, each individual electron is
affected by &lt;strong&gt;both&lt;/strong&gt; slits.&lt;/p&gt;
&lt;p&gt;This isn&#39;t special to electrons. It&#39;s been done with neutrons, atoms, and in 1999 with buckyballs,
football-shaped molecules of 60 carbon atoms. Anton Zeilinger&#39;s and Markus Arndt&#39;s groups in Vienna
have since pushed it to molecules made of around 2,000 atoms. Everything interferes, if you can keep
it isolated enough.&lt;/p&gt;
&lt;h2 id=&quot;act-three-what-if-we-look&quot;&gt;Act three: what if we look?&lt;/h2&gt;
&lt;p&gt;The natural instinct is to catch the electron in the act. Put a detector at the slits that registers
which slit each electron passes through, and then see where it lands.&lt;/p&gt;
&lt;p&gt;When you do this, the detector always reports &lt;strong&gt;one slit or the other&lt;/strong&gt;, never both. And the
interference pattern on the screen &lt;strong&gt;disappears&lt;/strong&gt;. You get two overlapping bands, exactly what
you&#39;d expect from particles that each went through one slit.&lt;/p&gt;
&lt;p&gt;Remove the detector, and the stripes come back.&lt;/p&gt;
&lt;p&gt;It&#39;s tempting to say &quot;the electron knows it&#39;s being watched&quot;, but that framing is misleading. There&#39;s
nothing mystical about a conscious observer here. Any physical interaction that leaves a record of
which slit was used, even a single stray photon bouncing off the electron that nobody ever looks
at, destroys the interference. What matters is whether the &lt;strong&gt;information exists&lt;/strong&gt; in the
universe, not whether a person reads it.&lt;/p&gt;
&lt;p&gt;Physicists call this &lt;strong&gt;complementarity&lt;/strong&gt; (Niels Bohr&#39;s term): the setup can reveal wave-like
behaviour (interference) or particle-like behaviour (which path), but not both fully at once. There&#39;s
even a smooth trade-off. A weak detector that only &lt;em&gt;partly&lt;/em&gt; tells you the path leaves you with
faded fringes, and the better the path information, the fainter the stripes.&lt;/p&gt;
&lt;h2 id=&quot;how-quantum-mechanics-describes-it&quot;&gt;How quantum mechanics describes it&lt;/h2&gt;
&lt;p&gt;Quantum mechanics doesn&#39;t say the electron is &quot;really&quot; a wave or &quot;really&quot; a particle. It says the
electron is described by a &lt;strong&gt;wavefunction&lt;/strong&gt;, usually written &lt;code&gt;ψ&lt;/code&gt; (psi). The wavefunction behaves
exactly like a wave: it spreads out, goes through both slits, and interferes with itself.&lt;/p&gt;
&lt;p&gt;But you never see the wavefunction directly. When the electron hits the screen, it&#39;s detected at a
single point. Where it appears is random, and the &lt;strong&gt;Born rule&lt;/strong&gt;, proposed by Max Born in 1926, gives
the odds:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;probability of finding the particle at a point ∝ |ψ|²&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The wave decides the &lt;strong&gt;probabilities&lt;/strong&gt;; each detection is a single &lt;strong&gt;event&lt;/strong&gt; drawn from them. That&#39;s
precisely what the simulation above does.&lt;/p&gt;
&lt;p&gt;The interference lives in the wavefunction. With both slits open, &lt;code&gt;ψ&lt;/code&gt; is the sum of a piece from
each slit, &lt;code&gt;ψ₁ + ψ₂&lt;/code&gt;, and squaring a sum gives cross terms that can be positive or negative. That&#39;s
where the bright and dark fringes come from. When a detector records which slit was used, the two
pieces become tied to different detector states, they can no longer interfere, and you&#39;re left with
&lt;code&gt;|ψ₁|² + |ψ₂|²&lt;/code&gt;: two plain bands.&lt;/p&gt;
&lt;h2 id=&quot;so-what-is-actually-going-on&quot;&gt;So what is actually going on?&lt;/h2&gt;
&lt;p&gt;Here&#39;s where honest physicists stop agreeing. The maths is not in dispute; it predicts every result
perfectly. What&#39;s disputed is what the maths &lt;strong&gt;means&lt;/strong&gt;. A few of the main camps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Copenhagen&lt;/strong&gt;: the wavefunction is a tool for predicting measurement results. Asking which slit
the electron &quot;really&quot; went through, when nothing recorded it, is not a meaningful question.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pilot wave (de Broglie–Bohm)&lt;/strong&gt;: there really is a particle, always at a definite position, and it
goes through one slit. But it&#39;s guided by a real wave that goes through both, and the wave steers
it into the fringe pattern.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Many worlds&lt;/strong&gt;: the wavefunction is all there is, and it never collapses. When a detector
records the path, the universe branches into one where it went left and one where it went right.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All three predict the same experimental results, which is why the debate has lasted a century. I
don&#39;t have a favourite yet. What I&#39;ve come to appreciate is that each one makes you give up some
piece of everyday intuition. You don&#39;t get to keep all of it.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;The double-slit experiment is short to describe and impossible to un-learn:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Waves interfere&lt;/strong&gt;, and dark fringes appear where a second open slit removes light.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Single particles build up interference patterns&lt;/strong&gt;, one random dot at a time, as if each passes
through both slits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Recording the path destroys the pattern&lt;/strong&gt;, whether or not anyone looks at the record.&lt;/li&gt;
&lt;li&gt;Quantum mechanics captures it all with a &lt;strong&gt;wavefunction&lt;/strong&gt; that interferes and the &lt;strong&gt;Born rule&lt;/strong&gt;
that turns it into probabilities. What that wavefunction &lt;em&gt;is&lt;/em&gt; remains an open question.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If the double-slit experiment shows that quantum objects don&#39;t have a definite path, the next
natural question is whether they have definite properties at all. That&#39;s where Heisenberg&#39;s
&lt;a href=&quot;https://brianobot.tech/blog/the-uncertainty-principle-is-not-about-clumsy-measurements/&quot;&gt;uncertainty principle&lt;/a&gt; comes in.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Nuclear Fission: How Splitting an Atom Powers a City</title>
        <published>2025-12-20T00:00:00+00:00</published>
        <updated>2025-12-20T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/nuclear-fission-how-splitting-an-atom-powers-a-city/"/>
        <id>https://brianobot.tech/blog/nuclear-fission-how-splitting-an-atom-powers-a-city/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/nuclear-fission-how-splitting-an-atom-powers-a-city/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;In my &lt;a href=&quot;https://brianobot.tech/blog/binding-energy-why-iron-is-the-end-of-the-line/&quot;&gt;previous post on binding energy&lt;/a&gt;
I described nuclear energy as climbing a hill: nuclei release energy whenever they move towards
iron, the most tightly bound nucleus there is. Light nuclei climb by fusing. Heavy nuclei climb by
splitting.&lt;/p&gt;
&lt;p&gt;This post is about the second route, &lt;strong&gt;fission&lt;/strong&gt;, and it&#39;s the one humanity actually has working
at scale. Roughly a tenth of the world&#39;s electricity comes from it today. As someone who studied
electrical engineering, I spent years treating power stations as boxes that turn &quot;heat&quot; into
electricity, without asking much about where the heat came from. In a nuclear plant, it comes from
splitting atoms, and the physics of how that&#39;s done safely is more interesting than I expected.&lt;/p&gt;
&lt;p&gt;You don&#39;t need to have read the previous post, but if terms like &quot;binding energy per nucleon&quot;
are new to you, it&#39;s a quick detour.&lt;/p&gt;
&lt;h2 id=&quot;tl-dr&quot;&gt;TL;DR&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;A slow neutron hitting &lt;strong&gt;uranium-235&lt;/strong&gt; makes it wobble apart into two medium-sized nuclei, a few
free neutrons, and about &lt;strong&gt;200 MeV&lt;/strong&gt; of energy.&lt;/li&gt;
&lt;li&gt;Those freed neutrons can split more uranium, giving a &lt;strong&gt;chain reaction&lt;/strong&gt;. The key number is the
&lt;strong&gt;multiplication factor &lt;code&gt;k&lt;/code&gt;&lt;/strong&gt;: on average, how many new fissions each fission causes.&lt;/li&gt;
&lt;li&gt;Bombs need &lt;code&gt;k&lt;/code&gt; well above 1 and fast. Reactors need &lt;code&gt;k&lt;/code&gt; held at &lt;strong&gt;exactly 1&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;That&#39;s only possible because a small fraction of neutrons arrive &lt;strong&gt;seconds&lt;/strong&gt; late. These
&lt;strong&gt;delayed neutrons&lt;/strong&gt; slow the reactor&#39;s response enough for control rods to keep up.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;a-short-history&quot;&gt;A short history&lt;/h2&gt;
&lt;p&gt;Fission was discovered almost by accident. In December 1938, Otto Hahn and Fritz Strassmann in
Berlin were bombarding uranium with neutrons, expecting to make slightly heavier elements. Instead,
their careful chemistry kept turning up &lt;strong&gt;barium&lt;/strong&gt;, an element barely half uranium&#39;s mass.&lt;/p&gt;
&lt;p&gt;Hahn wrote to his long-time collaborator Lise Meitner, who had fled Nazi Germany to Sweden. Over
the Christmas holiday, Meitner and her nephew Otto Frisch worked out what was happening: the
uranium nucleus wasn&#39;t being chipped, it was &lt;strong&gt;splitting in two&lt;/strong&gt;. They estimated the energy
released at around 200 MeV per split, borrowed the word &quot;fission&quot; from biology, and published in
early 1939.&lt;/p&gt;
&lt;p&gt;Within four years, on 2 December 1942, Enrico Fermi&#39;s team in Chicago ran the first self-sustaining
chain reaction in a pile of graphite and uranium under a stadium. Nature had beaten them to it,
though: around two billion years ago, uranium deposits at &lt;strong&gt;Oklo&lt;/strong&gt; in Gabon ran as natural fission
reactors, on and off, for hundreds of thousands of years. Groundwater acted as the moderator.&lt;/p&gt;
&lt;h2 id=&quot;how-a-nucleus-splits&quot;&gt;How a nucleus splits&lt;/h2&gt;
&lt;h3 id=&quot;the-liquid-drop-picture&quot;&gt;The liquid drop picture&lt;/h3&gt;
&lt;p&gt;A heavy nucleus like uranium is held together by the short-range strong force and pushed apart by
the repulsion between its 92 protons. It&#39;s like a drop of liquid with surface tension barely
holding against a charge trying to blow it apart.&lt;/p&gt;
&lt;p&gt;When uranium-235 absorbs a neutron, it becomes &lt;strong&gt;uranium-236&lt;/strong&gt; in an excited state, carrying an
extra ~6.5 MeV of energy. That energy sets the drop &lt;strong&gt;oscillating&lt;/strong&gt;. It stretches into a peanut
shape, and once the neck between the two lobes gets thin enough, the electric repulsion between
the lobes wins over the strong force across the neck. The drop snaps.&lt;/p&gt;
&lt;p&gt;The energy needed to reach that snapping point is the &lt;strong&gt;fission barrier&lt;/strong&gt;, about 6 MeV for
uranium-236. The neutron&#39;s arrival delivers more than that, so fission happens readily.&lt;/p&gt;
&lt;h3 id=&quot;why-u-235-and-not-u-238&quot;&gt;Why U-235 and not U-238&lt;/h3&gt;
&lt;p&gt;Natural uranium is 99.3% uranium-238 and only 0.7% uranium-235. Why does everyone care about the
rare one?&lt;/p&gt;
&lt;p&gt;It comes down to &lt;strong&gt;pairing&lt;/strong&gt;. Nucleons like to be in pairs. U-235 has an &lt;strong&gt;odd&lt;/strong&gt; number of
neutrons (143), so when it captures one, the new neutron completes a pair and the nucleus gets a
bonus chunk of binding energy, enough to clear the fission barrier even if the incoming neutron
was moving slowly.&lt;/p&gt;
&lt;p&gt;U-238 already has an even number of neutrons (146). Adding one more leaves an unpaired neutron, so
the energy bonus is smaller and falls short of the barrier. U-238 only fissions if the neutron
brings in more than about 1 MeV of kinetic energy of its own.&lt;/p&gt;
&lt;p&gt;Nuclei that fission with slow neutrons are called &lt;strong&gt;fissile&lt;/strong&gt;: U-235, U-233 and plutonium-239 are
the important ones. This is why reactor fuel is &lt;strong&gt;enriched&lt;/strong&gt;: commercial reactors typically use
uranium that&#39;s 3–5% U-235.&lt;/p&gt;
&lt;h3 id=&quot;a-typical-split&quot;&gt;A typical split&lt;/h3&gt;
&lt;p&gt;There&#39;s no single way for U-236 to break. It splits &lt;strong&gt;asymmetrically&lt;/strong&gt;, usually into one fragment
around mass 95 and another around mass 140. One common example:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;²³⁵U + n  →  ²³⁶U*  →  ¹⁴¹Ba + ⁹²Kr + 3n&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Check the bookkeeping: mass numbers &lt;code&gt;235 + 1 = 141 + 92 + 3 = 236&lt;/code&gt;, and protons &lt;code&gt;92 = 56 + 36&lt;/code&gt;.
Nothing is created or destroyed, just rearranged.&lt;/p&gt;
&lt;h2 id=&quot;where-the-200-mev-goes&quot;&gt;Where the 200 MeV goes&lt;/h2&gt;
&lt;p&gt;Barium and krypton both sit closer to iron on the binding energy curve than uranium does. Each
nucleon gains just under 1 MeV of binding, and with ~236 nucleons that sums to around 200 MeV.&lt;/p&gt;
&lt;p&gt;I can estimate it with the semi-empirical mass formula from the previous post, by comparing the
binding energy of the products to the binding energy of the compound nucleus:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;released&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; binding_energy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;56&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 141&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; binding_energy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;36&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 92&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; binding_energy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;92&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 236&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;U-236 -&amp;gt; Ba-141 + Kr-92 + 3n releases about &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;released&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:.0f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; MeV&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; U-236 -&amp;gt; Ba-141 + Kr-92 + 3n releases about 155 MeV&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A rough formula gives 155 MeV for the split itself, a bit below the measured ~170 MeV for this
channel, but the right ballpark. The rest comes later: the fragments are packed with too many
neutrons and &lt;strong&gt;beta-decay&lt;/strong&gt; over the following seconds to years, releasing more energy as they go.&lt;/p&gt;
&lt;p&gt;Averaged over all fissions, the budget for U-235 looks roughly like this:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Where the energy goes&lt;/th&gt;&lt;th&gt;MeV&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Kinetic energy of the two fragments&lt;/td&gt;&lt;td&gt;~169&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Kinetic energy of the free neutrons&lt;/td&gt;&lt;td&gt;~5&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Prompt gamma rays&lt;/td&gt;&lt;td&gt;~7&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Beta and gamma decay of fragments&lt;/td&gt;&lt;td&gt;~13&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Antineutrinos (escape, lost)&lt;/td&gt;&lt;td&gt;~10&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;~200&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The bulk of it is the two fragments flying apart, pushed by their mutual electric repulsion. They
slam into surrounding atoms within a few micrometres and their kinetic energy becomes &lt;strong&gt;heat&lt;/strong&gt;.
That&#39;s the heat that boils water and spins turbines.&lt;/p&gt;
&lt;p&gt;Note the fragment decay line. That energy keeps coming even after the chain reaction stops. Right
after shutdown, a reactor still produces about 6–7% of its full power as &lt;strong&gt;decay heat&lt;/strong&gt;, falling to
around 1% after a few hours. This is why reactors need cooling long after they&#39;re switched off,
and why losing that cooling at Fukushima in 2011 was a disaster even though the reactors had
already shut down.&lt;/p&gt;
&lt;h3 id=&quot;putting-it-in-perspective&quot;&gt;Putting it in perspective&lt;/h3&gt;
&lt;p&gt;200 MeV per atom is about &lt;code&gt;3.2 × 10⁻¹¹&lt;/code&gt; joules, which sounds tiny. But a kilogram of U-235 has
around &lt;code&gt;2.6 × 10²⁴&lt;/code&gt; atoms, so fissioning all of it releases about &lt;strong&gt;82 terajoules&lt;/strong&gt;. A kilogram of
good coal gives about 25 megajoules. That&#39;s a ratio of over &lt;strong&gt;three million to one&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-chain-reaction&quot;&gt;The chain reaction&lt;/h2&gt;
&lt;p&gt;Each fission releases on average about &lt;strong&gt;2.4 neutrons&lt;/strong&gt;. If at least one of those goes on to cause
another fission, the process sustains itself. The number that matters is the &lt;strong&gt;multiplication
factor&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;k = (fissions in this generation) / (fissions in the previous generation)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;k &amp;lt; 1&lt;/code&gt;, &lt;strong&gt;subcritical&lt;/strong&gt;: the reaction dies out.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;k = 1&lt;/code&gt;, &lt;strong&gt;critical&lt;/strong&gt;: the reaction holds steady. This is a reactor running normally.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;k &amp;gt; 1&lt;/code&gt;, &lt;strong&gt;supercritical&lt;/strong&gt;: the reaction grows exponentially.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that &quot;critical&quot; is a calm, steady state, not an emergency, despite how films use the word.&lt;/p&gt;
&lt;p&gt;Not every neutron causes another fission. Some escape through the surface. Some get absorbed by
U-238, structural materials, or fission products without causing a split. So &lt;code&gt;k&lt;/code&gt; depends on the
material, its enrichment, its shape, and its size. That last one leads to the idea of &lt;strong&gt;critical
mass&lt;/strong&gt;: a lump has to be big enough that neutrons are more likely to hit another nucleus than leak
out. For a bare sphere of pure U-235 that&#39;s around 50 kg; for plutonium-239 it&#39;s around 10 kg.&lt;/p&gt;
&lt;h2 id=&quot;slowing-neutrons-down-the-moderator&quot;&gt;Slowing neutrons down: the moderator&lt;/h2&gt;
&lt;p&gt;Neutrons are born fast, at around 2 MeV. But U-235 is hundreds of times more likely to absorb a
&lt;strong&gt;slow&lt;/strong&gt; (thermal) neutron than a fast one. So most reactors deliberately slow their neutrons
down with a &lt;strong&gt;moderator&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The trick is to let the neutrons bounce off light nuclei. Think of snooker: a ball hitting another
ball of the same mass can lose all its speed in one collision, but a ball hitting a bowling ball
just bounces off at almost the same speed. Hydrogen, with nearly the same mass as a neutron, is
the best at this. That&#39;s why ordinary water is the moderator (and coolant) in most reactors in the
world. Graphite and heavy water are the common alternatives, both absorbing fewer neutrons, which
is why reactors moderated with them can run on unenriched natural uranium.&lt;/p&gt;
&lt;p&gt;Water moderation also gives a built-in safety feature: if the water overheats and turns to steam,
it gets less dense, moderates less, and &lt;code&gt;k&lt;/code&gt; drops. The reactor turns itself down.&lt;/p&gt;
&lt;h2 id=&quot;why-reactors-can-be-controlled-at-all&quot;&gt;Why reactors can be controlled at all&lt;/h2&gt;
&lt;p&gt;This is the part that genuinely surprised me when I first worked through the numbers.&lt;/p&gt;
&lt;p&gt;In a water-moderated reactor, a neutron generation lasts somewhere around &lt;strong&gt;0.1 milliseconds&lt;/strong&gt;:
born, slowed down, absorbed, causing the next fission. That&#39;s 10,000 generations per second. If &lt;code&gt;k&lt;/code&gt;
were even slightly above 1, say 1.001, the power would multiply by &lt;code&gt;1.001^10000&lt;/code&gt;, about &lt;strong&gt;22,000
times&lt;/strong&gt;, in a single second. No mechanical control rod could possibly keep up.&lt;/p&gt;
&lt;p&gt;The rescue is that not all neutrons arrive instantly. About &lt;strong&gt;0.65%&lt;/strong&gt; of the neutrons from U-235
fission are &lt;strong&gt;delayed&lt;/strong&gt;: they&#39;re emitted by fission fragments that decay a few seconds to a minute
later. The average delay is around 13 seconds.&lt;/p&gt;
&lt;p&gt;0.65% sounds negligible, but it stretches the &lt;strong&gt;average&lt;/strong&gt; time between generations dramatically.
Here&#39;s the arithmetic:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;PROMPT_LIFETIME&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1e-4&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; seconds between neutron generations, prompt only&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;BETA&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0.0065&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;             #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; fraction of U-235 fission neutrons that are delayed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;DELAY&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 12.7&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;              #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; mean delay of those neutrons, in seconds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; growth_after&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;seconds&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; k&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; lifetime&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Factor the neutron population (and power) grows by after `seconds`.&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    period&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; lifetime&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;seconds&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span&gt; period&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;effective_lifetime&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; PROMPT_LIFETIME&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span&gt; BETA&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; DELAY&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;for&lt;/span&gt;&lt;span&gt; k&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1.0001&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1.001&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    prompt&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; growth_after&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; k&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; PROMPT_LIFETIME&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    delayed&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; growth_after&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; k&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; effective_lifetime&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;k = &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;: prompt only x&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;prompt&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:,.1f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; per second, with delayed neutrons x&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;delayed&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:.3f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; per second&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;k = 1.0001: prompt only x2.7 per second, with delayed neutrons x1.001 per second&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;k = 1.001: prompt only x22,026.5 per second, with delayed neutrons x1.012 per second&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With delayed neutrons included, the effective generation time is closer to &lt;strong&gt;0.08 seconds&lt;/strong&gt;, and
the same slight excess in &lt;code&gt;k&lt;/code&gt; grows the power by about 1% per second instead of 22,000-fold. That&#39;s
slow enough for control rods, made of neutron-hungry materials like boron or cadmium, to be moved in
and out to keep &lt;code&gt;k&lt;/code&gt; at exactly 1.&lt;/p&gt;
&lt;p&gt;The golden rule of reactor design follows from this: the reactor must &lt;strong&gt;never be critical on prompt
neutrons alone&lt;/strong&gt;. As long as it needs the delayed neutrons to stay critical, it changes slowly
enough to control. Chernobyl in 1986 is the tragic counterexample: a combination of design flaws
and operating decisions pushed reactor 4 past this line, and its power spiked by orders of
magnitude in seconds.&lt;/p&gt;
&lt;p&gt;A bomb is the opposite. It has no moderator, no delayed neutrons to wait for, and it&#39;s assembled to
be strongly supercritical on fast neutrons. The whole reaction is over in about a microsecond.&lt;/p&gt;
&lt;h2 id=&quot;inside-a-power-reactor&quot;&gt;Inside a power reactor&lt;/h2&gt;
&lt;p&gt;Putting the pieces together, a typical &lt;strong&gt;pressurised water reactor&lt;/strong&gt; looks like this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Fuel&lt;/strong&gt;: pellets of uranium dioxide enriched to 3–5% U-235, stacked in long metal rods, bundled
into assemblies.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Moderator and coolant&lt;/strong&gt;: water flows between the rods, slowing neutrons and carrying heat away.
It&#39;s held at about 155 bar so it stays liquid at around 300 °C.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Control rods&lt;/strong&gt;: slide between fuel rods to absorb neutrons and adjust &lt;code&gt;k&lt;/code&gt;. Dissolved boron in
the water provides fine adjustment.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Steam generator&lt;/strong&gt;: the hot, pressurised water heats a separate water loop, which boils.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Turbine and generator&lt;/strong&gt;: the steam spins a turbine connected to a generator. From here on it&#39;s
the same as a coal or gas plant.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;That last step is the humbling part for an electrical engineer: all that nuclear physics, and at
the end of the day it&#39;s an extremely sophisticated kettle.&lt;/p&gt;
&lt;h2 id=&quot;the-catch-waste&quot;&gt;The catch: waste&lt;/h2&gt;
&lt;p&gt;The fission fragments that made the energy are themselves the problem. They&#39;re neutron-rich and
intensely radioactive. Some, like caesium-137 and strontium-90, have half-lives around 30 years.
On top of that, U-238 in the fuel absorbs neutrons and turns into plutonium and other heavy
elements, some of which stay radioactive for tens of thousands of years.&lt;/p&gt;
&lt;p&gt;The volume is small. All the spent fuel a large reactor produces in a year would fit in a modest
room. But it has to be cooled for years, then isolated for millennia. Finland&#39;s Onkalo repository,
carved into bedrock hundreds of metres down, is the first deep geological disposal site for spent
fuel. For most countries, though, the long-term plan is still a plan.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Fission is the side of the binding energy hill we have actually conquered. The core ideas:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;U-235 splits easily&lt;/strong&gt; because absorbing one neutron completes a pair and pushes it over the
fission barrier.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;~200 MeV per fission&lt;/strong&gt;, mostly the kinetic energy of two fragments repelling each other, which
becomes heat.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Chain reactions&lt;/strong&gt; are governed by &lt;code&gt;k&lt;/code&gt;, and reactors live at &lt;code&gt;k = 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Delayed neutrons&lt;/strong&gt; are the quiet hero: a 0.65% sliver of late neutrons is the difference between
a power station and an uncontrollable spike.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Fission&#39;s drawbacks, long-lived waste, the risk of losing cooling, and its link to weapons, are
exactly what make the other side of the hill so tempting. In the &lt;a href=&quot;https://brianobot.tech/blog/nuclear-fusion-why-building-a-star-on-earth-is-so-hard/&quot;&gt;next post&lt;/a&gt; I&#39;ll look at fusion:
what it would take to build a star on Earth, and why it&#39;s taken so long.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Setting Up Rust Analyzer to work for Non-Cargo-Based Rust Projects</title>
        <published>2025-12-16T00:00:00+00:00</published>
        <updated>2025-12-16T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/setting-up-rust-analyzer-for-non-cargo-rust-projects/"/>
        <id>https://brianobot.tech/blog/setting-up-rust-analyzer-for-non-cargo-rust-projects/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/setting-up-rust-analyzer-for-non-cargo-rust-projects/">&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/setting-up-rust-analyzer-for-non-cargo-rust-projects/cover.webp&quot; alt=&quot;Layered mountain ridges in hazy golden light above a pine forest&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;While working on the &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://adventofcode.com/&quot;&gt;Advent of Code&lt;/a&gt; Challenge for this year, I planned to solve the challenges in Python and the Rust Programming Language, and after solving the first 2 days in Python, I decided to solve those in Rust too, it didn’t take long before I realized how difficult I found it working with Rust programs without the benefits of a Language server like Rust Analyzer.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Rust Analyzer is built around Cargo. When you run:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;cargo&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; my_project&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Cargo quietly generates the metadata Rust Analyzer relies on to understand your code: crate boundaries, dependencies, edition, and access to the standard library.&lt;/p&gt;
&lt;p&gt;But when you’re working with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a single &lt;code&gt;.rs&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;a mixed-language repository&lt;/li&gt;
&lt;li&gt;small, disposable programs (like Advent of Code)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You lose all of that context. Rust Analyzer has nothing to latch onto, and the editor experience degrades sharply.&lt;/p&gt;
&lt;p&gt;Unlike Python, where tooling scales gracefully from one file to one thousand, Rust’s tooling assumes Cargo by default.&lt;/p&gt;
&lt;h2 id=&quot;motivation&quot;&gt;Motivation&lt;/h2&gt;
&lt;p&gt;Conventionally, when you start a new Rust project with &lt;code&gt;cargo new &amp;lt;project_name&amp;gt;&lt;/code&gt;, cargo generates some configuration about your project which your Rust analyzer picks and uses to provide useful autocompletion features, go to definition support, and inline type hinting.&lt;/p&gt;
&lt;p&gt;But when working with simpler setups like a single Rust file, you lose out on the benefits that come with the Rust analyzer and the Cargo project configuration. Unlike Programming languages like Python, which can work straightforwardly as single files and as complex project structures. &lt;br /&gt;
Of course, with the benefits of the autocompletion and the rest, you can still very well write and compile single file rust programs.&lt;/p&gt;
&lt;p&gt;After stumbling into a bug while implementing a solution for the challenges in Advent of Code day 2 in Rust, and I realized how much harder it was to walkthrough the types in my program visually and get support for autocompletion, method names and signatures, I decided to do an internet search on how to configure Rust Analyzer to work for single file rust Project without Cargo.&lt;/p&gt;
&lt;p&gt;While searching, I came across many such advice;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/setting-up-rust-analyzer-for-non-cargo-rust-projects/image-2.webp&quot; alt=&quot;Reddit replies to a question about using Rust Analyzer without Cargo: most say to just use cargo, one points to the rust-project.json format&quot; /&gt;&lt;/p&gt;
&lt;p&gt;People kept generally hinting at using cargo, but, like in my case, it was not reasonable to set up a cargo project for every single day’s challenge, and it was a multi-language environment, and not a Rust-specific project.&lt;/p&gt;
&lt;p&gt;In my case:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Each Advent of Code solution was a standalone program&lt;/li&gt;
&lt;li&gt;The repository contained multiple languages&lt;/li&gt;
&lt;li&gt;Creating and maintaining a Cargo project per day felt like overkill&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I didn’t want a build system.&lt;br /&gt;
I wanted my editor to understand Rust.&lt;/p&gt;
&lt;h2 id=&quot;solution&quot;&gt;Solution&lt;/h2&gt;
&lt;p&gt;Rust Analyzer has support for non-cargo based project, just like the use case I wished to use it for above, but to give Rust Analyzer details on your setup and directory to find standard Rust libraries, you have to provide these details in a rust-project.json file in the root directory containing the single Rust files you wish to work on without Cargo. See &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://rust-analyzer.github.io/book/non_cargo_based_projects.html&quot;&gt;details here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;With a small amount of explicit configuration, you can give Rust Analyzer everything it needs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;where Rust is installed&lt;/li&gt;
&lt;li&gt;which files are crates&lt;/li&gt;
&lt;li&gt;which edition to use&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;No Cargo required.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# rust-project.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;sysroot&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/Users/Apple/.rustup/toolchains/stable-aarch64-apple-darwin&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;crates&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;display_name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;core&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;root_module&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/Users/Apple/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/lib.rs&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;edition&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;2024&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;deps&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;cfg&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;target_arch=&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;aarch64&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;target_os=&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;macos&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;unix&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      ]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;is_proc_macro&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;display_name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;root_module&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/Users/Apple/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/lib.rs&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;edition&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;2024&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;deps&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;crate&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;core&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      ]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;cfg&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;target_arch=&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;aarch64&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;target_os=&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;macos&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;unix&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      ]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;is_proc_macro&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;display_name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;root_module&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/Users/Apple/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/lib.rs&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;edition&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;2024&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;deps&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;crate&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;core&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;crate&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      ]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;cfg&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;target_arch=&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;aarch64&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;target_os=&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;macos&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;unix&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      ]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;is_proc_macro&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;display_name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;day_4&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;root_module&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/Users/Apple/Documents/advent-of-codes/2025/day_4.rs&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;edition&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;2024&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;deps&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;crate&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;cfg&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;target_arch=&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;aarch64&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;target_os=&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;macos&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;unix&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      ]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;is_proc_macro&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;display_name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;day_5&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;root_module&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/Users/Apple/Documents/advent-of-codes/2025/day_5.rs&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;edition&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;2024&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;deps&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;crate&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;cfg&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;target_arch=&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;aarch64&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;target_os=&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;macos&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;unix&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      ]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;      &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;is_proc_macro&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;  ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;explanation&quot;&gt;Explanation:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;sysroot&lt;/strong&gt;: Tells Rust Analyzer where your Rust toolchain lives—the standard library, core, alloc, and compiler support crates.&lt;br /&gt;
You can get this value by entering the following command&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;rustc&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;-print&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; sysroot&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;crates&lt;/strong&gt;: An array describing each logical Rust crate in the workspace.&lt;br /&gt;
For simple setups, this often maps** one-to-one with a single &lt;strong&gt;&lt;code&gt;.rs&lt;/code&gt;&lt;/strong&gt; file**, but it can also represent larger module trees.&lt;br /&gt;
Take note to pass the Absolute path of the &lt;code&gt;root_module&lt;/code&gt; attribute&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Notice that the first 3 crates are standard crates that Rust Analyzer needs to annotate builtin traits properly, ensure that you own defined crates (single Rust files) that depend on those traits to gain the complete type annotation benefits.&lt;/p&gt;
&lt;p&gt;After creating and saving this file to your working directory, you should reload your editor workspace to get all the great benefits of Rust Analyzer working on your single-file Rust Programs (non-Cargo projects)&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Rust doesn’t stop you from writing small, single-file programs, but without proper tooling, it certainly makes you &lt;em&gt;feel&lt;/em&gt; the absence. Rust Analyzer, by default, expects Cargo to be the narrator of your project’s story, and when Cargo is missing, the editor is left guessing in the dark.&lt;/p&gt;
&lt;p&gt;The good news is that this limitation is not a dead end. By explicitly describing your setup using a &lt;code&gt;rust-project.json&lt;/code&gt; file, you can restore nearly all the ergonomics you’d expect from a full Cargo-based project: autocomplete, type hints, navigation, and a clearer understanding of your code’s structure.&lt;/p&gt;
&lt;p&gt;For lightweight experiments, coding challenges, or mixed-language repositories, this approach strikes a practical balance. You keep the simplicity of single-file Rust while reclaiming the insight and confidence that come from a well-informed editor.&lt;/p&gt;
&lt;p&gt;Sometimes, the compiler already knows enough. You just need to teach your tools to listen.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Binding Energy: Why Iron Is the End of the Line</title>
        <published>2025-11-28T00:00:00+00:00</published>
        <updated>2025-11-28T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/binding-energy-why-iron-is-the-end-of-the-line/"/>
        <id>https://brianobot.tech/blog/binding-energy-why-iron-is-the-end-of-the-line/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/binding-energy-why-iron-is-the-end-of-the-line/">&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/binding-energy-why-iron-is-the-end-of-the-line/cover.webp&quot; alt=&quot;A glowing supernova remnant nebula rising above the curve of the Earth&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;There is a question that bothered me for a long time, and I suspect it bothers a lot of people
who have read a little about nuclear energy: if you get energy by &lt;strong&gt;splitting&lt;/strong&gt; heavy atoms
(fission), and you also get energy by &lt;strong&gt;joining&lt;/strong&gt; light atoms (fusion), how can both be true?
Surely one of them has to cost energy. You can&#39;t make money by both buying and selling the same
thing.&lt;/p&gt;
&lt;p&gt;The answer turns out to be one of the most elegant ideas in physics, and it all hangs on a single
graph: the &lt;strong&gt;binding energy curve&lt;/strong&gt;. Once that curve makes sense, fission, fusion, why stars die,
and why nobody is building iron-burning power plants all fall out of it almost for free.&lt;/p&gt;
&lt;p&gt;This post is the foundation for a small series on nuclear physics. You don&#39;t need any background
beyond high-school physics and a willingness to accept that mass and energy are the same thing.&lt;/p&gt;
&lt;h2 id=&quot;tl-dr&quot;&gt;TL;DR&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;A nucleus weighs &lt;strong&gt;less&lt;/strong&gt; than the protons and neutrons it is made from. The missing mass is the
&lt;strong&gt;binding energy&lt;/strong&gt;, via &lt;code&gt;E = mc²&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Divide that by the number of nucleons and you get &lt;strong&gt;binding energy per nucleon&lt;/strong&gt;: a measure of
how tightly each particle is held.&lt;/li&gt;
&lt;li&gt;That number rises steeply for light nuclei, peaks around &lt;strong&gt;iron and nickel&lt;/strong&gt; (~8.8 MeV), and
slowly falls for heavy nuclei.&lt;/li&gt;
&lt;li&gt;Any reaction that moves nuclei &lt;strong&gt;towards the peak&lt;/strong&gt; releases energy. Light nuclei climb by
fusing; heavy nuclei climb by splitting. Iron is already at the top, so it can do neither.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-nucleus-is-lighter-than-its-parts&quot;&gt;The nucleus is lighter than its parts&lt;/h2&gt;
&lt;p&gt;Start with helium-4, the nucleus of an ordinary helium atom: two protons and two neutrons. Look up
the masses (in atomic mass units, &lt;code&gt;u&lt;/code&gt;):&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Particle&lt;/th&gt;&lt;th&gt;Mass (u)&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Proton&lt;/td&gt;&lt;td&gt;1.007276&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Neutron&lt;/td&gt;&lt;td&gt;1.008665&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2 protons + 2 neutrons&lt;/td&gt;&lt;td&gt;4.031883&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Helium-4 nucleus&lt;/td&gt;&lt;td&gt;4.001506&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The assembled nucleus is lighter than its loose parts by &lt;code&gt;0.030377 u&lt;/code&gt;. That&#39;s not a measurement
error; it&#39;s the whole story. This missing mass is called the &lt;strong&gt;mass defect&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Where did it go? It left as energy when the nucleus formed. Using &lt;code&gt;E = mc²&lt;/code&gt;, and the handy fact
that &lt;code&gt;1 u&lt;/code&gt; of mass is equivalent to &lt;code&gt;931.494 MeV&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0.030377 u × 931.494 MeV/u ≈ 28.3 MeV&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So 28.3 MeV of energy was released when those four nucleons snapped together, and to pull
helium-4 apart again, you&#39;d have to put those 28.3 MeV &lt;strong&gt;back in&lt;/strong&gt;. That&#39;s the &lt;strong&gt;binding
energy&lt;/strong&gt;. It&#39;s the energy debt you&#39;d have to repay to dismantle the nucleus.&lt;/p&gt;
&lt;p&gt;To put &quot;MeV&quot; in perspective: a chemical bond, the thing that holds a molecule together and the
thing we break when we burn coal or petrol, is worth a few &lt;strong&gt;eV&lt;/strong&gt;. Nuclear binding energies are
in the &lt;strong&gt;millions&lt;/strong&gt; of eV. That factor of roughly a million is the whole reason nuclear power
exists.&lt;/p&gt;
&lt;h2 id=&quot;binding-energy-per-nucleon&quot;&gt;Binding energy per nucleon&lt;/h2&gt;
&lt;p&gt;Total binding energy grows with the size of the nucleus, which isn&#39;t very interesting on its own;
of course a bigger nucleus has more glue. The useful number is the binding energy &lt;strong&gt;per nucleon&lt;/strong&gt;:
divide the total by the mass number &lt;code&gt;A&lt;/code&gt; (protons + neutrons).&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Nucleus&lt;/th&gt;&lt;th&gt;Binding energy per nucleon&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Deuterium (²H)&lt;/td&gt;&lt;td&gt;1.11 MeV&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Helium-4&lt;/td&gt;&lt;td&gt;7.07 MeV&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Carbon-12&lt;/td&gt;&lt;td&gt;7.68 MeV&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Oxygen-16&lt;/td&gt;&lt;td&gt;7.98 MeV&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Iron-56&lt;/td&gt;&lt;td&gt;8.79 MeV&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Nickel-62&lt;/td&gt;&lt;td&gt;8.79 MeV (the actual record holder, by a hair)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Uranium-235&lt;/td&gt;&lt;td&gt;7.59 MeV&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Uranium-238&lt;/td&gt;&lt;td&gt;7.57 MeV&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Plot that for every known nucleus and you get the famous curve. In words:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; MeV per nucleon&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  9 |              Fe  Ni&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    |          .·&amp;#39;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯``&amp;#39;·..&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  8 |       .·&amp;#39;                       ``·.  U&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    |     .&amp;#39;                              ·&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  7 |   He&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    |   |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    |  /&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    | /&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  1 |D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    +------------------------------------------- mass number A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       4      56                          235&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;steep climb&lt;/strong&gt; through the light elements (with helium-4 sitting conspicuously high for its
size, because it is an unusually tight little package).&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;broad peak&lt;/strong&gt; around &lt;code&gt;A ≈ 56–62&lt;/code&gt;: iron and nickel.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;slow decline&lt;/strong&gt; all the way out to uranium and beyond.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Higher on the curve means more tightly bound, which means &lt;strong&gt;more stable&lt;/strong&gt; and, crucially, &lt;strong&gt;lower
in energy&lt;/strong&gt;. Nature, like water, flows downhill in energy. In this graph &quot;downhill&quot; means
&quot;upwards on the curve&quot;. Any nuclear reaction that ends with nucleons sitting higher on this curve
releases the difference as energy.&lt;/p&gt;
&lt;h2 id=&quot;why-the-curve-has-that-shape&quot;&gt;Why the curve has that shape&lt;/h2&gt;
&lt;p&gt;The shape isn&#39;t arbitrary. It comes from a tug-of-war between two forces.&lt;/p&gt;
&lt;h3 id=&quot;the-strong-force-short-range-glue&quot;&gt;The strong force: short-range glue&lt;/h3&gt;
&lt;p&gt;Protons and neutrons are held together by the &lt;strong&gt;strong nuclear force&lt;/strong&gt;. It&#39;s ferociously strong,
but it only reaches about one or two femtometres (about the width of a nucleon). Beyond that it
falls off a cliff. So each nucleon effectively only feels its immediate neighbours.&lt;/p&gt;
&lt;p&gt;In a tiny nucleus, most nucleons sit on the &lt;strong&gt;surface&lt;/strong&gt;, with few neighbours to bond with. They&#39;re
under-bonded. As the nucleus grows, a larger fraction of nucleons are buried in the interior with
a full set of neighbours, so the binding per nucleon climbs. That&#39;s the steep left side of the
curve.&lt;/p&gt;
&lt;h3 id=&quot;electromagnetism-long-range-repulsion&quot;&gt;Electromagnetism: long-range repulsion&lt;/h3&gt;
&lt;p&gt;Protons are positively charged and repel each other. Unlike the strong force, the electric force
has &lt;strong&gt;infinite range&lt;/strong&gt;: every proton pushes on every other proton in the nucleus, not just its
neighbours.&lt;/p&gt;
&lt;p&gt;That means the repulsion grows roughly as &lt;code&gt;Z²&lt;/code&gt; (with &lt;code&gt;Z&lt;/code&gt; protons) while the strong-force glue only
grows roughly as &lt;code&gt;A&lt;/code&gt;. In a small nucleus, the glue wins easily. In a big nucleus, the repulsion
catches up and starts eating into the binding. That&#39;s the slow decline on the right side.&lt;/p&gt;
&lt;h3 id=&quot;where-they-balance&quot;&gt;Where they balance&lt;/h3&gt;
&lt;p&gt;Somewhere around iron and nickel, the benefit of having more neighbours has mostly saturated, and
the cost of proton repulsion hasn&#39;t yet become dominant. That&#39;s the peak. Iron sits at the sweet
spot between &quot;too small to be well glued&quot; and &quot;too big to hold its own charge together&quot;.&lt;/p&gt;
&lt;h2 id=&quot;a-model-you-can-run-the-semi-empirical-mass-formula&quot;&gt;A model you can run: the semi-empirical mass formula&lt;/h2&gt;
&lt;p&gt;Physicists turned that intuition into an actual formula back in the 1930s. It treats the nucleus
like a drop of charged liquid, which is why it&#39;s often called the &lt;strong&gt;liquid drop model&lt;/strong&gt;, and it
adds up five effects:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Volume&lt;/strong&gt;: every nucleon gets glue from its neighbours (proportional to &lt;code&gt;A&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Surface&lt;/strong&gt;: nucleons on the surface have fewer neighbours, so subtract something proportional
to the surface area, &lt;code&gt;A^(2/3)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Coulomb&lt;/strong&gt;: subtract the proton–proton repulsion, roughly &lt;code&gt;Z(Z−1) / A^(1/3)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Asymmetry&lt;/strong&gt;: nuclei prefer balanced numbers of protons and neutrons (a quantum effect from
the Pauli exclusion principle), so subtract a penalty for imbalance, &lt;code&gt;(N−Z)² / A&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pairing&lt;/strong&gt;: nucleons like to pair up, so even–even nuclei get a small bonus and odd–odd nuclei
a small penalty.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here it is in Python:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Semi-empirical mass formula coefficients, in MeV&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;A_V&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; A_S&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; A_C&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; A_A&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; A_P&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 15.8&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 18.3&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0.714&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 23.2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 12.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; binding_energy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;Z&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Approximate total binding energy of a nucleus in MeV.&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    N&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span&gt; Z&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    volume&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; A_V&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    surface&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; A_S&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; **&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    coulomb&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; A_C&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span&gt; Z&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span&gt;Z&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; **&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    asymmetry&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; A_A&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span&gt;N&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span&gt; Z&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; **&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span&gt; A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    if&lt;/span&gt;&lt;span&gt; Z&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; and&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        pairing&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; A_P&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;A&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    elif&lt;/span&gt;&lt;span&gt; Z&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; and&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        pairing&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span&gt;A_P&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span&gt; math&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;A&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    else&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        pairing&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; volume&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span&gt; surface&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span&gt; coulomb&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; -&lt;/span&gt;&lt;span&gt; asymmetry&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span&gt; pairing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nuclei&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;He-4&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;C-12&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 12&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;O-16&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 16&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Fe-56&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 56&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Ni-62&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 62&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;U-235&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;92&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 235&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;U-238&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;92&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 238&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;for&lt;/span&gt;&lt;span&gt; name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; (&lt;/span&gt;&lt;span&gt;Z&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; nuclei&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    B&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; binding_energy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;Z&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:&amp;gt;7&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;B&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:8.1f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; MeV total, &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;B&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;:5.2f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; MeV per nucleon&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Output:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   He-4:     22.2 MeV total,  5.55 MeV per nucleon&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   C-12:     87.8 MeV total,  7.32 MeV per nucleon&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   O-16:    123.7 MeV total,  7.73 MeV per nucleon&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  Fe-56:    490.6 MeV total,  8.76 MeV per nucleon&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  Ni-62:    544.6 MeV total,  8.78 MeV per nucleon&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  U-235:   1790.7 MeV total,  7.62 MeV per nucleon&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  U-238:   1809.5 MeV total,  7.60 MeV per nucleon&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Five terms and a handful of constants, and it lands within about half a percent of reality for
iron and uranium. It even finds nickel-62 slightly ahead of iron-56. I find that genuinely
remarkable.&lt;/p&gt;
&lt;p&gt;It&#39;s also honest about its limits: helium-4 comes out at 22 MeV instead of the real 28 MeV.
Treating four particles as a &quot;liquid drop&quot; is a stretch, and helium-4 is special. Its two protons
and two neutrons fill the lowest quantum shell completely, a bit like a noble gas does with
electrons. That extra stability is why helium-4 shows up everywhere in nuclear physics: as the
product of fusion, as the alpha particle in radioactive decay, and as the ash in the cores of
stars.&lt;/p&gt;
&lt;h2 id=&quot;climbing-the-curve-from-both-sides&quot;&gt;Climbing the curve from both sides&lt;/h2&gt;
&lt;p&gt;Now the paradox from the introduction dissolves.&lt;/p&gt;
&lt;h3 id=&quot;fusion-climbing-from-the-left&quot;&gt;Fusion: climbing from the left&lt;/h3&gt;
&lt;p&gt;Take light nuclei on the steep left slope and merge them. Hydrogen isotopes (about 1–3 MeV per
nucleon) combining into helium-4 (7.07 MeV per nucleon) is a huge jump upwards. Every nucleon
involved ends up more tightly bound, and the difference comes out as energy. That&#39;s what powers
the Sun.&lt;/p&gt;
&lt;h3 id=&quot;fission-climbing-from-the-right&quot;&gt;Fission: climbing from the right&lt;/h3&gt;
&lt;p&gt;Take a heavy nucleus on the gently sloping right side, like uranium-235 at about 7.6 MeV per
nucleon, and split it into two medium-sized pieces around barium and krypton, at about 8.3–8.5 MeV
per nucleon. Each piece is closer to the peak than the original. The gain is under 1 MeV per
nucleon, but there are about 235 nucleons involved, so it adds up to roughly &lt;strong&gt;200 MeV per
fission&lt;/strong&gt;. That&#39;s what runs nuclear power stations.&lt;/p&gt;
&lt;h3 id=&quot;iron-already-at-the-top&quot;&gt;Iron: already at the top&lt;/h3&gt;
&lt;p&gt;Iron-56 and nickel-62 sit at the summit. Fuse two iron nuclei and the product is further down the
right slope. Split iron and the pieces are further down the left slope. Either way you have to
&lt;strong&gt;put energy in&lt;/strong&gt;. Iron is nuclear ash: there&#39;s nothing left to burn.&lt;/p&gt;
&lt;h2 id=&quot;why-stars-die-at-iron&quot;&gt;Why stars die at iron&lt;/h2&gt;
&lt;p&gt;This has a dramatic consequence for stars.&lt;/p&gt;
&lt;p&gt;A star spends most of its life fusing hydrogen into helium. When the hydrogen in its core runs low,
a sufficiently massive star contracts, heats up, and starts fusing helium into carbon and oxygen.
Then carbon into neon and magnesium. Then oxygen into silicon. Then silicon into iron. Each stage
climbs a little further up the curve, releases a little less energy per nucleon, and burns faster.
In the most massive stars, the final silicon-burning stage lasts about a day.&lt;/p&gt;
&lt;p&gt;Then the core is iron, and the climb is over. With no more energy being released to hold up the
weight of the star, the core collapses in under a second. The result is a &lt;strong&gt;core-collapse
supernova&lt;/strong&gt;, leaving behind a neutron star or a black hole.&lt;/p&gt;
&lt;p&gt;Every element heavier than iron, including the gold in your jewellery and the uranium in a
reactor, had to be made by processes that &lt;strong&gt;absorb&lt;/strong&gt; energy, such as rapid neutron capture during
supernovae and neutron-star mergers. In a real sense, when we split uranium in a power plant, we&#39;re
releasing energy that was stored in it by a stellar catastrophe billions of years ago.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;The binding energy curve is one of those diagrams that rewires how you see the world. A few things
I&#39;d take away from it:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Mass is energy.&lt;/strong&gt; The nucleus is lighter than its parts, and that missing mass is real,
measurable energy.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Nuclear energy is about a million times denser than chemical energy&lt;/strong&gt;, because nuclear binding
is measured in MeV while chemical bonds are measured in eV.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fission and fusion aren&#39;t opposites&lt;/strong&gt;. They&#39;re two routes up the same hill, approaching from
opposite sides.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Iron is the top of the hill&lt;/strong&gt;, which is why stars that make iron are about to die.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the next two posts I&#39;ll take each side of the hill in turn: first &lt;a href=&quot;https://brianobot.tech/blog/nuclear-fission-how-splitting-an-atom-powers-a-city/&quot;&gt;fission&lt;/a&gt;, the one we&#39;ve had
working for over 80 years, and then &lt;a href=&quot;https://brianobot.tech/blog/nuclear-fusion-why-building-a-star-on-earth-is-so-hard/&quot;&gt;fusion&lt;/a&gt;, the one we&#39;ve been chasing for nearly as long.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Starting Today, I&#39;m Going Deeper into Game Development with Rust</title>
        <published>2025-11-19T00:00:00+00:00</published>
        <updated>2025-11-19T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/starting-today-im-choosing-to-go-deeper/"/>
        <id>https://brianobot.tech/blog/starting-today-im-choosing-to-go-deeper/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/starting-today-im-choosing-to-go-deeper/">&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/starting-today-im-choosing-to-go-deeper/cover.webp&quot; alt=&quot;Digital art of a ringed planet and its moon in a purple and blue galaxy&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;br /&gt;
I am documenting my first entry in a series of articles that will come after this one on the topic of game development. I also intend to use this as a Medium to create the sort of learning material I would have appreciated a lot, following through on my game development hobby.&lt;/p&gt;
&lt;h2 id=&quot;motivation&quot;&gt;Motivation&lt;/h2&gt;
&lt;p&gt;Game development has been one of my most desired areas in the software technology sphere, one I have adored with great passion and interest. The ability to create immersive worlds and universes that defined their own rules and had a significant gravitational impact on their fanbase. And most times, it wasn’t just the universes that made people love games, it was the storytelling, the narrative it creates, the characterization, and a lot more in between. This is the part I want to understand and eventually master.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;background&quot;&gt;Background&lt;/h2&gt;
&lt;p&gt;My Journey of tinkering and building software has followed the web development route. From my days of creating web pages with HTML, CSS, and Bootstrap, through my experience building dynamic web pages with Django, I am currently focused on building backend infrastructures for RESTful APIs.&lt;/p&gt;
&lt;p&gt;But moving forward, I want to dedicate my free time to researching and creating games, and I want to follow a very academic-inclined methodology, going on deep dives into game development concepts and standards.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;“Preparing to do the thing isn’t doing the thing.&lt;/p&gt;
&lt;p&gt;Scheduling time to do the thing isn’t doing the thing.&lt;/p&gt;
&lt;p&gt;Making a to-do list for the thing isn’t doing the thing.&lt;/p&gt;
&lt;p&gt;… Go and do the thing.”—&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://en.wikipedia.org/wiki/Chris_Williamson_%28TV_personality%29&quot;&gt;Chris Williamson&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So this is me: doing the thing.&lt;/p&gt;
&lt;h2 id=&quot;beginning-the-work-proper&quot;&gt;Beginning the Work Proper&lt;/h2&gt;
&lt;p&gt;Game development involves developing the interface and logic of a game. Now there are some toolings out there that make it easy by offering visual tools to customize the different components of your game, but I intend to follow the old way, learning and building every part of my game piece by piece, as was the way of my father before me.&lt;/p&gt;
&lt;p&gt;For one, this is a sure way to gain mastery in this topic, but it also ensures that my ability to create worlds is not limited to the tooling at hand, but to my skills with the crude tool (a Programming language). I am sure along my journey, I would have needed to use other toolings for asset creation and management, and when I do, I would dedicate articles to those tools as they apply to game development.&lt;/p&gt;
&lt;p&gt;I started working with Rust a while back (Roughly a year now), my confidence in my ability to use its tooling is growing by the day, and I intend to use that as the primary language for my game development and research (I have good expertise with Python Programming, and I intend to use that for scripting when possible). In that same vein, I have decided to go with the Bevy Engine for game development in Rust.&lt;/p&gt;
&lt;h2 id=&quot;why-rust&quot;&gt;Why Rust&lt;/h2&gt;
&lt;p&gt;Rust feels like the next logical mutation of my craft; sharp, uncompromising, and strangely elegant. When you’re trying to sculpt worlds where every microsecond can make or break the illusion, safety and performance aren’t luxuries; they’re the foundations of the universe. If I’m going to build digital worlds, I want their limits to be the boundaries of physics, not the fragility of my code..&lt;/p&gt;
&lt;p&gt;In games, most times, a lot of things are happening at the same time, or the illusion of a lot of things happening at once is one that the game developer invokes in the minds of the players. Rust&#39;s concurrency model offers a sweet support for such a feature, allowing concurrent processing of things to optimize events.&lt;/p&gt;
&lt;p&gt;Rust has amazing documentation and support. The Rust community is a very active, religious-like community; there is almost always someone or a reference to help clarify any issues you might be facing with your code logic or program. This understanding creates a motivating mindset and helps new developers get access to support, information, and even tooling to improve their productivity. You definitely do not want to spend days trying to resolve a programming-related issue when you could focus more on your game narrative, story, and world.&lt;/p&gt;
&lt;h2 id=&quot;why-bevy&quot;&gt;Why Bevy&lt;/h2&gt;
&lt;p&gt;And then there is Bevy, a young Rust-based game engine. I chose Bevy because of the reasons mentioned for Rust, since it is a game engine built in Rust, but it also has some independent perks that make it a choice for my journey&lt;/p&gt;
&lt;p&gt;The thought put into Bevy is innovative and beautiful; it is a very data-centric game engine, which is the basis of any world; Data! Allowing game developers to model simple and complex game logics with a simple, systematic approach.&lt;/p&gt;
&lt;p&gt;Bevy is also very fast, both in operation and in compile times, which becomes a big deal when making iterative changes to your games during development.&lt;/p&gt;
&lt;p&gt;Since this article is written at the start of my journey, I can not speak on the technical sides of things. I have little to no real working experience using Bevy, but from my research, it has proven to be an excellent choice to commit to, and like the saying goes, “You can never know until you try”.&lt;/p&gt;
&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;You can never know until you try&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;the-road-ahead&quot;&gt;The Road Ahead&lt;/h2&gt;
&lt;p&gt;Since this is a hobby, I intend to have a systematic and consistent interval of work, documenting my progress in the open, and helping someone like me out there looking for a nice reference for learning game development. I am open to collaboration and corrections to the content and information I put out.&lt;/p&gt;
&lt;p&gt;If this sort of thing resonates with you, you should consider subscribing to my articles to get an update when I drop new posts. In the meantime, I would continue to work on my hobby of game development. Until next time. Adios&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>How to Implement Autocomplete Functionality for Your CLI Program in Python</title>
        <published>2025-11-17T00:00:00+00:00</published>
        <updated>2025-11-17T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/autocomplete-for-your-cli-program-in-python/"/>
        <id>https://brianobot.tech/blog/autocomplete-for-your-cli-program-in-python/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/autocomplete-for-your-cli-program-in-python/">&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/autocomplete-for-your-cli-program-in-python/cover.webp&quot; alt=&quot;Sunlit rocky mountain peak wrapped in cloud above a pine forest&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;motivation&quot;&gt;Motivation&lt;/h2&gt;
&lt;p&gt;While taking on a coding challenge on the &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://codecrafters.io/&quot;&gt;codecrafters&lt;/a&gt; platform. I was tasked to implement an autocompletion feature for my shell project. While researching how autocompletion works under the hood for most popular shell programs, I came across the &lt;code&gt;readline&lt;/code&gt; standard Python module which provides an interface round the GNU Readline library.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;readline&lt;/code&gt; module defines a number of functions to facilitate completion and reading/writing of history files from the Python interpreter.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Building a command‑line interface is a bit like conducting a conversation with a very literal friend. They wait for your every word, take everything at face value, and never finish your sentences , unless you teach them how. That’s where autocomplete comes in.&lt;/p&gt;
&lt;p&gt;In this guide, we’ll explore how to add smooth, shell‑like tab completion to a Python CLI program. We’ll draw inspiration from the &lt;code&gt;readline&lt;/code&gt; library, a small but mighty tool that can make your interface feel remarkably alive.&lt;/p&gt;
&lt;h2 id=&quot;why-autocomplete-matters&quot;&gt;Why Autocomplete Matters&lt;/h2&gt;
&lt;p&gt;A CLI with autocomplete feels like a machine that understands your intentions or at least predicts your vocabulary. It saves keystrokes, reduces errors, and gives your program a professional, polished feeling. Anyone who’s used Bash, Zsh, or Fish knows the joy of tapping Tab and watching the program finish their thought.&lt;/p&gt;
&lt;p&gt;We’re going to recreate a slice of that magic.&lt;/p&gt;
&lt;h2 id=&quot;the-core-idea&quot;&gt;The Core Idea&lt;/h2&gt;
&lt;p&gt;Autocomplete in Python generally works through the &lt;code&gt;readline&lt;/code&gt; library. This library lets you:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Capture user input with a customizable editing experience.&lt;/li&gt;
&lt;li&gt;Bind keys (like Tab) to specific behaviors.&lt;/li&gt;
&lt;li&gt;Install a &lt;em&gt;completer function&lt;/em&gt; that decides what suggestions to serve.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The flow looks like this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;User presses Tab.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;readline&lt;/code&gt; calls your completer function.&lt;/li&gt;
&lt;li&gt;Your function returns a possible match.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;readline&lt;/code&gt; cycles through the options as Tab is pressed repeatedly.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Simple but elegant.&lt;/p&gt;
&lt;h2 id=&quot;a-practical-example-building-a-vocabulary-based-completer&quot;&gt;A Practical Example: Building a Vocabulary-Based Completer&lt;/h2&gt;
&lt;p&gt;Below is a clean snippet that demonstrates how to create and register a simple completer that suggests words from a predefined vocabulary.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; make_completer&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;vocabulary&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; https://docs.python.org/3/library/rlcompleter.html#rlcompleter.Completer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;  def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; custom_complete&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; state&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; None is returned for the end of the completion session.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  results&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; for&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; vocabulary&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; if&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;startswith&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;text&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; A space is added to the completion since the Python readline doesn&amp;#39;t&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; do this on its own. When a word is fully completed we want to mimic&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; the default readline library behavior of adding a space after it.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;  return&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; results&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;return&lt;/span&gt;&lt;span&gt; custom_complete&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;vocabulary&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;readline&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;parse_and_bind&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;tab: complete&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;readline&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;parse_and_bind&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;bind ^I rl_complete&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;readline&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;set_completer&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;make_completer&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;vocabulary&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s break down what’s happening.&lt;/p&gt;
&lt;h2 id=&quot;understanding-the-completer-function&quot;&gt;Understanding the Completer Function&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;make_completer()&lt;/code&gt; function generates a nested &lt;code&gt;custom_complete()&lt;/code&gt; function tailored to the vocabulary you provide.&lt;/p&gt;
&lt;p&gt;When the user types:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;e&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and presses Tab, &lt;code&gt;custom_complete()&lt;/code&gt; steps in and returns options like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;echo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;exit&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It adds a trailing space because Python’s &lt;code&gt;readline&lt;/code&gt; does not automatically separate completed words. This little flourish makes the experience feel more natural.&lt;/p&gt;
&lt;h3 id=&quot;binding-the-tab-key&quot;&gt;Binding the Tab Key&lt;/h3&gt;
&lt;p&gt;This part can get interesting depending on your system.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Standard GNU Readline&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;On many Linux systems, this line is enough:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; this would work on most linux system&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;readline&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;parse_and_bind&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;tab: complete&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;macOS (using libedit)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;macOS ships with &lt;code&gt;editline&lt;/code&gt;, a readline impersonator with its own dialect. The line above won’t work alone, so you add:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;readline&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;parse_and_bind&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;bind ^I rl_complete&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This tells libedit: “When the user presses Tab (&lt;code&gt;^I&lt;/code&gt;), run the completer.&quot;&lt;/p&gt;
&lt;p&gt;Together, these bindings give your CLI the behavior you expect.&lt;/p&gt;
&lt;h2 id=&quot;using-the-completer-in-a-cli-loop&quot;&gt;Using the Completer in a CLI Loop&lt;/h2&gt;
&lt;p&gt;Once your completer is set up, you can drop it into an input loop like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; True&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  command&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; input&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; process command&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, each time your user begins a word and taps Tab, suggestions spring to life.&lt;/p&gt;
&lt;h3 id=&quot;extending-the-concept&quot;&gt;Extending the Concept&lt;/h3&gt;
&lt;p&gt;This basic vocabulary match is just the beginning. You can expand the idea in powerful ways:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Different vocabularies depending on context.&lt;/li&gt;
&lt;li&gt;Directory and file completion.&lt;/li&gt;
&lt;li&gt;Subcommand trees (like Git or Docker).&lt;/li&gt;
&lt;li&gt;Dynamic completions from APIs or internal state.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Once you get comfortable, you can build a CLI that rivals many native tools.&lt;/p&gt;
&lt;h3 id=&quot;final-thoughts&quot;&gt;Final Thoughts&lt;/h3&gt;
&lt;p&gt;Autocomplete transforms a stiff prompt into a partner in dialogue. With just a few thoughtful lines of Python, your CLI can anticipate the user’s needs, usher them gently forward, and turn typing into more of a dance than a chore.&lt;/p&gt;
&lt;p&gt;The code above plants the seed, what grows from it is up to you.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>How to generate an ERD for your Database with Python in seconds</title>
        <published>2025-11-06T00:00:00+00:00</published>
        <updated>2025-11-06T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/generate-an-erd-for-your-database-with-python/"/>
        <id>https://brianobot.tech/blog/generate-an-erd-for-your-database-with-python/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/generate-an-erd-for-your-database-with-python/">&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/generate-an-erd-for-your-database-with-python/cover.webp&quot; alt=&quot;Scenic Mountain picture by TERADAT SANTIVIVUT&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Scenic Mountain picture by &lt;strong&gt;TERADAT SANTIVIVUT&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;tl-dr&quot;&gt;TL;DR&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Install &lt;code&gt;eralchemy2&lt;/code&gt; (&lt;em&gt;if you have an issue in this step, visit the solution at the bottom&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Import &lt;code&gt;render_er&lt;/code&gt; function from &lt;code&gt;eralchemy2&lt;/code&gt; package&lt;/li&gt;
&lt;li&gt;(Optional) Replace &lt;code&gt;+asyncpg&lt;/code&gt; in your DB URL if you’re using async database connection.&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;render_er()&lt;/code&gt; to generate a &lt;code&gt;.png&lt;/code&gt;. It takes two parameters: your database connection string and the string path to save the image to.&lt;/li&gt;
&lt;li&gt;Admire your handiwork.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;An &lt;strong&gt;Entity–Relationship Diagram (ERD)&lt;/strong&gt; is a type of data modeling diagram used to visually represent the structure of a database. It illustrates the entities (tables) within a system, their attributes (columns), and the relationships that exist between them. ERDs are commonly used during the design phase of database development to provide a high-level understanding of how data elements interact and are organized.&lt;/p&gt;
&lt;p&gt;The concept of ERDs was first introduced by &lt;strong&gt;Peter Chen&lt;/strong&gt; in 1976 as part of the Entity–Relationship model, which aimed to create a more natural and conceptual view of data. Over time, ERDs have become a standard tool in database design and are widely supported by modern database management and modeling software.&lt;/p&gt;
&lt;p&gt;An ERD typically consists of three primary components:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Entities&lt;/strong&gt;—Represented as rectangles, entities correspond to database tables that store specific data objects (e.g., &lt;code&gt;Users&lt;/code&gt;, &lt;code&gt;Orders&lt;/code&gt;, &lt;code&gt;Products&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Attributes&lt;/strong&gt;—Represented as ovals or listed within entities, these describe the properties of each entity (e.g., &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;email&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Relationships&lt;/strong&gt;—Represented as lines connecting entities, these indicate how data in one table relates to data in another, often defined through primary and foreign keys.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;ERDs are used by software engineers, database administrators, and data analysts to communicate the logical structure of databases, ensuring clarity, consistency, and efficient system design. They play an essential role in both &lt;strong&gt;conceptual&lt;/strong&gt; and &lt;strong&gt;physical&lt;/strong&gt; database modeling, bridging the gap between abstract data theory and practical implementation.&lt;/p&gt;
&lt;h3 id=&quot;example-of-erd-diagram&quot;&gt;Example of ERD Diagram&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/generate-an-erd-for-your-database-with-python/image-2.webp&quot; alt=&quot;A sample ERD&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;A sample ERD&lt;/em&gt;&lt;/p&gt;
&lt;h3 id=&quot;why-this-matters&quot;&gt;Why This Matters&lt;/h3&gt;
&lt;p&gt;In modern data-driven apps, your schema is a living thing; constantly evolving. An ERD keeps it &lt;strong&gt;visible&lt;/strong&gt;,** understandable**, and** collaborative**.&lt;/p&gt;
&lt;p&gt;As developers, we spend too much time spelunking through migrations and table definitions. Let’s give our eyes a break; and our diagrams some love.&lt;/p&gt;
&lt;h3 id=&quot;script-to-generate-erd&quot;&gt;Script to Generate ERD&lt;/h3&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;settings&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; Settings&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; eralchemy2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; render_er&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; i prefer to load my environment variables with Pydantic-setting BaseSetting Model&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;settings&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; Settings&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;SYNC_DATABASE_URL&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; settings&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;DATABASE_URL&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;+asyncpg&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;render_er&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;SYNC_DATABASE_URL&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;erd_from_sqlite.png&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After running this script, an new image file will be created with the name you passed the second argument to the &lt;code&gt;render_er&lt;/code&gt; function.&lt;/p&gt;
&lt;h3 id=&quot;common-tips-pitfalls&quot;&gt;Common Tips &amp;amp; Pitfalls&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Async Database URL&lt;/strong&gt; won’t work with &lt;code&gt;eralchemy2&lt;/code&gt; as at the time of writing this.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;If nothing shows up:&lt;/strong&gt; Make sure your database is accessible and populated with actual tables.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SQLite quirks:&lt;/strong&gt; If you’re using SQLite in-memory (&lt;code&gt;sqlite:///:memory:&lt;/code&gt;), it won’t persist for introspection. Use a file-based or external DB instead.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Make it part of CI:&lt;/strong&gt; Automate ERD generation so your team always has an up-to-date schema map.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Common Issue with Installing Graphviz on macOS&lt;/strong&gt;&lt;br /&gt;
Sometimes when installing, there is an issue with the installation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;export&lt;/span&gt;&lt;span&gt; C_INCLUDE_PATH&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;brew&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;-prefix&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; graphviz&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/include/&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;export&lt;/span&gt;&lt;span&gt; LIBRARY_PATH&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;brew&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;-prefix&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; graphviz&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/lib/&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;pip&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; install&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;-use-pep517&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;-config-setting=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;--global-option=build_ext&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; pygraphviz&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;final-thought&quot;&gt;Final Thought&lt;/h3&gt;
&lt;p&gt;With just a few lines of Python, you can turn your database schema into a visual map; a living portrait of your data’s architecture. A simple tool, a tiny script, but then the structure of your data model can be communicated more visually.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Zed: The Text Editor Reimagined for the Modern Developer</title>
        <published>2025-11-05T00:00:00+00:00</published>
        <updated>2025-11-05T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/zed-the-text-editor-reimagined-for-the-modern-developer/"/>
        <id>https://brianobot.tech/blog/zed-the-text-editor-reimagined-for-the-modern-developer/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/zed-the-text-editor-reimagined-for-the-modern-developer/">&lt;p&gt;Why Zed might just change the way you write software.&lt;/p&gt;
&lt;h2 id=&quot;guide-to-setup-and-configure-zed-editor&quot;&gt;Guide to Setup and Configure Zed Editor&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/zed-the-text-editor-reimagined-for-the-modern-developer/cover.webp&quot; alt=&quot;Zed AI logo over a blurred code editor&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Why switching to Zed might improve your workflow as a developer.&lt;/p&gt;
&lt;p&gt;As a developer, the text editor is the one software application you would spend most of your time crafting programs, scripts, and updating text related to software development. It is only reasonable that you get the most out of your primary text editor of choice.&lt;/p&gt;
&lt;h2 id=&quot;why-zed&quot;&gt;Why Zed!&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;It’s New, Modern, and Built for the Future:&lt;/strong&gt; Zed isn’t just another text editor; it’s a re-imagination of what a coding environment should feel like in the age of blazing-fast CPUs, cloud collaboration, and intelligent code assistance. Born from the creators of Atom (Atom was my first ever experience with text editors when I started programming).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;It’s Very Fast :&lt;/strong&gt; Speed isn’t just a bragging right; it’s a philosophy. Zed is implemented in &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://rust-lang.org/&quot;&gt;&lt;strong&gt;Rust&lt;/strong&gt;&lt;/a&gt;, a language celebrated for its performance and safety. That means no Electron bloat, no sluggish startup, and no “fans going supersonic” moments. Opening large projects feels effortless. Searching across thousands of files happens in a blink. The editor responds to your typing as if it’s reading your mind; not your CPU cycles. Zed makes “instant” feel normal again.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;It’s Open Source and Transparent&lt;/strong&gt;: &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://github.com/zed-industries/zed&quot;&gt;Zed’s source code&lt;/a&gt; is fully open on GitHub; transparent, hackable, and alive with community input. You can explore the internals, submit improvements, or simply watch the evolution of one of the most ambitious open-source editors in recent years. Unlike many modern tools that hide their logic behind corporate curtains. This is particularly interesting if you’re a Rust developer, since the Zed extension ecosystem is still young, you can write your own extension that would become the foundation for developers in a few years.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;It’s Written in Rust; for Developers, by Developers:&lt;/strong&gt; By writing Zed entirely in Rust, the team didn’t just chase performance, they embraced correctness and safety. This foundation gives developers confidence that the tool they rely on most won’t buckle under pressure. Rust’s concurrency model also allows Zed to manage multiple tasks at once, background indexing, syntax parsing, and collaborative sessions, without skipping a beat. It’s not just fast; it’s &lt;em&gt;predictably&lt;/em&gt; fast&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;It’s Simple, Beautiful, and Purposeful:&lt;/strong&gt; Zed’s interface is an ode to minimalism. It’s clean, distraction-free, and elegant without being sterile. No cluttered panes or noisy toolbars fighting for your attention. Every pixel has intent. Whether you’re working in a dark room late into the night or in a bright café, Zed’s design feels at home. The UI becomes an extension of your mind, just you and your code, nothig else.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;It’s AI-centric&lt;/strong&gt;, integrates upcoming LLMs into your workflow to generate, transform, and analyze code. Where most editors treat AI as a plugin or novelty, Zed is building it into the heart of the workflow. It’s designed to integrate seamlessly with large language models (LLMs) to work with codes without breaking your flow.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;it’s Built for Collaboration&lt;/strong&gt;: When I first experienced the add contact feature in Zed, I was wowed, it felt more like a social media for developers, and that gave me more reasons to adore the intentionality put into the development of the text editor.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If any of these attributes interest you just a little bit, just wait and see the comparison against a popular option.&lt;/p&gt;
&lt;h2 id=&quot;how-does-zed-compare-against-vscode&quot;&gt;How does Zed compare against VSCode?&lt;/h2&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Feature&lt;/th&gt;&lt;th&gt;VSCode&lt;/th&gt;&lt;th&gt;Zed&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Startup time&lt;/td&gt;&lt;td&gt;⚡ Slow (Electron)&lt;/td&gt;&lt;td&gt;⚡⚡ Instant (Rust native)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Memory use&lt;/td&gt;&lt;td&gt;Heavy&lt;/td&gt;&lt;td&gt;Light&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Collaboration&lt;/td&gt;&lt;td&gt;Extensions required&lt;/td&gt;&lt;td&gt;Built-in multiplayer&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;AI tools&lt;/td&gt;&lt;td&gt;Copilot&lt;/td&gt;&lt;td&gt;Native integration planned&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;UI/UX&lt;/td&gt;&lt;td&gt;Customizable but dense&lt;/td&gt;&lt;td&gt;Minimal and elegant&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;License&lt;/td&gt;&lt;td&gt;MIT&lt;/td&gt;&lt;td&gt;Open Source&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Ecosystem&lt;/td&gt;&lt;td&gt;Matured&lt;/td&gt;&lt;td&gt;Young&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;installation&quot;&gt;Installation&lt;/h2&gt;
&lt;p&gt;If you do not already have Zed installed on your computer, follow this &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://zed.dev/download&quot;&gt;link&lt;/a&gt; to the Zed official page to download the version for your operating system. Currently, Zed supports macOS, Windows, and Linux.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/zed-the-text-editor-reimagined-for-the-modern-developer/image-2.webp&quot; alt=&quot;The Zed download page on zed.dev, with download options for macOS, Windows and Linux&quot; /&gt;&lt;/p&gt;
&lt;p&gt;After download and installation, you can proceed to start a new instance of the Zed Editor on your computer.&lt;/p&gt;
&lt;p&gt;If you’re like me, coming from a VSCode background, you must understand the great ergonomics gotten from interacting with VSCode from the terminal and vice versa. This can be your reality too; in order to gain this ability, you would need to install the Zed CLI. An example of the benefits gained from installing the CLI is opening a directory from the terminal in Zed with a command like &lt;code&gt;zed .&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;install-zed-cli&quot;&gt;Install Zed CLI&lt;/h3&gt;
&lt;p&gt;On Linux, the Zed CLI should come with the Linux distribution’s Zed package, as seen from the download step above. On macOS, the CLI comes with the binary and can be installed into your system via the command palette.&lt;/p&gt;
&lt;p&gt;I would focus on macOS for this guide, since I am using a Mac.&lt;/p&gt;
&lt;p&gt;You can access Zed’s Command Palette, which is very similar to the command palette found in VSCode. You can open the command palette with the following key binding: &lt;code&gt;cmd + shift + p&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/zed-the-text-editor-reimagined-for-the-modern-developer/image-3.webp&quot; alt=&quot;The Zed command palette listing commands such as zed: extensions, theme selector: toggle and cli: install cli binary&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Searching the command palette for cli: install would show the command for installing the CLI.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/zed-the-text-editor-reimagined-for-the-modern-developer/image-4.webp&quot; alt=&quot;Running cli: install cli binary from the Zed command palette, with a notice that zed was installed to /usr/local/bin/zed&quot; /&gt;&lt;/p&gt;
&lt;h3 id=&quot;choosing-a-theme-extension&quot;&gt;Choosing a Theme Extension&lt;/h3&gt;
&lt;p&gt;The minimalistic interface of the Zed editor can be greatly improved by choosing a theme that best aligns with your personality and self. For me, coming from VSCode and having worked with a certain shade of theme for a while, my go-to theme is this.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Steps to install a theme extension&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Open Command Palette with &lt;code&gt;cmd + shift + p&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Select &lt;code&gt;zed: extension&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Select Theme Tab&lt;/li&gt;
&lt;li&gt;Search for the Theme of your choice and install&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Once you’ve installed a theme of your choosing, you can toggle between these themes using the &lt;code&gt;theme selector: toggle&lt;/code&gt; command in the command palette.&lt;/p&gt;
&lt;p&gt;I use &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://github.com/tamimhasandev/colorizer&quot;&gt;Colorizer&lt;/a&gt; for my local development, because it offers a vibrant color contrast look and feel to text.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/zed-the-text-editor-reimagined-for-the-modern-developer/image-5.webp&quot; alt=&quot;Screenshot showing the Colorizer theme&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Screenshot showing the &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://github.com/tamimhasandev/colorizer&quot;&gt;Colorizer&lt;/a&gt; theme&lt;/em&gt;&lt;/p&gt;
&lt;h3 id=&quot;command-keys-bindings&quot;&gt;Command Keys Bindings&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;cmd + shift + p&lt;/code&gt;: opens the command palette for the text editor.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cmd + j&lt;/code&gt;: Toggles the integrated terminal within the editor.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cmd + b&lt;/code&gt;: Toggles the sidebar visibility.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cmd + k + 0&lt;/code&gt;: Collapse all regions in the current active text file (code region refers to function bodies and block structures)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cmd + k + j&lt;/code&gt;: Expand all regions in the current text editor. This is the opposite of the command above.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cmd + p&lt;/code&gt;: Finds a file in the current working directory.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cmd + k + w:&lt;/code&gt; Close all open tabs in the editor&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cmd + shift + f:&lt;/code&gt; Search all files in the current directory for text match&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;Zed is still relatively new compared to matured editors like VSCode, so it’s still lacking some features that you might have seen in other text editors, regardless, for what it does have, it does them very. It was built with the pain point of modern developers in mind, it’s embraced AI to boost development rate and keeps the developer flow at all time high with little to no noise from the UI. I am still very new to Zed, But I do intend to make it my new best editor moving forward.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>6 Python Tips That Quietly Level Up Your Code</title>
        <published>2025-10-25T00:00:00+00:00</published>
        <updated>2025-10-25T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/some-python-tips-that-quietly-level-up-your-code/"/>
        <id>https://brianobot.tech/blog/some-python-tips-that-quietly-level-up-your-code/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/some-python-tips-that-quietly-level-up-your-code/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;If you&#39;re like me at the moment, you&#39;ve gotten to a point in your software development journey
where you find yourself writing code to tick off tasks from a task board or complete features for
a sprint. At such times, the focus sometimes shifts from poetic code to functional code (not
implying that functional code cannot be easily poetic, but it takes mastery and experience to
maintain such quality when working under tight deadlines). It is at this point that you&#39;re more
concerned with getting things to work than with how they feel and look.&lt;/p&gt;
&lt;p&gt;Below are some Python tips and tricks I use and believe are not as emphasized as they should be.
They would do good to your mental health when writing code and, more importantly, when updating
that code 3 months from now.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Inline single-line blocks in block expressions&lt;/li&gt;
&lt;li&gt;Use underscores to delimit a large number&lt;/li&gt;
&lt;li&gt;Dictionary comprehension&lt;/li&gt;
&lt;li&gt;Using the &lt;code&gt;setdefault&lt;/code&gt; dictionary method&lt;/li&gt;
&lt;li&gt;Ignore exceptions with &lt;code&gt;contextlib.suppress&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Multiple statements on a single line&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;1-inline-single-line-blocks-in-block-expressions&quot;&gt;1. Inline single-line blocks in block expressions&lt;/h2&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;some_condition&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;if&lt;/span&gt;&lt;span&gt; some_condition&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Some Condition is True&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Some Condition is Falsey&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the code sample above, the correct conventional 4-space indentation is used for the expression
in a block expression. But in this particular case, the block contains just a single expression.
This might be a trivial case, but many such scenarios arise a lot in functional codebases, and you
can save some real estate by inlining the single expression on the same line as the &lt;code&gt;if&lt;/code&gt; block.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;some_condition&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;if&lt;/span&gt;&lt;span&gt; some_condition&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Some Condition is True&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Some Condition is False&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is perfectly valid Python, and in some cases makes your code easier to read and follow. Do
note that you &lt;strong&gt;can only&lt;/strong&gt; inline single-line expressions; if the expression is multi-line, you
have to use the conventional 4-space indentation to communicate the block scope to the Python
interpreter. If, however, always using the block format for block expressions works better for
you, by all means do use that way.&lt;/p&gt;
&lt;h2 id=&quot;2-use-underscores-to-delimit-a-large-number&quot;&gt;2. Use underscores to delimit a large number&lt;/h2&gt;
&lt;p&gt;This is more common than some of the tips on this list. Still, for the uninitiated, here goes
nothing. When dealing with literal values in your application, it can be counterintuitive or even
error-prone having to figure out the value of a large literal number at a glance, and this could
very well mean the only thing standing between you and your continued state of mental flow.
Python — and some other high-level programming languages like Rust — allows the use of &lt;code&gt;_&lt;/code&gt; in
literal numbers to show their unit.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;obscure_large_number&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 3422345365&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;clear_large_number&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 3_422_345_365&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; it is easier to tell the unit value when delimited by underscores&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;3-dictionary-comprehension&quot;&gt;3. Dictionary comprehension&lt;/h2&gt;
&lt;p&gt;The more popular sibling of the comprehension family in Python is the list comprehension, where a
list is generated from an iterable in a single expression without needing a conventional for-loop
to populate the list. This same process can be used to generate a dictionary or a set.&lt;/p&gt;
&lt;p&gt;Let&#39;s imagine we have a list of names and want to generate a dictionary whose key is a name and
whose value is the length of that name&#39;s characters. We can achieve this with a traditional
for-loop, like so:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;names&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Brian&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Anthony&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Maryann&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Joseph&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Emediong&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;word_count&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;for&lt;/span&gt;&lt;span&gt; name&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; names&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;    word_count&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; len&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;word_count&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Output&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; {&amp;#39;Brian&amp;#39;: 5, &amp;#39;Anthony&amp;#39;: 7, &amp;#39;Maryann&amp;#39;: 7, &amp;#39;Joseph&amp;#39;: 6, &amp;#39;Emediong&amp;#39;: 8}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now this same functionality can be achieved through dictionary comprehension in a more
comprehensive way (pun intended). It looks just like a list comprehension, but curly braces are
used, and a colon separates the key from the value being derived from the iterable.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;names&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Brian&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Anthony&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Maryann&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Joseph&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Emediong&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;word_count&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; len&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; for&lt;/span&gt;&lt;span&gt; name&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; names&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; general dictionary comprehension syntax&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; {key: derived_value for key in sequence}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;word_count&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Output&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; {&amp;#39;Brian&amp;#39;: 5, &amp;#39;Anthony&amp;#39;: 7, &amp;#39;Maryann&amp;#39;: 7, &amp;#39;Joseph&amp;#39;: 6, &amp;#39;Emediong&amp;#39;: 8}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;4-using-the-setdefault-dictionary-method&quot;&gt;4. Using the &lt;code&gt;setdefault&lt;/code&gt; dictionary method&lt;/h2&gt;
&lt;p&gt;Still on the topic of working with dictionaries in Python: there are times you wish to update a
value in a dictionary, but you have to ensure that said key already exists in the dictionary, or
Python would raise a &lt;code&gt;KeyError&lt;/code&gt;. You could write a one-liner to check for the existence of the key
before updating the key&#39;s value, like so:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;race_laps&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Brian&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 234&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Anthony&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 525&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Maryann&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 534&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Joseph&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 467&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Emediong&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 876&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;new_lap_data&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Brian&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 34&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Anthony&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 45&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Felix&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 100&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;for&lt;/span&gt;&lt;span&gt; driver&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; distance&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; new_lap_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    if&lt;/span&gt;&lt;span&gt; driver&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; race_laps&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        race_laps&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;driver&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +=&lt;/span&gt;&lt;span&gt; distance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    else&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        race_laps&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;driver&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; distance&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But it is clear that this introduces unnecessary verbosity into our program by introducing an
&lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; block. We can use the &lt;code&gt;setdefault&lt;/code&gt; method of a dictionary to set a default value for a
key before trying to update it, in case it is not present in the dictionary. The same logic above
can be rewritten like so with the &lt;code&gt;setdefault&lt;/code&gt; method:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;race_laps&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Brian&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 234&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Anthony&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 525&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Maryann&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 534&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Joseph&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 467&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Emediong&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 876&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;new_lap_data&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Brian&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 34&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Anthony&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 45&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Felix&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 100&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;for&lt;/span&gt;&lt;span&gt; driver&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; distance&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; new_lap_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    race_laps&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;setdefault&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;driver&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; this sets a value of 0 for the driver if not already in the dictionary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;    race_laps&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;driver&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; +=&lt;/span&gt;&lt;span&gt; distance&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This approach makes the code more concise and is more Pythonic than the structure with the
&lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; block.&lt;/p&gt;
&lt;h2 id=&quot;5-ignore-exceptions-with-contextlib-suppress&quot;&gt;5. Ignore exceptions with &lt;code&gt;contextlib.suppress&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;Sometimes we want to catch an exception and then do nothing with it — we&#39;re catching it to prevent
the program from breaking or stopping, and to continue with the flow. This can be done with a
&lt;code&gt;try&lt;/code&gt;/&lt;code&gt;except&lt;/code&gt; block, and if you have no logic to run in the &lt;code&gt;except&lt;/code&gt; block, you can use the &lt;code&gt;pass&lt;/code&gt;
statement to indicate nothingness.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; do_some_action&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; some action that might fail but is not critical to the whole program&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    pass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; True&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    try&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;        do_some_action&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    except&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; Exception&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        pass&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the code sample above, we put the logic that might raise an exception in the &lt;code&gt;try&lt;/code&gt; block and
then ignore the cases where it raises. This structure forces us to use a block; we could apply the
first tip from this post to further improve the structure and gain some real estate.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; do_some_action&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; some action that might fail but is not critical to the whole program&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    pass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; True&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    try&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; do_some_action&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    except&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; Exception&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; pass&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, we can go even further and use a standard library class that is not very well known, to
suppress exceptions and continue with your program as if they hadn&#39;t happened. The &lt;code&gt;contextlib&lt;/code&gt;
standard library provides a class called &lt;code&gt;suppress&lt;/code&gt; that can be used to suppress exceptions in our
code. Going back to the same example as above, the code can be rewritten with &lt;code&gt;suppress&lt;/code&gt; as
follows:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; contextlib&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; suppress&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; do_some_action&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; some action that might fail but is not critical to the whole program&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    pass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; True&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    with&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; suppress&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; the exception is suppressed silently&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;        do_some_action&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This suppresses the exception type passed to it, and just like the normal exception rule, a parent
class can suppress child exception classes. You can also suppress multiple exceptions with a
&lt;code&gt;with&lt;/code&gt; block by passing more exception classes to &lt;code&gt;suppress&lt;/code&gt;, separated by commas.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; suppress&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; ValidationError&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; ValueError and ValidationError are suppressed here&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;6-multiple-statements-on-a-single-line&quot;&gt;6. Multiple statements on a single line&lt;/h2&gt;
&lt;p&gt;Conventionally, a newline in Python code declares the end of a statement and the start of another.
Some other programming languages use semicolons to separate statements, but Python is a whitespace
language. Regardless, semicolons can be used in Python, and in rare situations you might need to
put more than one statement on a single line — you can do this by terminating each statement with a
semicolon.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Conventional Python statement flow&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; each newline denotes a different statement&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;A = &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;B = &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; using semicolons to separate statements&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;span&gt;;  &lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; this is also valid Python&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 3&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;A = &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; putting multiple statements on a single line&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 10&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;A = &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Clearly, putting each statement on its own line leads to better readability for your code, but in
rare scenarios, knowing that multiple statements can be placed on a single line can give you the
ability to do certain magic.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I love reading and learning about these little neat tricks and try to apply them when I see fit in
my projects. If there are similar tricks you know and would love to share, I&#39;d love to hear them.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>My FastAPI Project Blueprint</title>
        <published>2025-10-14T00:00:00+00:00</published>
        <updated>2025-10-14T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/my-favorite-fastapi-project-blueprint/"/>
        <id>https://brianobot.tech/blog/my-favorite-fastapi-project-blueprint/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/my-favorite-fastapi-project-blueprint/">&lt;blockquote&gt;
&lt;p&gt;I&#39;ve refined this structure in production environments serving over 500+ concurrent users; it scales effortlessly, stays sane to maintain, and keeps productivity at full throttle.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;tl-dr&quot;&gt;TL;DR&lt;/h2&gt;
&lt;p&gt;If you just want to use the template FastAPI structure directly in your project, check out
&lt;a href=&quot;#&quot;&gt;this GitHub repo&lt;/a&gt; for the complete project structure.&lt;/p&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I have worked with FastAPI almost daily in the last year. During that period, I developed a
standard project structure that works exceptionally well to keep me motivated to make changes
and add features easily, and it also scales incredibly.&lt;/p&gt;
&lt;p&gt;In this post, I will break down every part of my FastAPI go-to architecture — the one I optimize
whenever I&#39;m starting a new project or refactoring an old one. Whether you&#39;re trying to ship
ideas faster, organize your growing codebase, or harden your app&#39;s security, this structure will
help you build confidently and evolve your project like a pro.&lt;/p&gt;
&lt;h2 id=&quot;project-structure&quot;&gt;Project Structure&lt;/h2&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;├── Makefile&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;├── logs/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;│   └── app.log&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;└── app/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── __init__.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── api_router.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── dependencies.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── logger.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── main.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── middlewares.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── settings.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── routers/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    │   ├── admin.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    │   ├── auth.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    │   └── products.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── schemas/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    │   ├── admin.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    │   ├── auth.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    │   └── products.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    └── services/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ├── admin.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ├── auth.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        └── products.py&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;the-app-directory&quot;&gt;The App Directory&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;app&lt;/code&gt; directory is the engine and core of the application. It contains all the modules and
code needed to power the majority of use cases for a backend infrastructure. I&#39;ll explain each
directory and module in the app directory and justify their use, pointing out common issues and
highlighting steps taken to improve code quality and developer experience.&lt;/p&gt;
&lt;h3 id=&quot;init-py&quot;&gt;&lt;code&gt;__init__.py&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;It is standard practice to include an &lt;code&gt;__init__.py&lt;/code&gt; file in every Python package; this helps to
declare a folder as a regular Python package.&lt;/p&gt;
&lt;h3 id=&quot;api-router-py&quot;&gt;&lt;code&gt;api_router.py&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;I use this as a central module for managing all my routers in my application. This enables me to
have features like version controlling with &lt;code&gt;/v1/users&lt;/code&gt;, &lt;code&gt;/v2/users&lt;/code&gt; while keeping the codebase
easy to follow and clean.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; app/api_router.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; APIRouter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;api_v1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; APIRouter&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/v1&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;api_v2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; APIRouter&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/v2&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; include routes to a root route&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; from app.routers import routers as user_routers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; api_v1.include_router(users_routers)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;logger-py&quot;&gt;&lt;code&gt;logger.py&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Logging is a crucial part of every software application. It gives visibility into the operation
of any running software application and can help guide resolution of issues and bugs when they
do happen — and they &lt;em&gt;will&lt;/em&gt; happen.&lt;/p&gt;
&lt;p&gt;Instead of getting overwhelmed with configurations, this template works for me in almost all my
projects. You can extend it by setting up an external handler to connect your log records to a
pretty dashboard or analytics tool. This particular configuration allows me to group logs by day,
achieved with the &lt;code&gt;TimedRotatingFileHandler&lt;/code&gt;, making the process of debugging much easier.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; app/logger.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; logging&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; logging&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;handlers&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; TimedRotatingFileHandler&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;logger&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; logging&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;getLogger&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; JsonFormatter&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;Formatter&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; format&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-12 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; record&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        log_record&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-12 z-d-i&quot;&gt; self&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;formatTime&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;record&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-12 z-d-i&quot;&gt; self&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;datefmt&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; record&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;levelname&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; record&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;module&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;funcName&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; record&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;funcName&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;lineno&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; record&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;lineno&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; record&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;getMessage&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        return&lt;/span&gt;&lt;span&gt; json&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;dumps&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;log_record&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;file_handler&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; TimedRotatingFileHandler&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;logs/app.log&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; when&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;midnight&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; interval&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 86400&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; backupCount&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;file_handler&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;setFormatter&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;JsonFormatter&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;logger&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;handlers&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;span&gt;file_handler&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;logger&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;setLevel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;logging&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;INFO&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;main-py&quot;&gt;&lt;code&gt;main.py&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The main module is the entry point into the application. Most of the security policies and rules
for the application are set up here.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; app/main.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; contextlib&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; asynccontextmanager&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; datetime&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; datetime&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; UTC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; FastAPI&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; starlette&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;middleware&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;trustedhost&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; TrustedHostMiddleware&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; starlette&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;middleware&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;base&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; BaseHTTPMiddleware&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;middleware&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;gzip&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; GZipMiddleware&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;middleware&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;cors&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; CORSMiddleware&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;responses&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; RedirectResponse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;responses&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; JSONResponse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;requests&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; Request&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; HTTPException&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; FastAPI&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; slowapi&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; Limiter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; slowapi&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;util&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; get_remote_address&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; redis&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; asyncio&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; as&lt;/span&gt;&lt;span&gt; aioredis&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;logger&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; logger&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;api_router&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; api&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;settings&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; Settings&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;middlewares&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; log_request_middleware&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;settings&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; Settings&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;asynccontextmanager&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; lifespan&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; FastAPI&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    yield&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; initiate_app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    app&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; FastAPI&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        title&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;FastAPI Sample Project&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        summary&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;API for FastAPI Sample Project&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        lifespan&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span&gt;lifespan&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    origins&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;        #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Add allowed origins here&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;add_middleware&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        CORSMiddleware&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        allow_origins&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span&gt;origins&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        allow_credentials&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        allow_methods&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        allow_headers&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; tweak this to see the most efficient size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;add_middleware&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;GZipMiddleware&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; minimum_size&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;add_middleware&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        TrustedHostMiddleware&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        allowed_hosts&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;            #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Add allowed hosts here, this controls host that the application can be hosted on&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        ]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;add_middleware&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;BaseHTTPMiddleware&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; dispatch&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span&gt;log_request_middleware&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; this handles rate limiting protecting the API from abuse or Denial of Service Attacks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    limiter&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; Limiter&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;key_func&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span&gt;get_remote_address&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;state&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;limiter&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; limiter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;include_router&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;api&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;app&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; initiate_app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;exception_handler&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; http_exception_handler&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; Request&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; exc&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; HTTPException&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; JSONResponse&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        status_code&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span&gt;exc&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;status_code&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        content&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; exc&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;detail&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; request&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;url&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;path&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; datetime&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;UTC&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;isoformat&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;exception_handler&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; global_exception_handler&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; Request&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; exc&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; Exception&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    logger&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Unexpected error: &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;exc&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; JSONResponse&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        status_code&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        content&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;An unexpected error occurred&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;            &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; request&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;url&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;path&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; tags&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Root&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; root&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; this redirects root requests to the docs page&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; RedirectResponse&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/docs&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;settings-py&quot;&gt;&lt;code&gt;settings.py&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;I have discovered that using Pydantic&#39;s &lt;code&gt;BaseSettings&lt;/code&gt; model makes handling of environment
variables easy and clean. Types can be defined for the environment variables and they are
validated at app startup time, and default values can be provided for optional environment
variables.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; app/settings.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; pydantic_settings&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; BaseSettings&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; pydantic&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; ConfigDict&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; AnyUrl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; Settings&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseSettings&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    model_config&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; ConfigDict&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;env_file&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;.env&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    example_secret&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;example secret value&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    JWT_SECRET&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; required environment variable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    JWT_ALGORITHM&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;HS256&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;  #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; optional environment variable with default value&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;dependencies-py&quot;&gt;&lt;code&gt;dependencies.py&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;I use this module to group my custom route dependencies. Common dependencies like &lt;code&gt;get_db&lt;/code&gt;,
which is commonly used to get a session to interact with the database, can be declared here.
Using a dedicated module for dependencies makes code organization better and more modular, since
usually different routes depend on these dependencies. Testing is also easier since logic can be
tested in isolation.&lt;/p&gt;
&lt;h3 id=&quot;middlewares-py&quot;&gt;&lt;code&gt;middlewares.py&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;I use this module to group all the custom middlewares I define for my application. The modular
benefits outlined for a dedicated dependencies module also apply here.&lt;/p&gt;
&lt;h2 id=&quot;directories&quot;&gt;Directories&lt;/h2&gt;
&lt;h3 id=&quot;routers&quot;&gt;Routers&lt;/h3&gt;
&lt;p&gt;I find that creating a module for each logical grouping of routes makes development lovely and
easier — for example, an &lt;code&gt;auth.py&lt;/code&gt; module for all routes relating to user authentication and
profile management, a &lt;code&gt;products.py&lt;/code&gt; module for all routes relating to product management, and
&lt;code&gt;admin.py&lt;/code&gt; for all routes related to the admin API.&lt;/p&gt;
&lt;p&gt;I try to keep my route functions to at most 2 lines of code. The route function simply lays out
the route and defines the dependencies and query parameters needed, while the logic for each
route function is defined in a service function.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; app/routers/auth.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; typing&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; Annotated&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; pydantic&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; EmailStr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;routing&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; APIRouter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; sqlalchemy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;ext&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;asyncio&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; AsyncSession&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; Depends&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; Body&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; BackgroundTasks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;dependencies&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; get_db&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;schemas&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; auth&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; as&lt;/span&gt;&lt;span&gt; auth_schemas&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;services&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; auth&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; as&lt;/span&gt;&lt;span&gt; auth_services&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;router&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; APIRouter&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/auth&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; tags&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Authentication&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EmailBody&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; Annotated&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;EmailStr&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; Body&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;embed&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;DBDep&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; Annotated&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;AsyncSession&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; Depends&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot; z-l-i z-d-i&quot;&gt;get_db&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;router&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/signup&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; response_model&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span&gt;auth_schemas&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;UserModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; signup&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;    db&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; DBDep&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;    bg_task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; BackgroundTasks&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;    request_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; auth_schemas&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt;UserSignUpData&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; await&lt;/span&gt;&lt;span&gt; auth_services&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;signup_user&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;request_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; db&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; bg_task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The route function is easy on the eyes: the business logic is handled in the service function,
while the route focuses on defining the dependencies and requirements in the form of a JSON body
or query parameters.&lt;/p&gt;
&lt;h3 id=&quot;schemas&quot;&gt;Schemas&lt;/h3&gt;
&lt;p&gt;All my Pydantic models go into the schema module. Just like the routers directory, the schemas
directory houses all the schema modules. Usually I match each route module with a corresponding
schema module and a service module — this way each component of my application is grouped in a
very logical and predictable way. An example of a schema module is &lt;code&gt;auth.py&lt;/code&gt;, shown below.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; app/schemas/auth.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; uuid&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; UUID&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; datetime&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; datetime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; typing&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; Annotated&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; pydantic&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; EmailStr&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; Field&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; UserSignUpData&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    password&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; Annotated&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; Field&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;min_length&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; Annotated&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;EmailStr&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; Field&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;max_length&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;254&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt; UserModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-13 z-l-i z-d-15 z-d-i&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;    id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; UUID&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; EmailStr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    date_created&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; datetime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    date_updated&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; datetime&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;services&quot;&gt;Services&lt;/h3&gt;
&lt;p&gt;The services directory houses modules with the actual business logic to process the backend
functionality. This can include API calls to third-party services, functions to make query calls
to the database, and so on. Separating them this way allows a major separation of concerns
between the route&#39;s API requirements and the actual implementation.&lt;/p&gt;
&lt;p&gt;And just like I do for the routers and schemas directories, I match each module in those
directories with a corresponding module for the component covered in the services directory.
Working with the running example for the auth component:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; app/services/auth.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; datetime&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; timedelta&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; HTTPException&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; passlib&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;context&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; CryptContext&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;security&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; OAuth2PasswordBearer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; sqlalchemy&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; select&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; sqlalchemy&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;ext&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;asyncio&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; AsyncSession&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;settings&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; Settings&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;models&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; User&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; as&lt;/span&gt;&lt;span&gt; UserDB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;schemas&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; auth&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; as&lt;/span&gt;&lt;span&gt; auth_schema&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;settings&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; Settings&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;JWT_SECRET&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; settings&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;JWT_SECRET&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;JWT_ALGORITHM&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; settings&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;JWT_ALGORITHM&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ACCESS_TOKEN_LIFESPAN&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; timedelta&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;days&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;REFRESH_TOKEN_LIFESPAN&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; timedelta&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;days&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;pwd_context&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; CryptContext&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;schemes&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;bcrypt&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; deprecated&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;oauth2_scheme&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; OAuth2PasswordBearer&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;tokenUrl&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;v1/auth/token&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; verify_password&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;plain_password&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; hashed_password&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; pwd_context&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;verify&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;plain_password&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; hashed_password&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; get_password_hash&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; pwd_context&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;password&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; get_user&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; session&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; AsyncSession&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; UserDB&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; None&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    stmt&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; select&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;UserDB&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;where&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;UserDB&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; ==&lt;/span&gt;&lt;span&gt; email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    result&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; await&lt;/span&gt;&lt;span&gt; session&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;stmt&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;scalar_one_or_none&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; create_user&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;    user_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; auth_schema&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt;UserSignUpData&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;    session&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; AsyncSession&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    result&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; await&lt;/span&gt;&lt;span&gt; session&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;        select&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;UserDB&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;where&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;UserDB&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; ==&lt;/span&gt;&lt;span&gt; user_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    if&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;scalar_one_or_none&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        raise&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; HTTPException&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; detail&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Email already registered&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    result&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; await&lt;/span&gt;&lt;span&gt; session&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;        select&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;UserDB&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;where&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;UserDB&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; ==&lt;/span&gt;&lt;span&gt; user_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    if&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;scalar_one_or_none&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        raise&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; HTTPException&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; detail&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Username already used&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    hashed_password&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; get_password_hash&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;user_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;password&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    new_user&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; UserDB&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        email&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span&gt;user_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        password&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span&gt;hashed_password&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;        username&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span&gt;user_data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span&gt;email&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    session&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;new_user&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    await&lt;/span&gt;&lt;span&gt; session&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;commit&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    await&lt;/span&gt;&lt;span&gt; session&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;refresh&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;new_user&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; new_user&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; signup_user&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;    data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; auth_schema&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt;UserSignUpData&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;    session&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; AsyncSession&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; await&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; create_user&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; session&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;the-makefile&quot;&gt;The Makefile&lt;/h2&gt;
&lt;p&gt;Outside the core backend Python concerns, I have come to appreciate making use of tools like
&lt;code&gt;make&lt;/code&gt; to simplify invocation of common command line tools — like starting a FastAPI server,
starting a pytest session, and so on.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;make&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; Makefile&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;run-local&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	fastapi dev app/main.p&lt;/span&gt;&lt;span&gt;y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;test-local&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	pytest -s --co&lt;/span&gt;&lt;span&gt;v&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;coverage-report&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	coverage repor&lt;/span&gt;&lt;span&gt;t&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;coverage-html&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	coverage report &amp;amp;&amp;amp; coverage htm&lt;/span&gt;&lt;span&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In order to use the &lt;code&gt;make&lt;/code&gt; tool you have to have &lt;code&gt;make&lt;/code&gt; installed on your computer.&lt;/p&gt;
&lt;p&gt;With this file in place at the root of my project, whenever I want to start my FastAPI server, I
can do so with the following command:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;make&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; run-local&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Instead of typing out the full command. This contributes to increased productivity when running
certain command line tools over and over again.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I ignored setup for database connections in this template.&lt;/p&gt;
&lt;p&gt;Maintaining a modular project structure and codebase does more for you down the line than you
might think. A predictable and reasonable organization of functionality and tools gives beauty to
a codebase beyond the functionality itself.&lt;/p&gt;
&lt;p&gt;Inasmuch as this pattern has improved my productivity in the past year, I actively seek to learn
and adopt better and better approaches to constructing and crafting my backend infrastructure.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>How I Hacked FastAPI BackgroundTasks for Real-Time Progress Updates (Without Celery)</title>
        <published>2025-10-01T00:00:00+00:00</published>
        <updated>2025-10-01T00:00:00+00:00</updated>
        
        <author>
          <name>Brian Obot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://brianobot.tech/blog/how-i-hacked-fastapi-backgroundtasks-for-real-time-progress-updates/"/>
        <id>https://brianobot.tech/blog/how-i-hacked-fastapi-backgroundtasks-for-real-time-progress-updates/</id>
        
        <content type="html" xml:base="https://brianobot.tech/blog/how-i-hacked-fastapi-backgroundtasks-for-real-time-progress-updates/">&lt;p&gt;I Built a FastAPI Endpoint That Runs in the Background and Talks Back&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://brianobot.tech/blog/how-i-hacked-fastapi-backgroundtasks-for-real-time-progress-updates/cover.webp&quot; alt=&quot;Image was generated with ChatGPT&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Image was generated with ChatGPT&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;i-built-a-fastapi-endpoint-that-runs-in-the-background-and-talks-back&quot;&gt;I Built a FastAPI Endpoint That Runs in the Background and Talks Back&lt;/h2&gt;
&lt;h2 id=&quot;intro&quot;&gt;Intro&lt;/h2&gt;
&lt;p&gt;Recently, at work, I had to build a set of REST API endpoints to trigger an AI model process that could take several minutes to complete. The usual options weren’t ideal:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Long-polling&lt;/strong&gt; from the frontend until the result is ready (wasteful).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Blocking&lt;/strong&gt; the request until the model finishes (terrible UX).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fire-and-forget background job&lt;/strong&gt; with an email notification at the end (better, but not interactive).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Clearly, running the process in the background is efficient since it frees up the application to handle other requests. But here’s the problem: I wanted to &lt;em&gt;peek into the current state of the background process&lt;/em&gt; and report progress back to the client—without committing to a heavyweight background manager like Celery.&lt;/p&gt;
&lt;h2 id=&quot;problem&quot;&gt;Problem&lt;/h2&gt;
&lt;p&gt;You kick off an AI model through a FastAPI endpoint. It takes minutes. Your frontend can either keep hammering with polls, freeze the request until it’s done, or dump the job into a background worker and email the result later.&lt;/p&gt;
&lt;p&gt;None of those felt right for me. I wanted &lt;strong&gt;real-time progress updates&lt;/strong&gt;—without bringing in Celery, Redis, or Kubernetes-sized machinery. So I hacked FastAPI’s &lt;code&gt;BackgroundTasks&lt;/code&gt; into whispering back status updates while it ran. Here’s how.&lt;/p&gt;
&lt;p&gt;FastAPI supports background tasks through the &lt;code&gt;BackgroundTasks&lt;/code&gt; dependency. But it’s fire-and-forget. Once a task starts, you get no insight into its progress or result in real-time. Need to peek behind the curtain? You&#39;re out of luck—unless you add some extra plumbing.&lt;/p&gt;
&lt;h2 id=&quot;solution&quot;&gt;Solution&lt;/h2&gt;
&lt;p&gt;FastApi comes with a BackgroundTasks for managing Background tasks in a route, but it posses no native way of interacting with those background task once they have been initiated.&lt;/p&gt;
&lt;p&gt;below is a simple example of a route that initiates a background task that prints the numbers to a million&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; main.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; FastAPI&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; BackgroundTasks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;app&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; FastAPI&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; count_to&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; job_id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; update_progress&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; Callable&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; Awaitable&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    Iterates from 1 up to n and returns the values of n&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    This is meant to be used to represent a slow running function.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    result&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; range&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;        update_progress&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;job_id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;processing&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; round&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 100&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;        print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        result&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;    update_progress&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;job_id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;completed&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 100&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; ended_at&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span&gt;time&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/tasks&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; initiate_task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;background_task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; BackgroundTasks&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    background_task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;add_task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;count_to&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Task Started Successfully&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can run the FastAPI application with the followings commands&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;fastapi&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; dev&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; main.py&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;if you visit the endpoint you would immediately get a response that says&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;curl&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; localhost:8000/tasks&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; POST&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; {&amp;quot;detail&amp;quot;:&amp;quot;Task Started Successfully&amp;quot;}%  &lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And if you look at your application logs, you would see that numbers were printed from 1 through 999999&lt;/p&gt;
&lt;p&gt;This shows that the route started the count_to function in the background and immediately returned a message to the client&lt;/p&gt;
&lt;p&gt;Now, There is no direct way to obtain information about the running background task and report to the client (this can be setup with Redis and other Background process tools but for a simple case like us).&lt;/p&gt;
&lt;p&gt;I achieved this by making a global data structure to keep track of all the background task so that at any point after the background task has been started I can refer to this data structure to get update about the current state of the task and possibly it result&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; uuid&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;import&lt;/span&gt;&lt;span&gt; time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; typing&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; Callable&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; Awaitable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;from&lt;/span&gt;&lt;span&gt; fastapi&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; import&lt;/span&gt;&lt;span&gt; FastAPI&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; BackgroundTasks&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; HTTPException&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;app&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; FastAPI&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;task_jobs&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; count_to&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; job_id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; update_progress&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; Callable&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    Iterates from 1 up to n and returns the values of n&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    This is meant to be used to represent a slow running function.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    result&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; range&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;        update_progress&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;job_id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;processing&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt; round&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; /&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 100&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;        print&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-l-i z-d-7 z-d-i&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;{&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-13&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        result&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;    update_progress&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;job_id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;completed&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 100&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; ended_at&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span&gt;time&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/tasks&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; initiate_task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;background_task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-10&quot;&gt; BackgroundTasks&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    job_id&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;uuid&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;uuid4&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; register the task to the task db&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;    task_jobs&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;job_id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;initiated&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;progress&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 0.0&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; None&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;        &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; None&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; update_task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;job_id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; status&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; progress&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; None&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; errors&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; None&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; not&lt;/span&gt;&lt;span&gt; job_id&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; task_jobs&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;            return&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        task_jobs&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;          &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; status&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;          &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;progress&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; progress&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;        }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        if&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;            task_jobs&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;job_id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        if&lt;/span&gt;&lt;span&gt; errors&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;            task_jobs&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;job_id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt; =&lt;/span&gt;&lt;span&gt; errors&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    background_task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;add_task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span&gt;count_to&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1_000_000&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; job_id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span&gt; update_task&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;task started&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;job_id&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span&gt; job_id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-16 z-l-i z-d-16 z-d-i&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;/tasks/status&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; def&lt;/span&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt; get_task_status&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;job_id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-l-i z-d-9 z-d-i&quot;&gt; str&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    if&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; not&lt;/span&gt;&lt;span&gt; job_id&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt; in&lt;/span&gt;&lt;span&gt; task_jobs&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;        raise&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt; HTTPException&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;404&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; detail&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-5&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;Job ID not found&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-9&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt; task_jobs&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-14 z-l-i z-d-10 z-d-i&quot;&gt;job_id&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;demo-checking-task-status&quot;&gt;Demo: Checking Task Status&lt;/h2&gt;
&lt;p&gt;Trigger a job:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;curl&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; POST&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; localhost:8000/tasks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-8 z-l-i z-d-4 z-d-i&quot;&gt; {&amp;quot;message&amp;quot;:&amp;quot;task started&amp;quot;, &amp;quot;job_id&amp;quot;:&amp;quot;c32cfe25-7b9c-4e87-83ec-80e7d6dcbf9e&amp;quot;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now check its progress:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-l-i z-d-2 z-d-i&quot;&gt;curl&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;localhost:8000/tasks/status?job_id=c32cfe25-7b9c-4e87-83ec-80e7d6dcbf9e&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sample response:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;processing&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;progress&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 42.7&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; null&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; null&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;ended_at&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; null&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When it finishes:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;completed&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;progress&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 100&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 999999&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; null&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-2&quot;&gt;ended_at&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; 1727783087.12345&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now your background task is no longer mute—it talks back through the global dictionary.&lt;/p&gt;
&lt;p&gt;The route called initiate_task is a post route, that generate a unique id on each call to be able to track each task individually, the default task structure is registered in the task database (dict in our case), an inner function is defined within the route, this is important because this function would be passed into the background task as an argument to the function (task) we are tying to process in the background, now it is up to you to call the update_progress functions at different points in your background task function to update the progress of the function execution, This way, the background functions talks back through the global data structure (dict)on it current state.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;“That’s it. Your FastAPI now moonlights as a background worker. Tell your boss you just saved $10K in server costs—or buy yourself snacks. You earned it.”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;potential-issues&quot;&gt;Potential Issues&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;⚠️ &lt;em&gt;Heads up:&lt;/em&gt; &lt;code&gt;BackgroundTasks.add_task&lt;/code&gt; expects a &lt;strong&gt;sync function&lt;/strong&gt;. If you pass it an &lt;code&gt;async def&lt;/code&gt;, it won’t be awaited properly, which means FastAPI will just launch it and hope for the best. If you need &lt;code&gt;async&lt;/code&gt;, consider using a task queue like Celery, RQ, or a custom loop via &lt;code&gt;asyncio.create_task&lt;/code&gt; (outside of &lt;code&gt;BackgroundTasks&lt;/code&gt;).&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And just like that, your humble FastAPI app can now juggle tasks in the background &lt;em&gt;and&lt;/em&gt; whisper back progress updates like a responsible magician. You didn’t need Celery. You didn’t need Redis. You just needed a dict, a UUID, and a stubborn refusal to give up.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;Pro tip: Don’t forget to eventually replace that dict with Redis or some other reliable data store, or something else persistent, if you go to production. RAM amnesia is real.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
</content>
        
    </entry>
</feed>
