<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Tech&#39;n&#39;Take</title>
<link>https://tech.ycwu.space/</link>
<atom:link href="https://tech.ycwu.space/index.xml" rel="self" type="application/rss+xml"/>
<description>This is Jerry Wu&#39;s Tech&#39;n&#39;take.</description>
<generator>quarto-1.10.18</generator>
<lastBuildDate>Mon, 17 Aug 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Refreshing a Cache with a Python Background Thread</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/refresh-cache-thread/20260817.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/refresh-cache-thread/refresh_cache_thread.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Python cache refresh and image selection logs."></p>
</figure>
</div>
<p>Today I learned that a Python thread can be used to periodically refresh a cache in a long-running worker.</p>
<p>For example, imagine a worker that needs to select an image every second from a source that isn’t updated frequently. Instead of querying the source every time, the main thread can use a local cache for fast access, while a background thread periodically refreshes that cache.</p>
<p>Here’s a self-contained example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> logging</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> random</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> threading</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb1-5"></span>
<span id="cb1-6">IMAGE_REFRESH_INTERVAL <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2 seconds</span></span>
<span id="cb1-7"></span>
<span id="cb1-8">logger <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> logging.getLogger(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span>)</span>
<span id="cb1-9"></span>
<span id="cb1-10"></span>
<span id="cb1-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> refresh_cache(images):</span>
<span id="cb1-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Periodically refresh the shared image cache."""</span></span>
<span id="cb1-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>:</span>
<span id="cb1-14">        time.sleep(IMAGE_REFRESH_INTERVAL)</span>
<span id="cb1-15"></span>
<span id="cb1-16">        new_images <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> load_images()</span>
<span id="cb1-17"></span>
<span id="cb1-18">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Update the existing list instead of rebinding `images`.</span></span>
<span id="cb1-19">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This keeps the same list object shared with `worker()`.</span></span>
<span id="cb1-20">        images[:] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> new_images</span>
<span id="cb1-21"></span>
<span id="cb1-22">        logger.info(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cache refreshed: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> images"</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(images))</span>
<span id="cb1-23"></span>
<span id="cb1-24"></span>
<span id="cb1-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> load_images():</span>
<span id="cb1-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Simulate loading images from an external source."""</span></span>
<span id="cb1-27">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> random.choice((<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)):</span>
<span id="cb1-28">        images <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"image1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"image2"</span>]</span>
<span id="cb1-29">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb1-30">        images <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"image1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"image3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"image4"</span>]</span>
<span id="cb1-31"></span>
<span id="cb1-32">    logger.info(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Loaded images: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, images)</span>
<span id="cb1-33"></span>
<span id="cb1-34">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> images</span>
<span id="cb1-35"></span>
<span id="cb1-36"></span>
<span id="cb1-37"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> draw(images):</span>
<span id="cb1-38">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Simulate selecting an image from the current cache."""</span></span>
<span id="cb1-39">    image <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> random.choice(images)</span>
<span id="cb1-40"></span>
<span id="cb1-41">    logger.info(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Selected image: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, image)</span>
<span id="cb1-42"></span>
<span id="cb1-43"></span>
<span id="cb1-44"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> worker():</span>
<span id="cb1-45">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the initial cache before starting the refresh thread.</span></span>
<span id="cb1-46">    images <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> load_images()</span>
<span id="cb1-47"></span>
<span id="cb1-48">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The background thread periodically updates the same list object.</span></span>
<span id="cb1-49">    threading.Thread(</span>
<span id="cb1-50">        target<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>refresh_cache,</span>
<span id="cb1-51">        args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(images,),</span>
<span id="cb1-52">        daemon<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>,</span>
<span id="cb1-53">    ).start()</span>
<span id="cb1-54"></span>
<span id="cb1-55">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Continue using the shared cache while it is refreshed in the background.</span></span>
<span id="cb1-56">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>:</span>
<span id="cb1-57">        draw(images)</span>
<span id="cb1-58">        time.sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-59"></span>
<span id="cb1-60"></span>
<span id="cb1-61"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb1-62">    logging.basicConfig(</span>
<span id="cb1-63">        level<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>logging.INFO,</span>
<span id="cb1-64">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%(asctime)s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%(levelname)s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">] </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%(message)s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb1-65">    )</span>
<span id="cb1-66"></span>
<span id="cb1-67">    worker()</span></code></pre></div></div>
<p>The key part is:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">images[:] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> new_images</span></code></pre></div></div>
<p>This replaces the contents of the existing list rather than assigning a new list to the local <code>images</code> variable. Therefore, the main thread and the background thread continue to reference the same list object.</p>
<p>The <code>daemon=True</code> setting also makes the refresh thread a background thread that won’t prevent the Python process from exiting when the main thread finishes.</p>
<p>One thing to keep in mind is thread safety. If the operations performed by multiple threads become more complex—for example, if multiple threads need to modify the cache or several pieces of shared state must be updated together—a <code>threading.Lock</code> may be necessary.</p>
<p>In this example, the main thread only reads the cache to select a random image, while the refresh thread replaces its contents periodically, so the simple approach is sufficient for this demonstration.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>python</category>
  <guid>https://tech.ycwu.space/posts/refresh-cache-thread/20260817.html</guid>
  <pubDate>Mon, 17 Aug 2026 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/refresh-cache-thread/refresh_cache_thread.png" medium="image" type="image/png" height="86" width="144"/>
</item>
<item>
  <title>Rust Notes #9: An Interesting Finding While Exploring iter::repeat_n()</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/rust-notes-9/20260724.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/rust-notes-9/rust_notes_9.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Rust Notes 9"></p>
</figure>
</div>
<p>I found an interesting detail while reading the documentation of <a href="https://doc.rust-lang.org/stable/std/iter/fn.repeat_n.html">iter::repeat_n()</a>.</p>
<p>Here’s a simplified example from the docs:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::</span>iter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>with_capacity(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> it <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">iter::</span>repeat_n(v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-6"></span>
<span id="cb1-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// The first item is produced by cloning the stored value.</span></span>
<span id="cb1-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Note that Vec::clone() copies elements, not the spare capacity.</span></span>
<span id="cb1-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> cloned <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> it<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>next()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>unwrap()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-10">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(cloned<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>len()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-11">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(cloned<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>capacity()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-12"></span>
<span id="cb1-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// The final item is the original value moved out of the iterator.</span></span>
<span id="cb1-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// This avoids an unnecessary clone.</span></span>
<span id="cb1-15">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> last <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> it<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>next()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>unwrap()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-16">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(last<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>len()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-17">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(last<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>capacity()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-18"></span>
<span id="cb1-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// No more items remain.</span></span>
<span id="cb1-20">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">None</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> it<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>next())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-21"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p><code>iter::repeat_n(v, 2)</code> takes ownership of <code>v</code>. In other words, <code>v</code> is moved into the iterator, and the iterator becomes responsible for producing the repeated values.</p>
<p>For the first <code>next()</code> call, <code>repeat_n()</code> clones the stored value using <code>Vec::clone()</code>. However, the cloned vector does not preserve the original vector’s spare capacity. Since the original vector has a capacity of <code>123</code> but contains no elements, the cloned vector has a capacity of <code>0</code>.</p>
<p>The interesting part happens on the final <code>next()</code> call. Instead of cloning the value one more time, <code>repeat_n()</code> moves the original vector out of the iterator and returns it directly.</p>
<p>This means <code>repeat_n()</code> only needs to perform <code>n - 1</code> clones and can use the original value for the last item. It is a small but elegant optimization made possible by Rust’s ownership system.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>rust</category>
  <guid>https://tech.ycwu.space/posts/rust-notes-9/20260724.html</guid>
  <pubDate>Fri, 24 Jul 2026 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/rust-notes-9/rust_notes_9.png" medium="image" type="image/png" height="95" width="144"/>
</item>
<item>
  <title>Rust Notes #8: Exploring Iterator Adaptors with step_by() and zip()</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/rust-notes-8/20260721.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/rust-notes-8/rust_notes_8.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Rust Notes 8"></p>
</figure>
</div>
<p>Today, while reading the documentation for the <a href="https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html"><code>Iterator</code></a> trait, I learned more about how iterator adaptors can be combined to create custom iteration patterns.</p>
<p>The standard <a href="https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.enumerate">enumerate()</a> adaptor is a convenient way to add an index to each item in an iterator. However, Rust’s iterator ecosystem provides many other adaptors that can be combined together to build more flexible iteration behaviors.</p>
<p>For example, by combining adaptors such as <a href="https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.step_by">step_by()</a> and <a href="https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.zip">zip()</a>, we can create an iteration pattern that generates values with a specific start value, end value, and step size, then combines them with another iterator.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> zipper<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">..</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>step_by(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>zip(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"foobar"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-3">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{zipper:?}"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// [(1, 'f'), (4, 'o'), (7, 'o')]</span></span>
<span id="cb1-4"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>The iterator chain works as follows:</p>
<ul>
<li><code>(1..10)</code> creates a range: <code>1, 2, 3, 4, ... 9</code></li>
<li><code>.step_by(3)</code> keeps every third value: <code>1, 4, 7</code></li>
<li><code>.zip("foobar".chars())</code> combines the numbers with the characters</li>
<li><code>.collect()</code> consumes the iterator and collects the final result: <code>[(1, 'f'), (4, 'o'), (7, 'o')]</code></li>
</ul>
<p>For learning purposes, the same code can be expanded into smaller steps:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> expanded_version() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> (start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> end<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> step) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-3"></span>
<span id="cb2-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> numbers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">..</span>end)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>step_by(step)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> letters <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"foobar"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-6"></span>
<span id="cb2-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> zipper<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numbers<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>zip(letters)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-8"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>rust</category>
  <guid>https://tech.ycwu.space/posts/rust-notes-8/20260721.html</guid>
  <pubDate>Tue, 21 Jul 2026 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/rust-notes-8/rust_notes_8.png" medium="image" type="image/png" height="49" width="144"/>
</item>
<item>
  <title>Rust Notes #6: Understanding FromStr and TryFrom</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/rust-notes-6/20260718.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/rust-notes-6/rust_notes_6.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Rust Notes 6"></p>
</figure>
</div>
<p>Today I learned about the <code>FromStr</code> and <code>TryFrom</code> traits. At first glance, they seem very similar, but they represent different kinds of conversions.</p>
<p>According to the standard library documentation, <a href="https://doc.rust-lang.org/std/str/trait.FromStr.html"><code>FromStr</code></a> is used to <strong>parse a value from a string</strong>, while <a href="https://doc.rust-lang.org/std/convert/trait.TryFrom.html"><code>TryFrom</code></a> is for <strong>fallible type conversions</strong>—converting one type into another where the conversion may fail in a controlled way.</p>
<p>Another interesting point is that the convenient <a href="https://doc.rust-lang.org/std/primitive.str.html#method.parse">str::parse()</a> method is implemented through the <code>FromStr</code> trait.</p>
<p>Here’s a simple example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::convert::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">TryFrom</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">str</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">FromStr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>derive<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Debug</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// A validated port number instead of a raw u16.</span></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> Port(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u16</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>derive<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Debug</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb1-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">enum</span> PortError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-10">    NotNumber<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-11">    InvalidPort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">TryFrom</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u16</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> Port <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-15">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">type</span> Error <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PortError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-16"></span>
<span id="cb1-17">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> try_from(value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u16</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-18">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Validate domain-specific rules for a valid port.</span></span>
<span id="cb1-19">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// For example, port 0 is reserved and not accepted here.</span></span>
<span id="cb1-20">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-21">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Err</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">PortError::</span>InvalidPort)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-22">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-23"></span>
<span id="cb1-24">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span>(value))</span>
<span id="cb1-25">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-26"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-27"></span>
<span id="cb1-28"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">FromStr</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> Port <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-29">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">type</span> Err <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PortError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-30"></span>
<span id="cb1-31">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> from_str(s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Err</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-32">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// First parse the string representation into a numeric value.</span></span>
<span id="cb1-33">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u16</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>parse()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map_err(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">PortError::</span>NotNumber)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span></span>
<span id="cb1-34"></span>
<span id="cb1-35">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Delegate domain validation and construction to `TryFrom`.</span></span>
<span id="cb1-36">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>try_from(value)</span>
<span id="cb1-37">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-38"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-39"></span>
<span id="cb1-40"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> PortError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-41">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Parse a text representation into a validated `Port`.</span></span>
<span id="cb1-42">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> port1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Port <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"8000"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>parse()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span></span>
<span id="cb1-43"></span>
<span id="cb1-44">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Convert an existing numeric value into a validated `Port`.</span></span>
<span id="cb1-45">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> port2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Port::</span>try_from(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8000u16</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span></span>
<span id="cb1-46"></span>
<span id="cb1-47">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{:?}, {:?}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> port1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> port2)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Port(8000), Port(8000)</span></span>
<span id="cb1-48"></span>
<span id="cb1-49">    <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(())</span>
<span id="cb1-50"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>In this example, the call to <code>parse()</code> in <code>main()</code> automatically uses the <code>FromStr</code> implementation for <code>Port</code>. Rather than implementing all the conversion logic itself, <code>from_str()</code> acts as a composition layer: it first parses the text representation into an intermediate value (<code>u16</code>), then delegates domain validation and construction to <code>TryFrom&lt;u16&gt;</code>.</p>
<p>This allows users to construct a validated <code>Port</code> from either text input (<code>"8000".parse()</code>) or an existing numeric value (<code>Port::try_from(8000u16)</code>), while keeping the validation logic centralized in one place.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>rust</category>
  <guid>https://tech.ycwu.space/posts/rust-notes-6/20260718.html</guid>
  <pubDate>Sat, 18 Jul 2026 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/rust-notes-6/rust_notes_6.png" medium="image" type="image/png" height="41" width="144"/>
</item>
<item>
  <title>Rust Notes #7: Understanding thiserror Attributes</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/rust-notes-7/20260718.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/rust-notes-7/rust_notes_7.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Rust Notes 7"></p>
</figure>
</div>
<p><a href="https://docs.rs/thiserror/latest/thiserror/"><code>thiserror</code></a> provides a convenient way to remove repetitive boilerplate when implementing custom error types.</p>
<p>Under the hood, <code>thiserror</code> mainly generates implementations related to three standard Rust traits:</p>
<ul>
<li><code>Display</code></li>
<li><code>Error</code></li>
<li><code>From</code></li>
</ul>
<p>The most commonly used attributes are:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Attribute</th>
<th style="text-align: left;">Purpose</th>
<th style="text-align: left;">Rough equivalent</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>#[derive(Error)]</code></td>
<td style="text-align: left;">Implements <code>std::error::Error</code></td>
<td style="text-align: left;"><code>impl Error for MyError</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>#[error("...")]</code></td>
<td style="text-align: left;">Defines the error message</td>
<td style="text-align: left;"><code>impl Display for MyError</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>#[from]</code></td>
<td style="text-align: left;">Enables automatic error conversion with <code>?</code></td>
<td style="text-align: left;"><code>impl From&lt;T&gt; for MyError</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>#[source]</code></td>
<td style="text-align: left;">Marks the underlying cause of an error</td>
<td style="text-align: left;"><code>Error::source()</code> returning <code>Some(source)</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>#[error(transparent)]</code></td>
<td style="text-align: left;">Wraps an error while preserving its representation</td>
<td style="text-align: left;">Forward <code>Display</code> and delegate representation to the underlying error</td>
</tr>
</tbody>
</table>
<p>For example:</p>
<section id="using-thiserror" class="level2">
<h2 class="anchored" data-anchor-id="using-thiserror">Using <code>thiserror</code></h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::</span>io<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::num::</span>ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">thiserror::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>derive<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Debug</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">enum</span> AppError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// #[from] provides From&lt;ParseIntError&gt; and establishes the source relationship.</span></span>
<span id="cb1-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>error<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"invalid port number"</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb1-9">    NotNumber(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>from<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span> ParseIntError)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-10"></span>
<span id="cb1-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>error<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"port {0} is not allowed"</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb1-12">    InvalidPort(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u16</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-13"></span>
<span id="cb1-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// #[source] explicitly defines io::Error as the source.</span></span>
<span id="cb1-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>error<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"failed to load configuration"</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb1-16">    Config <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-17">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>source<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb1-18">        source<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">io::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-19">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">},</span></span>
<span id="cb1-20"></span>
<span id="cb1-21">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// #[error(transparent)] delegates representation to io::Error.</span></span>
<span id="cb1-22">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// #[from] provides From&lt;io::Error&gt; and establishes the source relationship.</span></span>
<span id="cb1-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>error<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span>transparent<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb1-24">    Io(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>from<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">io::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-25"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="roughly-equivalent-handwritten-implementation" class="level2">
<h2 class="anchored" data-anchor-id="roughly-equivalent-handwritten-implementation">Roughly equivalent handwritten implementation</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::error::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::</span>fmt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::</span>io<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::num::</span>ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>derive<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Debug</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb2-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">enum</span> AppError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-8">    NotNumber(ParseIntError)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-9">    InvalidPort(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u16</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-10">    Config <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> source<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">io::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">},</span></span>
<span id="cb2-11">    Io(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">io::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-13"></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Equivalent to #[error("...")]</span></span>
<span id="cb2-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">fmt::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Display</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> AppError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> fmt(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">fmt::</span>Formatter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'_</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">fmt::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-18">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>NotNumber(_) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-19">                <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">write!</span>(f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"invalid port number"</span>)</span>
<span id="cb2-20">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-21"></span>
<span id="cb2-22">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>InvalidPort(port) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-23">                <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">write!</span>(f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"port {} is not allowed"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> port)</span>
<span id="cb2-24">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-25"></span>
<span id="cb2-26">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>Config <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">..</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-27">                <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">write!</span>(f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"failed to load configuration"</span>)</span>
<span id="cb2-28">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-29"></span>
<span id="cb2-30">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Equivalent to #[error(transparent)]</span></span>
<span id="cb2-31">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>Io(err) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> err<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>fmt(f)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-32">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-33">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-34"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-35"></span>
<span id="cb2-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Equivalent to the Error implementation generated by:</span></span>
<span id="cb2-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// #[derive(Error)] together with #[source] and #[from].</span></span>
<span id="cb2-38"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> AppError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-39">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> source(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Option</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&amp;</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">dyn</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'static</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-40">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-41">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// #[from] implies #[source]</span></span>
<span id="cb2-42">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>NotNumber(err) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Some</span>(err)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-43"></span>
<span id="cb2-44">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>InvalidPort(_) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">None</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-45"></span>
<span id="cb2-46">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Explicit #[source]</span></span>
<span id="cb2-47">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>Config <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> source <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Some</span>(source)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-48"></span>
<span id="cb2-49">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// #[from] implies #[source]</span></span>
<span id="cb2-50">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// #[error(transparent)] delegates representation to the inner error.</span></span>
<span id="cb2-51">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>Io(err) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Some</span>(err)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-52">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-53">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-54"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-55"></span>
<span id="cb2-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Generated by #[from] on the ParseIntError variant.</span></span>
<span id="cb2-57"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">From</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> AppError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-58">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> from(err<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> ParseIntError) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-59">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>NotNumber(err)</span>
<span id="cb2-60">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-61"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-62"></span>
<span id="cb2-63"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Generated by #[from] on the io::Error variant.</span></span>
<span id="cb2-64"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">From</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">io::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> AppError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-65">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> from(err<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">io::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-66">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>Io(err)</span>
<span id="cb2-67">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-68"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>

 ]]></description>
  <category>rust</category>
  <guid>https://tech.ycwu.space/posts/rust-notes-7/20260718.html</guid>
  <pubDate>Sat, 18 Jul 2026 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/rust-notes-7/rust_notes_7.png" medium="image" type="image/png" height="127" width="144"/>
</item>
<item>
  <title>Rust Notes #5: Using Constructors as Iterator Adapters</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/rust-notes-5/20260706.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/rust-notes-5/rust_notes_5.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Rust Notes 5"></p>
</figure>
</div>
<p>When using the <code>map()</code> iterator adaptor, I usually expect to pass in a function or closure. I was already aware that we can pass a constructor such as <code>String::from()</code> directly, as shown in <a href="https://tech.ycwu.space/posts/rust-notes-2/20260509.html">Rust Notes #2</a>.</p>
<p>However, I recently discovered something interesting: when a function-like constructor takes a single argument—such as an enum variant or a tuple struct—it can also be passed directly to <code>map()</code>.</p>
<p>For example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> Single(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> v1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">..</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Some</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// [Some(1)]</span></span>
<span id="cb1-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> v2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">..</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(Single)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// [Single(1)]</span></span>
<span id="cb1-6"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>In both cases, <code>Some</code> and <code>Single</code> act as constructor functions:</p>
<ul>
<li><code>Some</code> has the type <code>i32 -&gt; Option&lt;i32&gt;</code></li>
<li><code>Single</code> has the type <code>i32 -&gt; Single</code></li>
</ul>
<p>Because <code>map()</code> expects a function of the form <code>FnMut(Item) -&gt; B</code>, these constructors can be used directly without writing an explicit closure like <code>|x| Some(x)</code> or <code>|x| Single(x)</code>.</p>
<p>This makes the code more concise when the transformation is a simple wrapping operation.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>rust</category>
  <guid>https://tech.ycwu.space/posts/rust-notes-5/20260706.html</guid>
  <pubDate>Mon, 06 Jul 2026 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/rust-notes-5/rust_notes_5.png" medium="image" type="image/png" height="26" width="144"/>
</item>
<item>
  <title>Rust Notes #4: Inherit-ish</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/rust-notes-4/20260531.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/rust-notes-4/rust_notes_4.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Rust Notes 4"></p>
</figure>
</div>
<p>After watching Carl Kadie’s excellent talk <a href="https://www.youtube.com/watch?v=3IyKC5EtNkM">Nine Ways to do Inheritance in Rust, a Language without Inheritance</a>, I decided to write down some notes from his work.</p>
<p>To be clear, all of the ideas and code presented here come from Carl Kadie. You can find the original material in the accompanying <a href="https://github.com/CarlKCarlK/inherit">GitHub repository</a>. My role here is simply that of a note-taker, with added commentary to help my future self (and perhaps others) revisit these concepts more easily.</p>
<section id="puzzle-1-trait-default-methods" class="level2">
<h2 class="anchored" data-anchor-id="puzzle-1-trait-default-methods">Puzzle 1: Trait Default Methods</h2>
<p><code>RangeSetBlaze</code> works with sets of integers such as <code>u8</code>, <code>i16</code>, and other integer types. Each integer type must provide fundamental operations like <code>min_value()</code> and <code>max_value()</code>. At the same time, we want all integer types to share additional behavior, such as an <code>exhausted_range()</code> method.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">classDiagram
    direction TB

    class Integer {
        &lt;&lt;abstract class&gt;&gt;
        +min_value() Self // required
        +max_value() Self // required
        +exhausted_range() RangeInclusive~Self~  // code
    }

    class u8 {
        &lt;&lt;concrete class&gt;&gt;
        +min_value() Self
        +max_value() Self
        +exhausted_range() RangeInclusive~Self~  // inherited
    }

    class i16 {
        &lt;&lt;concrete class&gt;&gt;
        +min_value() Self
        +max_value() Self
        +exhausted_range() RangeInclusive~Self~  // inherited
    }

    Integer &lt;|-- u8 : is-a
    Integer &lt;|-- i16 : is-a
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<p>In Rust, traits are often thought of as contracts: they define a set of required methods that implementors must provide. In that sense, a trait specifies <em>what</em> functionality a type must have.</p>
<p>However, traits can do more than define interfaces. They can also provide concrete method implementations. When a trait includes such methods, all implementors automatically gain access to them unless they choose to override the default behavior. This gives us something conceptually similar to inheritance: shared behavior defined once and reused across multiple types.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Trait Default Methods — Code
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::ops::</span>RangeInclusive<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// TECHNIQUE NAME: Trait Default Methods.</span></span>
<span id="cb1-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">trait</span> Integer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Copy</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Ord</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> min_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> max_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-7"></span>
<span id="cb1-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Default behavior inherited by implementors.</span></span>
<span id="cb1-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Any impl can override this method.</span></span>
<span id="cb1-10"></span>
<span id="cb1-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/// Returns an exhausted (empty) range`.</span></span>
<span id="cb1-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> exhausted_range() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> RangeInclusive<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-13">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">debug_assert!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>min_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>max_value()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Precondition"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-14">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>max_value()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">..=</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>min_value()</span>
<span id="cb1-15">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-16"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-17"></span>
<span id="cb1-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u8</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-19">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> min_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-20">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u8</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">MIN</span></span>
<span id="cb1-21">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-22"></span>
<span id="cb1-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> max_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-24">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u8</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">MAX</span></span>
<span id="cb1-25">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-26"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-27"></span>
<span id="cb1-28"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i16</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-29">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> min_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-30">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i16</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">MIN</span></span>
<span id="cb1-31">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-32"></span>
<span id="cb1-33">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> max_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-34">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i16</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">MAX</span></span>
<span id="cb1-35">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-36"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-37"></span>
<span id="cb1-38"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-39">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> r1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u8</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>exhausted_range()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-40">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> r2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i16</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>exhausted_range()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-41"></span>
<span id="cb1-42">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(r1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">..=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-43">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert!</span>(r2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>is_empty())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-44"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-45"></span>
<span id="cb1-46"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// TECHNIQUE NAME (again): Trait Default Methods.</span></span></code></pre></div></div>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>The key idea is that <code>u8</code> and <code>i16</code> only need to implement the required methods, <code>min_value()</code> and <code>max_value()</code>. The <code>exhausted_range()</code> method is defined once in the trait and automatically shared by all implementors.</p>
</blockquote>
</section>
<section id="puzzle-2-supertrait" class="level2">
<h2 class="anchored" data-anchor-id="puzzle-2-supertrait">Puzzle 2: Supertrait</h2>
<p>A servo is an electric motor that can move to a specified angle. We want a <code>ServoEsp</code> (our type that controls a servo on an ESP32 microcontroller) to work with any code that needs a servo. A <code>ServoPlayerEsp</code> is similar, but with animation ability. (Inspired by <code>device-envoy</code>.)</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">classDiagram
    direction TB

    class Servo {
        &lt;&lt;abstract class&gt;&gt;
        +set_degrees(degrees)
    }

    class ServoPlayer {
        &lt;&lt;abstract class&gt;&gt;
        +set_degrees(degrees) // from Servo
        +animate(steps)
    }

    class ServoEsp {
        &lt;&lt;concrete class&gt;&gt;
        +set_degrees(degrees)
    }

    class ServoPlayerEsp {
        &lt;&lt;concrete class&gt;&gt;
        +set_degrees(degrees)
        +animate(steps)
    }

    Servo &lt;|-- ServoPlayer : is-a
    Servo &lt;|-- ServoEsp : is-a
    ServoPlayer &lt;|-- ServoPlayerEsp : is-a
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<p>In Rust, a trait can depend on another trait. The syntax <code>trait B: A</code> means that <code>B</code> requires <code>A</code> as a prerequisite. In other words, any type that implements <code>B</code> must also implement <code>A</code>.</p>
<p>This is similar to saying that <code>B</code> extends <code>A</code>: anything that works with <code>A</code> will also work with <code>B</code>, but <code>B</code> provides additional functionality on top.</p>
<p>In traditional object-oriented terms, this feels like an “is-a” relationship where <code>B</code> can do everything <code>A</code> can do, plus more, while still being treated as an <code>A</code> when needed.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Supertrait — Code
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::</span>thread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::time::</span>Duration<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// `Servo` is an abstract class (a trait), not a concrete driver.</span></span>
<span id="cb2-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">trait</span> Servo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> set_degrees(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> degrees<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u16</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// `ServoPlayer` is also abstract. It extends `Servo` (supertrait), so any</span></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// `ServoPlayer` can do everything in `Servo` plus animation.</span></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// TECHNIQUE NAME: Supertraits</span></span>
<span id="cb2-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">trait</span> ServoPlayer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Servo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// (degrees, milliseconds to hold at that angle)</span></span>
<span id="cb2-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> animate(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> steps<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u16</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u64</span>)])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-15"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-16"></span>
<span id="cb2-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>derive<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Default</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb2-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Concrete servo driver (similar naming to the real example): `ServoEsp`.</span></span>
<span id="cb2-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> ServoEsp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-20"></span>
<span id="cb2-21"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Servo <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ServoEsp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-22">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> set_degrees(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> degrees<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u16</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-23">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[ServoEsp] set angle -&gt; {degrees}°"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-24">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-25"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-26"></span>
<span id="cb2-27"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>derive<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Default</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb2-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Concrete servo player driver that can animate.</span></span>
<span id="cb2-29"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> ServoPlayerEsp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-30"></span>
<span id="cb2-31"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Servo <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ServoPlayerEsp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-32">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> set_degrees(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> degrees<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u16</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-33">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[ServoPlayerEsp] set angle -&gt; {degrees}°"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-34">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-35"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-36"></span>
<span id="cb2-37"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> ServoPlayer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ServoPlayerEsp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-38">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> animate(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> steps<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u16</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u64</span>)]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-39">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (degrees<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ms) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> steps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-40">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>set_degrees(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>degrees)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-41">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[ServoPlayerEsp] hold for {ms}ms"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-42">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">thread::</span>sleep(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Duration::</span>from_millis(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>ms))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-43">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-44">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-45"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-46"></span>
<span id="cb2-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Generic program that only needs a `Servo`.</span></span>
<span id="cb2-48"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> center_servo(servo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Servo) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-49">    servo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>set_degrees(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-50"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-51"></span>
<span id="cb2-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Generic program that needs a `ServoPlayer`.</span></span>
<span id="cb2-53"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> run_wave(player<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> ServoPlayer) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-54">    player<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>animate(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[</span>
<span id="cb2-55">        (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-56">        (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-57">        (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-58">        (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">135</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-59">        (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-60">        (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">135</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-61">        (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-62">        (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-63">        (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-64">    ])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-65"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-66"></span>
<span id="cb2-67"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-68">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> servo_esp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ServoEsp::</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">default</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-69">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> servo_player_esp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ServoPlayerEsp::</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">default</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-70"></span>
<span id="cb2-71">    center_servo(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>servo_esp)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-72">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// `ServoPlayer` can do everything `Servo` can!</span></span>
<span id="cb2-73">    center_servo(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>servo_player_esp)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-74">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// and more.</span></span>
<span id="cb2-75">    run_wave(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>servo_player_esp)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-76"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>The key idea is that <code>ServoPlayer</code> builds on top of <code>Servo</code> rather than replacing it. Any type that implements <code>ServoPlayer</code> must also implement <code>Servo</code>, allowing it to be used wherever a <code>Servo</code> is expected while providing additional capabilities. Supertraits therefore offer a trait-based way to express “is-a” relationships and extend behavior incrementally.</p>
</blockquote>
</section>
<section id="puzzle-3-extension-traits" class="level2">
<h2 class="anchored" data-anchor-id="puzzle-3-extension-traits">Puzzle 3: Extension Traits</h2>
<p>We want to add a new method <code>is_odd()</code> to an existing concrete type <code>usize</code>, that type is defined outside our crate (“foreign”).</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">classDiagram
    direction TB

    class UsizeExtensions {
        &lt;&lt;abstract class&gt;&gt;
        +is_odd() bool
    }

    class usize {
        &lt;&lt;concrete class&gt;&gt;
        +is_odd() bool // inherited
    }

    UsizeExtensions &lt;|-- usize : is-a
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<p>For types that we define ourselves, such as structs and enums, we can add methods through an <code>impl</code> block. However, Rust does not allow us to write an inherent <code>impl</code> for a type defined in another crate, including primitive types like <code>usize</code>.</p>
<p>This restriction prevents different crates from attaching conflicting methods to the same type, which would make method resolution ambiguous.</p>
<p>Fortunately, traits provide a workaround. We can define our own trait, implement it for the foreign type, and thereby extend the type with new methods. From the caller’s perspective, these methods behave much like native methods on the type itself.</p>
<p>This pattern is known as <strong>extension traits</strong>.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Extension Traits — Code
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// impl usize {</span></span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//     fn is_odd(self) -&gt; bool {</span></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//         self &amp; 1 != 0</span></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//     }</span></span>
<span id="cb3-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// }</span></span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// error[E0390]: cannot define inherent `impl` for primitive types</span></span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">trait</span> UsizeExtensions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> is_odd(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb3-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> UsizeExtensions <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">usize</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> is_odd(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-14">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb3-15">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-16"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-17"></span>
<span id="cb3-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// TECHNIQUE NAME: extension traits.</span></span>
<span id="cb3-19"></span>
<span id="cb3-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> count<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">usize</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb3-22"></span>
<span id="cb3-23">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert!</span>(count<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>is_odd())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb3-24">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert!</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>is_odd())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb3-25"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>The key idea is that <code>usize</code> “inherits” the <code>is_odd()</code> method from <code>UsizeExtensions</code>. Although we cannot add methods directly to the foreign type, implementing an extension trait gives us a very similar result from the caller’s perspective.</p>
</blockquote>
</section>
<section id="puzzle-4-derive-generated-implementations" class="level2">
<h2 class="anchored" data-anchor-id="puzzle-4-derive-generated-implementations">Puzzle 4: Derive-Generated Implementations</h2>
<p>We want a small <code>LedLevel</code> enum type with two values (<code>On</code>, <code>Off</code>) that automatically participates in common behaviors (default value, debugging output, equality/order comparisons, hashing, copy/clone).</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">classDiagram
    direction TB

    class LedLevel {
        &lt;&lt;concrete class&gt;&gt;
        On
        Off
    }

    class Defaultable {
        &lt;&lt;abstract class&gt;&gt;
        +default()
    }

    class Debuggable {
        &lt;&lt;abstract class&gt;&gt;
        +debug_string()
    }

    class EquatableOrdered {
        &lt;&lt;abstract class&gt;&gt;
        +equals(other)
        +compare(other)
    }

    class Hashable {
        &lt;&lt;abstract class&gt;&gt;
        +hash()
    }

    class CopyableCloneable {
        &lt;&lt;abstract class&gt;&gt;
        +copy()
        +clone()
    }

    Defaultable &lt;|-- LedLevel : is-a
    Debuggable &lt;|-- LedLevel : is-a
    EquatableOrdered &lt;|-- LedLevel : is-a
    Hashable &lt;|-- LedLevel : is-a
    CopyableCloneable &lt;|-- LedLevel : is-a
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<p>In Rust, the <code>derive</code> macro is a powerful convenience feature. It allows us to automatically generate implementations for many common traits, saving us from writing repetitive boilerplate code. These derived traits often provide essential functionality such as formatting, comparison, hashing, and cloning.</p>
<p>Beyond the standard library, many third-party crates also extend this idea. Libraries like <code>serde</code>, for example, provide <code>derive</code> support for serialization and deserialization traits, making it easy to integrate types with external systems.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Derive-Generated Implementations — Code
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-4" class="callout-4-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// TECHNIQUE NAME: derive-generated implementation</span></span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>derive<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Clone</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Copy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Debug</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Eq</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Hash</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Ord</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">PartialEq</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">PartialOrd</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Default</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb4-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">enum</span> LedLevel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb4-5">    On<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">default</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb4-7">    Off<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-8"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb4-9"></span>
<span id="cb4-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb4-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> default_level <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">LedLevel::</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">default</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> on <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">LedLevel::</span>On<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> off <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">LedLevel::</span>Off<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-14"></span>
<span id="cb4-15">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(default_level<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">LedLevel::</span>Off)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-16">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_ne!</span>(on<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> off)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-17">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert!</span>(off <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> on)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-18"></span>
<span id="cb4-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// `Copy` + `Clone` come from derive too.</span></span>
<span id="cb4-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> copied <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> on<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> cloned <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> off<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>clone()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-22">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(copied<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> on)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-23">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(cloned<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> off)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-24"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>The key idea is that a tiny enum like <code>LedLevel</code> can immediately behave like a fully featured type—supporting comparison, copying, hashing, and defaults—simply by attaching <code>#[derive(...)]</code>, without writing any manual implementation code.</p>
</blockquote>
</section>
<section id="puzzle-5-deref-method-lookup" class="level2">
<h2 class="anchored" data-anchor-id="puzzle-5-deref-method-lookup">Puzzle 5: Deref Method Lookup</h2>
<p>We want an <code>HtmlBuffer</code> to feel like a string for everyday method calls, while still being its own distinct type. Note that <code>String</code> is a concrete type with storage, not just an interface/trait/abstract class.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">classDiagram
    direction TB

    class String {
        &lt;&lt;concrete class&gt;&gt;
        -bytes
        +push_str()
        +len()
        +as_bytes()
    }

    class HtmlBuffer {
        &lt;&lt;concrete class&gt;&gt;
        +new()
        +push_str() // inherited
        +len() // inherited
        +as_bytes() // inherited
    }

    String &lt;|-- HtmlBuffer : is-a
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<p>The <code>Deref</code> trait allows a type to specify what it should dereference into. This enables a powerful feature in Rust: <strong>method lookup through deref coercion</strong>.</p>
<p>When a method is not found on the original type, Rust will automatically try to dereference the value and search again on the target type. This process can repeat multiple times, meaning deref behavior can be <strong>chained across multiple wrapper types</strong>.</p>
<p>For example, a value like <code>Box&lt;String&gt;</code> does not implement <code>contains()</code> directly. However, Rust will first dereference the <code>Box&lt;String&gt;</code> into <code>String</code>, and then continue method lookup on <code>String</code>. Since <code>String</code> itself derefs into <code>str</code>, the call can continue one more step until <code>contains()</code> is found. As a result, a method call like the following works even though <code>contains</code> is defined on <code>str</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb5-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Box</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>new(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello world"</span>))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb5-3">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert!</span>(s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>contains(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"world"</span>))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb5-4"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-5-contents" aria-controls="callout-5" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Deref Method Lookup — Code
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-5" class="callout-5-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// struct HtmlBuffer;</span></span>
<span id="cb6-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// impl String for HtmlBuffer {</span></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// }</span></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Error because String is not a trait.</span></span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::ops::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Deref</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">DerefMut</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb6-7"></span>
<span id="cb6-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// HtmlBuffer 'wraps' a String</span></span>
<span id="cb6-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> HtmlBuffer(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-10"></span>
<span id="cb6-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> HtmlBuffer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb6-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> new() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb6-13">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>new())</span>
<span id="cb6-14">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb6-15"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb6-16"></span>
<span id="cb6-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Deref</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> HtmlBuffer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb6-18">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">type</span> Target <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-19"></span>
<span id="cb6-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> deref(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>Target <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb6-21">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb6-22">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb6-23"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb6-24"></span>
<span id="cb6-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">DerefMut</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> HtmlBuffer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb6-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> deref_mut(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>Target <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb6-27">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb6-28">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb6-29"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb6-30"></span>
<span id="cb6-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// TECHNIQUE NAME: deref lookup.</span></span>
<span id="cb6-32"></span>
<span id="cb6-33"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb6-34">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> page <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">HtmlBuffer::</span>new()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-35"></span>
<span id="cb6-36">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// `push_str` and `len` are String methods found via deref lookup.</span></span>
<span id="cb6-37">    page<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>push_str(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;h1&gt;Hello&lt;/h1&gt;"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-38">    page<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>push_str(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;p&gt;Rust&lt;/p&gt;"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-39"></span>
<span id="cb6-40">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(page<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>len()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-41">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Borrow the thing `page` points to and compare.</span></span>
<span id="cb6-42">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;*</span>page<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;h1&gt;Hello&lt;/h1&gt;&lt;p&gt;Rust&lt;/p&gt;"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-43"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb6-44"></span>
<span id="cb6-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Good for `Box`, `Rc`, etc., but not really good here.</span></span>
<span id="cb6-46"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Also see AsRef, From, and Borrow traits.</span></span></code></pre></div></div>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>The key idea is that <code>HtmlBuffer</code> <em>feels</em> like a <code>String</code> even though it is a wrapper type. Through <code>Deref</code> and <code>DerefMut</code>, method calls like <code>len()</code> and <code>push_str()</code> are automatically forwarded to the inner <code>String</code>. This makes the wrapper effectively transparent in everyday use, while still preserving its own distinct identity as a separate type.</p>
</blockquote>
</section>
<section id="puzzle-6-blanket-implementations" class="level2">
<h2 class="anchored" data-anchor-id="puzzle-6-blanket-implementations">Puzzle 6: Blanket Implementations</h2>
<p>We want to union any number of sets (<code>RangeSetBlaze&lt;T&gt;</code>). In OO terms, any collection that is an iterable of <code>RangeSetBlaze&lt;T&gt;</code> references should inherit this operation.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-6-contents" aria-controls="callout-6" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Slightly cleaner in my view
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-6" class="callout-6-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>We want to union any number of sets (<code>RangeSetBlaze</code>). In OO terms, any collection that can be iterated as <code>&amp;RangeSetBlaze</code> should inherit this operation.</p>
</div>
</div>
</div>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">classDiagram
    direction TB

    class Iterable {
        &lt;&lt;abstract class&gt;&gt;
        +iterator()
    }

    class RangeSetCollection {
        &lt;&lt;abstract class&gt;&gt;
        +iterator() // from Iterable
        +union()
    }

    class VectorOfRangeSetRefs {
        &lt;&lt;concrete class&gt;&gt;
        +iterator()
        +union() // inherited
    }

    class ArrayOfRangeSetRefs {
        &lt;&lt;concrete class&gt;&gt;
        +iterator()
        +union() // inherited
    }

    class AnyOtherRangeSetCollection {
        &lt;&lt;concrete class&gt;&gt;
        +iterator()
        +union() // inherited
    }

    Iterable &lt;|-- RangeSetCollection : is-a
    RangeSetCollection &lt;|-- VectorOfRangeSetRefs : is-a
    RangeSetCollection &lt;|-- ArrayOfRangeSetRefs : is-a
    RangeSetCollection &lt;|-- AnyOtherRangeSetCollection : is-a
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<p>A blanket implementation allows a trait to be implemented for an entire category of types rather than for a single concrete type. Instead of writing many individual implementations, we can describe a set of requirements and automatically grant the trait to every type that satisfies them.</p>
<p>The requirements are typically expressed through trait bounds, often using a <code>where</code> clause. This lets us precisely control which types receive the implementation while keeping the code concise and reusable.</p>
<p>Like other generic features in Rust, blanket implementations are resolved at compile time. The compiler generates the necessary code for each applicable type, providing flexibility without introducing runtime overhead.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-7-contents" aria-controls="callout-7" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Blanket Implementations — Code
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-7" class="callout-7-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb7-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::collections::</span>BTreeSet<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-2"></span>
<span id="cb7-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// For this example, use u64 as our stand-in integer type.</span></span>
<span id="cb7-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">type</span> Integer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u64</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-5"></span>
<span id="cb7-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Mock up RangeSetBlaze as a BTreeSet wrapper for demo purposes.</span></span>
<span id="cb7-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>derive<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Debug</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Clone</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">PartialEq</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Eq</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb7-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> RangeSetBlaze <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-9">    values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> BTreeSet<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Integer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;,</span></span>
<span id="cb7-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-11"></span>
<span id="cb7-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> RangeSetBlaze <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> new() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-14">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-15">            values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">BTreeSet::</span>new()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-16">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-17">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-18"></span>
<span id="cb7-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Construct from a slice of Integer values.</span></span>
<span id="cb7-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> from_slice(values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[Integer]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-21">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-22">            values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>iter()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>copied()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-23">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-24">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-25"></span>
<span id="cb7-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Return a new set that is the union of two borrowed sets.</span></span>
<span id="cb7-27">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> union(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> other<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-28">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-29">            values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">union</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>other<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>values)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>copied()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-30">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-31">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-32"></span>
<span id="cb7-33">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> is_empty(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-34">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>is_empty()</span>
<span id="cb7-35">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-36"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-37"></span>
<span id="cb7-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// A RangeSetCollection must be iterable over borrowed RangeSetBlaze values.</span></span>
<span id="cb7-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// In return, the trait gives it a union() method over those values.</span></span>
<span id="cb7-40"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">trait</span> RangeSetCollection<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'a</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">IntoIterator</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Item <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'a</span> RangeSetBlaze<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-41">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> union(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> RangeSetBlaze</span>
<span id="cb7-42">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">where</span></span>
<span id="cb7-43">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Sized</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-44">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-45">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">RangeSetBlaze::</span>new()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-46">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> set <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-47">            result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">RangeSetBlaze::</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">union</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> set)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-48">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-49">        result</span>
<span id="cb7-50">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-51"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-52"></span>
<span id="cb7-53"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// TECHNIQUE NAME: blanket implementation</span></span>
<span id="cb7-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span></span>
<span id="cb7-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// This is broader than a normal extension trait impl.</span></span>
<span id="cb7-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Instead of adding union() to one type, we add it to every type</span></span>
<span id="cb7-57"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// that can be turned into an iterator over &amp;RangeSetBlaze.</span></span>
<span id="cb7-58"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'a</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> I<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> RangeSetCollection<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'a</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> I <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">where</span> I<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">IntoIterator</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Item <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'a</span> RangeSetBlaze<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span></span>
<span id="cb7-59"></span>
<span id="cb7-60"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-61">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">RangeSetBlaze::</span>from_slice(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-62">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">RangeSetBlaze::</span>from_slice(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-63">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">RangeSetBlaze::</span>from_slice(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-64"></span>
<span id="cb7-65">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> expected <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">RangeSetBlaze::</span>from_slice(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-66">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// A vector of sets can be unioned together.</span></span>
<span id="cb7-67">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">vec!</span>[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>c]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">union</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> expected)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-68">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// An array of sets can be unioned together.</span></span>
<span id="cb7-69">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>([<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>c]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">union</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> expected)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-70">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// A filtered option can be unioned.</span></span>
<span id="cb7-71">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Some</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>a)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>filter(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>set<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>set<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>is_empty())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">union</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> a)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-72"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-8-contents" aria-controls="callout-8" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span><code>Option&lt;T&gt;</code> implements <code>IntoIterator</code>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-8" class="callout-8-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>One interesting thing I learned from this example is that the final test works because <a href="https://rust.docs.kernel.org/core/option/index.html#iterating-over-option"><code>Option&lt;T&gt;</code> implements <code>IntoIterator</code></a>. As a result, <code>Option&lt;&amp;RangeSetBlaze&gt;</code> becomes an iterator that yields either zero or one <code>&amp;RangeSetBlaze</code> value. Since it satisfies the blanket implementation’s trait bound, the expression</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb8-1"><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Some</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>a)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>filter(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>set<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>set<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>is_empty())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">union</span>()</span></code></pre></div></div>
<p>can call <code>union()</code> just like a vector or an array.</p>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>The key idea is that <code>union()</code> is written only once, yet it automatically becomes available on many different collection types. In the example, a vector, an array, and even a filtered <code>Option</code> can all call the same <code>union()</code> method because they satisfy the required iterator constraint.</p>
</blockquote>
</section>
<section id="puzzle-7-macro-generated-implementations" class="level2">
<h2 class="anchored" data-anchor-id="puzzle-7-macro-generated-implementations">Puzzle 7: Macro-Generated Implementations</h2>
<p>Treat 15 integer-like types uniformly: <code>u8</code>, …, <code>usize</code>, …<code>i128</code>, <code>char</code>, <code>IPv4</code>, <code>IPv6</code></p>
<p>For example, with a <code>min</code>, <code>max</code>, and ability to add one (unchecked).</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">classDiagram
    direction TB

    class Integer {
        &lt;&lt;abstract class&gt;&gt;
        +add_one()
        +min_value()
        +max_value()
    }

    class NumericInteger {
        &lt;&lt;abstract class&gt;&gt;
        +add_one()
        +min_value()
        +max_value()
    }

    class IpInteger {
        &lt;&lt;abstract class&gt;&gt;
        +add_one()
        +min_value()
        +max_value()
    }

    class CharInteger {
        &lt;&lt;abstract class&gt;&gt;
        +add_one()
        +min_value()
        +max_value()
    }

    class u8 {
        &lt;&lt;concrete class&gt;&gt;
        +add_one() // inherited
        +min_value() // inherited
        +max_value() // inherited
    }

    class IPv4Type {
        &lt;&lt;concrete class&gt;&gt;
        +add_one() // inherited
        +min_value() // inherited
        +max_value() // inherited
    }

    class IPv6Type {
        &lt;&lt;concrete class&gt;&gt;
        +add_one() // inherited
        +min_value() // inherited
        +max_value() // inherited
    }

    class CharType {
        &lt;&lt;concrete class&gt;&gt;
        +add_one() // inherited
        +min_value() // inherited
        +max_value() // inherited
    }

    Integer &lt;|-- NumericInteger : is-a
    Integer &lt;|-- IpInteger : is-a
    Integer &lt;|-- CharInteger : is-a

    NumericInteger &lt;|-- u8 : is-a
    IpInteger &lt;|-- IPv4Type : is-a
    IpInteger &lt;|-- IPv6Type : is-a
    CharInteger &lt;|-- CharType : is-a
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<p>In addition to procedural macros like <code>derive</code>, which take a token stream and transform it into another token stream, declarative macros (<code>macro_rules!</code>) can also generate code — including full method implementations.</p>
<p>The key difference here is that instead of manually writing nearly identical <code>impl</code> blocks for each type, we encode the shared logic once and reuse it across all implementations via macros. This significantly reduces repetition while keeping the specialization for each type explicit where needed.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-9-contents" aria-controls="callout-9" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Macro-Generated Implementations — Code
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-9" class="callout-9-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb9-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::net::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>Ipv4Addr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> Ipv6Addr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">trait</span> Integer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Copy</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Ord</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> add_one(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> min_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> max_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-8"></span>
<span id="cb9-9"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">macro_rules!</span> impl_integer_ops_num <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-10">    (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>ty) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-11">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> add_one(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-12">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb9-13">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-14"></span>
<span id="cb9-15">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> min_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-16">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;$</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">MIN</span></span>
<span id="cb9-17">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-18"></span>
<span id="cb9-19">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> max_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-20">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;$</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">MAX</span></span>
<span id="cb9-21">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-22">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb9-23"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-24"></span>
<span id="cb9-25"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">macro_rules!</span> impl_integer_ops_ip <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-26">    (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ip_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>ty<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>representation_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>ty) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-27">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> add_one(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-28">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;$</span>ip_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;$</span>representation_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb9-29">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-30"></span>
<span id="cb9-31">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> min_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-32">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;$</span>ip_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;$</span>representation_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">MIN</span>)</span>
<span id="cb9-33">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-34"></span>
<span id="cb9-35">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> max_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-36">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;$</span>ip_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;$</span>representation_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">MAX</span>)</span>
<span id="cb9-37">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-38">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb9-39"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-40"></span>
<span id="cb9-41"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">macro_rules!</span> impl_integer_ops_char <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-42">    () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-43">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> add_one(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-44">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> num <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u32</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-45">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> num <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xD800</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-46">                num <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xE000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-47">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-48">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from_u32(num)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>expect(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"next char must be a valid Unicode scalar value"</span>)</span>
<span id="cb9-49">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-50"></span>
<span id="cb9-51">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> min_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-52">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">MIN</span></span>
<span id="cb9-53">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-54"></span>
<span id="cb9-55">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> max_value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-56">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">MAX</span></span>
<span id="cb9-57">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-58">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb9-59"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-60"></span>
<span id="cb9-61"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i8</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_num!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i8</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-62"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u8</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_num!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u8</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-63"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i16</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_num!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i16</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-64"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u16</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_num!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u16</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-65"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_num!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-66"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u32</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_num!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u32</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-67"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i64</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_num!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i64</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-68"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u64</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_num!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u64</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-69"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i128</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_num!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i128</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-70"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u128</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_num!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u128</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-71"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">isize</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_num!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">isize</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-72"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">usize</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_num!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">usize</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-73"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> Ipv4Addr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_ip!</span>(Ipv4Addr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u32</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-74"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> Ipv6Addr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_ip!</span>(Ipv6Addr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u128</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-75"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Integer <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">impl_integer_ops_char!</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-76"></span>
<span id="cb9-77"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Can't use num_traits::PrimInt as a 'subclass'</span></span>
<span id="cb9-78"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// because Rust worries that 'char' etc. might</span></span>
<span id="cb9-79"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// be added to it later (coherence).</span></span>
<span id="cb9-80"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// HOWEVER: You could define your own PrimInt with 12 impls.</span></span>
<span id="cb9-81"></span>
<span id="cb9-82"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// TECHNIQUE NAME: macro-generated implementation.</span></span>
<span id="cb9-83"></span>
<span id="cb9-84"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-85">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u8</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">254</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-86">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>add_one()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-87">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'a'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>add_one()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-88"></span>
<span id="cb9-89">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>min_value()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">MIN</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-90">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>max_value()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">MAX</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-91">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Ipv4Addr::</span>min_value()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Ipv4Addr::</span>new(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-92">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Ipv4Addr::</span>max_value()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Ipv4Addr::</span>new(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-93"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>The key idea is that macros let us scale a single abstraction (<code>Integer</code>) across many unrelated types by generating repetitive <code>impl</code> blocks automatically, while still allowing special-case logic (like <code>char</code> and IP addresses) where needed.</p>
</blockquote>
</section>
<section id="puzzle-8-constraint-gated-methods" class="level2">
<h2 class="anchored" data-anchor-id="puzzle-8-constraint-gated-methods">Puzzle 8: Constraint-Gated Methods</h2>
<p>We have abstract class <code>OutputArray&lt;N&gt;</code> with two methods. For <code>N = 8</code>, however, we want one extra method because it maps cleanly to a <code>u8</code> bitmask.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">classDiagram
    direction TB

    class OutputArrayN["OutputArray~N~"] {
        &lt;&lt;abstract class&gt;&gt;
        +new()s
        +set_level_at_index(index, level)
    }

    class OutputArray4["OutputArray~4~"] {
        &lt;&lt;concrete class&gt;&gt;
        +new() // inherited
        +set_level_at_index(index, level) // inh.
    }

    class OutputArray8["OutputArray~8~"] {
        &lt;&lt;concrete class&gt;&gt;
        +new() // inherited
        +set_level_at_index(index, level) // inh.
        +set_from_bits(u8)
    }


    OutputArrayN &lt;|-- OutputArray4 : is-a
    OutputArrayN &lt;|-- OutputArray8 : is-a
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<p>Rust allows methods to be conditionally attached to a type by placing them in separate <code>impl</code> blocks with additional constraints. These constraints can take many forms, including const generic values, trait bounds, lifetimes, or combinations of them.</p>
<p>As a result, a type’s API can vary depending on the compile-time properties it satisfies. Rather than exposing every method to every instance of a type, Rust allows certain methods to exist only when specific requirements are met.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-10-contents" aria-controls="callout-10" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Constraint-Gated Methods — Code
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-10" class="callout-10-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb10-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>derive<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Debug</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Clone</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Copy</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb10-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> OutputArray<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">usize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-3">    levels<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> N]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb10-4"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-5"></span>
<span id="cb10-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">usize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> OutputArray<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> new() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-8">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> levels<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> N] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-9">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-10"></span>
<span id="cb10-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> set_level_at_index(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">usize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> level<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-12">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>levels[index] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> level<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-13">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-14"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-15"></span>
<span id="cb10-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Hardcoded 8, matching the bit width expected by a u8 mask.</span></span>
<span id="cb10-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> OutputArray<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-18">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> set_from_bits(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> bits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u8</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-19">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> slot <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>levels <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-20">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>slot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (bits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-21">            bits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-22">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-23">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-24"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-25"></span>
<span id="cb10-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// TECHNIQUE NAME: constraint-gated methods.</span></span>
<span id="cb10-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// - Put methods in a separate impl block.</span></span>
<span id="cb10-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// - The methods exist only when the impl's constraints are met.</span></span>
<span id="cb10-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// - Constraints can be const values, trait bounds, lifetimes, or combinations</span></span>
<span id="cb10-30"></span>
<span id="cb10-31"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-32">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> any <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">OutputArray::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>new()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-33">    any<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>set_level_at_index(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">true</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-34"></span>
<span id="cb10-35">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> eight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">OutputArray::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>new()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-36">    eight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>set_from_bits(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0b1011_0001</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-37"></span>
<span id="cb10-38">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(any<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>levels<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> [<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">false</span>])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-39">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(eight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>levels<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> [<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">true</span>])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-40"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-11-contents" aria-controls="callout-11" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Not a blanket implementation
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-11" class="callout-11-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>At first glance, <code>impl&lt;const N: usize&gt; OutputArray&lt;N&gt;</code> may look similar to a blanket implementation. However, there is an important distinction: constraint-gated methods apply to a specific generic type, while blanket implementations apply a trait across a family of types.</p>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>The key idea is that method availability can depend on compile-time constraints. In this example, only <code>OutputArray&lt;8&gt;</code> gains the <code>set_from_bits()</code> method, because only an 8-element output array can be represented directly by a <code>u8</code> bitmask. The restriction is enforced entirely at compile time, making invalid usages impossible to compile.</p>
</blockquote>
</section>
<section id="puzzle-9-method-level-constraints" class="level2">
<h2 class="anchored" data-anchor-id="puzzle-9-method-level-constraints">Puzzle 9: Method-Level Constraints</h2>
<p>We want two levels of <code>FlashBlock</code>: a top level with <code>new</code> and <code>clear</code>, and a constrained level that adds <code>save</code> and <code>load</code> only when <code>T</code> satisfies both serialization and deserialization capabilities.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">classDiagram
    direction TB

    class Serialize {
        &lt;&lt;abstract class&gt;&gt;
        +serialize()
    }

    class Deserialize {
        &lt;&lt;abstract class&gt;&gt;
        +deserialize()
    }

    class TConstrained["T: Serialize + Deserialize"] {
        &lt;&lt;abstract class&gt;&gt;
        +serialize() // inherited
        +deserialize() // inherited
    }

    class WifiCredentials {
        &lt;&lt;concrete class&gt;&gt;
        +serialize() // inherited
        +deserialize() // inherited
    }

    class FlashBlock {
        &lt;&lt;concrete class&gt;&gt;
        +new()
        +clear()
    }

    class FlashBlockForTSerializeDeserialize {
        &lt;&lt;concrete class&gt;&gt;
        +new() // inherited
        +clear() // inherited
        +save(value, key)
        +load(key) T
    }

    Serialize &lt;|-- TConstrained : is-a
    Deserialize &lt;|-- TConstrained : is-a
    TConstrained &lt;|-- WifiCredentials : is-a
    FlashBlock &lt;|-- FlashBlockForTSerializeDeserialize : is-a
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<p>Unlike constraint-gated methods, which apply bounds to an entire <code>impl</code> block, Rust allows constraints to be placed directly on individual methods via a <code>where</code> clause. This enables a type to remain generally usable while selectively enabling certain operations only when additional requirements are met.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-12-contents" aria-controls="callout-12" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Method-Level Constraints — Code
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-12" class="callout-12-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb11-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::collections::</span>HashMap<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-2"></span>
<span id="cb11-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">serde::de::</span>DeserializeOwned<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">serde::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>Deserialize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> Serialize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb11-5"></span>
<span id="cb11-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>derive<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Debug</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Clone</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> Serialize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> Deserialize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">PartialEq</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Eq</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb11-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> WifiCredentials <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-8">    ssid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-9">    password<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-11"></span>
<span id="cb11-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/// Key point: method-level gating.</span></span>
<span id="cb11-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/// - The type exists for all use-cases.</span></span>
<span id="cb11-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/// - Only `save/load&lt;T&gt;` require extra capabilities on `T`.</span></span>
<span id="cb11-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>derive<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Default</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb11-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> FlashBlock <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-17">    store<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> HashMap<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;,</span></span>
<span id="cb11-18"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-19"></span>
<span id="cb11-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> FlashBlock <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> new() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-22">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Self</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">default</span>()</span>
<span id="cb11-23">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-24"></span>
<span id="cb11-25">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> clear(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-26">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>store<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>clear()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-27">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-28"></span>
<span id="cb11-29">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> save<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">str</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>T) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">postcard::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb11-30">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">where</span></span>
<span id="cb11-31">        T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Serialize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> DeserializeOwned<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-32">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-33">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> bytes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">postcard::</span>to_stdvec(value)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span></span>
<span id="cb11-34">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>store<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>insert(key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> bytes)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-35">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(())</span>
<span id="cb11-36">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-37"></span>
<span id="cb11-38">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> load<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Option</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb11-39">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">where</span></span>
<span id="cb11-40">        T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Serialize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> DeserializeOwned<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-41">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-42">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> bytes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>store<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>get(key)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span></span>
<span id="cb11-43">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">postcard::</span>from_bytes(bytes)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>ok()</span>
<span id="cb11-44">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-45"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-46"></span>
<span id="cb11-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// TECHNIQUE NAME: method-level gating.</span></span>
<span id="cb11-48"></span>
<span id="cb11-49"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">postcard::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-50">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> flash <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FlashBlock::</span>new()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-51"></span>
<span id="cb11-52">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> credentials <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> WifiCredentials <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-53">        ssid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HomeWiFi"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-54">        password<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"secret"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-55">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb11-56"></span>
<span id="cb11-57">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Works: WifiCredentials satisfies Serialize + Deserialize.</span></span>
<span id="cb11-58">    flash<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>save(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"wifi"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>credentials)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span></span>
<span id="cb11-59">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> loaded<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Option</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>WifiCredentials<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> flash<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>load(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"wifi"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-60">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> loaded <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> loaded<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>unwrap()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-61">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>loaded<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>ssid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HomeWiFi"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-62">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>loaded<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>password<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"secret"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-63"></span>
<span id="cb11-64">    flash<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>clear()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-65">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert!</span>(flash<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">load::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>WifiCredentials<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"wifi"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>is_none())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-66"></span>
<span id="cb11-67">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// This compiles, but it asks for the wrong type.</span></span>
<span id="cb11-68"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// device-envoy stores type-identifying metadata with each value,</span></span>
<span id="cb11-69"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// so this kind of mismatch is likely to be caught at load time.</span></span>
<span id="cb11-70">    flash<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>save(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"number"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42u8</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span></span>
<span id="cb11-71">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> loaded<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Option</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> flash<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>load(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"number"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-72">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert!</span>(loaded<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>is_none())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-73"></span>
<span id="cb11-74">    <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(())</span>
<span id="cb11-75"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>The key idea is that constraints can be attached to individual methods instead of the entire type. In this example, basic operations are always available on <code>FlashBlock</code>, while storage-related operations are only enabled when the value type satisfies the required serialization and deserialization bounds. This keeps the core type simple while making advanced functionality conditional on capability.</p>
</blockquote>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>Here is the conclusion table from Carl Kadie:</p>
<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/rust-notes-4/conclusion.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Conclusion"></p>
</figure>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>

 ]]></description>
  <category>rust</category>
  <guid>https://tech.ycwu.space/posts/rust-notes-4/20260531.html</guid>
  <pubDate>Sun, 31 May 2026 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/rust-notes-4/rust_notes_4.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>Rust Notes #3: Iterator collect and Result short-circuiting</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/rust-notes-3/20260510.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/rust-notes-3/rust_notes_3.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Rust Notes 3"></p>
</figure>
</div>
<p>Today, while reading the Rust documentation for the <code>Iterator</code> trait, I came across an interesting note in the <a href="https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect">collect()</a> method section.</p>
<p>It explains that <code>collect()</code> is more flexible than it might first appear:</p>
<blockquote class="blockquote">
<p><code>collect()</code> can also create instances of types that are not typical collections. For example, a <code>String</code> can be built from <code>char</code>s, and an iterator of <code>Result&lt;T, E&gt;</code> items can be collected into <code>Result&lt;Collection&lt;T&gt;, E&gt;</code>.</p>
</blockquote>
<p>This behavior is particularly useful when you want either:</p>
<ul>
<li>to short-circuit immediately when the first error occurs, or</li>
<li>all operations to succeed and collect the results.</li>
</ul>
<p>For example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> v1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">vec!</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"not 42"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"43"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> r1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;,</span> _<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>iter()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-4">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{r1:?}"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Err(ParseIntError { kind: InvalidDigit })</span></span>
<span id="cb1-5"></span>
<span id="cb1-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> v2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">vec!</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"42"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"43"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> r2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;,</span> _<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>iter()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-8">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{r2:?}"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Ok([42, 43])</span></span>
<span id="cb1-9"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>In <code>r1</code>, the string <code>"not 42"</code> fails to parse as an <code>i32</code>, so the entire computation short-circuits and returns the first error (<code>ParseIntError</code>). The subsequent value <code>"43"</code> is never processed.</p>
<p>In contrast, <code>r2</code> contains only valid numeric strings, so all elements are successfully parsed and collected into a <code>Vec&lt;i32&gt;</code> wrapped in <code>Ok</code>.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>rust</category>
  <guid>https://tech.ycwu.space/posts/rust-notes-3/20260510.html</guid>
  <pubDate>Sun, 10 May 2026 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/rust-notes-3/rust_notes_3.png" medium="image" type="image/png" height="33" width="144"/>
</item>
<item>
  <title>Rust Notes #2: Trait-Based Conversions in Iterators</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/rust-notes-2/20260509.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/rust-notes-2/rust_notes_2.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Rust Notes 2"></p>
</figure>
</div>
<p>Today I read some code in <a href="https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/boxing_errors.html">Rust by Example</a> and came across an interesting usage of iterator combinators.</p>
<p>Typically, I would pass a closure to <code>map()</code>, but a more elegant approach is to use a trait function pointer directly. For example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">vec!</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"42"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> r1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>iter()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> r2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_iter()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Alternative <code>r1</code> implementation using <code>String::from()</code>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>An alternative implementation for <code>r1</code> is:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> r1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>iter()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&amp;</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from(s))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div></div>
<p>Using <code>.to_string()</code> or <code>String::from()</code> here is mostly a matter of coding style.</p>
<p>The item type produced by <code>v.iter()</code> is <code>&amp;&amp;str</code>. In the first version, we rely on Rust’s auto-deref feature with <code>|s| s.to_string()</code>. In the second version, we manually destructure the extra reference layer using <code>|&amp;s|</code>, converting the item from <code>&amp;&amp;str</code> to <code>&amp;str</code>, which can then be passed directly to <code>String::from()</code>.</p>
</div>
</div>
</div>
<p>For <code>r1</code>, we provide a closure that is applied to each item of the iterator. For <code>r2</code>, however, we directly pass the <code>String::from</code> function.</p>
<p>This technique becomes especially useful when type constraints are already clear from context. Instead of explicitly referring to a concrete constructor like <code>String::from</code>, Rust allows us to rely on more general trait-based conversions. For example, the book demonstrates that <code>.map_err(From::from)</code> leverages the <code>From::from</code> function as a generic conversion mechanism, letting the compiler infer the appropriate target error type from the surrounding <code>Result</code> context. This makes the code more flexible and reduces the need to commit to a specific concrete type at the point of transformation.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>rust</category>
  <guid>https://tech.ycwu.space/posts/rust-notes-2/20260509.html</guid>
  <pubDate>Sat, 09 May 2026 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/rust-notes-2/rust_notes_2.png" medium="image" type="image/png" height="23" width="144"/>
</item>
<item>
  <title>Rust Notes #1: Loop Scope vs Python</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/rust-notes-1/20260311.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/rust-notes-1/rust_notes_1.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Rust Notes 1"></p>
</figure>
</div>
<p>For the last couple of months, I’ve been busy learning Rust and suddenly realized that I haven’t written a blog post in a while. So I’d like to share an interesting pattern I encountered while learning Rust from the perspective of a Python developer.</p>
<p>In the following Rust code, I initially expected to see <code>idx=2</code> and <code>n=3</code>, but the compiler complained.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">vec!</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (idx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> n) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>iter()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>enumerate() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-4">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{idx}, {n}"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-6">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{idx}, {n}"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// panic!</span></span>
<span id="cb1-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>What I had in mind was that the Rust code would translate conceptually into something like the following Python code:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb2-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> idx, n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(v):</span>
<span id="cb2-3">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>idx<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb2-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>idx<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2, 3</span></span></code></pre></div></div>
<p>Frankly speaking, I was puzzled for a while before realizing that <code>(idx, n)</code> in the Rust <code>for</code> loop is actually a pattern. The variables <code>idx</code> and <code>n</code> exist only within the scope of the loop body, so they are not available outside the <code>for</code> block.</p>
<p>Learning Rust keeps things refreshing and interesting every time I come across a pattern that differs from Python. I hope to continue sharing short Rust notes like this in the future.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>rust</category>
  <category>python</category>
  <guid>https://tech.ycwu.space/posts/rust-notes-1/20260311.html</guid>
  <pubDate>Wed, 11 Mar 2026 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/rust-notes-1/rust_notes_1.png" medium="image" type="image/png" height="55" width="144"/>
</item>
<item>
  <title>Notes on Turtle Island</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/turtle-island-notes/20250721.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/turtle-island-notes/turtle_island_docs.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Turtle Island docs"></p>
</figure>
</div>
<p>This is a short post documenting some things I often forget while building <a href="https://github.com/jrycw/turtle-island">Turtle Island</a>.</p>
<section id="output-column-names" class="level2">
<h2 class="anchored" data-anchor-id="output-column-names">Output Column Names</h2>
<p>In Polars, if an explicit name isn’t provided, the output column will take the name of the first expression. This default behavior is generally helpful, but it can lead to unexpected results—especially when the first expression doesn’t directly represent a column. For example, if the first expression is <code>pl.lit()</code>, the resulting column name will be <code>"literal"</code>.</p>
<p>This isn’t a big issue when dealing with a single column, but it becomes problematic when using expressions like <code>pl.all()</code> or <code>pl.col("*")</code>. In such cases, logic that depends on column names often breaks.</p>
<p>My current workaround is to tweak the internal logic so that the returned expression doesn’t start with something like <code>pl.lit()</code>, especially in functions that aim to support wildcard expressions.</p>
</section>
<section id="expression-context-vs-dataframe-context" class="level2">
<h2 class="anchored" data-anchor-id="expression-context-vs-dataframe-context">Expression Context vs DataFrame Context</h2>
<p>Since Polars expressions are designed to be evaluated later (not immediately), we cannot rely on any runtime properties of the DataFrame when building them. That means things like the shape or number of rows in the DataFrame are not available during expression construction.</p>
<p>However, some built-in Polars functions help bridge this gap. For instance, <code>pl.len()</code> returns an expression that can be used to represent the number of rows in the future evaluation context. This is useful for writing general-purpose logic in <strong>Turtle Island</strong>.</p>
<p>A major caveat is that Polars expressions can’t be evaluated directly in Python. You can’t write something like <code>if pl.len()</code> or <code>pl.len() + 1</code> as regular Python code. These expressions must be used inside other Polars expressions or functions—such as <code>pl.int_range(0, pl.len() + 1)</code>—where they’ll be correctly interpreted and evaluated within the Polars execution context.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>polars</category>
  <category>ti</category>
  <guid>https://tech.ycwu.space/posts/turtle-island-notes/20250721.html</guid>
  <pubDate>Mon, 21 Jul 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/turtle-island-notes/turtle_island_docs.png" medium="image" type="image/png" height="56" width="144"/>
</item>
<item>
  <title>Turtle Island: A Utility Kit for Polars Expressions</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/turtle-island-intro/20250706.html</link>
  <description><![CDATA[ 






<p>This weekend, I started building a Python package called <a href="https://github.com/jrycw/turtle-island">Turtle Island</a>, which collects some of my ideas for helper functions aimed at reducing boilerplate when writing Polars expressions.</p>
<p>Currently, Turtle Island offers the following nine utilities:</p>
<ul>
<li><strong><code>case_when()</code></strong> – A cleaner and more readable way to construct chained conditional logic in Polars.</li>
<li><strong><code>make_index()</code></strong> – Creates a virtual row index as a Polars expression, without materializing it as a column.</li>
<li><strong><code>bucketize_lit()</code></strong> – Assigns literal values to rows in a round-robin pattern based on their index.</li>
<li><strong><code>bucketize()</code></strong> – A more general version of <code>bucketize_lit()</code> that cycles through multiple Polars expressions, enabling advanced use cases with dynamic column values.</li>
<li><strong><code>is_every_nth_row()</code></strong> – Identifies every <em>n</em>-th row using a modulo operation on the row index.</li>
<li><strong><code>move_cols_to_start()</code></strong> – Reorders selected columns to appear at the beginning of the DataFrame.</li>
<li><strong><code>move_cols_to_end()</code></strong> – Reorders selected columns to appear at the end of the DataFrame.</li>
<li><strong><code>make_hyperlink()</code></strong> – Generates HTML anchor (<code>&lt;a&gt;</code>) tags from <code>text</code> and <code>url</code> columns for rendering clickable links.</li>
<li><strong><code>make_tooltip()</code></strong> – Creates HTML tooltips from <code>label</code> and <code>tooltip</code> columns for hoverable text display.</li>
</ul>
<p>Here’s a quick example that uses <code>ti.is_every_nth_row()</code> to build a Polars expression suitable for styling tables interactively with Great Tables:</p>
<div id="08702e91" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, loc, style</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables.data <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> countrypops</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> turtle_island <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> ti</span>
<span id="cb1-6"></span>
<span id="cb1-7">df_pd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> countrypops.sample(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>).loc[:, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country_name"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"population"</span>]]</span>
<span id="cb1-8">df_pl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.from_pandas(df_pd)</span>
<span id="cb1-9">row_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ti.is_every_nth_row(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb1-10"></span>
<span id="cb1-11">(</span>
<span id="cb1-12">    GT(df_pl)</span>
<span id="cb1-13">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(rows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>row_expr))</span>
<span id="cb1-14">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(rows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=~</span>row_expr))</span>
<span id="cb1-15">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>)</span>
<span id="cb1-16">)</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/turtle-island-intro/turtle_island_table_styling.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Styling table using Turtle Island"></p>
</figure>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>python</category>
  <category>polars</category>
  <category>ti</category>
  <guid>https://tech.ycwu.space/posts/turtle-island-intro/20250706.html</guid>
  <pubDate>Sun, 06 Jul 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/turtle-island-intro/turtle_island_table_styling.png" medium="image" type="image/png" height="189" width="144"/>
</item>
<item>
  <title>Creating an Integrated marimo UI Explorer with Great Tables</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/marimo-gt-ui-reference/20250704.html</link>
  <description><![CDATA[ 






<p>While working with marimo widgets, I often find myself wanting a quick glance at what each widget looks like or which parameters it accepts. That’s when I realized—this was a perfect opportunity to build an interactive UI cheatsheet using Great Tables inside a marimo notebook.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/marimo-gt-ui-reference/marimo_gt_ui_refrence.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="marimo UI explorer with Great Tables"></p>
</figure>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Give It a Sec – WASM Magic Happening
</div>
</div>
<div class="callout-body-container callout-body">
<p>The widgets may take a few moments to load, as they rely on WebAssembly under the hood.</p>
</div>
</div>
<p>This interactive table includes four columns:</p>
<ol type="1">
<li>A link to the documentation.</li>
<li>A live widget preview.</li>
<li>Its current reactive value.</li>
<li>A code snippet with an accordion that reveals the widget’s signature.</li>
</ol>
<p>To keep things playful, I added a touch of randomness—so the table appears slightly different each time it loads.</p>
<marimo-island data-app-id="main" data-cell-id="Hbol" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">import%20marimo%20as%20mo%0A%0Aimport%20random%0Afrom%20collections.abc%20import%20Iterable%0A%0Aimport%20pandas%20as%20pd%0Afrom%20great_tables%20import%20GT%2C%20html</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="MJUe" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">def%20render_widget(widget)%3A%0A%20%20%20%20if%20hasattr(widget%2C%20%22_display_%22)%3A%0A%20%20%20%20%20%20%20%20render_method%20%3D%20%22_display_%22%0A%20%20%20%20elif%20hasattr(widget%2C%20%22_repr_html_%22)%3A%0A%20%20%20%20%20%20%20%20render_method%20%3D%20%22_repr_html_%22%0A%20%20%20%20elif%20hasattr(widget%2C%20%22_mime_%22)%3A%0A%20%20%20%20%20%20%20%20render_method%20%3D%20%22_mime_%22%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20raise%20ValueError(%22The%20object%20does%20not%20have%20a%20valid%20render%20method.%22)%0A%20%20%20%20return%20getattr(widget%2C%20render_method)()%0A%0Adef%20render_widgets(widgets)%3A%0A%20%20%20%20if%20not%20isinstance(widgets%2C%20Iterable)%3A%0A%20%20%20%20%20%20%20%20widgets%20%3D%20%5Bwidgets%5D%0A%20%20%20%20return%20%5Brender_widget(widget)%20for%20widget%20in%20widgets%5D%0A%0Adef%20strify_widget_value(widget)%3A%0A%20%20%20%20return%20str(widget.value)%0A%0Adef%20strify_widget_values(widgets)%3A%0A%20%20%20%20if%20not%20isinstance(widgets%2C%20Iterable)%3A%0A%20%20%20%20%20%20%20%20widgets%20%3D%20%5Bwidgets%5D%0A%20%20%20%20return%20%5Bstrify_widget_value(w)%20for%20w%20in%20widgets%5D</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="vblA" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">switch%20%3D%20mo.ui.switch()%0Acheckbox%20%3D%20mo.ui.checkbox(label%3D%22check%20me%22)%0Adate%20%3D%20mo.ui.date()%0Arun_botton%20%3D%20mo.ui.run_button(label%3D%22Run%22)%0Abutton%20%3D%20mo.ui.button(%0A%20%20%20%20value%3D0%2C%20on_click%3Dlambda%20value%3A%20value%20%2B%201%2C%20label%3D%22increment%22%0A)%0Anumber%20%3D%20mo.ui.number(1%2C%2010)%0Aslider%20%3D%20mo.ui.slider(1%2C%2010%2C%201)%0Arange_slider%20%3D%20mo.ui.range_slider(1%2C%2010%2C%202%2C%20value%3D%5B2%2C%206%5D)%0Aradio%20%3D%20mo.ui.radio(options%3D%5B%22Apples%22%2C%20%22Oranges%22%5D%2C%20value%3D%22Apples%22)%0Adropdown%20%3D%20mo.ui.dropdown(options%3D%5B%22Apples%22%2C%20%22Oranges%22%5D%2C%20value%3D%22Apples%22)%0Amultiselect%20%3D%20mo.ui.multiselect(options%3D%5B%22Apples%22%2C%20%22Oranges%22%5D)%0Atext%20%3D%20mo.ui.text(placeholder%3D%22placeholder...%22%2C%20debounce%3DFalse)%0Atext_area%20%3D%20mo.ui.text_area(placeholder%3D%22placeholder...%22%2C%20debounce%3DFalse)%0A%0Awidgets%20%3D%20mo.ui.array(%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20switch%2C%0A%20%20%20%20%20%20%20%20checkbox%2C%0A%20%20%20%20%20%20%20%20date%2C%0A%20%20%20%20%20%20%20%20run_botton%2C%0A%20%20%20%20%20%20%20%20button%2C%0A%20%20%20%20%20%20%20%20number%2C%0A%20%20%20%20%20%20%20%20slider%2C%0A%20%20%20%20%20%20%20%20range_slider%2C%0A%20%20%20%20%20%20%20%20radio%2C%0A%20%20%20%20%20%20%20%20dropdown%2C%0A%20%20%20%20%20%20%20%20multiselect%2C%0A%20%20%20%20%20%20%20%20text%2C%0A%20%20%20%20%20%20%20%20text_area%2C%0A%20%20%20%20%5D%0A)%0A%0Acol_widget_link%20%3D%20%5B%0A%20%20%20%20mo.md(%22%5BSwitch%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fswitch%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BCheckBox%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fcheckbox%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BDate%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fdates%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BRun%20Button%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Frun_button%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BButton%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fbutton%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BNumber%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fnumber%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BSlider%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fslider%2F)%22)%2C%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%5BRange%20Slider%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Frange_slider%2F)%22%0A%20%20%20%20)%2C%0A%20%20%20%20mo.md(%22%5BRadio%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fradio%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BDropdown%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fdropdown%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BMultiSelect%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fmultiselect%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BText%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Ftext%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BText%20Area%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Ftext_area%2F)%22)%2C%0A%5D%0A%0Acol_code%20%3D%20%5B%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22switch%20%3D%20mo.ui.switch()%22%3A%20%22%60switch(value%3A%20bool%20%3D%20False%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20disabled%3A%20bool%20%3D%20False%2C%20on_change%3A%20Optional%5BCallable%5B%5Bbool%5D%2C%20None%5D%5D%20%3D%20None)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'checkbox%20%3D%20mo.ui.checkbox(label%3D%22check%20me%22)'%3A%20%22%60checkbox(value%3A%20bool%20%3D%20False%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20disabled%3A%20bool%20%3D%20False%2C%20on_change%3A%20Optional%5BCallable%5B%5Bbool%5D%2C%20None%5D%5D%20%3D%20None)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22date%20%3D%20mo.ui.date()%22%3A%20%22%60date(start%3A%20Optional%5Bdate%20%7C%20str%5D%20%3D%20None%2C%20stop%3A%20Optional%5Bdate%20%7C%20str%5D%20%3D%20None%2C%20value%3A%20Optional%5Bdate%20%7C%20str%5D%20%3D%20None%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5Bdate%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False%2C%20disabled%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'run_botton%20%3D%20mo.ui.run_button(label%3D%22Run%22)'%3A%20%22%60run_button(kind%3A%20Literal%5B'neutral'%2C%20'success'%2C%20'warn'%2C%20'danger'%5D%20%3D%20'neutral'%2C%20disabled%3A%20bool%20%3D%20False%2C%20tooltip%3A%20Optional%5Bstr%5D%20%3D%20None%2C%20*%2C%20label%3A%20str%20%3D%20'click%20to%20run'%2C%20on_change%3A%20Optional%5BCallable%5B%5BAny%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False%2C%20keyboard_shortcut%3A%20Optional%5Bstr%5D%20%3D%20None)%60%22%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'button%20%3D%20mo.ui.button(value%3D0%2C%20on_click%3Dlambda%20value%3A%20value%20%2B%201%2C%20label%3D%22increment%22)'%3A%20%22%60button(on_click%3A%20Optional%5BCallable%5B%5BAny%5D%2C%20Any%5D%5D%20%3D%20None%2C%20value%3A%20Optional%5BAny%5D%20%3D%20None%2C%20kind%3A%20Literal%5B'neutral'%2C%20'success'%2C%20'warn'%2C%20'danger'%5D%20%3D%20'neutral'%2C%20disabled%3A%20bool%20%3D%20False%2C%20tooltip%3A%20Optional%5Bstr%5D%20%3D%20None%2C%20*%2C%20label%3A%20str%20%3D%20'click%20here'%2C%20on_change%3A%20Optional%5BCallable%5B%5BAny%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False%2C%20keyboard_shortcut%3A%20Optional%5Bstr%5D%20%3D%20None)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22number%20%3D%20mo.ui.number(1%2C%2010)%22%3A%20%22%60number(start%3A%20Optional%5Bfloat%5D%20%3D%20None%2C%20stop%3A%20Optional%5Bfloat%5D%20%3D%20None%2C%20step%3A%20Optional%5Bfloat%5D%20%3D%20None%2C%20value%3A%20Optional%5Bfloat%5D%20%3D%20None%2C%20debounce%3A%20bool%20%3D%20False%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5BOptional%5BNumeric%5D%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False%2C%20disabled%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22slider%20%3D%20mo.ui.slider(1%2C%2010%2C%201)%22%3A%20%22%60slider(start%3A%20Optional%5BNumeric%5D%20%3D%20None%2C%20stop%3A%20Optional%5BNumeric%5D%20%3D%20None%2C%20step%3A%20Optional%5BNumeric%5D%20%3D%20None%2C%20value%3A%20Optional%5BNumeric%5D%20%3D%20None%2C%20debounce%3A%20bool%20%3D%20False%2C%20disabled%3A%20bool%20%3D%20False%2C%20orientation%3A%20Literal%5B'horizontal'%2C%20'vertical'%5D%20%3D%20'horizontal'%2C%20show_value%3A%20bool%20%3D%20False%2C%20include_input%3A%20bool%20%3D%20False%2C%20steps%3A%20Optional%5BSequence%5BNumeric%5D%5D%20%3D%20None%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5BOptional%5BNumeric%5D%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22range_slider%20%3D%20mo.ui.range_slider(1%2C%2010%2C%202%2C%20value%3D%5B2%2C%206%5D)%22%3A%20%22%60range_slider(start%3A%20Optional%5BNumeric%5D%20%3D%20None%2C%20stop%3A%20Optional%5BNumeric%5D%20%3D%20None%2C%20step%3A%20Optional%5BNumeric%5D%20%3D%20None%2C%20value%3A%20Optional%5BSequence%5BNumeric%5D%5D%20%3D%20None%2C%20debounce%3A%20bool%20%3D%20False%2C%20orientation%3A%20Literal%5B'horizontal'%2C%20'vertical'%5D%20%3D%20'horizontal'%2C%20show_value%3A%20bool%20%3D%20False%2C%20steps%3A%20Optional%5BSequence%5BNumeric%5D%5D%20%3D%20None%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5BSequence%5BNumeric%5D%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False%2C%20disabled%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'radio%20%3D%20mo.ui.radio(options%3D%5B%22Apples%22%2C%20%22Oranges%22%5D%2C%20value%3D%22Apples%22)'%3A%20%22%60radio(options%3A%20Sequence%5Bstr%5D%20%7C%20dict%5Bstr%2C%20Any%5D%2C%20value%3A%20Optional%5Bstr%5D%20%3D%20None%2C%20inline%3A%20bool%20%3D%20False%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5BAny%5D%2C%20None%5D%5D%20%3D%20None%2C%20disabled%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'dropdown%20%3D%20mo.ui.dropdown(options%3D%5B%22Apples%22%2C%20%22Oranges%22%5D%2C%20value%3D%22Apples%22)'%3A%20%22%60dropdown(options%3A%20Sequence%5BAny%5D%20%7C%20dict%5Bstr%2C%20Any%5D%2C%20value%3A%20Optional%5BAny%5D%20%3D%20None%2C%20allow_select_none%3A%20Optional%5Bbool%5D%20%3D%20None%2C%20searchable%3A%20bool%20%3D%20False%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5BAny%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'multiselect%20%3D%20mo.ui.multiselect(options%3D%5B%22Apples%22%2C%20%22Oranges%22%5D)'%3A%20%22%60multiselect(options%3A%20Sequence%5BAny%5D%20%7C%20dict%5Bstr%2C%20Any%5D%2C%20value%3A%20Optional%5BSequence%5BAny%5D%5D%20%3D%20None%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5Blist%5Bobject%5D%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False%2C%20max_selections%3A%20Optional%5Bint%5D%20%3D%20None)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'text%20%3D%20mo.ui.text(placeholder%3D%22placeholder...%22%2C%20debounce%3DFalse)'%3A%20%22%60text(value%3A%20str%20%3D%20''%2C%20placeholder%3A%20str%20%3D%20''%2C%20kind%3A%20Literal%5B'text'%2C%20'password'%2C%20'email'%2C%20'url'%5D%20%3D%20'text'%2C%20max_length%3A%20Optional%5Bint%5D%20%3D%20None%2C%20disabled%3A%20bool%20%3D%20False%2C%20debounce%3A%20bool%20%7C%20int%20%3D%20True%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5Bstr%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'text_area%20%3D%20mo.ui.text_area(placeholder%3D%22placeholder...%22%2C%20debounce%3DFalse)'%3A%20%22%60text_area(value%3A%20str%20%3D%20''%2C%20placeholder%3A%20str%20%3D%20''%2C%20max_length%3A%20Optional%5Bint%5D%20%3D%20None%2C%20disabled%3A%20bool%20%3D%20False%2C%20debounce%3A%20bool%20%7C%20int%20%3D%20True%2C%20rows%3A%20Optional%5Bint%5D%20%3D%20None%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5Bstr%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%5D%0A%0A%0A%23%20table%20styling%0A_style_number_start%2C%20_style_number_end%20%3D%201%2C%206%0Astyle_widget%20%3D%20mo.ui.slider(%0A%20%20%20%20_style_number_start%2C%0A%20%20%20%20_style_number_end%2C%0A%20%20%20%20value%3Drandom.randint(_style_number_start%2C%20_style_number_end)%2C%0A%20%20%20%20label%3D%22Style%20Number%22%2C%0A)%0A%0A_colors%20%3D%20%5B%22blue%22%2C%20%22cyan%22%2C%20%22pink%22%2C%20%22green%22%2C%20%22red%22%2C%20%22gray%22%5D%0Acolor_widget%20%3D%20mo.ui.radio(%0A%20%20%20%20options%3D_colors%2C%0A%20%20%20%20value%3Drandom.choice(_colors)%2C%0A%20%20%20%20label%3D%22Style%20Color%22%2C%0A%20%20%20%20inline%3DTrue%2C%0A)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="bkHC" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">col_widget%20%3D%20render_widgets(widgets)%0Acol_value%20%3D%20strify_widget_values(widgets)%0A%0Adata%20%3D%20%7B%0A%20%20%20%20%22link%22%3A%20col_widget_link%2C%0A%20%20%20%20%22widget%22%3A%20col_widget%2C%0A%20%20%20%20%22value%22%3A%20col_value%2C%0A%20%20%20%20%22code%22%3A%20col_code%2C%0A%7D%0A%0Adf%20%3D%20pd.DataFrame(data)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="lEQa" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">gt%20%3D%20(%0A%20%20%20%20GT(df)%0A%20%20%20%20.cols_align(%22left%22)%0A%20%20%20%20.opt_all_caps()%0A%20%20%20%20.tab_header(html(style_widget)%2C%20html(color_widget))%0A)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="PKri" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">(%0A%20%20%20%20gt.opt_stylize(style%3Dstyle_widget.value%2C%20color%3Dcolor_widget.value)%0A%20%20%20%20.opt_align_table_header(%22left%22)%0A%20%20%20%20.cols_width(%7B%22widget%22%3A%20%2220%25%22%7D)%0A)</marimo-cell-code>
</marimo-island>
<p>One interesting discovery: the new state of the <a href="https://docs.marimo.io/api/inputs/run_button/">mo.ui.run_button</a> doesn’t persist as expected. This may be resolved by more effectively using marimo’s <a href="https://docs.marimo.io/guides/state/?h=state">reactive state</a>, which I plan to explore further.</p>
<p>Check out the full marimo code below or view it on <a href="https://molab.marimo.io/notebooks/nb_zeHb8T4sPPfbjokeEJnEn6/app">molab</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo</span>
<span id="cb1-2"></span>
<span id="cb1-3">__generated_with <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.14.10"</span></span>
<span id="cb1-4">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> marimo.App(width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb1-9">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> mo</span>
<span id="cb1-10"></span>
<span id="cb1-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (mo,)</span>
<span id="cb1-12"></span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb1-16">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> random</span>
<span id="cb1-17">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> collections.abc <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Iterable</span>
<span id="cb1-18"></span>
<span id="cb1-19">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-20">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, html</span>
<span id="cb1-21"></span>
<span id="cb1-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> GT, Iterable, html, pd, random</span>
<span id="cb1-23"></span>
<span id="cb1-24"></span>
<span id="cb1-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(Iterable):</span>
<span id="cb1-27">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> render_widget(widget):</span>
<span id="cb1-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(widget, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_display_"</span>):</span>
<span id="cb1-29">            render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_display_"</span></span>
<span id="cb1-30">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(widget, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_repr_html_"</span>):</span>
<span id="cb1-31">            render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_repr_html_"</span></span>
<span id="cb1-32">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(widget, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_mime_"</span>):</span>
<span id="cb1-33">            render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_mime_"</span></span>
<span id="cb1-34">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb1-35">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ValueError</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The object does not have a valid render method."</span>)</span>
<span id="cb1-36">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(widget, render_method)()</span>
<span id="cb1-37"></span>
<span id="cb1-38">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> render_widgets(widgets):</span>
<span id="cb1-39">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(widgets, Iterable):</span>
<span id="cb1-40">            widgets <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [widgets]</span>
<span id="cb1-41">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [render_widget(widget) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> widget <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> widgets]</span>
<span id="cb1-42"></span>
<span id="cb1-43">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> strify_widget_value(widget):</span>
<span id="cb1-44">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(widget.value)</span>
<span id="cb1-45"></span>
<span id="cb1-46">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> strify_widget_values(widgets):</span>
<span id="cb1-47">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(widgets, Iterable):</span>
<span id="cb1-48">            widgets <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [widgets]</span>
<span id="cb1-49">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [strify_widget_value(w) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> w <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> widgets]</span>
<span id="cb1-50"></span>
<span id="cb1-51">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> render_widgets, strify_widget_values</span>
<span id="cb1-52"></span>
<span id="cb1-53"></span>
<span id="cb1-54"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-55"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(mo, random):</span>
<span id="cb1-56">    switch <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.switch()</span>
<span id="cb1-57">    checkbox <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.checkbox(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"check me"</span>)</span>
<span id="cb1-58">    date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.date()</span>
<span id="cb1-59">    run_botton <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.run_button(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Run"</span>)</span>
<span id="cb1-60">    button <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.button(</span>
<span id="cb1-61">        value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, on_click<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> value: value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"increment"</span></span>
<span id="cb1-62">    )</span>
<span id="cb1-63">    number <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.number(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb1-64">    slider <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.slider(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-65">    range_slider <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.range_slider(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>])</span>
<span id="cb1-66">    radio <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.radio(options<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Apples"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Oranges"</span>], value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Apples"</span>)</span>
<span id="cb1-67">    dropdown <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.dropdown(options<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Apples"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Oranges"</span>], value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Apples"</span>)</span>
<span id="cb1-68">    multiselect <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.multiselect(options<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Apples"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Oranges"</span>])</span>
<span id="cb1-69">    text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.text(placeholder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"placeholder..."</span>, debounce<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb1-70">    text_area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.text_area(placeholder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"placeholder..."</span>, debounce<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb1-71"></span>
<span id="cb1-72">    widgets <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.array(</span>
<span id="cb1-73">        [</span>
<span id="cb1-74">            switch,</span>
<span id="cb1-75">            checkbox,</span>
<span id="cb1-76">            date,</span>
<span id="cb1-77">            run_botton,</span>
<span id="cb1-78">            button,</span>
<span id="cb1-79">            number,</span>
<span id="cb1-80">            slider,</span>
<span id="cb1-81">            range_slider,</span>
<span id="cb1-82">            radio,</span>
<span id="cb1-83">            dropdown,</span>
<span id="cb1-84">            multiselect,</span>
<span id="cb1-85">            text,</span>
<span id="cb1-86">            text_area,</span>
<span id="cb1-87">        ]</span>
<span id="cb1-88">    )</span>
<span id="cb1-89"></span>
<span id="cb1-90">    col_widget_link <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb1-91">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Switch](https://docs.marimo.io/api/inputs/switch/)"</span>),</span>
<span id="cb1-92">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[CheckBox](https://docs.marimo.io/api/inputs/checkbox/)"</span>),</span>
<span id="cb1-93">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Date](https://docs.marimo.io/api/inputs/dates/)"</span>),</span>
<span id="cb1-94">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Run Button](https://docs.marimo.io/api/inputs/run_button/)"</span>),</span>
<span id="cb1-95">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Button](https://docs.marimo.io/api/inputs/button/)"</span>),</span>
<span id="cb1-96">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Number](https://docs.marimo.io/api/inputs/number/)"</span>),</span>
<span id="cb1-97">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Slider](https://docs.marimo.io/api/inputs/slider/)"</span>),</span>
<span id="cb1-98">        mo.md(</span>
<span id="cb1-99">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Range Slider](https://docs.marimo.io/api/inputs/range_slider/)"</span></span>
<span id="cb1-100">        ),</span>
<span id="cb1-101">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Radio](https://docs.marimo.io/api/inputs/radio/)"</span>),</span>
<span id="cb1-102">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Dropdown](https://docs.marimo.io/api/inputs/dropdown/)"</span>),</span>
<span id="cb1-103">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[MultiSelect](https://docs.marimo.io/api/inputs/multiselect/)"</span>),</span>
<span id="cb1-104">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Text](https://docs.marimo.io/api/inputs/text/)"</span>),</span>
<span id="cb1-105">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Text Area](https://docs.marimo.io/api/inputs/text_area/)"</span>),</span>
<span id="cb1-106">    ]</span>
<span id="cb1-107"></span>
<span id="cb1-108">    col_code <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb1-109">        mo.accordion(</span>
<span id="cb1-110">            {</span>
<span id="cb1-111">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"switch = mo.ui.switch()"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`switch(value: bool = False, *, label: str = '', disabled: bool = False, on_change: Optional[Callable[[bool], None]] = None)`"</span></span>
<span id="cb1-112">            }</span>
<span id="cb1-113">        ),</span>
<span id="cb1-114">        mo.accordion(</span>
<span id="cb1-115">            {</span>
<span id="cb1-116">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'checkbox = mo.ui.checkbox(label="check me")'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`checkbox(value: bool = False, *, label: str = '', disabled: bool = False, on_change: Optional[Callable[[bool], None]] = None)`"</span></span>
<span id="cb1-117">            }</span>
<span id="cb1-118">        ),</span>
<span id="cb1-119">        mo.accordion(</span>
<span id="cb1-120">            {</span>
<span id="cb1-121">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"date = mo.ui.date()"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`date(start: Optional[date | str] = None, stop: Optional[date | str] = None, value: Optional[date | str] = None, *, label: str = '', on_change: Optional[Callable[[date], None]] = None, full_width: bool = False, disabled: bool = False)`"</span></span>
<span id="cb1-122">            }</span>
<span id="cb1-123">        ),</span>
<span id="cb1-124">        mo.accordion(</span>
<span id="cb1-125">            {</span>
<span id="cb1-126">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'run_botton = mo.ui.run_button(label="Run")'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`run_button(kind: Literal['neutral', 'success', 'warn', 'danger'] = 'neutral', disabled: bool = False, tooltip: Optional[str] = None, *, label: str = 'click to run', on_change: Optional[Callable[[Any], None]] = None, full_width: bool = False, keyboard_shortcut: Optional[str] = None)`"</span>,</span>
<span id="cb1-127">            }</span>
<span id="cb1-128">        ),</span>
<span id="cb1-129">        mo.accordion(</span>
<span id="cb1-130">            {</span>
<span id="cb1-131">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'button = mo.ui.button(value=0, on_click=lambda value: value + 1, label="increment")'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`button(on_click: Optional[Callable[[Any], Any]] = None, value: Optional[Any] = None, kind: Literal['neutral', 'success', 'warn', 'danger'] = 'neutral', disabled: bool = False, tooltip: Optional[str] = None, *, label: str = 'click here', on_change: Optional[Callable[[Any], None]] = None, full_width: bool = False, keyboard_shortcut: Optional[str] = None)`"</span></span>
<span id="cb1-132">            }</span>
<span id="cb1-133">        ),</span>
<span id="cb1-134">        mo.accordion(</span>
<span id="cb1-135">            {</span>
<span id="cb1-136">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"number = mo.ui.number(1, 10)"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`number(start: Optional[float] = None, stop: Optional[float] = None, step: Optional[float] = None, value: Optional[float] = None, debounce: bool = False, *, label: str = '', on_change: Optional[Callable[[Optional[Numeric]], None]] = None, full_width: bool = False, disabled: bool = False)`"</span></span>
<span id="cb1-137">            }</span>
<span id="cb1-138">        ),</span>
<span id="cb1-139">        mo.accordion(</span>
<span id="cb1-140">            {</span>
<span id="cb1-141">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"slider = mo.ui.slider(1, 10, 1)"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`slider(start: Optional[Numeric] = None, stop: Optional[Numeric] = None, step: Optional[Numeric] = None, value: Optional[Numeric] = None, debounce: bool = False, disabled: bool = False, orientation: Literal['horizontal', 'vertical'] = 'horizontal', show_value: bool = False, include_input: bool = False, steps: Optional[Sequence[Numeric]] = None, *, label: str = '', on_change: Optional[Callable[[Optional[Numeric]], None]] = None, full_width: bool = False)`"</span></span>
<span id="cb1-142">            }</span>
<span id="cb1-143">        ),</span>
<span id="cb1-144">        mo.accordion(</span>
<span id="cb1-145">            {</span>
<span id="cb1-146">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"range_slider = mo.ui.range_slider(1, 10, 2, value=[2, 6])"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`range_slider(start: Optional[Numeric] = None, stop: Optional[Numeric] = None, step: Optional[Numeric] = None, value: Optional[Sequence[Numeric]] = None, debounce: bool = False, orientation: Literal['horizontal', 'vertical'] = 'horizontal', show_value: bool = False, steps: Optional[Sequence[Numeric]] = None, *, label: str = '', on_change: Optional[Callable[[Sequence[Numeric]], None]] = None, full_width: bool = False, disabled: bool = False)`"</span></span>
<span id="cb1-147">            }</span>
<span id="cb1-148">        ),</span>
<span id="cb1-149">        mo.accordion(</span>
<span id="cb1-150">            {</span>
<span id="cb1-151">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'radio = mo.ui.radio(options=["Apples", "Oranges"], value="Apples")'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`radio(options: Sequence[str] | dict[str, Any], value: Optional[str] = None, inline: bool = False, *, label: str = '', on_change: Optional[Callable[[Any], None]] = None, disabled: bool = False)`"</span></span>
<span id="cb1-152">            }</span>
<span id="cb1-153">        ),</span>
<span id="cb1-154">        mo.accordion(</span>
<span id="cb1-155">            {</span>
<span id="cb1-156">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dropdown = mo.ui.dropdown(options=["Apples", "Oranges"], value="Apples")'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`dropdown(options: Sequence[Any] | dict[str, Any], value: Optional[Any] = None, allow_select_none: Optional[bool] = None, searchable: bool = False, *, label: str = '', on_change: Optional[Callable[[Any], None]] = None, full_width: bool = False)`"</span></span>
<span id="cb1-157">            }</span>
<span id="cb1-158">        ),</span>
<span id="cb1-159">        mo.accordion(</span>
<span id="cb1-160">            {</span>
<span id="cb1-161">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'multiselect = mo.ui.multiselect(options=["Apples", "Oranges"])'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`multiselect(options: Sequence[Any] | dict[str, Any], value: Optional[Sequence[Any]] = None, *, label: str = '', on_change: Optional[Callable[[list[object]], None]] = None, full_width: bool = False, max_selections: Optional[int] = None)`"</span></span>
<span id="cb1-162">            }</span>
<span id="cb1-163">        ),</span>
<span id="cb1-164">        mo.accordion(</span>
<span id="cb1-165">            {</span>
<span id="cb1-166">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text = mo.ui.text(placeholder="placeholder...", debounce=False)'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`text(value: str = '', placeholder: str = '', kind: Literal['text', 'password', 'email', 'url'] = 'text', max_length: Optional[int] = None, disabled: bool = False, debounce: bool | int = True, *, label: str = '', on_change: Optional[Callable[[str], None]] = None, full_width: bool = False)`"</span></span>
<span id="cb1-167">            }</span>
<span id="cb1-168">        ),</span>
<span id="cb1-169">        mo.accordion(</span>
<span id="cb1-170">            {</span>
<span id="cb1-171">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text_area = mo.ui.text_area(placeholder="placeholder...", debounce=False)'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`text_area(value: str = '', placeholder: str = '', max_length: Optional[int] = None, disabled: bool = False, debounce: bool | int = True, rows: Optional[int] = None, *, label: str = '', on_change: Optional[Callable[[str], None]] = None, full_width: bool = False)`"</span></span>
<span id="cb1-172">            }</span>
<span id="cb1-173">        ),</span>
<span id="cb1-174">    ]</span>
<span id="cb1-175"></span>
<span id="cb1-176">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># table styling</span></span>
<span id="cb1-177">    _style_number_start, _style_number_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb1-178">    style_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.slider(</span>
<span id="cb1-179">        _style_number_start,</span>
<span id="cb1-180">        _style_number_end,</span>
<span id="cb1-181">        value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>random.randint(_style_number_start, _style_number_end),</span>
<span id="cb1-182">        label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Style Number"</span>,</span>
<span id="cb1-183">    )</span>
<span id="cb1-184"></span>
<span id="cb1-185">    _colors <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cyan"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray"</span>]</span>
<span id="cb1-186">    color_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.radio(</span>
<span id="cb1-187">        options<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>_colors,</span>
<span id="cb1-188">        value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>random.choice(_colors),</span>
<span id="cb1-189">        label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Style Color"</span>,</span>
<span id="cb1-190">        inline<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>,</span>
<span id="cb1-191">    )</span>
<span id="cb1-192">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> col_code, col_widget_link, color_widget, style_widget, widgets</span>
<span id="cb1-193"></span>
<span id="cb1-194"></span>
<span id="cb1-195"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-196"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(</span>
<span id="cb1-197">    col_code,</span>
<span id="cb1-198">    col_widget_link,</span>
<span id="cb1-199">    pd,</span>
<span id="cb1-200">    render_widgets,</span>
<span id="cb1-201">    strify_widget_values,</span>
<span id="cb1-202">    widgets,</span>
<span id="cb1-203">):</span>
<span id="cb1-204">    col_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> render_widgets(widgets)</span>
<span id="cb1-205">    col_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> strify_widget_values(widgets)</span>
<span id="cb1-206"></span>
<span id="cb1-207">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-208">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"link"</span>: col_widget_link,</span>
<span id="cb1-209">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"widget"</span>: col_widget,</span>
<span id="cb1-210">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span>: col_value,</span>
<span id="cb1-211">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"code"</span>: col_code,</span>
<span id="cb1-212">    }</span>
<span id="cb1-213"></span>
<span id="cb1-214">    df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(data)</span>
<span id="cb1-215">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (df,)</span>
<span id="cb1-216"></span>
<span id="cb1-217"></span>
<span id="cb1-218"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-219"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(GT, color_widget, df, html, style_widget):</span>
<span id="cb1-220">    gt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-221">        GT(df)</span>
<span id="cb1-222">        .cols_align(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>)</span>
<span id="cb1-223">        .opt_all_caps()</span>
<span id="cb1-224">        .tab_header(html(style_widget), html(color_widget))</span>
<span id="cb1-225">    )</span>
<span id="cb1-226">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (gt,)</span>
<span id="cb1-227"></span>
<span id="cb1-228"></span>
<span id="cb1-229"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-230"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(color_widget, gt, style_widget):</span>
<span id="cb1-231">    (</span>
<span id="cb1-232">        gt.opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style_widget.value, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>color_widget.value)</span>
<span id="cb1-233">        .opt_align_table_header(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>)</span>
<span id="cb1-234">        .cols_width({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"widget"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"20%"</span>})</span>
<span id="cb1-235">    )</span>
<span id="cb1-236">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb1-237"></span>
<span id="cb1-238"></span>
<span id="cb1-239"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb1-240">    app.run()</span></code></pre></div></div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>python</category>
  <category>marimo</category>
  <category>gt</category>
  <guid>https://tech.ycwu.space/posts/marimo-gt-ui-reference/20250704.html</guid>
  <pubDate>Fri, 04 Jul 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/marimo-gt-ui-reference/marimo_gt_ui_refrence.png" medium="image" type="image/png" height="124" width="144"/>
</item>
<item>
  <title>Time Machine for Great Tables in marimo</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/marimo-gt-time-machine/20250626.html</link>
  <description><![CDATA[ 






<p>This post demonstrates how to build a “time machine” that lets you easily navigate through different stages of table construction using Great Tables in a marimo notebook.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/marimo-gt-time-machine/marimo_gt_time_machine.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Time Machine for Great Tables"></p>
</figure>
</div>
<section id="building-the-time-machine-core" class="level2">
<h2 class="anchored" data-anchor-id="building-the-time-machine-core">Building the Time Machine Core</h2>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Give It a Sec – WASM Magic Happening
</div>
</div>
<div class="callout-body-container callout-body">
<p>The widgets may take a few moments to load, as they rely on WebAssembly under the hood.</p>
</div>
</div>
<p>The goal is to create a <code>WigGT</code> object that behaves like a <code>GT</code> object from Great Tables. As we progressively build the table, we maintain two versions:</p>
<ul>
<li>The original (non-interactive) table</li>
<li>An interactive version that includes a marimo widget (in this case, a slider)</li>
</ul>
<p>This setup allows us to move back and forth through the table-building steps and see how each method transforms the output — effectively creating a table “time machine.”</p>
<p>Let’s break it down.</p>
<marimo-island data-app-id="main" data-cell-id="Hbol" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">import%20marimo%20as%20mo%0Aimport%20copy%0Aimport%20inspect%0Afrom%20functools%20import%20wraps%0Afrom%20typing%20import%20Callable%2C%20Self%0A%0Aimport%20pandas%20as%20pd%0Afrom%20great_tables%20import%20GT%2C%20html%0Afrom%20great_tables.data%20import%20airquality</marimo-cell-code>
</marimo-island>
<section id="getting-the-allowed-method-names" class="level3">
<h3 class="anchored" data-anchor-id="getting-the-allowed-method-names">Getting the Allowed Method Names</h3>
<p>First, we need a list of Great Tables methods that should be wrapped and recorded. For now, this list is constructed manually, but ideally, Great Tables could expose this for easier third-party use.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_allowed_member_names() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>]:</span>
<span id="cb1-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_number"</span>, ...]</span></code></pre></div></div>
<marimo-island data-app-id="main" data-cell-id="MJUe" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">def%20get_allowed_member_names()%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20return%20%5B%0A%20%20%20%20%20%20%20%20%22fmt%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_number%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_integer%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_percent%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_scientific%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_currency%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_bytes%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_roman%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_date%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_time%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_datetime%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_markdown%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_image%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_icon%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_flag%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_units%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_nanoplot%22%2C%0A%20%20%20%20%20%20%20%20%22data_color%22%2C%0A%20%20%20%20%20%20%20%20%22sub_missing%22%2C%0A%20%20%20%20%20%20%20%20%22sub_zero%22%2C%0A%20%20%20%20%20%20%20%20%22opt_stylize%22%2C%0A%20%20%20%20%20%20%20%20%22opt_align_table_header%22%2C%0A%20%20%20%20%20%20%20%20%22opt_all_caps%22%2C%0A%20%20%20%20%20%20%20%20%22opt_footnote_marks%22%2C%0A%20%20%20%20%20%20%20%20%22opt_row_striping%22%2C%0A%20%20%20%20%20%20%20%20%22opt_vertical_padding%22%2C%0A%20%20%20%20%20%20%20%20%22opt_horizontal_padding%22%2C%0A%20%20%20%20%20%20%20%20%22opt_table_outline%22%2C%0A%20%20%20%20%20%20%20%20%22opt_table_font%22%2C%0A%20%20%20%20%20%20%20%20%22cols_align%22%2C%0A%20%20%20%20%20%20%20%20%22cols_width%22%2C%0A%20%20%20%20%20%20%20%20%22cols_label%22%2C%0A%20%20%20%20%20%20%20%20%22cols_move%22%2C%0A%20%20%20%20%20%20%20%20%22cols_move_to_start%22%2C%0A%20%20%20%20%20%20%20%20%22cols_move_to_end%22%2C%0A%20%20%20%20%20%20%20%20%22cols_hide%22%2C%0A%20%20%20%20%20%20%20%20%22cols_unhide%22%2C%0A%20%20%20%20%20%20%20%20%22tab_header%22%2C%0A%20%20%20%20%20%20%20%20%22tab_source_note%22%2C%0A%20%20%20%20%20%20%20%20%22tab_spanner%22%2C%0A%20%20%20%20%20%20%20%20%22tab_stubhead%22%2C%0A%20%20%20%20%20%20%20%20%22tab_style%22%2C%0A%20%20%20%20%20%20%20%20%22tab_options%22%2C%0A%20%20%20%20%20%20%20%20%22row_group_order%22%2C%0A%20%20%20%20%20%20%20%20%22tab_stub%22%2C%0A%20%20%20%20%20%20%20%20%22with_id%22%2C%0A%20%20%20%20%20%20%20%20%22with_locale%22%2C%0A%20%20%20%20%20%20%20%20%22save%22%2C%0A%20%20%20%20%20%20%20%20%22show%22%2C%0A%20%20%20%20%20%20%20%20%22as_raw_html%22%2C%0A%20%20%20%20%20%20%20%20%22write_raw_html%22%2C%0A%20%20%20%20%20%20%20%20%22as_latex%22%2C%0A%20%20%20%20%20%20%20%20%22pipe%22%2C%0A%20%20%20%20%5D</marimo-cell-code>
</marimo-island>
</section>
<section id="the-lazify-decorator" class="level3">
<h3 class="anchored" data-anchor-id="the-lazify-decorator">The <code>lazify</code> Decorator</h3>
<p>The <code>lazify</code> decorator wraps each method call and stores it in a pipeline. This allows us to defer execution until we’re ready to materialize the final table using <code>.collect()</code>. Only the selected methods from Great Tables will be decorated.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> lazify(cls: GT) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> GT:</span>
<span id="cb2-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> add_to_pipeline(func: Callable[..., GT]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">callable</span>:</span>
<span id="cb2-3">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@wraps</span>(func)</span>
<span id="cb2-4">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> wrapper(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs):</span>
<span id="cb2-5">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> inner(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb2-6">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> func(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs)</span>
<span id="cb2-7"></span>
<span id="cb2-8">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># container for storing callable objects</span></span>
<span id="cb2-9">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline.append(inner)</span>
<span id="cb2-10">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb2-11"></span>
<span id="cb2-12">        wrapper.__signature__ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> inspect.signature(func)</span>
<span id="cb2-13">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> wrapper</span>
<span id="cb2-14"></span>
<span id="cb2-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> member_name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> get_allowed_member_names():</span>
<span id="cb2-16">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">setattr</span>(</span>
<span id="cb2-17">            cls, member_name, add_to_pipeline(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(GT, member_name))</span>
<span id="cb2-18">        )</span>
<span id="cb2-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> cls</span></code></pre></div></div>
<marimo-island data-app-id="main" data-cell-id="vblA" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">def%20lazify(cls%3A%20GT)%20-%3E%20GT%3A%0A%20%20%20%20def%20add_to_pipeline(func%3A%20Callable%5B...%2C%20GT%5D)%20-%3E%20callable%3A%0A%20%20%20%20%20%20%20%20%40wraps(func)%0A%20%20%20%20%20%20%20%20def%20wrapper(self%2C%20*args%2C%20**kwargs)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20def%20inner(self)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20func(self%2C%20*args%2C%20**kwargs)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20container%20for%20storing%20callable%20objects%0A%20%20%20%20%20%20%20%20%20%20%20%20self._pipeline.append(inner)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20self%0A%0A%20%20%20%20%20%20%20%20wrapper.__signature__%20%3D%20inspect.signature(func)%0A%20%20%20%20%20%20%20%20return%20wrapper%0A%0A%20%20%20%20for%20member_name%20in%20get_allowed_member_names()%3A%0A%20%20%20%20%20%20%20%20setattr(%0A%20%20%20%20%20%20%20%20%20%20%20%20cls%2C%20member_name%2C%20add_to_pipeline(getattr(GT%2C%20member_name))%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20cls</marimo-cell-code>
</marimo-island>
</section>
<section id="designing-the-wiggt-class" class="level3">
<h3 class="anchored" data-anchor-id="designing-the-wiggt-class">Designing the <code>WigGT</code> Class</h3>
<p>Here are the main ideas behind the design:</p>
<ol type="1">
<li>Original tables are stored in <code>self._tables</code>, accessible via the <code>.tables</code> property.</li>
<li>Interactive versions with embedded marimo widgets are stored in <code>self._wtables</code>. These aren’t exposed directly — interaction is done through the marimo UI.</li>
<li>The rendering method follows <a href="https://docs.marimo.io/guides/integrating_with_marimo/displaying_objects/">marimo’s convention</a>, trying <code>_display_</code>, then <code>_repr_html_</code>, and finally <code>_mime_</code>, in that order.</li>
</ol>
<p>With this setup, <code>WigGT</code> tracks the full transformation pipeline and allows easy replay of each step through a slider.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@lazify</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> WigGT:</span>
<span id="cb3-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, widget, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs):</span>
<span id="cb3-4">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._args <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> args</span>
<span id="cb3-5">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> widget</span>
<span id="cb3-6">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._kwargs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> kwargs</span>
<span id="cb3-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.set_to_init()</span>
<span id="cb3-8"></span>
<span id="cb3-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@property</span></span>
<span id="cb3-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> tables(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[GT]:</span>
<span id="cb3-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># return a new list</span></span>
<span id="cb3-12"></span>
<span id="cb3-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _widgetify(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, obj: GT) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> GT:</span>
<span id="cb3-14">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> obj.tab_source_note(html(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widget))</span>
<span id="cb3-15"></span>
<span id="cb3-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> collect(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Self:</span>
<span id="cb3-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._is_collect:</span>
<span id="cb3-18">            new_obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># don't use self._wtables[0]</span></span>
<span id="cb3-19">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline:</span>
<span id="cb3-20">                new_obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>copy.copy(new_obj))</span>
<span id="cb3-21">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables.append(new_obj)</span>
<span id="cb3-22">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables.append(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widgetify(new_obj))</span>
<span id="cb3-23">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._is_collect <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb3-24">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb3-25"></span>
<span id="cb3-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _repr_html_(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb3-27">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb3-28">            obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables[<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widget.value]</span>
<span id="cb3-29">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">IndexError</span>:</span>
<span id="cb3-30">            obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-31"></span>
<span id="cb3-32">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_display_"</span>):</span>
<span id="cb3-33">            render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_display_"</span></span>
<span id="cb3-34">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_repr_html_"</span>):</span>
<span id="cb3-35">            render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_repr_html_"</span></span>
<span id="cb3-36">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_mime_"</span>):</span>
<span id="cb3-37">            render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_mime_"</span></span>
<span id="cb3-38">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb3-39">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">AttributeError</span>(</span>
<span id="cb3-40">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The object does not have a valid render method."</span></span>
<span id="cb3-41">            )</span>
<span id="cb3-42">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(obj, render_method)()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remember to invoke</span></span>
<span id="cb3-43"></span>
<span id="cb3-44">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> set_to_init(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb3-45">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_pipeline"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb3-46">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">callable</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb3-47">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb3-48">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline.clear()</span>
<span id="cb3-49"></span>
<span id="cb3-50">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_tables"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb3-51">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb3-52">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb3-53">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables.clear()</span>
<span id="cb3-54"></span>
<span id="cb3-55">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_wtables"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb3-56">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb3-57">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb3-58">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables.clear()</span>
<span id="cb3-59"></span>
<span id="cb3-60">        obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._make_gt()</span>
<span id="cb3-61">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables.append(obj)</span>
<span id="cb3-62">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables.append(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widgetify(obj))</span>
<span id="cb3-63">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._is_collect <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb3-64"></span>
<span id="cb3-65">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _make_gt(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> GT:</span>
<span id="cb3-66">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> GT(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._kwargs)</span></code></pre></div></div>
<marimo-island data-app-id="main" data-cell-id="bkHC" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">%40lazify%0Aclass%20WigGT%3A%0A%20%20%20%20def%20__init__(self%2C%20*args%2C%20widget%2C%20**kwargs)%3A%0A%20%20%20%20%20%20%20%20self._args%20%3D%20args%0A%20%20%20%20%20%20%20%20self._widget%20%3D%20widget%0A%20%20%20%20%20%20%20%20self._kwargs%20%3D%20kwargs%0A%20%20%20%20%20%20%20%20self.set_to_init()%0A%0A%20%20%20%20%40property%0A%20%20%20%20def%20tables(self)%20-%3E%20list%5BGT%5D%3A%0A%20%20%20%20%20%20%20%20return%20list(self._tables)%20%20%23%20return%20a%20new%20list%0A%0A%20%20%20%20def%20_widgetify(self%2C%20obj%3A%20GT)%20-%3E%20GT%3A%0A%20%20%20%20%20%20%20%20return%20obj.tab_source_note(html(self._widget))%0A%0A%20%20%20%20def%20collect(self)%20-%3E%20Self%3A%0A%20%20%20%20%20%20%20%20if%20not%20self._is_collect%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20new_obj%20%3D%20self._tables%5B0%5D%20%20%23%20don't%20use%20self._wtables%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20f%20in%20self._pipeline%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20new_obj%20%3D%20f(self%3Dcopy.copy(new_obj))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self._tables.append(new_obj)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self._wtables.append(self._widgetify(new_obj))%0A%20%20%20%20%20%20%20%20%20%20%20%20self._is_collect%20%3D%20True%0A%20%20%20%20%20%20%20%20return%20self%0A%0A%20%20%20%20def%20_repr_html_(self)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20obj%20%3D%20self._wtables%5Bself._widget.value%5D%0A%20%20%20%20%20%20%20%20except%20IndexError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20obj%20%3D%20self._wtables%5B-1%5D%0A%0A%20%20%20%20%20%20%20%20if%20hasattr(obj%2C%20%22_display_%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20render_method%20%3D%20%22_display_%22%0A%20%20%20%20%20%20%20%20elif%20hasattr(obj%2C%20%22_repr_html_%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20render_method%20%3D%20%22_repr_html_%22%0A%20%20%20%20%20%20%20%20elif%20hasattr(obj%2C%20%22_mime_%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20render_method%20%3D%20%22_mime_%22%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20AttributeError(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22The%20object%20does%20not%20have%20a%20valid%20render%20method.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20return%20getattr(obj%2C%20render_method)()%20%20%23%20remember%20to%20invoke%0A%0A%20%20%20%20def%20set_to_init(self)%20-%3E%20None%3A%0A%20%20%20%20%20%20%20%20if%20not%20getattr(self%2C%20%22_pipeline%22%2C%20None)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._pipeline%3A%20list%5Bcallable%5D%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._pipeline.clear()%0A%0A%20%20%20%20%20%20%20%20if%20not%20getattr(self%2C%20%22_tables%22%2C%20None)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._tables%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._tables.clear()%0A%0A%20%20%20%20%20%20%20%20if%20not%20getattr(self%2C%20%22_wtables%22%2C%20None)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._wtables%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._wtables.clear()%0A%0A%20%20%20%20%20%20%20%20obj%20%3D%20self._make_gt()%0A%20%20%20%20%20%20%20%20self._tables.append(obj)%0A%20%20%20%20%20%20%20%20self._wtables.append(self._widgetify(obj))%0A%20%20%20%20%20%20%20%20self._is_collect%20%3D%20False%0A%0A%20%20%20%20def%20_make_gt(self)%20-%3E%20GT%3A%0A%20%20%20%20%20%20%20%20return%20GT(*self._args%2C%20**self._kwargs)</marimo-cell-code>
</marimo-island>
</section>
</section>
<section id="marimo" class="level2">
<h2 class="anchored" data-anchor-id="marimo">marimo</h2>
<p>Now we’re ready to <strong>time travel</strong> through the table-building process!</p>
<section id="create-a-slider-to-navigate-table-states" class="level3">
<h3 class="anchored" data-anchor-id="create-a-slider-to-navigate-table-states">Create a Slider to Navigate Table States</h3>
<p>We begin by creating a slider that represents each step in the build pipeline.</p>
<ul>
<li><code>Step 0</code> is the original <code>GT</code> object.</li>
<li><code>Step 1</code> applies the first method call, and so on.</li>
</ul>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Slider Range Limitation
</div>
</div>
<div class="callout-body-container callout-body">
<p>Because all method calls are deferred, we can’t determine the total number of steps ahead of time. This means the developer needs to manually specify a reasonable range for the slider. If the selected index exceeds the available range, the last table will be shown by default.</p>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">time_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.slider(start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, stop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, step<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Step"</span>)</span></code></pre></div></div>
<marimo-island data-app-id="main" data-cell-id="lEQa" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">time_widget%20%3D%20mo.ui.slider(start%3D0%2C%20stop%3D6%2C%20step%3D1%2C%20value%3D0%2C%20label%3D%22Step%22)</marimo-cell-code>
</marimo-island>
</section>
<section id="build-the-lazy-table-sequence" class="level3">
<h3 class="anchored" data-anchor-id="build-the-lazy-table-sequence">Build the Lazy Table Sequence</h3>
<p>Next, we use an example from the <a href="https://posit-dev.github.io/great-tables/examples/">Great Tables documentation</a> to create a lazy <code>WigGT</code> object. This will internally store each method call but won’t execute them just yet.</p>
<p>You’ll see a slider widget embedded in the table’s source note — but it won’t be functional until we finalize the process.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">lazy_wig_gt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb5-2">    WigGT(airquality.head(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>).assign(Year<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973</span>), widget<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>time_widget)</span>
<span id="cb5-3">    .opt_stylize(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>, style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb5-4">    .tab_header(</span>
<span id="cb5-5">        title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"New York Air Quality Measurements"</span>,</span>
<span id="cb5-6">        subtitle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Daily measurements in New York City (May 1-10, 1973)"</span>,</span>
<span id="cb5-7">    )</span>
<span id="cb5-8">    .tab_spanner(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time"</span>, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Month"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Day"</span>])</span>
<span id="cb5-9">    .tab_spanner(</span>
<span id="cb5-10">        label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Measurement"</span>, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ozone"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Solar_R"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wind"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temp"</span>]</span>
<span id="cb5-11">    )</span>
<span id="cb5-12">    .cols_move_to_start(columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Month"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Day"</span>])</span>
<span id="cb5-13">    .cols_label(</span>
<span id="cb5-14">        Ozone<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ozone,&lt;br&gt;ppbV"</span>),</span>
<span id="cb5-15">        Solar_R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Solar R.,&lt;br&gt;cal/m&lt;sup&gt;2&lt;/sup&gt;"</span>),</span>
<span id="cb5-16">        Wind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wind,&lt;br&gt;mph"</span>),</span>
<span id="cb5-17">        Temp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temp,&lt;br&gt;&amp;deg;F"</span>),</span>
<span id="cb5-18">    )</span>
<span id="cb5-19">)</span>
<span id="cb5-20">lazy_wig_gt</span></code></pre></div></div>
<marimo-island data-app-id="main" data-cell-id="PKri" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">lazy_wig_gt%20%3D%20(%0A%20%20%20%20WigGT(airquality.head(10).assign(Year%3D1973)%2C%20widget%3Dtime_widget)%0A%20%20%20%20.opt_stylize(color%3D%22pink%22%2C%20style%3D2)%0A%20%20%20%20.tab_header(%0A%20%20%20%20%20%20%20%20title%3D%22New%20York%20Air%20Quality%20Measurements%22%2C%0A%20%20%20%20%20%20%20%20subtitle%3D%22Daily%20measurements%20in%20New%20York%20City%20(May%201-10%2C%201973)%22%2C%0A%20%20%20%20)%0A%20%20%20%20.tab_spanner(label%3D%22Time%22%2C%20columns%3D%5B%22Year%22%2C%20%22Month%22%2C%20%22Day%22%5D)%0A%20%20%20%20.tab_spanner(%0A%20%20%20%20%20%20%20%20label%3D%22Measurement%22%2C%20columns%3D%5B%22Ozone%22%2C%20%22Solar_R%22%2C%20%22Wind%22%2C%20%22Temp%22%5D%0A%20%20%20%20)%0A%20%20%20%20.cols_move_to_start(columns%3D%5B%22Year%22%2C%20%22Month%22%2C%20%22Day%22%5D)%0A%20%20%20%20.cols_label(%0A%20%20%20%20%20%20%20%20Ozone%3Dhtml(%22Ozone%2C%3Cbr%3EppbV%22)%2C%0A%20%20%20%20%20%20%20%20Solar_R%3Dhtml(%22Solar%20R.%2C%3Cbr%3Ecal%2Fm%3Csup%3E2%3C%2Fsup%3E%22)%2C%0A%20%20%20%20%20%20%20%20Wind%3Dhtml(%22Wind%2C%3Cbr%3Emph%22)%2C%0A%20%20%20%20%20%20%20%20Temp%3Dhtml(%22Temp%2C%3Cbr%3E%26deg%3BF%22)%2C%0A%20%20%20%20)%0A)%0Alazy_wig_gt</marimo-cell-code>
</marimo-island>
</section>
<section id="trigger-execution-with-.collect" class="level3">
<h3 class="anchored" data-anchor-id="trigger-execution-with-.collect">Trigger Execution with <code>.collect()</code></h3>
<p>To make the slider work, we call <code>.collect()</code> on the <code>WigGT</code> instance. This executes the stored methods and generates a list of tables, one for each step.</p>
<p>Once collected, you can interactively slide through each version of the table!</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">wig_gt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lazy_wig_gt.collect()</span>
<span id="cb6-2">wig_gt</span></code></pre></div></div>
<marimo-island data-app-id="main" data-cell-id="Xref" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">wig_gt%20%3D%20lazy_wig_gt.collect()%0Awig_gt</marimo-cell-code>
</marimo-island>
</section>
<section id="access-all-built-tables" class="level3">
<h3 class="anchored" data-anchor-id="access-all-built-tables">Access All Built Tables</h3>
<p>Finally, you can retrieve all constructed tables using the <code>.tables</code> property, which returns a list of <code>GT</code> objects.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">wig_gt.tables</span></code></pre></div></div>
</section>
<section id="full-marimo-code" class="level3">
<h3 class="anchored" data-anchor-id="full-marimo-code">Full marimo code</h3>
<p>Check out the full marimo code below or view it on <a href="https://molab.marimo.io/notebooks/nb_FfAE5bVJ9P6DWDDy4Gng2h/app">molab</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo</span>
<span id="cb8-2"></span>
<span id="cb8-3">__generated_with <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.14.8"</span></span>
<span id="cb8-4">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> marimo.App(width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>)</span>
<span id="cb8-5"></span>
<span id="cb8-6"></span>
<span id="cb8-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb8-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb8-9">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> mo</span>
<span id="cb8-10"></span>
<span id="cb8-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (mo,)</span>
<span id="cb8-12"></span>
<span id="cb8-13"></span>
<span id="cb8-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb8-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb8-16">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> copy</span>
<span id="cb8-17">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> inspect</span>
<span id="cb8-18">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> functools <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> wraps</span>
<span id="cb8-19">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Callable, Self</span>
<span id="cb8-20"></span>
<span id="cb8-21">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb8-22">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, html</span>
<span id="cb8-23">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables.data <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> airquality</span>
<span id="cb8-24"></span>
<span id="cb8-25">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> Callable, GT, Self, airquality, copy, html, inspect, wraps</span>
<span id="cb8-26"></span>
<span id="cb8-27"></span>
<span id="cb8-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb8-29"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(Callable, GT, Self, copy, html, inspect, wraps):</span>
<span id="cb8-30">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_allowed_member_names() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>]:</span>
<span id="cb8-31">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb8-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Manually constructing the list —</span></span>
<span id="cb8-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        it would be great if Great Tables exposed the available method names.</span></span>
<span id="cb8-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb8-35">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [</span>
<span id="cb8-36">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt"</span>,</span>
<span id="cb8-37">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_number"</span>,</span>
<span id="cb8-38">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_integer"</span>,</span>
<span id="cb8-39">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_percent"</span>,</span>
<span id="cb8-40">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_scientific"</span>,</span>
<span id="cb8-41">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_currency"</span>,</span>
<span id="cb8-42">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_bytes"</span>,</span>
<span id="cb8-43">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_roman"</span>,</span>
<span id="cb8-44">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_date"</span>,</span>
<span id="cb8-45">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_time"</span>,</span>
<span id="cb8-46">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_datetime"</span>,</span>
<span id="cb8-47">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_markdown"</span>,</span>
<span id="cb8-48">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_image"</span>,</span>
<span id="cb8-49">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_icon"</span>,</span>
<span id="cb8-50">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_flag"</span>,</span>
<span id="cb8-51">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_units"</span>,</span>
<span id="cb8-52">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_nanoplot"</span>,</span>
<span id="cb8-53">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data_color"</span>,</span>
<span id="cb8-54">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sub_missing"</span>,</span>
<span id="cb8-55">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sub_zero"</span>,</span>
<span id="cb8-56">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_stylize"</span>,</span>
<span id="cb8-57">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_align_table_header"</span>,</span>
<span id="cb8-58">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_all_caps"</span>,</span>
<span id="cb8-59">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_footnote_marks"</span>,</span>
<span id="cb8-60">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_row_striping"</span>,</span>
<span id="cb8-61">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_vertical_padding"</span>,</span>
<span id="cb8-62">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_horizontal_padding"</span>,</span>
<span id="cb8-63">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_table_outline"</span>,</span>
<span id="cb8-64">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_table_font"</span>,</span>
<span id="cb8-65">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_align"</span>,</span>
<span id="cb8-66">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_width"</span>,</span>
<span id="cb8-67">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_label"</span>,</span>
<span id="cb8-68">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_move"</span>,</span>
<span id="cb8-69">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_move_to_start"</span>,</span>
<span id="cb8-70">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_move_to_end"</span>,</span>
<span id="cb8-71">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_hide"</span>,</span>
<span id="cb8-72">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_unhide"</span>,</span>
<span id="cb8-73">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tab_header"</span>,</span>
<span id="cb8-74">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tab_source_note"</span>,</span>
<span id="cb8-75">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tab_spanner"</span>,</span>
<span id="cb8-76">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tab_stubhead"</span>,</span>
<span id="cb8-77">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tab_style"</span>,</span>
<span id="cb8-78">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tab_options"</span>,</span>
<span id="cb8-79">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row_group_order"</span>,</span>
<span id="cb8-80">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tab_stub"</span>,</span>
<span id="cb8-81">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"with_id"</span>,</span>
<span id="cb8-82">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"with_locale"</span>,</span>
<span id="cb8-83">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"save"</span>,</span>
<span id="cb8-84">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"show"</span>,</span>
<span id="cb8-85">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"as_raw_html"</span>,</span>
<span id="cb8-86">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"write_raw_html"</span>,</span>
<span id="cb8-87">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"as_latex"</span>,</span>
<span id="cb8-88">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pipe"</span>,</span>
<span id="cb8-89">        ]</span>
<span id="cb8-90"></span>
<span id="cb8-91">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> lazify(cls: GT) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> GT:</span>
<span id="cb8-92">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> add_to_pipeline(func: Callable[..., GT]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">callable</span>:</span>
<span id="cb8-93">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@wraps</span>(func)</span>
<span id="cb8-94">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> wrapper(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs):</span>
<span id="cb8-95">                <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> inner(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb8-96">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> func(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs)</span>
<span id="cb8-97"></span>
<span id="cb8-98">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># container for storing callable objects</span></span>
<span id="cb8-99">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline.append(inner)</span>
<span id="cb8-100">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb8-101"></span>
<span id="cb8-102">            wrapper.__signature__ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> inspect.signature(func)</span>
<span id="cb8-103">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> wrapper</span>
<span id="cb8-104"></span>
<span id="cb8-105">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> member_name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> get_allowed_member_names():</span>
<span id="cb8-106">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">setattr</span>(</span>
<span id="cb8-107">                cls, member_name, add_to_pipeline(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(GT, member_name))</span>
<span id="cb8-108">            )</span>
<span id="cb8-109">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> cls</span>
<span id="cb8-110"></span>
<span id="cb8-111">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@lazify</span></span>
<span id="cb8-112">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> WigGT:</span>
<span id="cb8-113">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, widget, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs):</span>
<span id="cb8-114">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._args <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> args</span>
<span id="cb8-115">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> widget</span>
<span id="cb8-116">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._kwargs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> kwargs</span>
<span id="cb8-117">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.set_to_init()</span>
<span id="cb8-118"></span>
<span id="cb8-119">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@property</span></span>
<span id="cb8-120">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> tables(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[GT]:</span>
<span id="cb8-121">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># return a new list</span></span>
<span id="cb8-122"></span>
<span id="cb8-123">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _widgetify(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, obj: GT) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> GT:</span>
<span id="cb8-124">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> obj.tab_source_note(html(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widget))</span>
<span id="cb8-125"></span>
<span id="cb8-126">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> collect(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Self:</span>
<span id="cb8-127">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._is_collect:</span>
<span id="cb8-128">                new_obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># don't use self._wtables[0]</span></span>
<span id="cb8-129">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline:</span>
<span id="cb8-130">                    new_obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>copy.copy(new_obj))</span>
<span id="cb8-131">                    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables.append(new_obj)</span>
<span id="cb8-132">                    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables.append(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widgetify(new_obj))</span>
<span id="cb8-133">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._is_collect <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb8-134">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb8-135"></span>
<span id="cb8-136">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _repr_html_(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb8-137">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb8-138">                obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables[<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widget.value]</span>
<span id="cb8-139">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">IndexError</span>:</span>
<span id="cb8-140">                obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb8-141"></span>
<span id="cb8-142">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_display_"</span>):</span>
<span id="cb8-143">                render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_display_"</span></span>
<span id="cb8-144">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_repr_html_"</span>):</span>
<span id="cb8-145">                render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_repr_html_"</span></span>
<span id="cb8-146">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_mime_"</span>):</span>
<span id="cb8-147">                render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_mime_"</span></span>
<span id="cb8-148">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb8-149">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">AttributeError</span>(</span>
<span id="cb8-150">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The object does not have a valid render method."</span></span>
<span id="cb8-151">                )</span>
<span id="cb8-152">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(obj, render_method)()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remember to invoke</span></span>
<span id="cb8-153"></span>
<span id="cb8-154">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> set_to_init(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb8-155">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_pipeline"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb8-156">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">callable</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb8-157">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb8-158">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline.clear()</span>
<span id="cb8-159"></span>
<span id="cb8-160">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_tables"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb8-161">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb8-162">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb8-163">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables.clear()</span>
<span id="cb8-164"></span>
<span id="cb8-165">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_wtables"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb8-166">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb8-167">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb8-168">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables.clear()</span>
<span id="cb8-169"></span>
<span id="cb8-170">            obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._make_gt()</span>
<span id="cb8-171">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables.append(obj)</span>
<span id="cb8-172">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables.append(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widgetify(obj))</span>
<span id="cb8-173">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._is_collect <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb8-174"></span>
<span id="cb8-175">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _make_gt(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> GT:</span>
<span id="cb8-176">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> GT(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._kwargs)</span>
<span id="cb8-177"></span>
<span id="cb8-178">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (WigGT,)</span>
<span id="cb8-179"></span>
<span id="cb8-180"></span>
<span id="cb8-181"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb8-182"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(mo):</span>
<span id="cb8-183">    time_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.slider(start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, stop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, step<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Step"</span>)</span>
<span id="cb8-184">    time_widget</span>
<span id="cb8-185">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (time_widget,)</span>
<span id="cb8-186"></span>
<span id="cb8-187"></span>
<span id="cb8-188"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb8-189"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(WigGT, airquality, html, time_widget):</span>
<span id="cb8-190">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The lazy_wig_gt object is not interactive until collect() is called.</span></span>
<span id="cb8-191">    lazy_wig_gt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb8-192">        WigGT(airquality.head(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>).assign(Year<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973</span>), widget<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>time_widget)</span>
<span id="cb8-193">        .opt_stylize(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>, style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb8-194">        .tab_header(</span>
<span id="cb8-195">            title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"New York Air Quality Measurements"</span>,</span>
<span id="cb8-196">            subtitle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Daily measurements in New York City (May 1-10, 1973)"</span>,</span>
<span id="cb8-197">        )</span>
<span id="cb8-198">        .tab_spanner(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time"</span>, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Month"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Day"</span>])</span>
<span id="cb8-199">        .tab_spanner(</span>
<span id="cb8-200">            label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Measurement"</span>, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ozone"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Solar_R"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wind"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temp"</span>]</span>
<span id="cb8-201">        )</span>
<span id="cb8-202">        .cols_move_to_start(columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Month"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Day"</span>])</span>
<span id="cb8-203">        .cols_label(</span>
<span id="cb8-204">            Ozone<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ozone,&lt;br&gt;ppbV"</span>),</span>
<span id="cb8-205">            Solar_R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Solar R.,&lt;br&gt;cal/m&lt;sup&gt;2&lt;/sup&gt;"</span>),</span>
<span id="cb8-206">            Wind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wind,&lt;br&gt;mph"</span>),</span>
<span id="cb8-207">            Temp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temp,&lt;br&gt;&amp;deg;F"</span>),</span>
<span id="cb8-208">        )</span>
<span id="cb8-209">    )</span>
<span id="cb8-210">    lazy_wig_gt</span>
<span id="cb8-211">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (lazy_wig_gt,)</span>
<span id="cb8-212"></span>
<span id="cb8-213"></span>
<span id="cb8-214"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb8-215"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(lazy_wig_gt):</span>
<span id="cb8-216">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The wig_gt object is now interactive</span></span>
<span id="cb8-217">    wig_gt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lazy_wig_gt.collect()</span>
<span id="cb8-218">    wig_gt</span>
<span id="cb8-219">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (wig_gt,)</span>
<span id="cb8-220"></span>
<span id="cb8-221"></span>
<span id="cb8-222"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb8-223"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(wig_gt):</span>
<span id="cb8-224">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># retrieve all `gt` tables</span></span>
<span id="cb8-225">    wig_gt.tables</span>
<span id="cb8-226">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb8-227"></span>
<span id="cb8-228"></span>
<span id="cb8-229"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb8-230">    app.run()</span></code></pre></div></div>
</section>
</section>
<section id="in-closing" class="level2">
<h2 class="anchored" data-anchor-id="in-closing">In Closing</h2>
<p>marimo has turned out to be far more powerful than I expected — I’ve had a lot of fun experimenting with it.</p>
<p>I highly recommend checking out their <a href="https://www.youtube.com/@marimo-team">YouTube channel</a>, which is full of high-quality content across many Python topics.</p>
<p>This idea was heavily inspired by their videos — all credit goes to the amazing marimo team!</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>marimo</category>
  <category>gt</category>
  <guid>https://tech.ycwu.space/posts/marimo-gt-time-machine/20250626.html</guid>
  <pubDate>Thu, 26 Jun 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/marimo-gt-time-machine/marimo_gt_time_machine.png" medium="image" type="image/png" height="213" width="144"/>
</item>
<item>
  <title>Send Email via marimo</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/resend-via-marimo/20250625.html</link>
  <description><![CDATA[ 






<p>This post demonstrates how to send emails using <a href="https://resend.com/">Resend</a> from within a marimo app.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/resend-via-marimo/resend_via_marimo.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Resend via marimo"></p>
</figure>
</div>
<p>It seems the app runs fine locally but doesn’t work in a WASM environment. It’ll be interesting to explore where the boundaries of WASM lie.</p>
<p>Check out the full marimo code below or view it on <a href="https://molab.marimo.io/notebooks/nb_9b9N9Lh1payY9HvowyzvSB/app">molab</a>.</p>
<div id="b9962e8e" class="cell" data-execution_count="1">
<details class="code-fold">
<summary>Show full code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo</span>
<span id="cb1-2"></span>
<span id="cb1-3">__generated_with <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.14.7"</span></span>
<span id="cb1-4">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> marimo.App(width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb1-9">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> mo</span>
<span id="cb1-10">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb1-11">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> html <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> escape</span>
<span id="cb1-12">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-13">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> resend</span>
<span id="cb1-14"></span>
<span id="cb1-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> escape, mo, resend</span>
<span id="cb1-16"></span>
<span id="cb1-17"></span>
<span id="cb1-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(escape, resend):</span>
<span id="cb1-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> send_email(d: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb1-21">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        https://resend.com/docs/send-with-python</span></span>
<span id="cb1-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb1-24">        resend.api_key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RESEND_API_KEY"</span>]</span>
<span id="cb1-25">        from_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"from_"</span>]</span>
<span id="cb1-26">        to <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [mail.strip() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> mail <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"to"</span>].split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>)]</span>
<span id="cb1-27">        subject <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"subject"</span>]</span>
<span id="cb1-28">        html_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> escape(d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"content"</span>]).replace(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;br&gt;"</span>)</span>
<span id="cb1-29"></span>
<span id="cb1-30">        params: resend.Emails.SendParams <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-31">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"from"</span>: from_,</span>
<span id="cb1-32">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"to"</span>: to,</span>
<span id="cb1-33">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"subject"</span>: subject,</span>
<span id="cb1-34">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"html"</span>: html_,</span>
<span id="cb1-35">        }</span>
<span id="cb1-36"></span>
<span id="cb1-37">        email <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> resend.Emails.send(params)</span>
<span id="cb1-38"></span>
<span id="cb1-39">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (send_email,)</span>
<span id="cb1-40"></span>
<span id="cb1-41"></span>
<span id="cb1-42"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-43"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(mo, send_email):</span>
<span id="cb1-44">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a form with multiple eleme</span></span>
<span id="cb1-45">    form <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-46">        mo.md(</span>
<span id="cb1-47">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb1-48"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        **marimo mail**</span></span>
<span id="cb1-49"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{RESEND_API_KEY}</span></span>
<span id="cb1-50"></span>
<span id="cb1-51"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{from_}</span></span>
<span id="cb1-52"></span>
<span id="cb1-53"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{to}</span></span>
<span id="cb1-54"></span>
<span id="cb1-55"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{subject}</span></span>
<span id="cb1-56"></span>
<span id="cb1-57"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{content}</span></span>
<span id="cb1-58"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb1-59">        )</span>
<span id="cb1-60">        .batch(</span>
<span id="cb1-61">            RESEND_API_KEY<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mo.ui.text(</span>
<span id="cb1-62">                label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RESEND_API_KEY"</span>, kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"password"</span>, full_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb1-63">            ),</span>
<span id="cb1-64">            from_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mo.ui.text(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"From"</span>, kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>, full_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>),</span>
<span id="cb1-65">            to<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mo.ui.text(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"To"</span>, kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>, full_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>),</span>
<span id="cb1-66">            subject<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mo.ui.text(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subject"</span>, full_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>),</span>
<span id="cb1-67">            content<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mo.ui.text_area(full_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>),</span>
<span id="cb1-68">        )</span>
<span id="cb1-69">        .form(show_clear_button<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, on_change<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>send_email)</span>
<span id="cb1-70">    )</span>
<span id="cb1-71">    form</span>
<span id="cb1-72">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb1-73"></span>
<span id="cb1-74"></span>
<span id="cb1-75"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb1-76">    app.run()</span></code></pre></div></div>
</details>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>python</category>
  <category>marimo</category>
  <category>resend</category>
  <guid>https://tech.ycwu.space/posts/resend-via-marimo/20250625.html</guid>
  <pubDate>Wed, 25 Jun 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/resend-via-marimo/resend_via_marimo.png" medium="image" type="image/png" height="64" width="144"/>
</item>
<item>
  <title>Interactive Django Deployment Checklist with marimo</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/marimo-django-deployment-checklist/20250625.html</link>
  <description><![CDATA[ 






<p>I love Django—it covers pretty much everything I need in a web environment. However, when it comes time to deploy a project to production, there are always a bunch of pre-deployment checks, and I can never seem to remember them all. I find myself constantly revisiting the official Django deployment checklist page.</p>
<p>Today I realized I don’t need all the detailed information every time—just a simple reminder list is enough. So, I built an interactive Django deployment checklist using Great Tables in marimo and hosted it on <a href="https://marimo.app/?slug=loep4h">marimo.app</a>. Now I can interact with it whenever I need a quick double-check.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/marimo-django-deployment-checklist/marimo_django_deployment_checklist.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Django deployment checklist"></p>
</figure>
</div>
<section id="marimo" class="level3">
<h3 class="anchored" data-anchor-id="marimo">marimo</h3>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Give It a Sec – WASM Magic Happening
</div>
</div>
<div class="callout-body-container callout-body">
<p>The widgets may take a few moments to load, as they rely on WebAssembly under the hood.</p>
</div>
</div>
<ol type="1">
<li>I asked AI to generate a checklist and wrapped it in a Polars <code>DataFrame</code> called <code>df</code>.</li>
<li>I created 10 <a href="https://docs.marimo.io/api/inputs/switch/">switch widgets</a> and stacked them into an <a href="https://docs.marimo.io/api/inputs/array/">array widget</a> named <code>status_widgets</code> to represent the status of each checklist item.</li>
<li>I extracted the HTML representation of each widget via its <code>_repr_html_()</code> method and inserted it as a new <code>"Status"</code> column in <code>df</code>, which I then wrapped in a Great Tables <a href="https://posit-dev.github.io/great-tables/reference/GT.html#great_tables.GT">GT</a> object.</li>
<li>I added two source notes using <a href="https://posit-dev.github.io/great-tables/reference/GT.tab_source_note.html#great_tables.GT.tab_source_note">GT.tab_source_note()</a>—one to display progress, and another for a visual progress bar.</li>
<li>Finally, I gave the table a nice header with <a href="https://posit-dev.github.io/great-tables/reference/GT.tab_header.html#great_tables.GT.tab_header">GT.tab_header()</a> and applied some styling using <a href="https://posit-dev.github.io/great-tables/reference/GT.opt_stylize.html#great_tables.GT.opt_stylize">GT.opt_stylize()</a>.</li>
</ol>
<marimo-island data-app-id="main" data-cell-id="Hbol" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">import%20marimo%20as%20mo%0Aimport%20polars%20as%20pl%0Afrom%20great_tables%20import%20GT%2C%20html%2C%20md</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="MJUe" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">tasks%20%3D%20%5B%0A%20%20%20%20%22Set%20DEBUG%20%3D%20False%22%2C%0A%20%20%20%20%22Configure%20ALLOWED_HOSTS%22%2C%0A%20%20%20%20%22Set%20up%20a%20secret%20key%22%2C%0A%20%20%20%20%22Collect%20static%20files%22%2C%0A%20%20%20%20%22Apply%20database%20migrations%22%2C%0A%20%20%20%20%22Set%20up%20gunicorn%20or%20uWSGI%22%2C%0A%20%20%20%20%22Configure%20reverse%20proxy%20(e.g.%2C%20Nginx)%22%2C%0A%20%20%20%20%22Secure%20the%20database%22%2C%0A%20%20%20%20%22Set%20up%20HTTPS%20(SSL)%22%2C%0A%20%20%20%20%22Configure%20logging%20%26%20monitoring%22%2C%0A%5D%0A%0Anotes%20%3D%20%5B%0A%20%20%20%20%22Never%20deploy%20with%20DEBUG%20%3D%20True%20%E2%9A%A0%EF%B8%8F%22%2C%0A%20%20%20%20%22Include%20your%20domain(s)%20or%20IP%20address%20%F0%9F%8C%90%22%2C%0A%20%20%20%20%22Use%20a%20strong%2C%20secure%20key%20from%20an%20environment%20variable%20%F0%9F%94%90%22%2C%0A%20%20%20%20%22Run%20%60python%20manage.py%20collectstatic%60%20%F0%9F%93%A6%22%2C%0A%20%20%20%20%22Run%20%60python%20manage.py%20migrate%60%20%F0%9F%97%83%EF%B8%8F%22%2C%0A%20%20%20%20%22Use%20as%20a%20WSGI%20server%20in%20production%20%F0%9F%94%84%22%2C%0A%20%20%20%20%22Serve%20static%2Fmedia%20files%20and%20forward%20to%20WSGI%20server%20%F0%9F%A7%AD%22%2C%0A%20%20%20%20%22Use%20strong%20credentials%2C%20disable%20remote%20root%20login%20%F0%9F%9B%A1%EF%B8%8F%22%2C%0A%20%20%20%20%22Use%20Let's%20Encrypt%20or%20your%20own%20certificate%20%F0%9F%94%92%22%2C%0A%20%20%20%20%22Track%20errors%20and%20app%20performance%20%F0%9F%93%8A%22%2C%0A%5D%0A%0An_row%20%3D%20len(tasks)%0Astatus%20%3D%20%5B%22%E2%98%90%22%5D%20*%20n_row%0Adata%20%3D%20%7B%22Status%22%3A%20status%2C%20%22Task%22%3A%20tasks%2C%20%22Notes%22%3A%20notes%7D%0A%0Adf%20%3D%20pl.DataFrame(data)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="vblA" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">status_widget%20%3D%20mo.ui.switch()%0Astatus_widgets%20%3D%20mo.ui.array(%5Bstatus_widget%5D%20*%20n_row)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="bkHC" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">def%20create_bar(%0A%20%20%20%20x%3A%20float%2C%0A%20%20%20%20max_width%3A%20int%2C%0A%20%20%20%20height%3A%20int%2C%0A%20%20%20%20background_color1%3A%20str%2C%0A%20%20%20%20background_color2%3A%20str%2C%0A)%20-%3E%20str%3A%0A%20%20%20%20width%20%3D%20round(max_width%20*%20x%2C%202)%0A%20%20%20%20px_width%20%3D%20f%22%7Bwidth%7Dpx%22%0A%20%20%20%20return%20f%22%22%22%5C%0A%20%20%20%20%3Cdiv%20style%3D%22width%3A%20%7Bmax_width%7Dpx%3B%20background-color%3A%20%7Bbackground_color1%7D%3B%22%3E%5C%0A%20%20%20%20%20%20%20%20%3Cdiv%20style%3D%22height%3A%7Bheight%7Dpx%3Bwidth%3A%7Bpx_width%7D%3Bbackground-color%3A%7Bbackground_color2%7D%3B%22%3E%3C%2Fdiv%3E%5C%0A%20%20%20%20%3C%2Fdiv%3E%5C%0A%20%20%20%20%22%22%22</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="lEQa" data-reactive="true">
            <marimo-cell-output>
            <div id="hanlrfbbth" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>
#hanlrfbbth table {
          font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif;
          -webkit-font-smoothing: antialiased;
          -moz-osx-font-smoothing: grayscale;
        }
#hanlrfbbth thead, tbody, tfoot, tr, td, th { border-style: none; }
 tr { background-color: transparent; }
#hanlrfbbth p { margin: 0; padding: 0; }
 #hanlrfbbth .gt_table { display: table; border-collapse: collapse; line-height: normal; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 3px; border-top-color: #D5D5D5; border-right-style: solid; border-right-width: 3px; border-right-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 3px; border-bottom-color: #D5D5D5; border-left-style: solid; border-left-width: 3px; border-left-color: #D5D5D5; }
 #hanlrfbbth .gt_caption { padding-top: 4px; padding-bottom: 4px; }
 #hanlrfbbth .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; border-bottom-color: #FFFFFF; border-bottom-width: 0; }
 #hanlrfbbth .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 3px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; border-top-color: #FFFFFF; border-top-width: 0; }
 #hanlrfbbth .gt_heading { background-color: #FFFFFF; text-align: center; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #hanlrfbbth .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; }
 #hanlrfbbth .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #hanlrfbbth .gt_col_heading { color: #FFFFFF; background-color: #01837B; font-size: 100%; font-weight: normal; text-transform: inherit; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; overflow-x: hidden; }
 #hanlrfbbth .gt_column_spanner_outer { color: #FFFFFF; background-color: #01837B; font-size: 100%; font-weight: normal; text-transform: inherit; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px; }
 #hanlrfbbth .gt_column_spanner_outer:first-child { padding-left: 0; }
 #hanlrfbbth .gt_column_spanner_outer:last-child { padding-right: 0; }
 #hanlrfbbth .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; overflow-x: hidden; display: inline-block; width: 100%; }
 #hanlrfbbth .gt_spanner_row { border-bottom-style: hidden; }
 #hanlrfbbth .gt_group_heading { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-top-style: solid; border-top-width: 2px; border-top-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; text-align: left; }
 #hanlrfbbth .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; border-top-style: solid; border-top-width: 2px; border-top-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; vertical-align: middle; }
 #hanlrfbbth .gt_from_md> :first-child { margin-top: 0; }
 #hanlrfbbth .gt_from_md> :last-child { margin-bottom: 0; }
 #hanlrfbbth .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: dashed; border-top-width: 1px; border-top-color: #D5D5D5; border-left-style: dashed; border-left-width: 1px; border-left-color: #D5D5D5; border-right-style: dashed; border-right-width: 1px; border-right-color: #D5D5D5; vertical-align: middle; overflow-x: hidden; }
 #hanlrfbbth .gt_stub { color: #333333; background-color: #929292; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: dashed; border-right-width: 2px; border-right-color: #D5D5D5; padding-left: 5px; padding-right: 5px; }
 #hanlrfbbth .gt_indent_1 { text-indent: 5px; }
 #hanlrfbbth .gt_indent_2 { text-indent: calc(5px * 2); }
 #hanlrfbbth .gt_indent_3 { text-indent: calc(5px * 3); }
 #hanlrfbbth .gt_indent_4 { text-indent: calc(5px * 4); }
 #hanlrfbbth .gt_indent_5 { text-indent: calc(5px * 5); }
 #hanlrfbbth .gt_stub_row_group { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; vertical-align: top; }
 #hanlrfbbth .gt_row_group_first td { border-top-width: 2px; }
 #hanlrfbbth .gt_row_group_first th { border-top-width: 2px; }
 #hanlrfbbth .gt_striped { color: #333333; background-color: #F4F4F4; }
 #hanlrfbbth .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; }
 #hanlrfbbth .gt_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; }
 #hanlrfbbth .gt_first_summary_row { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; }
 #hanlrfbbth .gt_last_summary_row_top { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; }
 #hanlrfbbth .gt_grand_summary_row { color: #FFFFFF; background-color: #01837B; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; }
 #hanlrfbbth .gt_first_grand_summary_row_bottom { border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3; }
 #hanlrfbbth .gt_last_grand_summary_row_top { border-bottom-style: double; border-bottom-width: 6px; border-bottom-color: #D3D3D3; }
 #hanlrfbbth .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #hanlrfbbth .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; text-align: left; }
 #hanlrfbbth .gt_left { text-align: left; }
 #hanlrfbbth .gt_center { text-align: center; }
 #hanlrfbbth .gt_right { text-align: right; font-variant-numeric: tabular-nums; }
 #hanlrfbbth .gt_font_normal { font-weight: normal; }
 #hanlrfbbth .gt_font_bold { font-weight: bold; }
 #hanlrfbbth .gt_font_italic { font-style: italic; }
 #hanlrfbbth .gt_super { font-size: 65%; }
 #hanlrfbbth .gt_footnotes { color: font-color(#FFFFFF); background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #hanlrfbbth .gt_footnote { margin: 0px; font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; }
 #hanlrfbbth .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #hanlrfbbth .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; text-align: left; }
 #hanlrfbbth .gt_footnote_marks { font-size: 75%; vertical-align: 0.4em; position: initial; }
 #hanlrfbbth .gt_asterisk { font-size: 100%; vertical-align: 0; }
</style>
<table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<thead>
  <tr class="gt_heading">
    <td colspan="3" class="gt_heading gt_title gt_font_normal">✅ Django Deployment Checklist</td>
  </tr>
<tr class="gt_col_headings">
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="Status">Status</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="Task">Task</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="Notes">Notes</th>
</tr>
</thead>
<tbody class="gt_table_body">
  <tr>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-1" random-id="1c35b359-4fea-dbb9-f021-75e2810179c1"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left">Set DEBUG = False</td>
    <td class="gt_row gt_left">Never deploy with DEBUG = True ⚠️</td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-2" random-id="4f3ae81d-78db-9bab-9635-526568a2662c"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">Configure ALLOWED_HOSTS</td>
    <td class="gt_row gt_left gt_striped">Include your domain(s) or IP address 🌐</td>
  </tr>
  <tr>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-3" random-id="10e5a8e2-0db8-92d5-9b8b-957d9d2adacd"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left">Set up a secret key</td>
    <td class="gt_row gt_left">Use a strong, secure key from an environment variable 🔐</td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-4" random-id="f7888345-7fe4-99f3-1b19-65ff296779b9"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">Collect static files</td>
    <td class="gt_row gt_left gt_striped">Run `python manage.py collectstatic` 📦</td>
  </tr>
  <tr>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-5" random-id="9e503506-9cc1-19fd-a997-35519d6bd11f"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left">Apply database migrations</td>
    <td class="gt_row gt_left">Run `python manage.py migrate` 🗃️</td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-6" random-id="b281b4be-d209-3b39-c218-6651d0b0743a"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">Set up gunicorn or uWSGI</td>
    <td class="gt_row gt_left gt_striped">Use as a WSGI server in production 🔄</td>
  </tr>
  <tr>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-7" random-id="c23f45d0-afcb-96d8-1b43-e2348b8d027f"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left">Configure reverse proxy (e.g., Nginx)</td>
    <td class="gt_row gt_left">Serve static/media files and forward to WSGI server 🧭</td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-8" random-id="25233bee-bc47-1935-ae10-24f43e6be695"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">Secure the database</td>
    <td class="gt_row gt_left gt_striped">Use strong credentials, disable remote root login 🛡️</td>
  </tr>
  <tr>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-9" random-id="e6d62814-0fab-b1cb-446f-32e91c7d6082"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left">Set up HTTPS (SSL)</td>
    <td class="gt_row gt_left">Use Let's Encrypt or your own certificate 🔒</td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-10" random-id="d98ad328-525a-9809-6eaa-8d0bde000c60"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">Configure logging &amp; monitoring</td>
    <td class="gt_row gt_left gt_striped">Track errors and app performance 📊</td>
  </tr>
</tbody>
<tfoot><tr class="gt_sourcenotes"><td class="gt_sourcenote" colspan="3"><span class="gt_from_md">0 / 10</span></td></tr><tr class="gt_sourcenotes"><td class="gt_sourcenote" colspan="3"><span class="gt_from_md">    <div style="width: 750px; background-color: lightgray;">        <div style="height:20px;width:0.0px;background-color:#66CDAA;"></div>    </div>    </span></td></tr></tfoot>
</table>
</div>
            </marimo-cell-output>
            <marimo-cell-code hidden="">done_count%20%3D%20sum(s.value%20for%20s%20in%20status_widgets)%0A%0Agt%20%3D%20(%0A%20%20%20%20GT(%0A%20%20%20%20%20%20%20%20df.with_columns(%0A%20%20%20%20%20%20%20%20%20%20%20%20pl.Series(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Bstatus._repr_html_()%20for%20status%20in%20status_widgets%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20).alias(%22Status%22)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20.tab_source_note(f%22%7Bdone_count%7D%20%2F%20%7Bn_row%7D%22)%0A%20%20%20%20.tab_source_note(%0A%20%20%20%20%20%20%20%20html(%0A%20%20%20%20%20%20%20%20%20%20%20%20create_bar(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20done_count%20%2F%20n_row%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_width%3D750%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20height%3D20%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20background_color1%3D%22lightgray%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20background_color2%3D%22%2366CDAA%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20.tab_header(%22%E2%9C%85%20Django%20Deployment%20Checklist%22)%0A%20%20%20%20.opt_stylize(color%3D%22cyan%22%2C%20style%3D4)%0A)%0Agt</marimo-cell-code>
        </marimo-island>
<p>Check out the full marimo code below or view it on <a href="https://molab.marimo.io/notebooks/nb_xEAc6KCZkX9h8hxoCkKNBc/app">molab</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo</span>
<span id="cb1-2"></span>
<span id="cb1-3">__generated_with <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.14.7"</span></span>
<span id="cb1-4">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> marimo.App(width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb1-9">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> mo</span>
<span id="cb1-10"></span>
<span id="cb1-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (mo,)</span>
<span id="cb1-12"></span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb1-16">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-17">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, html, md</span>
<span id="cb1-18"></span>
<span id="cb1-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> GT, html, pl</span>
<span id="cb1-20"></span>
<span id="cb1-21"></span>
<span id="cb1-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-23"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(pl):</span>
<span id="cb1-24">    tasks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb1-25">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set DEBUG = False"</span>,</span>
<span id="cb1-26">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Configure ALLOWED_HOSTS"</span>,</span>
<span id="cb1-27">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set up a secret key"</span>,</span>
<span id="cb1-28">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Collect static files"</span>,</span>
<span id="cb1-29">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Apply database migrations"</span>,</span>
<span id="cb1-30">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set up gunicorn or uWSGI"</span>,</span>
<span id="cb1-31">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Configure reverse proxy (e.g., Nginx)"</span>,</span>
<span id="cb1-32">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Secure the database"</span>,</span>
<span id="cb1-33">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set up HTTPS (SSL)"</span>,</span>
<span id="cb1-34">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Configure logging &amp; monitoring"</span>,</span>
<span id="cb1-35">    ]</span>
<span id="cb1-36"></span>
<span id="cb1-37">    notes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb1-38">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Never deploy with DEBUG = True ⚠️"</span>,</span>
<span id="cb1-39">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Include your domain(s) or IP address 🌐"</span>,</span>
<span id="cb1-40">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Use a strong, secure key from an environment variable 🔐"</span>,</span>
<span id="cb1-41">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Run `python manage.py collectstatic` 📦"</span>,</span>
<span id="cb1-42">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Run `python manage.py migrate` 🗃️"</span>,</span>
<span id="cb1-43">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Use as a WSGI server in production 🔄"</span>,</span>
<span id="cb1-44">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Serve static/media files and forward to WSGI server 🧭"</span>,</span>
<span id="cb1-45">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Use strong credentials, disable remote root login 🛡️"</span>,</span>
<span id="cb1-46">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Use Let's Encrypt or your own certificate 🔒"</span>,</span>
<span id="cb1-47">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Track errors and app performance 📊"</span>,</span>
<span id="cb1-48">    ]</span>
<span id="cb1-49"></span>
<span id="cb1-50">    n_row <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(tasks)</span>
<span id="cb1-51">    status <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"☐"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n_row</span>
<span id="cb1-52">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Status"</span>: status, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Task"</span>: tasks, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Notes"</span>: notes}</span>
<span id="cb1-53"></span>
<span id="cb1-54">    df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.DataFrame(data)</span>
<span id="cb1-55">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df, n_row</span>
<span id="cb1-56"></span>
<span id="cb1-57"></span>
<span id="cb1-58"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-59"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(mo, n_row):</span>
<span id="cb1-60">    status_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.switch()</span>
<span id="cb1-61">    status_widgets <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.array([status_widget] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n_row)</span>
<span id="cb1-62">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (status_widgets,)</span>
<span id="cb1-63"></span>
<span id="cb1-64"></span>
<span id="cb1-65"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.function</span></span>
<span id="cb1-66"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create_bar(</span>
<span id="cb1-67">    x: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>,</span>
<span id="cb1-68">    max_width: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>,</span>
<span id="cb1-69">    height: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>,</span>
<span id="cb1-70">    background_color1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>,</span>
<span id="cb1-71">    background_color2: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>,</span>
<span id="cb1-72">) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb1-73">    width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(max_width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-74">    px_width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>width<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">px"</span></span>
<span id="cb1-75">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-76"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;div style="width: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>max_width<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">px; background-color: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>background_color1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">;"&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-77"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">        &lt;div style="height:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>height<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">px;width:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>px_width<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">;background-color:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>background_color2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">;"&gt;&lt;/div&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-78"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;/div&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-79"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb1-80"></span>
<span id="cb1-81"></span>
<span id="cb1-82"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-83"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(GT, df, html, n_row, pl, status_widgets):</span>
<span id="cb1-84">    done_count <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(s.value <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> s <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> status_widgets)</span>
<span id="cb1-85"></span>
<span id="cb1-86">    gt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-87">        GT(</span>
<span id="cb1-88">            df.with_columns(</span>
<span id="cb1-89">                pl.Series(</span>
<span id="cb1-90">                    [status._repr_html_() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> status <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> status_widgets]</span>
<span id="cb1-91">                ).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Status"</span>)</span>
<span id="cb1-92">            )</span>
<span id="cb1-93">        )</span>
<span id="cb1-94">        .tab_source_note(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>done_count<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> / </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>n_row<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb1-95">        .tab_source_note(</span>
<span id="cb1-96">            html(</span>
<span id="cb1-97">                create_bar(</span>
<span id="cb1-98">                    done_count <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n_row,</span>
<span id="cb1-99">                    max_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">750</span>,</span>
<span id="cb1-100">                    height<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>,</span>
<span id="cb1-101">                    background_color1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgray"</span>,</span>
<span id="cb1-102">                    background_color2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#66CDAA"</span>,</span>
<span id="cb1-103">                )</span>
<span id="cb1-104">            )</span>
<span id="cb1-105">        )</span>
<span id="cb1-106">        .tab_header(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✅ Django Deployment Checklist"</span>)</span>
<span id="cb1-107">        .opt_stylize(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cyan"</span>, style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb1-108">    )</span>
<span id="cb1-109">    gt</span>
<span id="cb1-110">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb1-111"></span>
<span id="cb1-112"></span>
<span id="cb1-113"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb1-114">    app.run()</span></code></pre></div></div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<ol type="1">
<li>This table is for demonstration purposes only. You should customize it based on your own needs.</li>
<li>This post was drafted by me, with AI assistance to refine the content.</li>
</ol>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>marimo</category>
  <category>gt</category>
  <category>django</category>
  <guid>https://tech.ycwu.space/posts/marimo-django-deployment-checklist/20250625.html</guid>
  <pubDate>Wed, 25 Jun 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/marimo-django-deployment-checklist/marimo_django_deployment_checklist.png" medium="image" type="image/png" height="95" width="144"/>
</item>
<item>
  <title>Great Tables with marimo in Quarto</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/gt-marimo-in-quarto/20250612.html</link>
  <description><![CDATA[ 






<p>This is a follow-up to my <a href="../../posts/marimo-in-quarto/20250607.html">previous post</a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/gt-marimo-in-quarto/gt_marimo_in_quarto.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Great Tables with marimo in Quarto"></p>
</figure>
</div>
<section id="marimo" class="level2">
<h2 class="anchored" data-anchor-id="marimo">marimo</h2>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Give It a Sec – WASM Magic Happening
</div>
</div>
<div class="callout-body-container callout-body">
<p>The widgets may take a few moments to load, as they rely on WebAssembly under the hood.</p>
</div>
</div>
<p>Here, I demonstrate how marimo widgets can be embedded in Great Tables by wrapping them with the <code>html()</code> function provided by the library. This allows interactive widgets to control the table’s appearance in a Quarto environment via WASM — a surprisingly powerful capability, in my opinion.</p>
<marimo-island data-app-id="main" data-cell-id="Hbol" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">import%20marimo%20as%20mo%0Aimport%20polars%20as%20pl%0Afrom%20great_tables%20import%20GT%2C%20html</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="MJUe" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">data%20%3D%20%7B%0A%20%20%20%20%22col1%22%3A%20%5B2%2C%205%2C%207%2C%2010%2C%2015%5D%2C%0A%20%20%20%20%22col2%22%3A%20%5B%22x%22%2C%20%22y%22%2C%20%22y%22%2C%20%22z%22%2C%20%22z%22%5D%2C%0A%20%20%20%20%22color%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%22lightgrey%22%2C%0A%20%20%20%20%20%20%20%20%22lightblue%22%2C%0A%20%20%20%20%20%20%20%20%22lightblue%22%2C%0A%20%20%20%20%20%20%20%20%22papayawhip%22%2C%0A%20%20%20%20%20%20%20%20%22papayawhip%22%2C%0A%20%20%20%20%5D%2C%0A%7D%0Adf%20%3D%20pl.DataFrame(data)%0Aprint(df)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="vblA" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">style_widget%20%3D%20mo.ui.slider(1%2C%206%2C%20label%3D%22Style%20Number%22)%0A%0A_colors%20%3D%20%5B%22blue%22%2C%20%22cyan%22%2C%20%22pink%22%2C%20%22green%22%2C%20%22red%22%2C%20%22gray%22%5D%0Acolor_widget%20%3D%20mo.ui.radio(%0A%20%20%20%20options%3D_colors%2C%20value%3D_colors%5B0%5D%2C%20label%3D%22Style%20Color%22%2C%20inline%3DTrue%0A)%0A%0Arow_striping_widget%20%3D%20mo.ui.switch(value%3DTrue%2C%20label%3D%22Row%20Striping%3F%22)%0A%0Agt%20%3D%20(%0A%20%20%20%20GT(df)%0A%20%20%20%20.tab_header(html(style_widget)%2C%20html(color_widget))%0A%20%20%20%20.tab_source_note(html(row_striping_widget))%0A%20%20%20%20.opt_align_table_header(%22left%22)%0A)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="bkHC" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">gt.opt_stylize(%0A%20%20%20%20style%3Dstyle_widget.value%2C%0A%20%20%20%20color%3Dcolor_widget.value%2C%0A%20%20%20%20add_row_striping%3Drow_striping_widget.value%2C%0A)</marimo-cell-code>
</marimo-island>
<p>Check out the full marimo code below or view it on <a href="https://molab.marimo.io/notebooks/nb_En2k3WtpkE1Uf6a8QvCS7n/app">molab</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo</span>
<span id="cb1-2"></span>
<span id="cb1-3">__generated_with <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.13.15"</span></span>
<span id="cb1-4">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> marimo.App(width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb1-9">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> mo</span>
<span id="cb1-10">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-11">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, html</span>
<span id="cb1-12"></span>
<span id="cb1-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> GT, html, mo, pl</span>
<span id="cb1-14"></span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(pl):</span>
<span id="cb1-18">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-19">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col1"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>],</span>
<span id="cb1-20">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>],</span>
<span id="cb1-21">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: [</span>
<span id="cb1-22">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>,</span>
<span id="cb1-23">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>,</span>
<span id="cb1-24">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>,</span>
<span id="cb1-25">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>,</span>
<span id="cb1-26">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>,</span>
<span id="cb1-27">        ],</span>
<span id="cb1-28">    }</span>
<span id="cb1-29">    df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.DataFrame(data)</span>
<span id="cb1-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (df,)</span>
<span id="cb1-31"></span>
<span id="cb1-32"></span>
<span id="cb1-33"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-34"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(GT, df, html, mo):</span>
<span id="cb1-35">    style_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.slider(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Style Number"</span>)</span>
<span id="cb1-36"></span>
<span id="cb1-37">    _colors <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cyan"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray"</span>]</span>
<span id="cb1-38">    color_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.radio(</span>
<span id="cb1-39">        options<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>_colors, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>_colors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Style Color"</span>, inline<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb1-40">    )</span>
<span id="cb1-41"></span>
<span id="cb1-42">    row_striping_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.switch(value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Row Striping?"</span>)</span>
<span id="cb1-43"></span>
<span id="cb1-44">    gt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-45">        GT(df)</span>
<span id="cb1-46">        .tab_header(html(style_widget), html(color_widget))</span>
<span id="cb1-47">        .tab_source_note(html(row_striping_widget))</span>
<span id="cb1-48">        .opt_align_table_header(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>)</span>
<span id="cb1-49">    )</span>
<span id="cb1-50">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> color_widget, gt, row_striping_widget, style_widget</span>
<span id="cb1-51"></span>
<span id="cb1-52"></span>
<span id="cb1-53"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-54"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(color_widget, gt, row_striping_widget, style_widget):</span>
<span id="cb1-55">    gt.opt_stylize(</span>
<span id="cb1-56">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style_widget.value,</span>
<span id="cb1-57">        color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>color_widget.value,</span>
<span id="cb1-58">        add_row_striping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>row_striping_widget.value,</span>
<span id="cb1-59">    )</span>
<span id="cb1-60">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb1-61"></span>
<span id="cb1-62"></span>
<span id="cb1-63"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb1-64">    app.run()</span></code></pre></div></div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>gt</category>
  <category>marimo</category>
  <guid>https://tech.ycwu.space/posts/gt-marimo-in-quarto/20250612.html</guid>
  <pubDate>Thu, 12 Jun 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/gt-marimo-in-quarto/gt_marimo_in_quarto.png" medium="image" type="image/png" height="70" width="144"/>
</item>
<item>
  <title>marimo in Quarto</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/marimo-in-quarto/20250607.html</link>
  <description><![CDATA[ 






<p>This post demonstrates that Polars, Great Tables, and marimo can successfully run within a Quarto environment (as shown in this post). The example uses a table styler selector from Great Tables—and I’m honestly surprised it works!</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/marimo-in-quarto/marimo_in_quarto.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="marimo in Quarto"></p>
</figure>
</div>
<section id="marimo" class="level2">
<h2 class="anchored" data-anchor-id="marimo">marimo</h2>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Give It a Sec – WASM Magic Happening
</div>
</div>
<div class="callout-body-container callout-body">
<p>The widgets may take a few moments to load, as they rely on WebAssembly under the hood.</p>
</div>
</div>
<marimo-island data-app-id="main" data-cell-id="Hbol" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">import%20marimo%20as%20mo%0Aimport%20polars%20as%20pl%0Afrom%20great_tables%20import%20GT%2C%20loc%2C%20style</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="MJUe" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">data%20%3D%20%7B%0A%20%20%20%20%22col1%22%3A%20%5B2%2C%205%2C%207%2C%2010%2C%2015%5D%2C%0A%20%20%20%20%22col2%22%3A%20%5B%22x%22%2C%20%22y%22%2C%20%22y%22%2C%20%22z%22%2C%20%22z%22%5D%2C%0A%20%20%20%20%22color%22%3A%20%5B%22lightgrey%22%2C%20%22lightblue%22%2C%20%22lightblue%22%2C%20%22papayawhip%22%2C%20%22papayawhip%22%5D%2C%0A%7D%0Adf%20%3D%20pl.DataFrame(data)%0Aprint(df)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="vblA" data-reactive="true">
    <marimo-cell-output>
    <div style="display: flex;flex: 1;flex-direction: column;justify-content: flex-start;align-items: normal;flex-wrap: nowrap;gap: 0.5rem"><marimo-ui-element object-id="vblA-0" random-id="a7e8278d-518e-b316-a263-9e60858c604e"><marimo-slider data-initial-value="1" data-label="&quot;\u003cspan class=\&quot;markdown prose dark:prose-invert contents\&quot;\u003e\u003cspan class=\&quot;paragraph\&quot;\u003eSelect Style Number\u003c/span\u003e\u003c/span\u003e&quot;" data-start="1" data-stop="6" data-steps="[]" data-debounce="false" data-disabled="false" data-orientation="&quot;horizontal&quot;" data-show-value="false" data-include-input="false" data-full-width="false"></marimo-slider></marimo-ui-element><marimo-ui-element object-id="vblA-1" random-id="11c8b65c-a9c9-5324-18d9-17968765d268"><marimo-radio data-initial-value="&quot;blue&quot;" data-label="&quot;\u003cspan class=\&quot;markdown prose dark:prose-invert contents\&quot;\u003e\u003cspan class=\&quot;paragraph\&quot;\u003eSelect Style Color\u003c/span\u003e\u003c/span\u003e&quot;" data-options="[&quot;blue&quot;,&quot;cyan&quot;,&quot;pink&quot;,&quot;green&quot;,&quot;red&quot;,&quot;gray&quot;]" data-inline="false" data-disabled="false"></marimo-radio></marimo-ui-element><marimo-ui-element object-id="vblA-2" random-id="91537791-b41b-03da-ad7d-2126edbb5ab1"><marimo-switch data-initial-value="true" data-label="&quot;\u003cspan class=\&quot;markdown prose dark:prose-invert contents\&quot;\u003e\u003cspan class=\&quot;paragraph\&quot;\u003eAdd Row Striping?\u003c/span\u003e\u003c/span\u003e&quot;" data-disabled="false"></marimo-switch></marimo-ui-element></div>
    </marimo-cell-output>
    <marimo-cell-code hidden="">style_widget%20%3D%20mo.ui.slider(1%2C%206%2C%20label%3D%22Select%20Style%20Number%22)%0Amo.output.append(style_widget)%0A%0A_colors%20%3D%20%5B%22blue%22%2C%20%22cyan%22%2C%20%22pink%22%2C%20%22green%22%2C%20%22red%22%2C%20%22gray%22%5D%0Acolor_widget%20%3D%20mo.ui.radio(%0A%20%20%20%20options%3D_colors%2C%20value%3D_colors%5B0%5D%2C%20label%3D%22Select%20Style%20Color%22%0A)%0Amo.output.append(color_widget)%0A%0Arow_striping_widget%20%3D%20mo.ui.switch(value%3DTrue%2C%20label%3D%22Add%20Row%20Striping%3F%22)%0Amo.output.append(row_striping_widget)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="bkHC" data-reactive="true">
            <marimo-cell-output>
            <div id="tddhsexkbo" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>
#tddhsexkbo table {
          font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif;
          -webkit-font-smoothing: antialiased;
          -moz-osx-font-smoothing: grayscale;
        }
#tddhsexkbo thead, tbody, tfoot, tr, td, th { border-style: none; }
 tr { background-color: transparent; }
#tddhsexkbo p { margin: 0; padding: 0; }
 #tddhsexkbo .gt_table { display: table; border-collapse: collapse; line-height: normal; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 2px; border-top-color: #004D80; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #004D80; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; }
 #tddhsexkbo .gt_caption { padding-top: 4px; padding-bottom: 4px; }
 #tddhsexkbo .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; border-bottom-color: #FFFFFF; border-bottom-width: 0; }
 #tddhsexkbo .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 3px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; border-top-color: #FFFFFF; border-top-width: 0; }
 #tddhsexkbo .gt_heading { background-color: #FFFFFF; text-align: center; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #tddhsexkbo .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; }
 #tddhsexkbo .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #0076BA; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #tddhsexkbo .gt_col_heading { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; overflow-x: hidden; }
 #tddhsexkbo .gt_column_spanner_outer { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px; }
 #tddhsexkbo .gt_column_spanner_outer:first-child { padding-left: 0; }
 #tddhsexkbo .gt_column_spanner_outer:last-child { padding-right: 0; }
 #tddhsexkbo .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; overflow-x: hidden; display: inline-block; width: 100%; }
 #tddhsexkbo .gt_spanner_row { border-bottom-style: hidden; }
 #tddhsexkbo .gt_group_heading { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-top-style: solid; border-top-width: 2px; border-top-color: #0076BA; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; text-align: left; }
 #tddhsexkbo .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; border-top-style: solid; border-top-width: 2px; border-top-color: #0076BA; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; vertical-align: middle; }
 #tddhsexkbo .gt_from_md> :first-child { margin-top: 0; }
 #tddhsexkbo .gt_from_md> :last-child { margin-bottom: 0; }
 #tddhsexkbo .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: none; border-top-width: 1px; border-top-color: #89D3FE; border-left-style: none; border-left-width: 1px; border-left-color: #89D3FE; border-right-style: none; border-right-width: 1px; border-right-color: #89D3FE; vertical-align: middle; overflow-x: hidden; }
 #tddhsexkbo .gt_stub { color: #FFFFFF; background-color: #0076BA; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #0076BA; padding-left: 5px; padding-right: 5px; }
 #tddhsexkbo .gt_indent_1 { text-indent: 5px; }
 #tddhsexkbo .gt_indent_2 { text-indent: calc(5px * 2); }
 #tddhsexkbo .gt_indent_3 { text-indent: calc(5px * 3); }
 #tddhsexkbo .gt_indent_4 { text-indent: calc(5px * 4); }
 #tddhsexkbo .gt_indent_5 { text-indent: calc(5px * 5); }
 #tddhsexkbo .gt_stub_row_group { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; vertical-align: top; }
 #tddhsexkbo .gt_row_group_first td { border-top-width: 2px; }
 #tddhsexkbo .gt_row_group_first th { border-top-width: 2px; }
 #tddhsexkbo .gt_striped { color: #333333; background-color: #F4F4F4; }
 #tddhsexkbo .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #0076BA; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; }
 #tddhsexkbo .gt_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; }
 #tddhsexkbo .gt_first_summary_row { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; }
 #tddhsexkbo .gt_last_summary_row_top { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; }
 #tddhsexkbo .gt_grand_summary_row { color: #333333; background-color: #89D3FE; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; }
 #tddhsexkbo .gt_first_grand_summary_row_bottom { border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3; }
 #tddhsexkbo .gt_last_grand_summary_row_top { border-bottom-style: double; border-bottom-width: 6px; border-bottom-color: #D3D3D3; }
 #tddhsexkbo .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #tddhsexkbo .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; text-align: left; }
 #tddhsexkbo .gt_left { text-align: left; }
 #tddhsexkbo .gt_center { text-align: center; }
 #tddhsexkbo .gt_right { text-align: right; font-variant-numeric: tabular-nums; }
 #tddhsexkbo .gt_font_normal { font-weight: normal; }
 #tddhsexkbo .gt_font_bold { font-weight: bold; }
 #tddhsexkbo .gt_font_italic { font-style: italic; }
 #tddhsexkbo .gt_super { font-size: 65%; }
 #tddhsexkbo .gt_footnotes { color: font-color(#FFFFFF); background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #tddhsexkbo .gt_footnote { margin: 0px; font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; }
 #tddhsexkbo .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #tddhsexkbo .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; text-align: left; }
 #tddhsexkbo .gt_footnote_marks { font-size: 75%; vertical-align: 0.4em; position: initial; }
 #tddhsexkbo .gt_asterisk { font-size: 100%; vertical-align: 0; }
</style>
<table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings">
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="col1">col1</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="col2">col2</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="color">color</th>
</tr>
</thead>
<tbody class="gt_table_body">
  <tr>
    <td class="gt_row gt_right">2</td>
    <td class="gt_row gt_left">x</td>
    <td class="gt_row gt_left">lightgrey</td>
  </tr>
  <tr>
    <td class="gt_row gt_right gt_striped">5</td>
    <td class="gt_row gt_left gt_striped">y</td>
    <td class="gt_row gt_left gt_striped">lightblue</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">7</td>
    <td class="gt_row gt_left">y</td>
    <td class="gt_row gt_left">lightblue</td>
  </tr>
  <tr>
    <td class="gt_row gt_right gt_striped">10</td>
    <td class="gt_row gt_left gt_striped">z</td>
    <td class="gt_row gt_left gt_striped">papayawhip</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">15</td>
    <td class="gt_row gt_left">z</td>
    <td class="gt_row gt_left">papayawhip</td>
  </tr>
</tbody>
</table>
</div>
            </marimo-cell-output>
            <marimo-cell-code hidden="">GT(df).opt_stylize(%0A%20%20%20%20style%3Dstyle_widget.value%2C%0A%20%20%20%20color%3Dcolor_widget.value%2C%0A%20%20%20%20add_row_striping%3Drow_striping_widget.value%2C%0A)</marimo-cell-code>
        </marimo-island>
<p>Check out the full marimo code below or view it on <a href="https://molab.marimo.io/notebooks/nb_uHQvXDXF3UsvFQD9xZkaiu/app">molab</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo</span>
<span id="cb1-2"></span>
<span id="cb1-3">__generated_with <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.13.15"</span></span>
<span id="cb1-4">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> marimo.App(width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb1-9">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> mo</span>
<span id="cb1-10">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-11">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, loc, style</span>
<span id="cb1-12"></span>
<span id="cb1-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> GT, mo, pl</span>
<span id="cb1-14"></span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(pl):</span>
<span id="cb1-18">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-19">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col1"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>],</span>
<span id="cb1-20">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>],</span>
<span id="cb1-21">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>],</span>
<span id="cb1-22">    }</span>
<span id="cb1-23">    df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.DataFrame(data)</span>
<span id="cb1-24">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (df,)</span>
<span id="cb1-25"></span>
<span id="cb1-26"></span>
<span id="cb1-27"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-28"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(mo):</span>
<span id="cb1-29">    style_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.slider(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Select Style Number"</span>)</span>
<span id="cb1-30">    mo.output.append(style_widget)</span>
<span id="cb1-31"></span>
<span id="cb1-32">    _colors <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cyan"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray"</span>]</span>
<span id="cb1-33">    color_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.radio(</span>
<span id="cb1-34">        options<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>_colors, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>_colors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Select Style Color"</span></span>
<span id="cb1-35">    )</span>
<span id="cb1-36">    mo.output.append(color_widget)</span>
<span id="cb1-37"></span>
<span id="cb1-38">    row_striping_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.switch(value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Add Row Striping?"</span>)</span>
<span id="cb1-39">    mo.output.append(row_striping_widget)</span>
<span id="cb1-40">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> color_widget, row_striping_widget, style_widget</span>
<span id="cb1-41"></span>
<span id="cb1-42"></span>
<span id="cb1-43"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-44"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(GT, color_widget, df, row_striping_widget, style_widget):</span>
<span id="cb1-45">    GT(df).opt_stylize(</span>
<span id="cb1-46">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style_widget.value,</span>
<span id="cb1-47">        color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>color_widget.value,</span>
<span id="cb1-48">        add_row_striping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>row_striping_widget.value,</span>
<span id="cb1-49">    )</span>
<span id="cb1-50">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb1-51"></span>
<span id="cb1-52"></span>
<span id="cb1-53"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb1-54">    app.run()</span></code></pre></div></div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>marimo</category>
  <guid>https://tech.ycwu.space/posts/marimo-in-quarto/20250607.html</guid>
  <pubDate>Sat, 07 Jun 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/marimo-in-quarto/marimo_in_quarto.png" medium="image" type="image/png" height="111" width="144"/>
</item>
<item>
  <title>Styling tricks in Great Tables</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/styling-tricks-table-body/20250526.html</link>
  <description><![CDATA[ 






<p>This post highlights a powerful yet underused feature in Great Tables: using existing columns to style the table body via <a href="https://posit-dev.github.io/great-tables/reference/from_column.html">from_column()</a>.</p>
<p>Let’s say you have the following Polars DataFrame <code>df</code>:</p>
<div id="a094d6dc" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, from_column, loc, style</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables.data <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> films</span>
<span id="cb1-4"></span>
<span id="cb1-5"></span>
<span id="cb1-6">df_style <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.DataFrame(</span>
<span id="cb1-7">    {</span>
<span id="cb1-8">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgreen"</span>],</span>
<span id="cb1-9">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"small"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x-large"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>],</span>
<span id="cb1-10">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"weight"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lighter"</span>],</span>
<span id="cb1-11">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"align"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>],</span>
<span id="cb1-12">    }</span>
<span id="cb1-13">)</span>
<span id="cb1-14"></span>
<span id="cb1-15">columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"title"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"run_time"</span>]</span>
<span id="cb1-16"></span>
<span id="cb1-17">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.concat(</span>
<span id="cb1-18">    [</span>
<span id="cb1-19">        pl.from_pandas(films.head(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>).loc[:, columns]),</span>
<span id="cb1-20">        df_style,</span>
<span id="cb1-21">    ],</span>
<span id="cb1-22">    how<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"horizontal_extend"</span>,</span>
<span id="cb1-23">)</span>
<span id="cb1-24"></span>
<span id="cb1-25"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>shape: (3, 7)
┌──────┬───────────────────────────┬──────────┬────────────┬─────────┬─────────┬────────┐
│ year ┆ title                     ┆ run_time ┆ color      ┆ size    ┆ weight  ┆ align  │
│ ---  ┆ ---                       ┆ ---      ┆ ---        ┆ ---     ┆ ---     ┆ ---    │
│ i64  ┆ str                       ┆ str      ┆ str        ┆ str     ┆ str     ┆ str    │
╞══════╪═══════════════════════════╪══════════╪════════════╪═════════╪═════════╪════════╡
│ 1946 ┆ The Lovers                ┆ 1h 30m   ┆ papayawhip ┆ small   ┆ normal  ┆ right  │
│ 1946 ┆ Anna and the King of Siam ┆ 2h 8m    ┆ lightblue  ┆ x-large ┆ bold    ┆ center │
│ 1946 ┆ Blood and Fire            ┆ 1h 40m   ┆ lightgreen ┆ medium  ┆ lighter ┆ left   │
└──────┴───────────────────────────┴──────────┴────────────┴─────────┴─────────┴────────┘</code></pre>
</div>
</div>
<p>The <code>color</code>, <code>size</code>, <code>weight</code>, and <code>align</code> columns contain style-related metadata. We can use <code>from_column()</code> to map this metadata to four styling options provided by Great Tables—<a href="https://posit-dev.github.io/great-tables/reference/style.fill.html">style.fill()</a>, <a href="https://posit-dev.github.io/great-tables/reference/style.text.html">style.text()</a>, <a href="https://posit-dev.github.io/great-tables/reference/style.borders.html">style.borders()</a>, and <a href="https://posit-dev.github.io/great-tables/reference/style.css.html">style.css()</a>—all of which are built on the internal <code>CellStyle</code> class.</p>
<div id="7e6b39ad" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">(</span>
<span id="cb3-2">    GT(df)</span>
<span id="cb3-3">    .tab_style(</span>
<span id="cb3-4">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[</span>
<span id="cb3-5">            style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>from_column(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>)),</span>
<span id="cb3-6">            style.text(</span>
<span id="cb3-7">                size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>from_column(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>),</span>
<span id="cb3-8">                weight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>from_column(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"weight"</span>),</span>
<span id="cb3-9">                align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>from_column(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"align"</span>),</span>
<span id="cb3-10">            ),</span>
<span id="cb3-11">        ],</span>
<span id="cb3-12">        locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(columns),</span>
<span id="cb3-13">    )</span>
<span id="cb3-14">    .cols_hide(df_style.columns)</span>
<span id="cb3-15">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray"</span>)</span>
<span id="cb3-16">)</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="2">
<div id="fwimwduncs" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>
#fwimwduncs table {
          font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif;
          -webkit-font-smoothing: antialiased;
          -moz-osx-font-smoothing: grayscale;
        }

#fwimwduncs thead, tbody, tfoot, tr, td, th { border-style: none; }
 tr { background-color: transparent; }
#fwimwduncs p { margin: 0; padding: 0; }
 #fwimwduncs .gt_table { display: table; border-collapse: collapse; line-height: normal; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 2px; border-top-color: #5F5F5F; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #5F5F5F; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; }
 #fwimwduncs .gt_caption { padding-top: 4px; padding-bottom: 4px; }
 #fwimwduncs .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; border-bottom-color: #FFFFFF; border-bottom-width: 0; }
 #fwimwduncs .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 3px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; border-top-color: #FFFFFF; border-top-width: 0; }
 #fwimwduncs .gt_heading { background-color: #FFFFFF; text-align: center; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #fwimwduncs .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #5F5F5F; }
 #fwimwduncs .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #5F5F5F; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #5F5F5F; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #fwimwduncs .gt_col_heading { color: #FFFFFF; background-color: #5F5F5F; font-size: 100%; font-weight: normal; text-transform: inherit; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; overflow-x: hidden; }
 #fwimwduncs .gt_column_spanner_outer { color: #FFFFFF; background-color: #5F5F5F; font-size: 100%; font-weight: normal; text-transform: inherit; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px; }
 #fwimwduncs .gt_column_spanner_outer:first-child { padding-left: 0; }
 #fwimwduncs .gt_column_spanner_outer:last-child { padding-right: 0; }
 #fwimwduncs .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #5F5F5F; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; overflow-x: hidden; display: inline-block; width: 100%; }
 #fwimwduncs .gt_spanner_row { border-bottom-style: hidden; }
 #fwimwduncs .gt_group_heading { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-top-style: solid; border-top-width: 2px; border-top-color: #5F5F5F; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #5F5F5F; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; text-align: left; }
 #fwimwduncs .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; border-top-style: solid; border-top-width: 2px; border-top-color: #5F5F5F; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #5F5F5F; vertical-align: middle; }
 #fwimwduncs .gt_from_md> :first-child { margin-top: 0; }
 #fwimwduncs .gt_from_md> :last-child { margin-bottom: 0; }
 #fwimwduncs .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: none; border-top-width: 1px; border-top-color: #D5D5D5; border-left-style: none; border-left-width: 1px; border-left-color: #D5D5D5; border-right-style: none; border-right-width: 1px; border-right-color: #D5D5D5; vertical-align: middle; overflow-x: hidden; }
 #fwimwduncs .gt_stub { color: #333333; background-color: #D5D5D5; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D5D5D5; padding-left: 5px; padding-right: 5px; }
 #fwimwduncs .gt_indent_1 { text-indent: 5px; }
 #fwimwduncs .gt_indent_2 { text-indent: calc(5px * 2); }
 #fwimwduncs .gt_indent_3 { text-indent: calc(5px * 3); }
 #fwimwduncs .gt_indent_4 { text-indent: calc(5px * 4); }
 #fwimwduncs .gt_indent_5 { text-indent: calc(5px * 5); }
 #fwimwduncs .gt_stub_row_group { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; vertical-align: top; }
 #fwimwduncs .gt_row_group_first td { border-top-width: 2px; }
 #fwimwduncs .gt_row_group_first th { border-top-width: 2px; }
 #fwimwduncs .gt_striped { color: #333333; background-color: #F4F4F4; }
 #fwimwduncs .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #5F5F5F; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #5F5F5F; }
 #fwimwduncs .gt_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; }
 #fwimwduncs .gt_first_summary_row { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; }
 #fwimwduncs .gt_last_summary_row_top { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; }
 #fwimwduncs .gt_grand_summary_row { color: #333333; background-color: #D5D5D5; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; }
 #fwimwduncs .gt_first_grand_summary_row_bottom { border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3; }
 #fwimwduncs .gt_last_grand_summary_row_top { border-bottom-style: double; border-bottom-width: 6px; border-bottom-color: #D3D3D3; }
 #fwimwduncs .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #fwimwduncs .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; text-align: left; }
 #fwimwduncs .gt_left { text-align: left; }
 #fwimwduncs .gt_center { text-align: center; }
 #fwimwduncs .gt_right { text-align: right; font-variant-numeric: tabular-nums; }
 #fwimwduncs .gt_font_normal { font-weight: normal; }
 #fwimwduncs .gt_font_bold { font-weight: bold; }
 #fwimwduncs .gt_font_italic { font-style: italic; }
 #fwimwduncs .gt_super { font-size: 65%; }
 #fwimwduncs .gt_footnotes { color: font-color(#FFFFFF); background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #fwimwduncs .gt_footnote { margin: 0px; font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; }
 #fwimwduncs .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #fwimwduncs .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; text-align: left; }
 #fwimwduncs .gt_footnote_marks { font-size: 75%; vertical-align: 0.4em; position: initial; }
 #fwimwduncs .gt_asterisk { font-size: 100%; vertical-align: 0; }
 
</style>
<table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<thead>

<tr class="gt_col_headings">
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="year">year</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="title">title</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="run_time">run_time</th>
</tr>
</thead>
<tbody class="gt_table_body">
  <tr>
    <td style="background-color: papayawhip; font-size: small;text-align: right;font-weight: normal;" class="gt_row gt_right">1946</td>
    <td style="background-color: papayawhip; font-size: small;text-align: right;font-weight: normal;" class="gt_row gt_left">The Lovers</td>
    <td style="background-color: papayawhip; font-size: small;text-align: right;font-weight: normal;" class="gt_row gt_left">1h 30m</td>
  </tr>
  <tr>
    <td style="background-color: lightblue; font-size: x-large;text-align: center;font-weight: bold;" class="gt_row gt_right gt_striped">1946</td>
    <td style="background-color: lightblue; font-size: x-large;text-align: center;font-weight: bold;" class="gt_row gt_left gt_striped">Anna and the King of Siam</td>
    <td style="background-color: lightblue; font-size: x-large;text-align: center;font-weight: bold;" class="gt_row gt_left gt_striped">2h 8m</td>
  </tr>
  <tr>
    <td style="background-color: lightgreen; font-size: medium;text-align: left;font-weight: lighter;" class="gt_row gt_right">1946</td>
    <td style="background-color: lightgreen; font-size: medium;text-align: left;font-weight: lighter;" class="gt_row gt_left">Blood and Fire</td>
    <td style="background-color: lightgreen; font-size: medium;text-align: left;font-weight: lighter;" class="gt_row gt_left">1h 40m</td>
  </tr>
</tbody>

</table>

</div>
</div>
</div>
<p><strong>One last note:</strong> <code>from_column()</code> works with both Pandas and Polars DataFrames. For Polars users, you can also pass expressions directly without wrapping them in <code>from_column()</code>. The following code produces the same styled table as shown above:</p>
<div id="a106cc95" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">(</span>
<span id="cb4-2">    GT(df)</span>
<span id="cb4-3">    .tab_style(</span>
<span id="cb4-4">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[</span>
<span id="cb4-5">            style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>)),</span>
<span id="cb4-6">            style.text(</span>
<span id="cb4-7">                size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>), weight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"weight"</span>), align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"align"</span>)</span>
<span id="cb4-8">            ),</span>
<span id="cb4-9">        ],</span>
<span id="cb4-10">        locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(columns),</span>
<span id="cb4-11">    )</span>
<span id="cb4-12">    .cols_hide(df_style.columns)</span>
<span id="cb4-13">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray"</span>)</span>
<span id="cb4-14">)</span></code></pre></div></div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>python</category>
  <category>pandas</category>
  <category>polars</category>
  <category>gt</category>
  <guid>https://tech.ycwu.space/posts/styling-tricks-table-body/20250526.html</guid>
  <pubDate>Mon, 26 May 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/styling-tricks-table-body/table_body.png" medium="image" type="image/png" height="49" width="144"/>
</item>
<item>
  <title>case_when() in Polars</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/casewhen-in-polars/20250525.html</link>
  <description><![CDATA[ 






<p>This is a follow-up to my <a href="../../posts/polars-custom-expr-namespace/20250417.html">previous post</a>.</p>
<p>While the conditional branching mechanism of <a href="https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.when.html">pl.when().then().otherwise()</a> is quite powerful, I often find it a bit verbose—especially when the conditions are complex. In those cases, it becomes harder to validate the correctness of each branch at a glance.</p>
<p>On the other hand, I find the <a href="https://pandas.pydata.org/docs/reference/api/pandas.Series.case_when.html">pd.Series.case_when()</a> pattern in Pandas slightly more concise and readable. However, I’ve always wished it supported a fallback mechanism like Polars’ <code>.otherwise()</code>.</p>
<p>In the end, I thought it would be interesting to borrow the concept behind <code>pd.Series.case_when()</code> and implement it as a standalone utility function in Polars.</p>
<section id="case_when" class="level2">
<h2 class="anchored" data-anchor-id="case_when"><code>case_when()</code></h2>
<p>The <code>case_when()</code> function accepts two arguments:</p>
<ul>
<li><code>caselist</code>: A list of two-element tuples, where the first item is the condition (used in <code>pl.when()</code>), and the second is the corresponding result expression (used in <code>.then()</code>).</li>
<li><code>otherwise</code>: A fallback expression used in <code>.otherwise()</code> if no conditions match.</li>
</ul>
<p>The given example demonstrates how <code>case_when()</code> can simplify conditional logic compared to the more verbose <code>pl.when().then().otherwise()</code> chain.</p>
<div id="4d429c22" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> functools <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> cache</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Any</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-5"></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> case_when(</span>
<span id="cb1-8">    caselist: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">tuple</span>[pl.Expr, pl.Expr]], otherwise: pl.Expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb1-9">) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb1-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Simplifies conditional logic in Polars by chaining multiple `when-then` expressions.</span></span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Parameters</span></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ----------</span></span>
<span id="cb1-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    caselist</span></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        A list of (condition, value) pairs. Each condition is evaluated in order,</span></span>
<span id="cb1-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        and the corresponding value is returned when a condition is met.</span></span>
<span id="cb1-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    otherwise</span></span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        The fallback value to use if none of the conditions match.</span></span>
<span id="cb1-20"></span>
<span id="cb1-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Returns</span></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    -------</span></span>
<span id="cb1-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    pl.Expr</span></span>
<span id="cb1-24"></span>
<span id="cb1-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Examples:</span></span>
<span id="cb1-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    -------</span></span>
<span id="cb1-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ```python</span></span>
<span id="cb1-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    import polars as pl</span></span>
<span id="cb1-29"></span>
<span id="cb1-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    df = pl.DataFrame({"x": [1, 2, 3, 4]})</span></span>
<span id="cb1-31"></span>
<span id="cb1-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    expr = case_when(</span></span>
<span id="cb1-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        caselist=[</span></span>
<span id="cb1-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">            (pl.col("x") &lt; 2, pl.lit("small")),</span></span>
<span id="cb1-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">            (pl.col("x") &lt; 4, pl.lit("medium"))</span></span>
<span id="cb1-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        ],</span></span>
<span id="cb1-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        otherwise=pl.lit("large"),</span></span>
<span id="cb1-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ).alias("size")</span></span>
<span id="cb1-39"></span>
<span id="cb1-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    # This is equivalent to writing:</span></span>
<span id="cb1-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    # expr = (</span></span>
<span id="cb1-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    #     pl.when(pl.col("x") &lt; 2)</span></span>
<span id="cb1-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    #       .then(pl.lit("small"))</span></span>
<span id="cb1-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    #       .when(pl.col("x") &lt; 4)</span></span>
<span id="cb1-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    #       .then(pl.lit("medium"))</span></span>
<span id="cb1-46"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    #       .otherwise(pl.lit("large"))</span></span>
<span id="cb1-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    #       .alias("size")</span></span>
<span id="cb1-48"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    # )</span></span>
<span id="cb1-49"></span>
<span id="cb1-50"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    df.with_columns(expr)</span></span>
<span id="cb1-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ```</span></span>
<span id="cb1-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    shape: (4, 2)</span></span>
<span id="cb1-53"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ┌─────┬────────┐</span></span>
<span id="cb1-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    │ x   ┆ size   │</span></span>
<span id="cb1-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    │ --- ┆ ---    │</span></span>
<span id="cb1-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    │ i64 ┆ str    │</span></span>
<span id="cb1-57"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ├─────┼────────┤</span></span>
<span id="cb1-58"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    │ 1   ┆ small  │</span></span>
<span id="cb1-59"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    │ 2   ┆ medium │</span></span>
<span id="cb1-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    │ 3   ┆ medium │</span></span>
<span id="cb1-61"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    │ 4   ┆ large  │</span></span>
<span id="cb1-62"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    └─────┴────────┘</span></span>
<span id="cb1-63"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb1-64">    (first_when, first_then), <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>cases <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> caselist</span>
<span id="cb1-65"></span>
<span id="cb1-66">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># first</span></span>
<span id="cb1-67">    expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.when(first_when).then(first_then)</span>
<span id="cb1-68"></span>
<span id="cb1-69">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># middles</span></span>
<span id="cb1-70">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> when, then <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cases:</span>
<span id="cb1-71">        expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr.when(when).then(then)</span>
<span id="cb1-72"></span>
<span id="cb1-73">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># last</span></span>
<span id="cb1-74">    expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr.otherwise(otherwise)</span>
<span id="cb1-75"></span>
<span id="cb1-76">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> expr</span></code></pre></div></div>
</div>
</section>
<section id="custom-expression-namespace" class="level2">
<h2 class="anchored" data-anchor-id="custom-expression-namespace">Custom Expression Namespace</h2>
<p>With <code>case_when()</code> in place, we can refactor the <code>DiscreteSplitter</code> expression namespace like this:</p>
<div id="7c520db6" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@cache</span></span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _mod_expr(n: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb2-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> pl.int_range(pl.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.UInt32).mod(n)</span>
<span id="cb2-4"></span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _litify(lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[pl.lit]:</span>
<span id="cb2-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [pl.lit(lit) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> lit <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> lits]</span>
<span id="cb2-8"></span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@pl.api.register_expr_namespace</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spt"</span>)</span>
<span id="cb2-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DiscreteSplitter:</span>
<span id="cb2-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, expr: pl.Expr) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb2-13">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr</span>
<span id="cb2-14"></span>
<span id="cb2-15">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _get_expr(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any], name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>):</span>
<span id="cb2-16">        n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(lits)</span>
<span id="cb2-17">        mod_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _mod_expr(n)</span>
<span id="cb2-18">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>litified, litified_otherwise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _litify(lits)</span>
<span id="cb2-19">        caselist <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(mod_expr.eq(i), lit) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, lit <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(litified)]</span>
<span id="cb2-20">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> case_when(caselist, litified_otherwise).alias(name)</span>
<span id="cb2-21"></span>
<span id="cb2-22">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> binarize(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lit1: Any, lit2: Any, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binarized"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb2-23">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.bucketize([lit1, lit2], name)</span>
<span id="cb2-24"></span>
<span id="cb2-25">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> trinarize(</span>
<span id="cb2-26">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lit1: Any, lit2: Any, lit3: Any, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trinarized"</span></span>
<span id="cb2-27">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb2-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.bucketize([lit1, lit2, lit3], name)</span>
<span id="cb2-29"></span>
<span id="cb2-30">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> bucketize(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any], name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bucketized"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb2-31">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._get_expr(lits, name)</span></code></pre></div></div>
</div>
<p>Now, <code>bucketize()</code> is the primary method that encapsulates the core logic for categorical mapping. <code>binarize()</code> and <code>trinarize()</code> are just convenient wrappers for common cases.</p>
<p>Here’s a simple example of using the custom expression namespace:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb3-2">    pl.DataFrame({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">72</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">83</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">97</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">51</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">77</span>]})</span>
<span id="cb3-3">    .with_row_index(offset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-4">    .with_columns(</span>
<span id="cb3-5">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>).spt.binarize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>),</span>
<span id="cb3-6">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>).spt.trinarize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"one"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"three"</span>),</span>
<span id="cb3-7">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>).spt.bucketize([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]),</span>
<span id="cb3-8">    )</span>
<span id="cb3-9">)</span></code></pre></div></div>
<pre><code>shape: (9, 5)
┌───────┬─────┬────────────┬────────────┬────────────┐
│ index ┆ n   ┆ binarized  ┆ trinarized ┆ bucketized │
│ ---   ┆ --- ┆ ---        ┆ ---        ┆ ---        │
│ u32   ┆ i64 ┆ str        ┆ str        ┆ i32        │
╞═══════╪═════╪════════════╪════════════╪════════════╡
│ 1     ┆ 100 ┆ lightblue  ┆ one        ┆ 1          │
│ 2     ┆ 50  ┆ papayawhip ┆ two        ┆ 2          │
│ 3     ┆ 72  ┆ lightblue  ┆ three      ┆ 3          │
│ 4     ┆ 83  ┆ papayawhip ┆ one        ┆ 4          │
│ 5     ┆ 97  ┆ lightblue  ┆ two        ┆ 1          │
│ 6     ┆ 42  ┆ papayawhip ┆ three      ┆ 2          │
│ 7     ┆ 20  ┆ lightblue  ┆ one        ┆ 3          │
│ 8     ┆ 51  ┆ papayawhip ┆ two        ┆ 4          │
│ 9     ┆ 77  ┆ lightblue  ┆ three      ┆ 1          │
└───────┴─────┴────────────┴────────────┴────────────┘</code></pre>
</section>
<section id="custom-dataframe-namespace" class="level2">
<h2 class="anchored" data-anchor-id="custom-dataframe-namespace">Custom DataFrame Namespace</h2>
<p>Instead of relying on <a href="https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.with_row_index.html">pl.DataFrame.with_row_index()</a>, we can also use <code>_mod_expr()</code> directly to enable similar categorization.</p>
<p>Here’s how the <code>DiscreteSplitter</code> can be implemented as a custom DataFrame namespace:</p>
<div id="8ee88836" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@pl.api.register_dataframe_namespace</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spt"</span>)</span>
<span id="cb5-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DiscreteSplitter:</span>
<span id="cb5-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, df: pl.DataFrame) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb5-4">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df</span>
<span id="cb5-5"></span>
<span id="cb5-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _get_expr(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any], name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>):</span>
<span id="cb5-7">        n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(lits)</span>
<span id="cb5-8">        mod_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _mod_expr(n)</span>
<span id="cb5-9">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>litified, litified_otherwise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _litify(lits)</span>
<span id="cb5-10">        caselist <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(mod_expr.eq(i), lit) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, lit <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(litified)]</span>
<span id="cb5-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> case_when(caselist, litified_otherwise).alias(name)</span>
<span id="cb5-12"></span>
<span id="cb5-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _get_final_df(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any], name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb5-14">        cls <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>)</span>
<span id="cb5-15">        expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._get_expr(lits, name)</span>
<span id="cb5-16">        new_spt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cls(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._df.with_columns(expr))</span>
<span id="cb5-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> new_spt._df</span>
<span id="cb5-18"></span>
<span id="cb5-19">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> binarize(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lit1: Any, lit2: Any, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binarized"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb5-20">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.bucketize([lit1, lit2], name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>name)</span>
<span id="cb5-21"></span>
<span id="cb5-22">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> trinarize(</span>
<span id="cb5-23">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lit1: Any, lit2: Any, lit3: Any, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trinarized"</span></span>
<span id="cb5-24">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb5-25">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.bucketize([lit1, lit2, lit3], name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>name)</span>
<span id="cb5-26"></span>
<span id="cb5-27">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> bucketize(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any], name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bucketized"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb5-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._get_final_df(lits, name)</span></code></pre></div></div>
</div>
<p>Example usage:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb6-2">    pl.DataFrame({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">72</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">83</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">97</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">51</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">77</span>]})</span>
<span id="cb6-3">    .spt.binarize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>)</span>
<span id="cb6-4">    .spt.trinarize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"one"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"three"</span>)</span>
<span id="cb6-5">    .spt.bucketize([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])</span>
<span id="cb6-6">    .with_row_index(offset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb6-7">)</span></code></pre></div></div>
<pre><code>shape: (9, 5)
┌───────┬─────┬────────────┬────────────┬────────────┐
│ index ┆ n   ┆ binarized  ┆ trinarized ┆ bucketized │
│ ---   ┆ --- ┆ ---        ┆ ---        ┆ ---        │
│ u32   ┆ i64 ┆ str        ┆ str        ┆ i32        │
╞═══════╪═════╪════════════╪════════════╪════════════╡
│ 1     ┆ 100 ┆ lightblue  ┆ one        ┆ 1          │
│ 2     ┆ 50  ┆ papayawhip ┆ two        ┆ 2          │
│ 3     ┆ 72  ┆ lightblue  ┆ three      ┆ 3          │
│ 4     ┆ 83  ┆ papayawhip ┆ one        ┆ 4          │
│ 5     ┆ 97  ┆ lightblue  ┆ two        ┆ 1          │
│ 6     ┆ 42  ┆ papayawhip ┆ three      ┆ 2          │
│ 7     ┆ 20  ┆ lightblue  ┆ one        ┆ 3          │
│ 8     ┆ 51  ┆ papayawhip ┆ two        ┆ 4          │
│ 9     ┆ 77  ┆ lightblue  ┆ three      ┆ 1          │
└───────┴─────┴────────────┴────────────┴────────────┘</code></pre>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>Extracting the conditional logic into a standalone <code>case_when()</code> function turned out to be both a practical and satisfying exercise—perfect for a rainy afternoon of coding. It not only improves readability but also makes the branching logic easier to reuse and reason about.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>polars</category>
  <guid>https://tech.ycwu.space/posts/casewhen-in-polars/20250525.html</guid>
  <pubDate>Sun, 25 May 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/casewhen-in-polars/code.png" medium="image" type="image/png" height="90" width="144"/>
</item>
</channel>
</rss>
