<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>til on Lud.cc ~ Ludwig's digital home</title><link>https://lud.cc/tags/til/</link><description>Recent content in til on Lud.cc ~ Ludwig's digital home</description><generator>Hugo</generator><language>en-us</language><copyright>CC BY-SA 4.0</copyright><lastBuildDate>Wed, 30 Apr 2025 11:59:54 +0100</lastBuildDate><atom:link href="https://lud.cc/tags/til/index.xml" rel="self" type="application/rss+xml"/><item><title>Micromort</title><link>https://lud.cc/micromort/</link><pubDate>Tue, 18 Feb 2025 00:00:00 +0000</pubDate><guid>https://lud.cc/micromort/</guid><description>&lt;blockquote&gt;
&lt;p&gt;A micromort (from micro- and mortality) is a unit of risk defined as a one-in-a-million chance of death. Micromorts can be used to measure the riskiness of various day-to-day activities. A microprobability is a one-in-a million chance of some event; thus, a micromort is the microprobability of death.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;— &lt;a href="https://en.wikipedia.org/wiki/Micromort"&gt;Wikipedia&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I discovered this word while reading this question:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;How many micromorts is a day in the life of a 90-year old?&lt;/p&gt;</description></item><item><title>Eager vs lazy evaluation</title><link>https://lud.cc/eager-vs-lazy-evaluation/</link><pubDate>Sat, 15 Feb 2025 00:00:00 +0000</pubDate><guid>https://lud.cc/eager-vs-lazy-evaluation/</guid><description>&lt;h1 id="til-boolthen_some-in-rust"&gt;TIL &lt;code&gt;bool::then_some&lt;/code&gt; in Rust&lt;/h1&gt;
&lt;p&gt;While participating in Advent of Code, I learned about &lt;a href="https://doc.rust-lang.org/std/primitive.bool.html#method.then_some"&gt;&lt;code&gt;bool::then_some&lt;/code&gt;&lt;/a&gt;. Because I tried to finish the problem on time, I did not read fully the documentation. I wrote a &lt;code&gt;get&lt;/code&gt; helper function to access an element in a 2D array. The code (simplified to 1D) looked like:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#586e75;background-color:#eee8d5;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#859900"&gt;fn&lt;/span&gt; &lt;span style="color:#268bd2"&gt;main&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#268bd2"&gt;dbg!&lt;/span&gt;(&lt;span style="color:#268bd2"&gt;get&lt;/span&gt;(&amp;amp;&lt;span style="color:#268bd2"&gt;vec!&lt;/span&gt;[&lt;span style="color:#2aa198;font-weight:bold"&gt;42&lt;/span&gt;, &lt;span style="color:#2aa198;font-weight:bold"&gt;51&lt;/span&gt;], &lt;span style="color:#2aa198;font-weight:bold"&gt;4&lt;/span&gt;));
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#859900"&gt;fn&lt;/span&gt; &lt;span style="color:#268bd2"&gt;get&lt;/span&gt;(&lt;span style="color:#268bd2"&gt;v&lt;/span&gt;: &lt;span style="color:#859900"&gt;&amp;amp;&lt;/span&gt;[&lt;span style="color:#859900;font-weight:bold"&gt;u8&lt;/span&gt;], &lt;span style="color:#268bd2"&gt;i&lt;/span&gt;: &lt;span style="color:#859900;font-weight:bold"&gt;usize&lt;/span&gt;) -&amp;gt; &lt;span style="color:#cb4b16"&gt;Option&lt;/span&gt;&amp;lt;&lt;span style="color:#859900;font-weight:bold"&gt;u8&lt;/span&gt;&amp;gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; (&lt;span style="color:#268bd2"&gt;i&lt;/span&gt; &amp;lt; &lt;span style="color:#268bd2"&gt;v&lt;/span&gt;.&lt;span style="color:#268bd2"&gt;len&lt;/span&gt;()).&lt;span style="color:#268bd2"&gt;then_some&lt;/span&gt;(&lt;span style="color:#268bd2"&gt;v&lt;/span&gt;[&lt;span style="color:#268bd2"&gt;i&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It compiles fine, but it crashes:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#586e75;background-color:#eee8d5;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;thread &amp;#39;main&amp;#39; panicked at src/main.rs:6:29:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;index out of bounds: the len is 2 but the index is 4
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Cargo-Process exited abnormally with code 101 at Thu Feb 13 21:04:58
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;What is wrong with this code?&lt;/p&gt;</description></item></channel></rss>