<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[The Cave of Programming Blog: Exploring Rust]]></title><description><![CDATA[A tutorial on Rust for people who already know how to program in other languages.]]></description><link>https://blog.caveofprogramming.com/s/rust-for-programmers</link><image><url>https://substackcdn.com/image/fetch/$s_!azPy!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c4c1ed6-3a97-4be6-922e-54978ab5d302_816x816.png</url><title>The Cave of Programming Blog: Exploring Rust</title><link>https://blog.caveofprogramming.com/s/rust-for-programmers</link></image><generator>Substack</generator><lastBuildDate>Sun, 03 May 2026 10:03:12 GMT</lastBuildDate><atom:link href="https://blog.caveofprogramming.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[John Purcell]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[caveofprogramming@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[caveofprogramming@substack.com]]></itunes:email><itunes:name><![CDATA[John Purcell]]></itunes:name></itunes:owner><itunes:author><![CDATA[John Purcell]]></itunes:author><googleplay:owner><![CDATA[caveofprogramming@substack.com]]></googleplay:owner><googleplay:email><![CDATA[caveofprogramming@substack.com]]></googleplay:email><googleplay:author><![CDATA[John Purcell]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Realtime Producer-Consumer in Rust]]></title><description><![CDATA[Watch now | Using channels to feed data in realtime to a consumer (for audio or video)]]></description><link>https://blog.caveofprogramming.com/p/realtime-producer-consumer-in-rust</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/realtime-producer-consumer-in-rust</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Fri, 16 Jan 2026 20:02:00 GMT</pubDate><enclosure url="https://api.substack.com/feed/podcast/184763013/7b4b0d00a38c5e8d50a80788dbc38cc4.mp3" length="0" type="audio/mpeg"/><content:encoded><![CDATA[<p>In this video I&#8217;m setting up a producer-consumer that can handle audio or video data, where the rate at which the producer creates data is constrained by the rate at which the consumer consumes and processes data.</p><p>You can find the full source code here: <a href="https://github.com/caveofprogramming/rust/tree/6577c7c11343420822467ac8462f195955108afc/043%20Handling%20Realtime%20Data/realtime">https://github.com/caveofprogramming/rust/</a></p><p>To do this I&#8217;m implementing a data block recycling mechanism, so that after some initial buffering, the consumer passes used data blocks back to the producer, which fills in new data and adds them to the queue as soon as the consumer has read more data from the queue.</p><p>Here I&#8217;m using crossbeam 0.8, so my cargo.toml has this in it:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!H6qY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3de746b1-151d-416a-8afa-424e4f527c68_502x156.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!H6qY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3de746b1-151d-416a-8afa-424e4f527c68_502x156.png 424w, https://substackcdn.com/image/fetch/$s_!H6qY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3de746b1-151d-416a-8afa-424e4f527c68_502x156.png 848w, https://substackcdn.com/image/fetch/$s_!H6qY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3de746b1-151d-416a-8afa-424e4f527c68_502x156.png 1272w, https://substackcdn.com/image/fetch/$s_!H6qY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3de746b1-151d-416a-8afa-424e4f527c68_502x156.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!H6qY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3de746b1-151d-416a-8afa-424e4f527c68_502x156.png" width="274" height="85.14741035856574" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3de746b1-151d-416a-8afa-424e4f527c68_502x156.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:156,&quot;width&quot;:502,&quot;resizeWidth&quot;:274,&quot;bytes&quot;:16568,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/184763013?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3de746b1-151d-416a-8afa-424e4f527c68_502x156.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!H6qY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3de746b1-151d-416a-8afa-424e4f527c68_502x156.png 424w, https://substackcdn.com/image/fetch/$s_!H6qY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3de746b1-151d-416a-8afa-424e4f527c68_502x156.png 848w, https://substackcdn.com/image/fetch/$s_!H6qY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3de746b1-151d-416a-8afa-424e4f527c68_502x156.png 1272w, https://substackcdn.com/image/fetch/$s_!H6qY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3de746b1-151d-416a-8afa-424e4f527c68_502x156.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p>My imports look like this:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!K7-7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c667e-581b-4927-b6bb-e2d9c090de27_1372x198.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!K7-7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c667e-581b-4927-b6bb-e2d9c090de27_1372x198.png 424w, https://substackcdn.com/image/fetch/$s_!K7-7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c667e-581b-4927-b6bb-e2d9c090de27_1372x198.png 848w, https://substackcdn.com/image/fetch/$s_!K7-7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c667e-581b-4927-b6bb-e2d9c090de27_1372x198.png 1272w, https://substackcdn.com/image/fetch/$s_!K7-7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c667e-581b-4927-b6bb-e2d9c090de27_1372x198.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!K7-7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c667e-581b-4927-b6bb-e2d9c090de27_1372x198.png" width="634" height="91.49562682215743" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b69c667e-581b-4927-b6bb-e2d9c090de27_1372x198.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:198,&quot;width&quot;:1372,&quot;resizeWidth&quot;:634,&quot;bytes&quot;:49554,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/184763013?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c667e-581b-4927-b6bb-e2d9c090de27_1372x198.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!K7-7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c667e-581b-4927-b6bb-e2d9c090de27_1372x198.png 424w, https://substackcdn.com/image/fetch/$s_!K7-7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c667e-581b-4927-b6bb-e2d9c090de27_1372x198.png 848w, https://substackcdn.com/image/fetch/$s_!K7-7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c667e-581b-4927-b6bb-e2d9c090de27_1372x198.png 1272w, https://substackcdn.com/image/fetch/$s_!K7-7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c667e-581b-4927-b6bb-e2d9c090de27_1372x198.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><p>I&#8217;m using a bounded crossbeam channel. This is a channel which will only accept a specified number of data blocks. This is what constrains the speed of my producer; the producer cannot add new data blocks into the channel, once the channel&#8217;s full, until the consumer reads a block from the channel.</p><p>I&#8217;m using <code>Duration</code> here just to limit the speed of the consumer, to simulate processing video or audio at a fixed pace.</p><p>Then I have some definitions:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!jp27!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77c2f7f3-bd5e-4aef-a643-a7116cdd13f6_986x216.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!jp27!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77c2f7f3-bd5e-4aef-a643-a7116cdd13f6_986x216.png 424w, https://substackcdn.com/image/fetch/$s_!jp27!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77c2f7f3-bd5e-4aef-a643-a7116cdd13f6_986x216.png 848w, https://substackcdn.com/image/fetch/$s_!jp27!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77c2f7f3-bd5e-4aef-a643-a7116cdd13f6_986x216.png 1272w, https://substackcdn.com/image/fetch/$s_!jp27!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77c2f7f3-bd5e-4aef-a643-a7116cdd13f6_986x216.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!jp27!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77c2f7f3-bd5e-4aef-a643-a7116cdd13f6_986x216.png" width="446" height="97.70385395537525" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/77c2f7f3-bd5e-4aef-a643-a7116cdd13f6_986x216.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:216,&quot;width&quot;:986,&quot;resizeWidth&quot;:446,&quot;bytes&quot;:48028,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/184763013?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77c2f7f3-bd5e-4aef-a643-a7116cdd13f6_986x216.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!jp27!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77c2f7f3-bd5e-4aef-a643-a7116cdd13f6_986x216.png 424w, https://substackcdn.com/image/fetch/$s_!jp27!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77c2f7f3-bd5e-4aef-a643-a7116cdd13f6_986x216.png 848w, https://substackcdn.com/image/fetch/$s_!jp27!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77c2f7f3-bd5e-4aef-a643-a7116cdd13f6_986x216.png 1272w, https://substackcdn.com/image/fetch/$s_!jp27!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77c2f7f3-bd5e-4aef-a643-a7116cdd13f6_986x216.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><p><code>BLOCK_SIZE</code> is the size of a single block of data. More typically it might be 512, 1024 or whatever.</p><p><code>N_BLOCKS</code> is the size of the crossbeam channel.</p><p>Then <code>BLOCK</code> is the block definition itself. A block in this case is an array of <code>BLOCK_SIZE</code> elements, of type <code>f64</code> (but you can use whatever type is useful). I wrap this in a <code>Box</code> smart pointer just so that these blocks exist on the heap, not the stack, and the channel actually contains references to them.</p><p>My two channels are created like this:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!I6fe!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e94682-6bf0-4832-b11d-ba1f0af38ca4_2172x130.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!I6fe!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e94682-6bf0-4832-b11d-ba1f0af38ca4_2172x130.png 424w, https://substackcdn.com/image/fetch/$s_!I6fe!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e94682-6bf0-4832-b11d-ba1f0af38ca4_2172x130.png 848w, https://substackcdn.com/image/fetch/$s_!I6fe!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e94682-6bf0-4832-b11d-ba1f0af38ca4_2172x130.png 1272w, https://substackcdn.com/image/fetch/$s_!I6fe!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e94682-6bf0-4832-b11d-ba1f0af38ca4_2172x130.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!I6fe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e94682-6bf0-4832-b11d-ba1f0af38ca4_2172x130.png" width="1456" height="87" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/65e94682-6bf0-4832-b11d-ba1f0af38ca4_2172x130.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:87,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83344,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/184763013?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e94682-6bf0-4832-b11d-ba1f0af38ca4_2172x130.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!I6fe!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e94682-6bf0-4832-b11d-ba1f0af38ca4_2172x130.png 424w, https://substackcdn.com/image/fetch/$s_!I6fe!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e94682-6bf0-4832-b11d-ba1f0af38ca4_2172x130.png 848w, https://substackcdn.com/image/fetch/$s_!I6fe!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e94682-6bf0-4832-b11d-ba1f0af38ca4_2172x130.png 1272w, https://substackcdn.com/image/fetch/$s_!I6fe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e94682-6bf0-4832-b11d-ba1f0af38ca4_2172x130.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>I have one channel for sending data to the consumer from the producer, and a second for &#8216;recycling&#8217; the blocks by sending them from the consumer back to the producer.</p><p>In the producer I use <code>send() </code>to send the blocks and <code>recv()</code> to receive them. These methods both wait until (in the case of <code>send()</code>) there is space on the queue to send, or until (in the case of <code>recv()</code>) a block is available to be received.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VNJk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3894f00-a66c-4553-876d-429897bc0f70_1472x1228.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VNJk!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3894f00-a66c-4553-876d-429897bc0f70_1472x1228.png 424w, https://substackcdn.com/image/fetch/$s_!VNJk!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3894f00-a66c-4553-876d-429897bc0f70_1472x1228.png 848w, https://substackcdn.com/image/fetch/$s_!VNJk!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3894f00-a66c-4553-876d-429897bc0f70_1472x1228.png 1272w, https://substackcdn.com/image/fetch/$s_!VNJk!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3894f00-a66c-4553-876d-429897bc0f70_1472x1228.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VNJk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3894f00-a66c-4553-876d-429897bc0f70_1472x1228.png" width="1456" height="1215" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c3894f00-a66c-4553-876d-429897bc0f70_1472x1228.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1215,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:193045,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/184763013?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3894f00-a66c-4553-876d-429897bc0f70_1472x1228.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VNJk!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3894f00-a66c-4553-876d-429897bc0f70_1472x1228.png 424w, https://substackcdn.com/image/fetch/$s_!VNJk!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3894f00-a66c-4553-876d-429897bc0f70_1472x1228.png 848w, https://substackcdn.com/image/fetch/$s_!VNJk!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3894f00-a66c-4553-876d-429897bc0f70_1472x1228.png 1272w, https://substackcdn.com/image/fetch/$s_!VNJk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3894f00-a66c-4553-876d-429897bc0f70_1472x1228.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The consumer is as follows. Now I&#8217;m using try_recv() to receive data. This returns immediately and will return an error if there&#8217;s no data to receive, so I&#8217;m relying on the producer successfully keeping up.</p><p>Then I use send to return the data block back to the producer. I think this should be OK, but we could also consider making the return queue unbounded (or just really large) to avoid any possible delay.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gLBp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76c37f73-2fef-48b9-a0c5-6c43f8b98901_1242x840.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gLBp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76c37f73-2fef-48b9-a0c5-6c43f8b98901_1242x840.png 424w, https://substackcdn.com/image/fetch/$s_!gLBp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76c37f73-2fef-48b9-a0c5-6c43f8b98901_1242x840.png 848w, https://substackcdn.com/image/fetch/$s_!gLBp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76c37f73-2fef-48b9-a0c5-6c43f8b98901_1242x840.png 1272w, https://substackcdn.com/image/fetch/$s_!gLBp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76c37f73-2fef-48b9-a0c5-6c43f8b98901_1242x840.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gLBp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76c37f73-2fef-48b9-a0c5-6c43f8b98901_1242x840.png" width="1242" height="840" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/76c37f73-2fef-48b9-a0c5-6c43f8b98901_1242x840.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:840,&quot;width&quot;:1242,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:142474,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/184763013?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76c37f73-2fef-48b9-a0c5-6c43f8b98901_1242x840.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gLBp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76c37f73-2fef-48b9-a0c5-6c43f8b98901_1242x840.png 424w, https://substackcdn.com/image/fetch/$s_!gLBp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76c37f73-2fef-48b9-a0c5-6c43f8b98901_1242x840.png 848w, https://substackcdn.com/image/fetch/$s_!gLBp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76c37f73-2fef-48b9-a0c5-6c43f8b98901_1242x840.png 1272w, https://substackcdn.com/image/fetch/$s_!gLBp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76c37f73-2fef-48b9-a0c5-6c43f8b98901_1242x840.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Finally, we wait for the threads to finish.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!T4RW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe5d21f1-b54c-420f-8b70-932d8537b6ad_514x118.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!T4RW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe5d21f1-b54c-420f-8b70-932d8537b6ad_514x118.png 424w, https://substackcdn.com/image/fetch/$s_!T4RW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe5d21f1-b54c-420f-8b70-932d8537b6ad_514x118.png 848w, https://substackcdn.com/image/fetch/$s_!T4RW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe5d21f1-b54c-420f-8b70-932d8537b6ad_514x118.png 1272w, https://substackcdn.com/image/fetch/$s_!T4RW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe5d21f1-b54c-420f-8b70-932d8537b6ad_514x118.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!T4RW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe5d21f1-b54c-420f-8b70-932d8537b6ad_514x118.png" width="330" height="75.75875486381322" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fe5d21f1-b54c-420f-8b70-932d8537b6ad_514x118.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:118,&quot;width&quot;:514,&quot;resizeWidth&quot;:330,&quot;bytes&quot;:21373,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/184763013?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe5d21f1-b54c-420f-8b70-932d8537b6ad_514x118.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!T4RW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe5d21f1-b54c-420f-8b70-932d8537b6ad_514x118.png 424w, https://substackcdn.com/image/fetch/$s_!T4RW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe5d21f1-b54c-420f-8b70-932d8537b6ad_514x118.png 848w, https://substackcdn.com/image/fetch/$s_!T4RW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe5d21f1-b54c-420f-8b70-932d8537b6ad_514x118.png 1272w, https://substackcdn.com/image/fetch/$s_!T4RW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe5d21f1-b54c-420f-8b70-932d8537b6ad_514x118.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The output from running the program looks like this:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!64iC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89a38fb4-af79-4e28-93d5-2281614785b0_1138x1264.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!64iC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89a38fb4-af79-4e28-93d5-2281614785b0_1138x1264.png 424w, https://substackcdn.com/image/fetch/$s_!64iC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89a38fb4-af79-4e28-93d5-2281614785b0_1138x1264.png 848w, https://substackcdn.com/image/fetch/$s_!64iC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89a38fb4-af79-4e28-93d5-2281614785b0_1138x1264.png 1272w, https://substackcdn.com/image/fetch/$s_!64iC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89a38fb4-af79-4e28-93d5-2281614785b0_1138x1264.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!64iC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89a38fb4-af79-4e28-93d5-2281614785b0_1138x1264.png" width="424" height="470.94551845342704" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/89a38fb4-af79-4e28-93d5-2281614785b0_1138x1264.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1264,&quot;width&quot;:1138,&quot;resizeWidth&quot;:424,&quot;bytes&quot;:294162,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/184763013?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89a38fb4-af79-4e28-93d5-2281614785b0_1138x1264.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!64iC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89a38fb4-af79-4e28-93d5-2281614785b0_1138x1264.png 424w, https://substackcdn.com/image/fetch/$s_!64iC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89a38fb4-af79-4e28-93d5-2281614785b0_1138x1264.png 848w, https://substackcdn.com/image/fetch/$s_!64iC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89a38fb4-af79-4e28-93d5-2281614785b0_1138x1264.png 1272w, https://substackcdn.com/image/fetch/$s_!64iC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89a38fb4-af79-4e28-93d5-2281614785b0_1138x1264.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p>]]></content:encoded></item><item><title><![CDATA[The Most Frustrating Problem in Rust]]></title><description><![CDATA[Watch now | Referring to persistent entities in multiple threads]]></description><link>https://blog.caveofprogramming.com/p/the-most-frustrating-problem-in-rust</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/the-most-frustrating-problem-in-rust</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Fri, 19 Dec 2025 19:07:20 GMT</pubDate><enclosure url="https://api.substack.com/feed/podcast/182084728/a09a134985b40ca70dc3d2cf331f085c.mp3" length="0" type="audio/mpeg"/><content:encoded><![CDATA[<p>Code: <a href="https://github.com/caveofprogramming/rust/tree/0a6c295cb4eac303d0a817784c56a3cc12720973/042%20Most%20Frustrating/demo">https://github.com/caveofprogramming/rust/</a></p><p>The most frustrating thing about Rust that I&#8217;ve found so far is the problem of how to refer to persistent entities that have to maintain their own state, across multiple threads.</p><p>In this video I&#8217;ll show you how to tackle the problem using <code>Arc</code> and <code>Mutex</code>.</p><p>However, this isn&#8217;t suitable for every situation.</p><p>For example, if your persistent entity happens to manage a collection of software instruments which have to be modified from a particular thread, while your audio thread separately has to read audio samples from those instruments, and the persistent entity must remain modifiable&#8212;then probably Mutexes aren&#8217;t the answer.</p><p>Locking mutexes in audio threads, or real-time threads generally, is widely considered a bad idea.</p><p>The best way I&#8217;ve found so far of dealing with this, after trying out a bunch of different things, is to use channels, which we covered earlier. These may not be fast enough to send one audio sample at a time, but we can send small blocks of data (maybe 512 samples) at a time, and recycle the blocks to and from the producer and consumer threads. That&#8217;s a topic for a future post!</p>]]></content:encoded></item><item><title><![CDATA[About Box, Dyn and clone_box in Rust]]></title><description><![CDATA[The lengths you have to go to for object-orientation in Rust ....]]></description><link>https://blog.caveofprogramming.com/p/about-box-dyn-and-clone_box-in-rust</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/about-box-dyn-and-clone_box-in-rust</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Thu, 13 Nov 2025 19:46:17 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/6386a4ce-8e75-48e8-aa7c-7e87d4da8eb3_1024x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p></p><div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;2958b282-0edb-482f-b7fb-b30cab27184a&quot;,&quot;duration&quot;:null}"></div><p>Git repo: <a href="https://github.com/caveofprogramming/rust/tree/6b1c90043fb3a56d1949a2e3b0ed1919d90cdb56/041%20Dyn%20and%20Cloning">https://github.com/caveofprogramming/rust/</a></p><p>For many of us, writing Rust in an object-oriented style would seem natural, since we&#8217;re used to Java and Python and what-not.</p><p>But Rust does make you jump through a few hoops, for the sake of efficiency.</p><p>In this post we&#8217;ll see how to get object oriented behaviour in Rust, even when we want to be able to clone a vector of objects, which is sometimes very useful.</p><p>First let&#8217;s start with a simple trait.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MpPQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe514cd5a-246f-4870-b771-2a1cab000420_638x210.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MpPQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe514cd5a-246f-4870-b771-2a1cab000420_638x210.png 424w, https://substackcdn.com/image/fetch/$s_!MpPQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe514cd5a-246f-4870-b771-2a1cab000420_638x210.png 848w, https://substackcdn.com/image/fetch/$s_!MpPQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe514cd5a-246f-4870-b771-2a1cab000420_638x210.png 1272w, https://substackcdn.com/image/fetch/$s_!MpPQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe514cd5a-246f-4870-b771-2a1cab000420_638x210.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MpPQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe514cd5a-246f-4870-b771-2a1cab000420_638x210.png" width="362" height="119.15360501567397" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e514cd5a-246f-4870-b771-2a1cab000420_638x210.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:210,&quot;width&quot;:638,&quot;resizeWidth&quot;:362,&quot;bytes&quot;:24983,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe514cd5a-246f-4870-b771-2a1cab000420_638x210.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!MpPQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe514cd5a-246f-4870-b771-2a1cab000420_638x210.png 424w, https://substackcdn.com/image/fetch/$s_!MpPQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe514cd5a-246f-4870-b771-2a1cab000420_638x210.png 848w, https://substackcdn.com/image/fetch/$s_!MpPQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe514cd5a-246f-4870-b771-2a1cab000420_638x210.png 1272w, https://substackcdn.com/image/fetch/$s_!MpPQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe514cd5a-246f-4870-b771-2a1cab000420_638x210.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p>Anything that implements this trait must have a <code>get</code> method that returns a float.</p><p>Now we&#8217;ll create a struct that implements the trait.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3hvF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3bcc0e1-0a28-4cb2-a493-113f3546a1ec_786x1122.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3hvF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3bcc0e1-0a28-4cb2-a493-113f3546a1ec_786x1122.png 424w, https://substackcdn.com/image/fetch/$s_!3hvF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3bcc0e1-0a28-4cb2-a493-113f3546a1ec_786x1122.png 848w, https://substackcdn.com/image/fetch/$s_!3hvF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3bcc0e1-0a28-4cb2-a493-113f3546a1ec_786x1122.png 1272w, https://substackcdn.com/image/fetch/$s_!3hvF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3bcc0e1-0a28-4cb2-a493-113f3546a1ec_786x1122.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3hvF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3bcc0e1-0a28-4cb2-a493-113f3546a1ec_786x1122.png" width="340" height="485.3435114503817" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f3bcc0e1-0a28-4cb2-a493-113f3546a1ec_786x1122.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1122,&quot;width&quot;:786,&quot;resizeWidth&quot;:340,&quot;bytes&quot;:90910,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3bcc0e1-0a28-4cb2-a493-113f3546a1ec_786x1122.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!3hvF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3bcc0e1-0a28-4cb2-a493-113f3546a1ec_786x1122.png 424w, https://substackcdn.com/image/fetch/$s_!3hvF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3bcc0e1-0a28-4cb2-a493-113f3546a1ec_786x1122.png 848w, https://substackcdn.com/image/fetch/$s_!3hvF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3bcc0e1-0a28-4cb2-a493-113f3546a1ec_786x1122.png 1272w, https://substackcdn.com/image/fetch/$s_!3hvF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3bcc0e1-0a28-4cb2-a493-113f3546a1ec_786x1122.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I&#8217;ve implemented the trait for <code>Pi</code>, and I&#8217;ve also added a constructor.</p><p>Now we can use <code>Pi</code> like this:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!nXQd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2110555f-6f3a-4a9a-96fd-1af6702e8256_986x294.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!nXQd!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2110555f-6f3a-4a9a-96fd-1af6702e8256_986x294.png 424w, https://substackcdn.com/image/fetch/$s_!nXQd!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2110555f-6f3a-4a9a-96fd-1af6702e8256_986x294.png 848w, https://substackcdn.com/image/fetch/$s_!nXQd!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2110555f-6f3a-4a9a-96fd-1af6702e8256_986x294.png 1272w, https://substackcdn.com/image/fetch/$s_!nXQd!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2110555f-6f3a-4a9a-96fd-1af6702e8256_986x294.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!nXQd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2110555f-6f3a-4a9a-96fd-1af6702e8256_986x294.png" width="462" height="137.75659229208924" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2110555f-6f3a-4a9a-96fd-1af6702e8256_986x294.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:294,&quot;width&quot;:986,&quot;resizeWidth&quot;:462,&quot;bytes&quot;:40398,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2110555f-6f3a-4a9a-96fd-1af6702e8256_986x294.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!nXQd!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2110555f-6f3a-4a9a-96fd-1af6702e8256_986x294.png 424w, https://substackcdn.com/image/fetch/$s_!nXQd!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2110555f-6f3a-4a9a-96fd-1af6702e8256_986x294.png 848w, https://substackcdn.com/image/fetch/$s_!nXQd!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2110555f-6f3a-4a9a-96fd-1af6702e8256_986x294.png 1272w, https://substackcdn.com/image/fetch/$s_!nXQd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2110555f-6f3a-4a9a-96fd-1af6702e8256_986x294.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This prints:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!nIGs!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56a2df0-0cea-4e52-a4b6-6c993a661484_396x38.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!nIGs!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56a2df0-0cea-4e52-a4b6-6c993a661484_396x38.png 424w, https://substackcdn.com/image/fetch/$s_!nIGs!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56a2df0-0cea-4e52-a4b6-6c993a661484_396x38.png 848w, https://substackcdn.com/image/fetch/$s_!nIGs!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56a2df0-0cea-4e52-a4b6-6c993a661484_396x38.png 1272w, https://substackcdn.com/image/fetch/$s_!nIGs!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56a2df0-0cea-4e52-a4b6-6c993a661484_396x38.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!nIGs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56a2df0-0cea-4e52-a4b6-6c993a661484_396x38.png" width="222" height="21.303030303030305" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f56a2df0-0cea-4e52-a4b6-6c993a661484_396x38.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:38,&quot;width&quot;:396,&quot;resizeWidth&quot;:222,&quot;bytes&quot;:5708,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06b67852-f542-4331-8e9b-d89cac9755ac_396x48.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!nIGs!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56a2df0-0cea-4e52-a4b6-6c993a661484_396x38.png 424w, https://substackcdn.com/image/fetch/$s_!nIGs!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56a2df0-0cea-4e52-a4b6-6c993a661484_396x38.png 848w, https://substackcdn.com/image/fetch/$s_!nIGs!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56a2df0-0cea-4e52-a4b6-6c993a661484_396x38.png 1272w, https://substackcdn.com/image/fetch/$s_!nIGs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56a2df0-0cea-4e52-a4b6-6c993a661484_396x38.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>I can also add Pi to vectors:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kUIJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11da37cd-e10a-42fd-9efa-4ed49f3a2d92_940x332.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kUIJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11da37cd-e10a-42fd-9efa-4ed49f3a2d92_940x332.png 424w, https://substackcdn.com/image/fetch/$s_!kUIJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11da37cd-e10a-42fd-9efa-4ed49f3a2d92_940x332.png 848w, https://substackcdn.com/image/fetch/$s_!kUIJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11da37cd-e10a-42fd-9efa-4ed49f3a2d92_940x332.png 1272w, https://substackcdn.com/image/fetch/$s_!kUIJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11da37cd-e10a-42fd-9efa-4ed49f3a2d92_940x332.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kUIJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11da37cd-e10a-42fd-9efa-4ed49f3a2d92_940x332.png" width="388" height="137.03829787234042" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/11da37cd-e10a-42fd-9efa-4ed49f3a2d92_940x332.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:332,&quot;width&quot;:940,&quot;resizeWidth&quot;:388,&quot;bytes&quot;:48843,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11da37cd-e10a-42fd-9efa-4ed49f3a2d92_940x332.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!kUIJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11da37cd-e10a-42fd-9efa-4ed49f3a2d92_940x332.png 424w, https://substackcdn.com/image/fetch/$s_!kUIJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11da37cd-e10a-42fd-9efa-4ed49f3a2d92_940x332.png 848w, https://substackcdn.com/image/fetch/$s_!kUIJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11da37cd-e10a-42fd-9efa-4ed49f3a2d92_940x332.png 1272w, https://substackcdn.com/image/fetch/$s_!kUIJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11da37cd-e10a-42fd-9efa-4ed49f3a2d92_940x332.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This works fine.</p><p>However, there&#8217;s a problem if I change the type of the vector to <code>Vec&lt;Value&gt;</code>, intending to put other things that implement the Value trait into the vector.</p><p>This doesn&#8217;t work:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MMsY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa04b5334-5c1b-4574-ad37-e2a49e9003bd_1072x338.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MMsY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa04b5334-5c1b-4574-ad37-e2a49e9003bd_1072x338.png 424w, https://substackcdn.com/image/fetch/$s_!MMsY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa04b5334-5c1b-4574-ad37-e2a49e9003bd_1072x338.png 848w, https://substackcdn.com/image/fetch/$s_!MMsY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa04b5334-5c1b-4574-ad37-e2a49e9003bd_1072x338.png 1272w, https://substackcdn.com/image/fetch/$s_!MMsY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa04b5334-5c1b-4574-ad37-e2a49e9003bd_1072x338.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MMsY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa04b5334-5c1b-4574-ad37-e2a49e9003bd_1072x338.png" width="432" height="136.2089552238806" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a04b5334-5c1b-4574-ad37-e2a49e9003bd_1072x338.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:338,&quot;width&quot;:1072,&quot;resizeWidth&quot;:432,&quot;bytes&quot;:56509,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa04b5334-5c1b-4574-ad37-e2a49e9003bd_1072x338.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!MMsY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa04b5334-5c1b-4574-ad37-e2a49e9003bd_1072x338.png 424w, https://substackcdn.com/image/fetch/$s_!MMsY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa04b5334-5c1b-4574-ad37-e2a49e9003bd_1072x338.png 848w, https://substackcdn.com/image/fetch/$s_!MMsY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa04b5334-5c1b-4574-ad37-e2a49e9003bd_1072x338.png 1272w, https://substackcdn.com/image/fetch/$s_!MMsY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa04b5334-5c1b-4574-ad37-e2a49e9003bd_1072x338.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The problem is that Rust, for efficiency, wants to know the size of everything you put in a vector, and here it doesn&#8217;t know that. We could create other things of difference sizes that implement <code>Value</code>, even at runtime, and put them in the vector.</p><p>The values in the vector will just be stored in contiguous blocks of memory, which can&#8217;t work if Rust doesn&#8217;t even know the size of them.</p><p>Furthermore, we don&#8217;t get the polymorphic behaviour we expect. We want the correct <code>get</code> method to be called, depending on what objects are found in the vector.</p><p>We can fix the first issue by storing an array of pointers, so that the actual objects are stored on the heap. To make polymorphism work, we need to use the <code>dyn</code> keyword.</p><p>We end up with the following working code:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ETxZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1abf0d70-0749-4a24-a7aa-5280197d0c2a_1574x354.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ETxZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1abf0d70-0749-4a24-a7aa-5280197d0c2a_1574x354.png 424w, https://substackcdn.com/image/fetch/$s_!ETxZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1abf0d70-0749-4a24-a7aa-5280197d0c2a_1574x354.png 848w, https://substackcdn.com/image/fetch/$s_!ETxZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1abf0d70-0749-4a24-a7aa-5280197d0c2a_1574x354.png 1272w, https://substackcdn.com/image/fetch/$s_!ETxZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1abf0d70-0749-4a24-a7aa-5280197d0c2a_1574x354.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ETxZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1abf0d70-0749-4a24-a7aa-5280197d0c2a_1574x354.png" width="538" height="120.8282967032967" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1abf0d70-0749-4a24-a7aa-5280197d0c2a_1574x354.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:327,&quot;width&quot;:1456,&quot;resizeWidth&quot;:538,&quot;bytes&quot;:70697,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1abf0d70-0749-4a24-a7aa-5280197d0c2a_1574x354.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ETxZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1abf0d70-0749-4a24-a7aa-5280197d0c2a_1574x354.png 424w, https://substackcdn.com/image/fetch/$s_!ETxZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1abf0d70-0749-4a24-a7aa-5280197d0c2a_1574x354.png 848w, https://substackcdn.com/image/fetch/$s_!ETxZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1abf0d70-0749-4a24-a7aa-5280197d0c2a_1574x354.png 1272w, https://substackcdn.com/image/fetch/$s_!ETxZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1abf0d70-0749-4a24-a7aa-5280197d0c2a_1574x354.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>By wrapping the vector elements in a <code>Box</code> and by using the <code>dyn</code> keyword, we get the result we expect.</p><p>Now suppose we want to clone the entire vector for some reason.</p><p>The following code still doesn&#8217;t work.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kaBG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cd550c8-317d-48fb-9852-58a07d795fa0_1580x728.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kaBG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cd550c8-317d-48fb-9852-58a07d795fa0_1580x728.png 424w, https://substackcdn.com/image/fetch/$s_!kaBG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cd550c8-317d-48fb-9852-58a07d795fa0_1580x728.png 848w, https://substackcdn.com/image/fetch/$s_!kaBG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cd550c8-317d-48fb-9852-58a07d795fa0_1580x728.png 1272w, https://substackcdn.com/image/fetch/$s_!kaBG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cd550c8-317d-48fb-9852-58a07d795fa0_1580x728.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kaBG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cd550c8-317d-48fb-9852-58a07d795fa0_1580x728.png" width="1456" height="671" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9cd550c8-317d-48fb-9852-58a07d795fa0_1580x728.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:671,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:126459,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cd550c8-317d-48fb-9852-58a07d795fa0_1580x728.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!kaBG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cd550c8-317d-48fb-9852-58a07d795fa0_1580x728.png 424w, https://substackcdn.com/image/fetch/$s_!kaBG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cd550c8-317d-48fb-9852-58a07d795fa0_1580x728.png 848w, https://substackcdn.com/image/fetch/$s_!kaBG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cd550c8-317d-48fb-9852-58a07d795fa0_1580x728.png 1272w, https://substackcdn.com/image/fetch/$s_!kaBG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cd550c8-317d-48fb-9852-58a07d795fa0_1580x728.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Rust knows how to clone the vector; it just doesn&#8217;t know how to clone </p><p><code>Box&lt;dyn Value&gt;</code></p><p>To fix this, we have to implement Clone for Box&lt;dyn Value&gt;.</p><p>The following implementation would work if we only ever expected <code>Pi</code> to implement <code>Value</code> (which is obviously a bit silly).</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CC-_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d69af04-985a-46ce-b083-d695854c84e4_1014x346.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CC-_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d69af04-985a-46ce-b083-d695854c84e4_1014x346.png 424w, https://substackcdn.com/image/fetch/$s_!CC-_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d69af04-985a-46ce-b083-d695854c84e4_1014x346.png 848w, https://substackcdn.com/image/fetch/$s_!CC-_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d69af04-985a-46ce-b083-d695854c84e4_1014x346.png 1272w, https://substackcdn.com/image/fetch/$s_!CC-_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d69af04-985a-46ce-b083-d695854c84e4_1014x346.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CC-_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d69af04-985a-46ce-b083-d695854c84e4_1014x346.png" width="526" height="179.48323471400394" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3d69af04-985a-46ce-b083-d695854c84e4_1014x346.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:346,&quot;width&quot;:1014,&quot;resizeWidth&quot;:526,&quot;bytes&quot;:57530,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d69af04-985a-46ce-b083-d695854c84e4_1014x346.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CC-_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d69af04-985a-46ce-b083-d695854c84e4_1014x346.png 424w, https://substackcdn.com/image/fetch/$s_!CC-_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d69af04-985a-46ce-b083-d695854c84e4_1014x346.png 848w, https://substackcdn.com/image/fetch/$s_!CC-_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d69af04-985a-46ce-b083-d695854c84e4_1014x346.png 1272w, https://substackcdn.com/image/fetch/$s_!CC-_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d69af04-985a-46ce-b083-d695854c84e4_1014x346.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Now the program runs, but the problem is that when we clone a <code>Vector</code> of <code>Value</code> objects, we&#8217;re always going to get a <code>Vector</code> of <code>Pi</code>.</p><p>The solution is to give each implementation of Value a method which returns a box containing the cloned version of itself.</p><p>An easy way to make Pi cloneable is just to add <code>#[derive(Clone)]</code>at the top of it. But we could alternatively implement <code>Clone</code> for <code>Pi</code> however we want.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9C3U!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde78fe69-d4c3-4600-b0d4-1f6d92fb873d_464x276.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9C3U!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde78fe69-d4c3-4600-b0d4-1f6d92fb873d_464x276.png 424w, https://substackcdn.com/image/fetch/$s_!9C3U!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde78fe69-d4c3-4600-b0d4-1f6d92fb873d_464x276.png 848w, https://substackcdn.com/image/fetch/$s_!9C3U!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde78fe69-d4c3-4600-b0d4-1f6d92fb873d_464x276.png 1272w, https://substackcdn.com/image/fetch/$s_!9C3U!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde78fe69-d4c3-4600-b0d4-1f6d92fb873d_464x276.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9C3U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde78fe69-d4c3-4600-b0d4-1f6d92fb873d_464x276.png" width="296" height="176.06896551724137" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/de78fe69-d4c3-4600-b0d4-1f6d92fb873d_464x276.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:276,&quot;width&quot;:464,&quot;resizeWidth&quot;:296,&quot;bytes&quot;:26289,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde78fe69-d4c3-4600-b0d4-1f6d92fb873d_464x276.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9C3U!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde78fe69-d4c3-4600-b0d4-1f6d92fb873d_464x276.png 424w, https://substackcdn.com/image/fetch/$s_!9C3U!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde78fe69-d4c3-4600-b0d4-1f6d92fb873d_464x276.png 848w, https://substackcdn.com/image/fetch/$s_!9C3U!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde78fe69-d4c3-4600-b0d4-1f6d92fb873d_464x276.png 1272w, https://substackcdn.com/image/fetch/$s_!9C3U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde78fe69-d4c3-4600-b0d4-1f6d92fb873d_464x276.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Now we add a method to the <code>Value</code> trait called <code>clone_box</code>.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3-mc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8889f4fb-de81-4e02-92bd-9cdf5578d3a0_1084x274.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3-mc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8889f4fb-de81-4e02-92bd-9cdf5578d3a0_1084x274.png 424w, https://substackcdn.com/image/fetch/$s_!3-mc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8889f4fb-de81-4e02-92bd-9cdf5578d3a0_1084x274.png 848w, https://substackcdn.com/image/fetch/$s_!3-mc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8889f4fb-de81-4e02-92bd-9cdf5578d3a0_1084x274.png 1272w, https://substackcdn.com/image/fetch/$s_!3-mc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8889f4fb-de81-4e02-92bd-9cdf5578d3a0_1084x274.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3-mc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8889f4fb-de81-4e02-92bd-9cdf5578d3a0_1084x274.png" width="478" height="120.82287822878229" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8889f4fb-de81-4e02-92bd-9cdf5578d3a0_1084x274.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:274,&quot;width&quot;:1084,&quot;resizeWidth&quot;:478,&quot;bytes&quot;:49712,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8889f4fb-de81-4e02-92bd-9cdf5578d3a0_1084x274.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!3-mc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8889f4fb-de81-4e02-92bd-9cdf5578d3a0_1084x274.png 424w, https://substackcdn.com/image/fetch/$s_!3-mc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8889f4fb-de81-4e02-92bd-9cdf5578d3a0_1084x274.png 848w, https://substackcdn.com/image/fetch/$s_!3-mc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8889f4fb-de81-4e02-92bd-9cdf5578d3a0_1084x274.png 1272w, https://substackcdn.com/image/fetch/$s_!3-mc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8889f4fb-de81-4e02-92bd-9cdf5578d3a0_1084x274.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Then we implement this for <code>Pi</code>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yitO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d2a0fb-b0b0-47b4-89ab-ebe3497dc7df_1116x618.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yitO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d2a0fb-b0b0-47b4-89ab-ebe3497dc7df_1116x618.png 424w, https://substackcdn.com/image/fetch/$s_!yitO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d2a0fb-b0b0-47b4-89ab-ebe3497dc7df_1116x618.png 848w, https://substackcdn.com/image/fetch/$s_!yitO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d2a0fb-b0b0-47b4-89ab-ebe3497dc7df_1116x618.png 1272w, https://substackcdn.com/image/fetch/$s_!yitO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d2a0fb-b0b0-47b4-89ab-ebe3497dc7df_1116x618.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yitO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d2a0fb-b0b0-47b4-89ab-ebe3497dc7df_1116x618.png" width="514" height="284.63440860215053" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/29d2a0fb-b0b0-47b4-89ab-ebe3497dc7df_1116x618.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:618,&quot;width&quot;:1116,&quot;resizeWidth&quot;:514,&quot;bytes&quot;:82368,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d2a0fb-b0b0-47b4-89ab-ebe3497dc7df_1116x618.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yitO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d2a0fb-b0b0-47b4-89ab-ebe3497dc7df_1116x618.png 424w, https://substackcdn.com/image/fetch/$s_!yitO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d2a0fb-b0b0-47b4-89ab-ebe3497dc7df_1116x618.png 848w, https://substackcdn.com/image/fetch/$s_!yitO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d2a0fb-b0b0-47b4-89ab-ebe3497dc7df_1116x618.png 1272w, https://substackcdn.com/image/fetch/$s_!yitO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d2a0fb-b0b0-47b4-89ab-ebe3497dc7df_1116x618.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Finally, we use this in our implementation of <code>Clone</code> for <code>Box&lt;dyn Value&gt;</code>.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!NhLP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cf2610d-e5a5-4126-8120-16eb03649d90_1030x352.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NhLP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cf2610d-e5a5-4126-8120-16eb03649d90_1030x352.png 424w, https://substackcdn.com/image/fetch/$s_!NhLP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cf2610d-e5a5-4126-8120-16eb03649d90_1030x352.png 848w, https://substackcdn.com/image/fetch/$s_!NhLP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cf2610d-e5a5-4126-8120-16eb03649d90_1030x352.png 1272w, https://substackcdn.com/image/fetch/$s_!NhLP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cf2610d-e5a5-4126-8120-16eb03649d90_1030x352.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!NhLP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cf2610d-e5a5-4126-8120-16eb03649d90_1030x352.png" width="480" height="164.03883495145632" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5cf2610d-e5a5-4126-8120-16eb03649d90_1030x352.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:352,&quot;width&quot;:1030,&quot;resizeWidth&quot;:480,&quot;bytes&quot;:57224,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cf2610d-e5a5-4126-8120-16eb03649d90_1030x352.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!NhLP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cf2610d-e5a5-4126-8120-16eb03649d90_1030x352.png 424w, https://substackcdn.com/image/fetch/$s_!NhLP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cf2610d-e5a5-4126-8120-16eb03649d90_1030x352.png 848w, https://substackcdn.com/image/fetch/$s_!NhLP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cf2610d-e5a5-4126-8120-16eb03649d90_1030x352.png 1272w, https://substackcdn.com/image/fetch/$s_!NhLP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cf2610d-e5a5-4126-8120-16eb03649d90_1030x352.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Now our program works properly.</p><p>We can create something else that implements <code>Value</code>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Bcin!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd63a0f52-de7e-4b77-9709-7b0fa79b5622_1138x1418.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Bcin!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd63a0f52-de7e-4b77-9709-7b0fa79b5622_1138x1418.png 424w, https://substackcdn.com/image/fetch/$s_!Bcin!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd63a0f52-de7e-4b77-9709-7b0fa79b5622_1138x1418.png 848w, https://substackcdn.com/image/fetch/$s_!Bcin!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd63a0f52-de7e-4b77-9709-7b0fa79b5622_1138x1418.png 1272w, https://substackcdn.com/image/fetch/$s_!Bcin!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd63a0f52-de7e-4b77-9709-7b0fa79b5622_1138x1418.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Bcin!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd63a0f52-de7e-4b77-9709-7b0fa79b5622_1138x1418.png" width="512" height="637.97539543058" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d63a0f52-de7e-4b77-9709-7b0fa79b5622_1138x1418.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1418,&quot;width&quot;:1138,&quot;resizeWidth&quot;:512,&quot;bytes&quot;:162933,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd63a0f52-de7e-4b77-9709-7b0fa79b5622_1138x1418.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Bcin!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd63a0f52-de7e-4b77-9709-7b0fa79b5622_1138x1418.png 424w, https://substackcdn.com/image/fetch/$s_!Bcin!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd63a0f52-de7e-4b77-9709-7b0fa79b5622_1138x1418.png 848w, https://substackcdn.com/image/fetch/$s_!Bcin!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd63a0f52-de7e-4b77-9709-7b0fa79b5622_1138x1418.png 1272w, https://substackcdn.com/image/fetch/$s_!Bcin!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd63a0f52-de7e-4b77-9709-7b0fa79b5622_1138x1418.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>Now we can add Surprise to our vector and everything still works correctly.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!AN7J!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b6dc3e1-9197-484f-a785-12df0f469093_1184x982.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!AN7J!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b6dc3e1-9197-484f-a785-12df0f469093_1184x982.png 424w, https://substackcdn.com/image/fetch/$s_!AN7J!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b6dc3e1-9197-484f-a785-12df0f469093_1184x982.png 848w, https://substackcdn.com/image/fetch/$s_!AN7J!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b6dc3e1-9197-484f-a785-12df0f469093_1184x982.png 1272w, https://substackcdn.com/image/fetch/$s_!AN7J!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b6dc3e1-9197-484f-a785-12df0f469093_1184x982.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!AN7J!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b6dc3e1-9197-484f-a785-12df0f469093_1184x982.png" width="498" height="413.0371621621622" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3b6dc3e1-9197-484f-a785-12df0f469093_1184x982.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:982,&quot;width&quot;:1184,&quot;resizeWidth&quot;:498,&quot;bytes&quot;:149035,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b6dc3e1-9197-484f-a785-12df0f469093_1184x982.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!AN7J!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b6dc3e1-9197-484f-a785-12df0f469093_1184x982.png 424w, https://substackcdn.com/image/fetch/$s_!AN7J!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b6dc3e1-9197-484f-a785-12df0f469093_1184x982.png 848w, https://substackcdn.com/image/fetch/$s_!AN7J!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b6dc3e1-9197-484f-a785-12df0f469093_1184x982.png 1272w, https://substackcdn.com/image/fetch/$s_!AN7J!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b6dc3e1-9197-484f-a785-12df0f469093_1184x982.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>When we run the program, the contents of both the original vector and the cloned vector behave appropriately.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ITlU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a894ea5-c01b-4ead-93ce-00cbc152a344_426x190.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ITlU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a894ea5-c01b-4ead-93ce-00cbc152a344_426x190.png 424w, https://substackcdn.com/image/fetch/$s_!ITlU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a894ea5-c01b-4ead-93ce-00cbc152a344_426x190.png 848w, https://substackcdn.com/image/fetch/$s_!ITlU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a894ea5-c01b-4ead-93ce-00cbc152a344_426x190.png 1272w, https://substackcdn.com/image/fetch/$s_!ITlU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a894ea5-c01b-4ead-93ce-00cbc152a344_426x190.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ITlU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a894ea5-c01b-4ead-93ce-00cbc152a344_426x190.png" width="276" height="123.09859154929578" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7a894ea5-c01b-4ead-93ce-00cbc152a344_426x190.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:190,&quot;width&quot;:426,&quot;resizeWidth&quot;:276,&quot;bytes&quot;:22659,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178805571?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a894ea5-c01b-4ead-93ce-00cbc152a344_426x190.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ITlU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a894ea5-c01b-4ead-93ce-00cbc152a344_426x190.png 424w, https://substackcdn.com/image/fetch/$s_!ITlU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a894ea5-c01b-4ead-93ce-00cbc152a344_426x190.png 848w, https://substackcdn.com/image/fetch/$s_!ITlU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a894ea5-c01b-4ead-93ce-00cbc152a344_426x190.png 1272w, https://substackcdn.com/image/fetch/$s_!ITlU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a894ea5-c01b-4ead-93ce-00cbc152a344_426x190.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p>]]></content:encoded></item><item><title><![CDATA[Sending Messages Across Threads in Rust]]></title><description><![CDATA[A solution to your MVC nightmares]]></description><link>https://blog.caveofprogramming.com/p/sending-messages-across-threads-in</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/sending-messages-across-threads-in</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Fri, 07 Nov 2025 21:17:08 GMT</pubDate><enclosure url="https://api.substack.com/feed/podcast/178296875/a16be1ff18a873a74fdf8387699ced19.mp3" length="0" type="audio/mpeg"/><content:encoded><![CDATA[<p>You can find the full code for the complete example I cover in the video here: <a href="https://github.com/caveofprogramming/rust/tree/cc5f16911852975c174ba30702cad57cf6aa85be/040%20Messaging%20Between%20Threads/messaging">https://github.com/caveofprogramming/rust/</a></p><p></p><h3>Sending and Receiving Messages</h3><p>Rust&#8217;s ownership rules can make it difficult to have multiple threads reading and writing to some data model.</p><p>Inter-thread messaging is a very elegant alternative.</p><p>For this example you need <code>crossbeam-channel = &#8220;0.5&#8221;</code> in your dependencies.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2jiG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff763e69c-6ccb-42c8-a99e-96c745f3f629_1432x620.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2jiG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff763e69c-6ccb-42c8-a99e-96c745f3f629_1432x620.png 424w, https://substackcdn.com/image/fetch/$s_!2jiG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff763e69c-6ccb-42c8-a99e-96c745f3f629_1432x620.png 848w, https://substackcdn.com/image/fetch/$s_!2jiG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff763e69c-6ccb-42c8-a99e-96c745f3f629_1432x620.png 1272w, https://substackcdn.com/image/fetch/$s_!2jiG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff763e69c-6ccb-42c8-a99e-96c745f3f629_1432x620.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2jiG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff763e69c-6ccb-42c8-a99e-96c745f3f629_1432x620.png" width="1432" height="620" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f763e69c-6ccb-42c8-a99e-96c745f3f629_1432x620.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:620,&quot;width&quot;:1432,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:113458,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178296875?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff763e69c-6ccb-42c8-a99e-96c745f3f629_1432x620.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!2jiG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff763e69c-6ccb-42c8-a99e-96c745f3f629_1432x620.png 424w, https://substackcdn.com/image/fetch/$s_!2jiG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff763e69c-6ccb-42c8-a99e-96c745f3f629_1432x620.png 848w, https://substackcdn.com/image/fetch/$s_!2jiG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff763e69c-6ccb-42c8-a99e-96c745f3f629_1432x620.png 1272w, https://substackcdn.com/image/fetch/$s_!2jiG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff763e69c-6ccb-42c8-a99e-96c745f3f629_1432x620.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In this program I use crossbeam to create an &#8220;unbounded&#8221; channel, then I send a message consisting of the value <code>123</code> down the channel. At the other end of the channel, a receiver receives the value, and we print it.</p><p>Note: the channel is &#8220;unbounded&#8221; in the sense that we can send as many messages as we like, even if they haven&#8217;t been received yet. We can also create bounded channels that limit the number of messages that can be sent when they are not being received.</p><p>You can send vectors, structs, enums and complex data structures through a crossbeam channel.</p><p>The real value of this is:</p><ul><li><p>The sender and receiver do not have to exist in the same thread. They can be in different threads, and thread synchronisation problems are thereby avoided, since we&#8217;re not reading or writing shared data.</p></li><li><p>With crossbeam you can clone the sender and receiver as many times as you want, and pass them around to different functions and to different threads, just as you like. A function or thread can take ownership of the clone, but it still refers to the same channel.</p></li></ul><p>For example, you can have an &#8220;update engine&#8221; thread which solely deals with a data model, and fields requests for bits of data or applies updates to the model on the basis of messages sent to it.</p><p></p><p>We can also create a &#8220;bounded&#8221; channel.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!aMsz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ee54701-24af-443c-8205-029faf298703_1450x606.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!aMsz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ee54701-24af-443c-8205-029faf298703_1450x606.png 424w, https://substackcdn.com/image/fetch/$s_!aMsz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ee54701-24af-443c-8205-029faf298703_1450x606.png 848w, https://substackcdn.com/image/fetch/$s_!aMsz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ee54701-24af-443c-8205-029faf298703_1450x606.png 1272w, https://substackcdn.com/image/fetch/$s_!aMsz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ee54701-24af-443c-8205-029faf298703_1450x606.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!aMsz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ee54701-24af-443c-8205-029faf298703_1450x606.png" width="1450" height="606" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3ee54701-24af-443c-8205-029faf298703_1450x606.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:606,&quot;width&quot;:1450,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:108700,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/178296875?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e714980-287e-4cbf-abe0-35c8c04cc195_1450x636.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!aMsz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ee54701-24af-443c-8205-029faf298703_1450x606.png 424w, https://substackcdn.com/image/fetch/$s_!aMsz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ee54701-24af-443c-8205-029faf298703_1450x606.png 848w, https://substackcdn.com/image/fetch/$s_!aMsz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ee54701-24af-443c-8205-029faf298703_1450x606.png 1272w, https://substackcdn.com/image/fetch/$s_!aMsz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ee54701-24af-443c-8205-029faf298703_1450x606.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This channel will only let us send messages one at a time; each message must be received before a new one can be sent. This channel is apparently a little more lightweight, and can be used to send replies to messages containing data.</p><p>I cover both in the video, and we also see how this looks in the (more realistic) context of using threads.</p>]]></content:encoded></item><item><title><![CDATA[Dynamic Dispatch]]></title><description><![CDATA[Some pseudo-object orientation in Rust]]></description><link>https://blog.caveofprogramming.com/p/dynamic-dispatch</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/dynamic-dispatch</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Wed, 22 Oct 2025 13:57:41 GMT</pubDate><enclosure url="https://api.substack.com/feed/podcast/176502487/15e37a7df85fbd3f67e700c10033b23b.mp3" length="0" type="audio/mpeg"/><content:encoded><![CDATA[<p>Code: <a href="https://github.com/caveofprogramming/rust/tree/c848baecc63cc156cf8ebf3b4581291b0c53f879/039%20Dynamic%20Dispatch/disp">https://github.com/caveofprogramming/rust/</a></p><h3>What is Dynamic Dispatch?</h3><p>In object-oriented languages we can normally create subclasses of a superclass type, then have a variable of the superclass type which can be made to refer &#8220;dynamically&#8221; at runtime to whichever of the subclasses we like.</p><p>For example, we could have an <code>Animal</code> superclass, and <code>Dog</code> and <code>Cat</code> subclasses.</p><p><code>Dog</code> and <code>Cat</code> are then kinds of <code>Animal</code>.</p><p>Suppose <code>Animal</code> has a <code>speak()</code> method. <code>Dog</code> and <code>Cat</code> then inherit this method, but they can override it, so that each has its own version of <code>speak()</code> with different functionality.</p><p>It&#8217;s then possible to write a program which, for example, asks the user what kind of <code>Animal</code> to create, and sets a variable of type <code>Animal</code> to refer to whatever they&#8217;ve chosen.</p><p>When the <code>speak() </code>method of this method is called, the correct version of it must then by called by the program at runtime. If the user chose to create a <code>Dog</code>, the <code>Dog</code> <code>speak()</code> method is called; if they chose a <code>Cat</code>, the <code>Cat</code> <code>speak()</code> method is called.</p><p>This is known as <em>dynamic dispatch</em>. The appropriate method is dynamically dispatched at runtime.</p><p></p><h3>How This Works In Rust</h3><p>Rust is a kind of semi-object-oriented language. It doesn&#8217;t have inheritance, so we can&#8217;t create a superclass object and then have subclasses of it.</p><p>However, it does have traits, which define functionality. So it is possible to have an <code>Animal</code> trait and then <code>Dog</code> and <code>Cat</code> structs which implement this trait.</p><p>Then we implement the trait for these classes. If <code>Animal</code> defines a <code>speak()</code> method, <code>Dog</code> and <code>Cat</code> must have one.</p><p>It is then possible, in Rust, to do dynamic dispatch, even just using ordinary references.</p><p>However, I ran into some problems with this when I wanted to create objects in the new() function of some other struct, as you&#8217;ll see in the video.</p><p>These problems can be solved using the reference counter, <code>Rc</code>.</p><p>In addition, when you specify a trait as the type of a variable, you must make the variable a reference and use the <code>dyn</code> keyword, which is specifically intended to allow dynamic dispatch, apparently. Otherwise your program won&#8217;t compile.</p><p></p><h3>Dynamic Dispatch with Ordinary References</h3><p>First let&#8217;s take a look at dynamic dispatch using only references.</p><p>Here&#8217;s our trait.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!W95j!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08b6b4d-1fdd-471a-8d36-e2d5b8f972ab_450x182.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!W95j!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08b6b4d-1fdd-471a-8d36-e2d5b8f972ab_450x182.png 424w, https://substackcdn.com/image/fetch/$s_!W95j!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08b6b4d-1fdd-471a-8d36-e2d5b8f972ab_450x182.png 848w, https://substackcdn.com/image/fetch/$s_!W95j!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08b6b4d-1fdd-471a-8d36-e2d5b8f972ab_450x182.png 1272w, https://substackcdn.com/image/fetch/$s_!W95j!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08b6b4d-1fdd-471a-8d36-e2d5b8f972ab_450x182.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!W95j!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08b6b4d-1fdd-471a-8d36-e2d5b8f972ab_450x182.png" width="280" height="113.24444444444444" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f08b6b4d-1fdd-471a-8d36-e2d5b8f972ab_450x182.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:182,&quot;width&quot;:450,&quot;resizeWidth&quot;:280,&quot;bytes&quot;:18904,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/176502487?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08b6b4d-1fdd-471a-8d36-e2d5b8f972ab_450x182.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!W95j!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08b6b4d-1fdd-471a-8d36-e2d5b8f972ab_450x182.png 424w, https://substackcdn.com/image/fetch/$s_!W95j!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08b6b4d-1fdd-471a-8d36-e2d5b8f972ab_450x182.png 848w, https://substackcdn.com/image/fetch/$s_!W95j!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08b6b4d-1fdd-471a-8d36-e2d5b8f972ab_450x182.png 1272w, https://substackcdn.com/image/fetch/$s_!W95j!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08b6b4d-1fdd-471a-8d36-e2d5b8f972ab_450x182.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Dog and Cat are then just structs which implement this trait.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Q5h2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93e7283c-5e2d-45d6-be4a-f01d272930bd_1114x986.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Q5h2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93e7283c-5e2d-45d6-be4a-f01d272930bd_1114x986.png 424w, https://substackcdn.com/image/fetch/$s_!Q5h2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93e7283c-5e2d-45d6-be4a-f01d272930bd_1114x986.png 848w, https://substackcdn.com/image/fetch/$s_!Q5h2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93e7283c-5e2d-45d6-be4a-f01d272930bd_1114x986.png 1272w, https://substackcdn.com/image/fetch/$s_!Q5h2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93e7283c-5e2d-45d6-be4a-f01d272930bd_1114x986.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Q5h2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93e7283c-5e2d-45d6-be4a-f01d272930bd_1114x986.png" width="576" height="509.8168761220826" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/93e7283c-5e2d-45d6-be4a-f01d272930bd_1114x986.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:986,&quot;width&quot;:1114,&quot;resizeWidth&quot;:576,&quot;bytes&quot;:116085,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/176502487?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93e7283c-5e2d-45d6-be4a-f01d272930bd_1114x986.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Q5h2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93e7283c-5e2d-45d6-be4a-f01d272930bd_1114x986.png 424w, https://substackcdn.com/image/fetch/$s_!Q5h2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93e7283c-5e2d-45d6-be4a-f01d272930bd_1114x986.png 848w, https://substackcdn.com/image/fetch/$s_!Q5h2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93e7283c-5e2d-45d6-be4a-f01d272930bd_1114x986.png 1272w, https://substackcdn.com/image/fetch/$s_!Q5h2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93e7283c-5e2d-45d6-be4a-f01d272930bd_1114x986.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Give this, the following actually works perfectly well:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ivxL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcea294e3-ccad-45c5-ab7a-3bd14f6c4b5b_970x530.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ivxL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcea294e3-ccad-45c5-ab7a-3bd14f6c4b5b_970x530.png 424w, https://substackcdn.com/image/fetch/$s_!ivxL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcea294e3-ccad-45c5-ab7a-3bd14f6c4b5b_970x530.png 848w, https://substackcdn.com/image/fetch/$s_!ivxL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcea294e3-ccad-45c5-ab7a-3bd14f6c4b5b_970x530.png 1272w, https://substackcdn.com/image/fetch/$s_!ivxL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcea294e3-ccad-45c5-ab7a-3bd14f6c4b5b_970x530.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ivxL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcea294e3-ccad-45c5-ab7a-3bd14f6c4b5b_970x530.png" width="530" height="289.58762886597935" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cea294e3-ccad-45c5-ab7a-3bd14f6c4b5b_970x530.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:530,&quot;width&quot;:970,&quot;resizeWidth&quot;:530,&quot;bytes&quot;:80600,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/176502487?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcea294e3-ccad-45c5-ab7a-3bd14f6c4b5b_970x530.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ivxL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcea294e3-ccad-45c5-ab7a-3bd14f6c4b5b_970x530.png 424w, https://substackcdn.com/image/fetch/$s_!ivxL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcea294e3-ccad-45c5-ab7a-3bd14f6c4b5b_970x530.png 848w, https://substackcdn.com/image/fetch/$s_!ivxL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcea294e3-ccad-45c5-ab7a-3bd14f6c4b5b_970x530.png 1272w, https://substackcdn.com/image/fetch/$s_!ivxL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcea294e3-ccad-45c5-ab7a-3bd14f6c4b5b_970x530.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SGsM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28ad84d6-436a-4577-800e-8fb3643a4e66_648x91.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SGsM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28ad84d6-436a-4577-800e-8fb3643a4e66_648x91.png 424w, https://substackcdn.com/image/fetch/$s_!SGsM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28ad84d6-436a-4577-800e-8fb3643a4e66_648x91.png 848w, https://substackcdn.com/image/fetch/$s_!SGsM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28ad84d6-436a-4577-800e-8fb3643a4e66_648x91.png 1272w, https://substackcdn.com/image/fetch/$s_!SGsM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28ad84d6-436a-4577-800e-8fb3643a4e66_648x91.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SGsM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28ad84d6-436a-4577-800e-8fb3643a4e66_648x91.png" width="424" height="59.54320987654321" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/28ad84d6-436a-4577-800e-8fb3643a4e66_648x91.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:91,&quot;width&quot;:648,&quot;resizeWidth&quot;:424,&quot;bytes&quot;:11895,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/176502487?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F73b6d704-a06c-4036-93be-f2802f8d5364_648x108.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!SGsM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28ad84d6-436a-4577-800e-8fb3643a4e66_648x91.png 424w, https://substackcdn.com/image/fetch/$s_!SGsM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28ad84d6-436a-4577-800e-8fb3643a4e66_648x91.png 848w, https://substackcdn.com/image/fetch/$s_!SGsM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28ad84d6-436a-4577-800e-8fb3643a4e66_648x91.png 1272w, https://substackcdn.com/image/fetch/$s_!SGsM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28ad84d6-436a-4577-800e-8fb3643a4e66_648x91.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The problem is that this doesn&#8217;t work when we want to create the struct instances in a method, such as <code>new()</code>, and store references in struct fields, because then we end up with dangling references to objects that have gone out of scope.</p><p>Then we need reference counters, and I cover this in the video.</p><p>Let&#8217;s take a look here at a simple example of dynamic dispatch using reference counters.</p><p></p><h3>Dynamic Dispatch with Reference Counters.</h3><p>We&#8217;re going to need </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2eRq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3dece6da-f184-49e0-b163-60acc96566d5_344x43.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2eRq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3dece6da-f184-49e0-b163-60acc96566d5_344x43.png 424w, https://substackcdn.com/image/fetch/$s_!2eRq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3dece6da-f184-49e0-b163-60acc96566d5_344x43.png 848w, https://substackcdn.com/image/fetch/$s_!2eRq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3dece6da-f184-49e0-b163-60acc96566d5_344x43.png 1272w, https://substackcdn.com/image/fetch/$s_!2eRq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3dece6da-f184-49e0-b163-60acc96566d5_344x43.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2eRq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3dece6da-f184-49e0-b163-60acc96566d5_344x43.png" width="244" height="30.5" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3dece6da-f184-49e0-b163-60acc96566d5_344x43.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:43,&quot;width&quot;:344,&quot;resizeWidth&quot;:244,&quot;bytes&quot;:5247,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/176502487?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa159596b-8a92-47d5-a5e5-a550c46bb9d4_344x60.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!2eRq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3dece6da-f184-49e0-b163-60acc96566d5_344x43.png 424w, https://substackcdn.com/image/fetch/$s_!2eRq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3dece6da-f184-49e0-b163-60acc96566d5_344x43.png 848w, https://substackcdn.com/image/fetch/$s_!2eRq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3dece6da-f184-49e0-b163-60acc96566d5_344x43.png 1272w, https://substackcdn.com/image/fetch/$s_!2eRq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3dece6da-f184-49e0-b163-60acc96566d5_344x43.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>at the top of the file.</p><p>Then we can do the following, which can also be made to work with struct methods and fields.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WlqO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cbdf5a1-36aa-4e23-987e-4264adcf552a_1392x528.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WlqO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cbdf5a1-36aa-4e23-987e-4264adcf552a_1392x528.png 424w, https://substackcdn.com/image/fetch/$s_!WlqO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cbdf5a1-36aa-4e23-987e-4264adcf552a_1392x528.png 848w, https://substackcdn.com/image/fetch/$s_!WlqO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cbdf5a1-36aa-4e23-987e-4264adcf552a_1392x528.png 1272w, https://substackcdn.com/image/fetch/$s_!WlqO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cbdf5a1-36aa-4e23-987e-4264adcf552a_1392x528.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WlqO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cbdf5a1-36aa-4e23-987e-4264adcf552a_1392x528.png" width="1392" height="528" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9cbdf5a1-36aa-4e23-987e-4264adcf552a_1392x528.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:528,&quot;width&quot;:1392,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:118458,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/176502487?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cbdf5a1-36aa-4e23-987e-4264adcf552a_1392x528.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WlqO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cbdf5a1-36aa-4e23-987e-4264adcf552a_1392x528.png 424w, https://substackcdn.com/image/fetch/$s_!WlqO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cbdf5a1-36aa-4e23-987e-4264adcf552a_1392x528.png 848w, https://substackcdn.com/image/fetch/$s_!WlqO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cbdf5a1-36aa-4e23-987e-4264adcf552a_1392x528.png 1272w, https://substackcdn.com/image/fetch/$s_!WlqO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cbdf5a1-36aa-4e23-987e-4264adcf552a_1392x528.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Why Don&#8217;t Plain References Work with Struct Fields?</h3><p>This issue I had with plain references was that I wanted to have two struct fields referring to a <code>Dog</code> and a <code>Cat</code> then a third, &#8220;<code>pet</code>&#8221; that refers to one of the above, and I wanted to arrange this in a function called new() that&#8217;s associated with the struct.</p><p>One you&#8217;ve directly initialised a struct field with a <code>Dog</code>, for example, you can&#8217;t then make <code>pet</code> refer to that field via self, because self doesn&#8217;t exist yet.</p><p>You can&#8217;t create dog and cat local variables and just use references to those either, because then you&#8217;re left with dangling references.</p><p>Try it and you&#8217;ll see what I mean!</p><p>However, <code>Rc</code> fixes the problem.</p><p>If you need your objects to be mutable, you can use <code>RefCell</code> for interior mutability and use types like <code>Rc&lt;RefCell&lt;dyn Animal&#187;</code>.</p><p>If you need to pass objects between threads you could also consider</p><p><code>Arc&lt;Mutex&lt;dyn Animal&#187;</code></p><p>or</p><p><code>Rc&lt;RwLock&lt;dyn Animal&#187;.</code></p><p></p>]]></content:encoded></item><item><title><![CDATA[Model-View-Controller in Rust]]></title><description><![CDATA[Way more difficult than I thought!]]></description><link>https://blog.caveofprogramming.com/p/model-view-controller-in-rust</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/model-view-controller-in-rust</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Fri, 10 Oct 2025 21:29:37 GMT</pubDate><enclosure url="https://api.substack.com/feed/podcast/175836858/38eae53fad48f13bbf54a61123321db7.mp3" length="0" type="audio/mpeg"/><content:encoded><![CDATA[<p>I&#8217;ve been working on a software tracker synth in Rust, inspired by <a href="https://dirtywave.com/products/m8-tracker">this</a>.</p><p>One major problem I&#8217;ve faced was, MVC in Rust just doesn&#8217;t work like I thought.</p><p>It appears you can&#8217;t really just have a model and some views, and then have a controller class that creates these and passes parts of the model to the views.</p><p>In this video I&#8217;ll show you the kinds of problems I ran into. Basically they come down to problems with borrowing; you can&#8217;t have two mutable references to the same thing at the same time, so your controller just freely pass around mutable links to views.</p><p>Passing different parts of a model to different views would require multiple mutable links to different parts of the same data structure, and I couldn&#8217;t find a way to get this to work.</p><p>However, it can be done, and the trick is to use RefCell (for interior mutability) and Rc (for multiple ownership).</p><p>Full code is here: <a href="https://github.com/caveofprogramming/rust/tree/main/038%20Model%20View%20Controller/mvc">https://github.com/caveofprogramming/rust/</a></p><p>I&#8217;ll probably also make videos on the software synth I&#8217;m working on, as soon as it can play an actual melody.</p><p></p>]]></content:encoded></item><item><title><![CDATA[Mutexes in Rust]]></title><description><![CDATA[Git repo: https://github.com/caveofprogramming/rust/]]></description><link>https://blog.caveofprogramming.com/p/mutexes-in-rust</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/mutexes-in-rust</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Thu, 21 Aug 2025 14:44:10 GMT</pubDate><enclosure url="https://api.substack.com/feed/podcast/170349874/3cb7f12ec287a13c2b6f6fbc7fe084bd.mp3" length="0" type="audio/mpeg"/><content:encoded><![CDATA[<p>Git repo: <a href="https://github.com/caveofprogramming/rust/tree/main/036%20Mutexes/mutexes">https://github.com/caveofprogramming/rust/</a></p><p>As programmers familiar with multithreading will know, you can&#8217;t just write to shared data using multiple different threads. Rust even stops you from doing this via its ownership system.</p><p>For example, in C (or practically any other language), even if two threads try to increment an integer, this creates problems, because even this isn&#8217;t an atomic (single step) operation.</p><p>Behind the scenes, the value of the integer gets copied to a register, incremented, and then copied back to the original memory location. Two threads doing this at the same time will sometimes both copy the original value before incrementing and copying back, and this means some increments will get skipped.</p><p>One way to deal with this is by using thread-safe integers, in this particular case. Rust has <code>AtomicI32</code> for example.</p><p>Another, more general but also trickier method, is to use mutexes (locks) to ensure that two threads cannot write to the same variable at the same time.</p><p>The examples in this page are all built around the Rust documentation at <a href="https://doc.rust-lang.org/book/ch16-03-shared-state.html">https://doc.rust-lang.org/book/ch16-03-shared-state.html</a></p><p></p><h3>A Simple Mutex Example</h3><p>The new method of <code>Mutex</code> expects the initial value of the type that we want to protect.</p><p>In this example, a <code>Mutex</code> wraps an integer. To access the integer for writing, we have to lock the mutex.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!QLKf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5027d9-51a8-4492-b817-458a4cf06c3f_1248x794.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!QLKf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5027d9-51a8-4492-b817-458a4cf06c3f_1248x794.png 424w, https://substackcdn.com/image/fetch/$s_!QLKf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5027d9-51a8-4492-b817-458a4cf06c3f_1248x794.png 848w, https://substackcdn.com/image/fetch/$s_!QLKf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5027d9-51a8-4492-b817-458a4cf06c3f_1248x794.png 1272w, https://substackcdn.com/image/fetch/$s_!QLKf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5027d9-51a8-4492-b817-458a4cf06c3f_1248x794.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!QLKf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5027d9-51a8-4492-b817-458a4cf06c3f_1248x794.png" width="455" height="289.4791666666667" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8b5027d9-51a8-4492-b817-458a4cf06c3f_1248x794.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:794,&quot;width&quot;:1248,&quot;resizeWidth&quot;:455,&quot;bytes&quot;:102428,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/170349874?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5027d9-51a8-4492-b817-458a4cf06c3f_1248x794.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!QLKf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5027d9-51a8-4492-b817-458a4cf06c3f_1248x794.png 424w, https://substackcdn.com/image/fetch/$s_!QLKf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5027d9-51a8-4492-b817-458a4cf06c3f_1248x794.png 848w, https://substackcdn.com/image/fetch/$s_!QLKf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5027d9-51a8-4492-b817-458a4cf06c3f_1248x794.png 1272w, https://substackcdn.com/image/fetch/$s_!QLKf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5027d9-51a8-4492-b817-458a4cf06c3f_1248x794.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The <code>lock()</code> method returns a <code>Result</code> which wraps either a <code>MutexGuard</code> or a <code>PoisonError</code>. <code>MutexGuard</code> is a kind of smart pointer which implements the <code>Deref</code> trait, so we can dereference it to get at the actual value.</p><p>The key point about a mutex is that two threads (or the same thread) cannot lock it twice at the same time.</p><p>The following program just hangs, because the main thread waits indefinitely on the second lock attempt. It&#8217;s waiting for the lock to be unlocked by another thread, which never happens.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tC-3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc686d18b-3052-4438-92c9-63e91d803f00_1276x1034.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tC-3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc686d18b-3052-4438-92c9-63e91d803f00_1276x1034.png 424w, https://substackcdn.com/image/fetch/$s_!tC-3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc686d18b-3052-4438-92c9-63e91d803f00_1276x1034.png 848w, https://substackcdn.com/image/fetch/$s_!tC-3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc686d18b-3052-4438-92c9-63e91d803f00_1276x1034.png 1272w, https://substackcdn.com/image/fetch/$s_!tC-3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc686d18b-3052-4438-92c9-63e91d803f00_1276x1034.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tC-3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc686d18b-3052-4438-92c9-63e91d803f00_1276x1034.png" width="462" height="374.37931034482756" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c686d18b-3052-4438-92c9-63e91d803f00_1276x1034.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1034,&quot;width&quot;:1276,&quot;resizeWidth&quot;:462,&quot;bytes&quot;:136273,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/170349874?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc686d18b-3052-4438-92c9-63e91d803f00_1276x1034.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tC-3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc686d18b-3052-4438-92c9-63e91d803f00_1276x1034.png 424w, https://substackcdn.com/image/fetch/$s_!tC-3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc686d18b-3052-4438-92c9-63e91d803f00_1276x1034.png 848w, https://substackcdn.com/image/fetch/$s_!tC-3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc686d18b-3052-4438-92c9-63e91d803f00_1276x1034.png 1272w, https://substackcdn.com/image/fetch/$s_!tC-3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc686d18b-3052-4438-92c9-63e91d803f00_1276x1034.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Using Mutexes to Increment a Value</h3><p>In the following program I create 10 threads and, in each of them, I increment the value guarded by the mutex.</p><p>When you try to do this naively, the first problem that comes up is you cannot access the mutex variable from the closure, because then the closure code might outlive the variable.</p><p>You are forced to add <code>move</code> to the closer declaration, to move the variable into the closure. But then, you can&#8217;t move it into subsequent closures in further iterations of the loop.</p><p>You can try to fix this by using <code>Rc</code>, with <code>clone</code>, but then the problem is that <code>Rc</code> isn&#8217;t threadsafe, so you still get an error and the code won&#8217;t compile.</p><p>However, if we swap <code>Rc</code> for <code>Arc</code> (atomic reference counter), which is thread safe, the code now works and finally prints 10.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PCuA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98525dc0-effc-439b-8f46-314cdccac1c5_1108x1376.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PCuA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98525dc0-effc-439b-8f46-314cdccac1c5_1108x1376.png 424w, https://substackcdn.com/image/fetch/$s_!PCuA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98525dc0-effc-439b-8f46-314cdccac1c5_1108x1376.png 848w, https://substackcdn.com/image/fetch/$s_!PCuA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98525dc0-effc-439b-8f46-314cdccac1c5_1108x1376.png 1272w, https://substackcdn.com/image/fetch/$s_!PCuA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98525dc0-effc-439b-8f46-314cdccac1c5_1108x1376.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PCuA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98525dc0-effc-439b-8f46-314cdccac1c5_1108x1376.png" width="1108" height="1376" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/98525dc0-effc-439b-8f46-314cdccac1c5_1108x1376.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1376,&quot;width&quot;:1108,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:182534,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/170349874?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98525dc0-effc-439b-8f46-314cdccac1c5_1108x1376.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!PCuA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98525dc0-effc-439b-8f46-314cdccac1c5_1108x1376.png 424w, https://substackcdn.com/image/fetch/$s_!PCuA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98525dc0-effc-439b-8f46-314cdccac1c5_1108x1376.png 848w, https://substackcdn.com/image/fetch/$s_!PCuA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98525dc0-effc-439b-8f46-314cdccac1c5_1108x1376.png 1272w, https://substackcdn.com/image/fetch/$s_!PCuA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98525dc0-effc-439b-8f46-314cdccac1c5_1108x1376.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.caveofprogramming.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">The Cave of Programming Blog is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[Multithreading in Rust]]></title><description><![CDATA[Creating Threads and Passing Messages]]></description><link>https://blog.caveofprogramming.com/p/multithreading-in-rust</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/multithreading-in-rust</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Tue, 24 Jun 2025 21:15:35 GMT</pubDate><enclosure url="https://api.substack.com/feed/podcast/166531950/0a8cce84a33882cc2bab29aebb307973.mp3" length="0" type="audio/mpeg"/><content:encoded><![CDATA[<p>GitHub Repository: <a href="https://github.com/caveofprogramming/rust/tree/fc7b0acb17ba5843feea34227f85ccc52427bab5/036%20Multithreading/threads">https://github.com/caveofprogramming/rust/</a></p><h3>Creating a Thread</h3><p>The following program spawns a thread and runs some code in it.</p><p>The program thus runs two threads: the main thread, and one additional thread. The sleep (which pauses the main thread here) is needed because otherwise the main thread will exit, destroying all spawned threads, before the text can be printed.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!cWB4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F266a15bc-e0d1-4850-b4ae-12c81cd38b8b_1670x956.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cWB4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F266a15bc-e0d1-4850-b4ae-12c81cd38b8b_1670x956.png 424w, https://substackcdn.com/image/fetch/$s_!cWB4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F266a15bc-e0d1-4850-b4ae-12c81cd38b8b_1670x956.png 848w, https://substackcdn.com/image/fetch/$s_!cWB4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F266a15bc-e0d1-4850-b4ae-12c81cd38b8b_1670x956.png 1272w, https://substackcdn.com/image/fetch/$s_!cWB4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F266a15bc-e0d1-4850-b4ae-12c81cd38b8b_1670x956.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!cWB4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F266a15bc-e0d1-4850-b4ae-12c81cd38b8b_1670x956.png" width="482" height="275.75961538461536" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/266a15bc-e0d1-4850-b4ae-12c81cd38b8b_1670x956.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:833,&quot;width&quot;:1456,&quot;resizeWidth&quot;:482,&quot;bytes&quot;:128825,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/166531950?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F266a15bc-e0d1-4850-b4ae-12c81cd38b8b_1670x956.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cWB4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F266a15bc-e0d1-4850-b4ae-12c81cd38b8b_1670x956.png 424w, https://substackcdn.com/image/fetch/$s_!cWB4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F266a15bc-e0d1-4850-b4ae-12c81cd38b8b_1670x956.png 848w, https://substackcdn.com/image/fetch/$s_!cWB4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F266a15bc-e0d1-4850-b4ae-12c81cd38b8b_1670x956.png 1272w, https://substackcdn.com/image/fetch/$s_!cWB4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F266a15bc-e0d1-4850-b4ae-12c81cd38b8b_1670x956.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h3>Waiting for Threads to Finish</h3><p>Instead of pausing, which necessitates having to try to guess how long we need for the spawned thread to finish, or else just wait excessively long, we can wait till a thread finishes.</p><p>The unwrap here causes a panic if the thread can&#8217;t be run, because the <code>join()</code> function, which waits for a thread to finish, returns a <code>Result</code>.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!N2ll!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70290734-26d9-4d5c-959f-868fd9838bce_1152x468.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!N2ll!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70290734-26d9-4d5c-959f-868fd9838bce_1152x468.png 424w, https://substackcdn.com/image/fetch/$s_!N2ll!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70290734-26d9-4d5c-959f-868fd9838bce_1152x468.png 848w, https://substackcdn.com/image/fetch/$s_!N2ll!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70290734-26d9-4d5c-959f-868fd9838bce_1152x468.png 1272w, https://substackcdn.com/image/fetch/$s_!N2ll!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70290734-26d9-4d5c-959f-868fd9838bce_1152x468.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!N2ll!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70290734-26d9-4d5c-959f-868fd9838bce_1152x468.png" width="494" height="200.6875" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/70290734-26d9-4d5c-959f-868fd9838bce_1152x468.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:468,&quot;width&quot;:1152,&quot;resizeWidth&quot;:494,&quot;bytes&quot;:63261,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/166531950?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70290734-26d9-4d5c-959f-868fd9838bce_1152x468.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!N2ll!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70290734-26d9-4d5c-959f-868fd9838bce_1152x468.png 424w, https://substackcdn.com/image/fetch/$s_!N2ll!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70290734-26d9-4d5c-959f-868fd9838bce_1152x468.png 848w, https://substackcdn.com/image/fetch/$s_!N2ll!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70290734-26d9-4d5c-959f-868fd9838bce_1152x468.png 1272w, https://substackcdn.com/image/fetch/$s_!N2ll!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70290734-26d9-4d5c-959f-868fd9838bce_1152x468.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><h3>Thread Interleaving</h3><p>To prove that threads run concurrently, which is the point of them, we can run two threads at the same time. </p><p>In the following code, a pause is used within the loops because otherwise, one thread might finish before the other can start. There&#8217;s no guarantee about which thread runs first.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PrUs!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23b1fd09-c856-4111-a5f2-0e6cc845e708_1734x1140.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PrUs!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23b1fd09-c856-4111-a5f2-0e6cc845e708_1734x1140.png 424w, https://substackcdn.com/image/fetch/$s_!PrUs!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23b1fd09-c856-4111-a5f2-0e6cc845e708_1734x1140.png 848w, https://substackcdn.com/image/fetch/$s_!PrUs!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23b1fd09-c856-4111-a5f2-0e6cc845e708_1734x1140.png 1272w, https://substackcdn.com/image/fetch/$s_!PrUs!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23b1fd09-c856-4111-a5f2-0e6cc845e708_1734x1140.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PrUs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23b1fd09-c856-4111-a5f2-0e6cc845e708_1734x1140.png" width="548" height="360.18956043956047" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/23b1fd09-c856-4111-a5f2-0e6cc845e708_1734x1140.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:957,&quot;width&quot;:1456,&quot;resizeWidth&quot;:548,&quot;bytes&quot;:182710,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/166531950?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23b1fd09-c856-4111-a5f2-0e6cc845e708_1734x1140.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!PrUs!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23b1fd09-c856-4111-a5f2-0e6cc845e708_1734x1140.png 424w, https://substackcdn.com/image/fetch/$s_!PrUs!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23b1fd09-c856-4111-a5f2-0e6cc845e708_1734x1140.png 848w, https://substackcdn.com/image/fetch/$s_!PrUs!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23b1fd09-c856-4111-a5f2-0e6cc845e708_1734x1140.png 1272w, https://substackcdn.com/image/fetch/$s_!PrUs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23b1fd09-c856-4111-a5f2-0e6cc845e708_1734x1140.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Alternatively, we can spawn two new threads and print output from those. In that case, we&#8217;d better wait for both of them to finish.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bxL6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23594b74-e4df-44c2-a10a-aea51640bfac_1440x1160.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bxL6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23594b74-e4df-44c2-a10a-aea51640bfac_1440x1160.png 424w, https://substackcdn.com/image/fetch/$s_!bxL6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23594b74-e4df-44c2-a10a-aea51640bfac_1440x1160.png 848w, https://substackcdn.com/image/fetch/$s_!bxL6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23594b74-e4df-44c2-a10a-aea51640bfac_1440x1160.png 1272w, https://substackcdn.com/image/fetch/$s_!bxL6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23594b74-e4df-44c2-a10a-aea51640bfac_1440x1160.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bxL6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23594b74-e4df-44c2-a10a-aea51640bfac_1440x1160.png" width="1440" height="1160" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/23594b74-e4df-44c2-a10a-aea51640bfac_1440x1160.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1160,&quot;width&quot;:1440,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:170093,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/166531950?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23594b74-e4df-44c2-a10a-aea51640bfac_1440x1160.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!bxL6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23594b74-e4df-44c2-a10a-aea51640bfac_1440x1160.png 424w, https://substackcdn.com/image/fetch/$s_!bxL6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23594b74-e4df-44c2-a10a-aea51640bfac_1440x1160.png 848w, https://substackcdn.com/image/fetch/$s_!bxL6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23594b74-e4df-44c2-a10a-aea51640bfac_1440x1160.png 1272w, https://substackcdn.com/image/fetch/$s_!bxL6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23594b74-e4df-44c2-a10a-aea51640bfac_1440x1160.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In both cases, the output ends up interleaved.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BYWQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfb1a11f-5811-40e7-b506-39f1a599e9f6_132x448.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BYWQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfb1a11f-5811-40e7-b506-39f1a599e9f6_132x448.png 424w, https://substackcdn.com/image/fetch/$s_!BYWQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfb1a11f-5811-40e7-b506-39f1a599e9f6_132x448.png 848w, https://substackcdn.com/image/fetch/$s_!BYWQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfb1a11f-5811-40e7-b506-39f1a599e9f6_132x448.png 1272w, https://substackcdn.com/image/fetch/$s_!BYWQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfb1a11f-5811-40e7-b506-39f1a599e9f6_132x448.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BYWQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfb1a11f-5811-40e7-b506-39f1a599e9f6_132x448.png" width="88" height="298.6666666666667" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cfb1a11f-5811-40e7-b506-39f1a599e9f6_132x448.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:448,&quot;width&quot;:132,&quot;resizeWidth&quot;:88,&quot;bytes&quot;:14907,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/166531950?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfb1a11f-5811-40e7-b506-39f1a599e9f6_132x448.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BYWQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfb1a11f-5811-40e7-b506-39f1a599e9f6_132x448.png 424w, https://substackcdn.com/image/fetch/$s_!BYWQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfb1a11f-5811-40e7-b506-39f1a599e9f6_132x448.png 848w, https://substackcdn.com/image/fetch/$s_!BYWQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfb1a11f-5811-40e7-b506-39f1a599e9f6_132x448.png 1272w, https://substackcdn.com/image/fetch/$s_!BYWQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfb1a11f-5811-40e7-b506-39f1a599e9f6_132x448.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h3>Accessing Variables at the Enclosing Scope</h3><p>The following code does not compile. We are not allowed to access the variable <code>v</code> within the closure, because if this code were to actually compile, <code>v</code> could be dropped before the thread has finished with it.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9JaC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F983a8501-83e4-42c2-be62-f8da1a485a44_822x660.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9JaC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F983a8501-83e4-42c2-be62-f8da1a485a44_822x660.png 424w, https://substackcdn.com/image/fetch/$s_!9JaC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F983a8501-83e4-42c2-be62-f8da1a485a44_822x660.png 848w, https://substackcdn.com/image/fetch/$s_!9JaC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F983a8501-83e4-42c2-be62-f8da1a485a44_822x660.png 1272w, https://substackcdn.com/image/fetch/$s_!9JaC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F983a8501-83e4-42c2-be62-f8da1a485a44_822x660.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9JaC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F983a8501-83e4-42c2-be62-f8da1a485a44_822x660.png" width="418" height="335.6204379562044" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/983a8501-83e4-42c2-be62-f8da1a485a44_822x660.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:660,&quot;width&quot;:822,&quot;resizeWidth&quot;:418,&quot;bytes&quot;:60281,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/166531950?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F983a8501-83e4-42c2-be62-f8da1a485a44_822x660.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9JaC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F983a8501-83e4-42c2-be62-f8da1a485a44_822x660.png 424w, https://substackcdn.com/image/fetch/$s_!9JaC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F983a8501-83e4-42c2-be62-f8da1a485a44_822x660.png 848w, https://substackcdn.com/image/fetch/$s_!9JaC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F983a8501-83e4-42c2-be62-f8da1a485a44_822x660.png 1272w, https://substackcdn.com/image/fetch/$s_!9JaC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F983a8501-83e4-42c2-be62-f8da1a485a44_822x660.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The solution is to add the <code>move</code> keyword to the closure. This ensure local variables access within the closure are moved, so that the closure takes ownership. Drop after the thread code is then not allowed</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZTiT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9998e5f2-0669-4fb8-9610-9f30a3edf21a_1084x728.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZTiT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9998e5f2-0669-4fb8-9610-9f30a3edf21a_1084x728.png 424w, https://substackcdn.com/image/fetch/$s_!ZTiT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9998e5f2-0669-4fb8-9610-9f30a3edf21a_1084x728.png 848w, https://substackcdn.com/image/fetch/$s_!ZTiT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9998e5f2-0669-4fb8-9610-9f30a3edf21a_1084x728.png 1272w, https://substackcdn.com/image/fetch/$s_!ZTiT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9998e5f2-0669-4fb8-9610-9f30a3edf21a_1084x728.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZTiT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9998e5f2-0669-4fb8-9610-9f30a3edf21a_1084x728.png" width="445" height="298.8560885608856" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9998e5f2-0669-4fb8-9610-9f30a3edf21a_1084x728.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:728,&quot;width&quot;:1084,&quot;resizeWidth&quot;:445,&quot;bytes&quot;:83936,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/166531950?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9998e5f2-0669-4fb8-9610-9f30a3edf21a_1084x728.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ZTiT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9998e5f2-0669-4fb8-9610-9f30a3edf21a_1084x728.png 424w, https://substackcdn.com/image/fetch/$s_!ZTiT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9998e5f2-0669-4fb8-9610-9f30a3edf21a_1084x728.png 848w, https://substackcdn.com/image/fetch/$s_!ZTiT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9998e5f2-0669-4fb8-9610-9f30a3edf21a_1084x728.png 1272w, https://substackcdn.com/image/fetch/$s_!ZTiT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9998e5f2-0669-4fb8-9610-9f30a3edf21a_1084x728.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This code compiles and works fine.</p><p></p><h3>Passing Messages</h3><p>When two or more threads try to modify the same variable, problems typically ensue, because the work of one thread conflicts with the other.</p><p>But this can&#8217;t easily happen in Rust, because a thread has to take ownership of a variable to be able to modify it.</p><p>There are various alternative ways of communicating between threads where needed, instead of multiple threads modifying the same variables.</p><p>One of these is to make use of messages.</p><p>The following code creates a channel that can be used to communicate between threads. <code>mpsc</code> stands for &#8220;multiple producer, single consumer&#8221;.</p><p>That is, multiple threads can send data down the channel, and one thread can consume it.</p><p><code>tx</code> is the &#8220;transmitter&#8221; and <code>rx</code> the &#8220;receiver&#8221;.</p><p>The <code>send</code> method is used to send data, and the <code>recv</code> (short for receive) method is used to receive it.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EYMP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6275b55-3a3e-4dfb-a875-e65633f4f63c_1038x942.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EYMP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6275b55-3a3e-4dfb-a875-e65633f4f63c_1038x942.png 424w, https://substackcdn.com/image/fetch/$s_!EYMP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6275b55-3a3e-4dfb-a875-e65633f4f63c_1038x942.png 848w, https://substackcdn.com/image/fetch/$s_!EYMP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6275b55-3a3e-4dfb-a875-e65633f4f63c_1038x942.png 1272w, https://substackcdn.com/image/fetch/$s_!EYMP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6275b55-3a3e-4dfb-a875-e65633f4f63c_1038x942.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!EYMP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6275b55-3a3e-4dfb-a875-e65633f4f63c_1038x942.png" width="424" height="384.78612716763007" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a6275b55-3a3e-4dfb-a875-e65633f4f63c_1038x942.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:942,&quot;width&quot;:1038,&quot;resizeWidth&quot;:424,&quot;bytes&quot;:123770,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/166531950?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6275b55-3a3e-4dfb-a875-e65633f4f63c_1038x942.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EYMP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6275b55-3a3e-4dfb-a875-e65633f4f63c_1038x942.png 424w, https://substackcdn.com/image/fetch/$s_!EYMP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6275b55-3a3e-4dfb-a875-e65633f4f63c_1038x942.png 848w, https://substackcdn.com/image/fetch/$s_!EYMP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6275b55-3a3e-4dfb-a875-e65633f4f63c_1038x942.png 1272w, https://substackcdn.com/image/fetch/$s_!EYMP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6275b55-3a3e-4dfb-a875-e65633f4f63c_1038x942.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h3>Multiple Senders</h3><p>In this code, minimally adapted from the Rust documentation, multiple receivers send to a channel.</p><p>Note:</p><ul><li><p>The transmitter is cloned. Otherwise it would be unavailable after moving into the first sender thread code.</p></li><li><p>The senders send text with one-second delays. The receiver actually blocks until data is available, so the receiver gradually spits out the transmitted strings.</p></li><li><p>The receiver thread is using the receiver as an iterator, rather than calling <code>recv()</code>.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!QOa1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffde9dcad-2957-4b81-8955-c7a1fc74a74f_1060x1326.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!QOa1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffde9dcad-2957-4b81-8955-c7a1fc74a74f_1060x1326.png 424w, https://substackcdn.com/image/fetch/$s_!QOa1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffde9dcad-2957-4b81-8955-c7a1fc74a74f_1060x1326.png 848w, https://substackcdn.com/image/fetch/$s_!QOa1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffde9dcad-2957-4b81-8955-c7a1fc74a74f_1060x1326.png 1272w, https://substackcdn.com/image/fetch/$s_!QOa1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffde9dcad-2957-4b81-8955-c7a1fc74a74f_1060x1326.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!QOa1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffde9dcad-2957-4b81-8955-c7a1fc74a74f_1060x1326.png" width="1060" height="1326" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fde9dcad-2957-4b81-8955-c7a1fc74a74f_1060x1326.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1326,&quot;width&quot;:1060,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:217520,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/166531950?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffde9dcad-2957-4b81-8955-c7a1fc74a74f_1060x1326.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!QOa1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffde9dcad-2957-4b81-8955-c7a1fc74a74f_1060x1326.png 424w, https://substackcdn.com/image/fetch/$s_!QOa1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffde9dcad-2957-4b81-8955-c7a1fc74a74f_1060x1326.png 848w, https://substackcdn.com/image/fetch/$s_!QOa1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffde9dcad-2957-4b81-8955-c7a1fc74a74f_1060x1326.png 1272w, https://substackcdn.com/image/fetch/$s_!QOa1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffde9dcad-2957-4b81-8955-c7a1fc74a74f_1060x1326.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The order of the output may vary.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!xe1L!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0e3f219-2632-4c2a-a314-dbdce1293afc_418x311.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!xe1L!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0e3f219-2632-4c2a-a314-dbdce1293afc_418x311.png 424w, https://substackcdn.com/image/fetch/$s_!xe1L!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0e3f219-2632-4c2a-a314-dbdce1293afc_418x311.png 848w, https://substackcdn.com/image/fetch/$s_!xe1L!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0e3f219-2632-4c2a-a314-dbdce1293afc_418x311.png 1272w, https://substackcdn.com/image/fetch/$s_!xe1L!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0e3f219-2632-4c2a-a314-dbdce1293afc_418x311.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!xe1L!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0e3f219-2632-4c2a-a314-dbdce1293afc_418x311.png" width="204" height="151.7799043062201" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a0e3f219-2632-4c2a-a314-dbdce1293afc_418x311.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:311,&quot;width&quot;:418,&quot;resizeWidth&quot;:204,&quot;bytes&quot;:27125,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/166531950?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcf5129e-e39d-4ef2-b9fe-42d5bd2701f9_418x320.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!xe1L!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0e3f219-2632-4c2a-a314-dbdce1293afc_418x311.png 424w, https://substackcdn.com/image/fetch/$s_!xe1L!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0e3f219-2632-4c2a-a314-dbdce1293afc_418x311.png 848w, https://substackcdn.com/image/fetch/$s_!xe1L!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0e3f219-2632-4c2a-a314-dbdce1293afc_418x311.png 1272w, https://substackcdn.com/image/fetch/$s_!xe1L!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0e3f219-2632-4c2a-a314-dbdce1293afc_418x311.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p>]]></content:encoded></item><item><title><![CDATA[Refcells, and Binary Trees in Rust]]></title><description><![CDATA[Not even Microsoft Copilot could write a program to my specifications.]]></description><link>https://blog.caveofprogramming.com/p/refcells-and-binary-trees-in-rust</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/refcells-and-binary-trees-in-rust</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Sun, 01 Jun 2025 23:44:43 GMT</pubDate><enclosure url="https://api.substack.com/feed/podcast/164828449/28fa17b8c11838c5049ceebad3137c01.mp3" length="0" type="audio/mpeg"/><content:encoded><![CDATA[<p></p><p>Code on GitHub: <a href="https://github.com/caveofprogramming/rust/tree/dd05cba76e2b43ca6720ed4f4bdacac8fa2bbeae/034%20RefCells%20and%20Mutable%20Data%20Structures/refcell">https://github.com/caveofprogramming/rust/</a></p><h3>RefCell</h3><p><code>RefCell</code> (<code>std::cell::RefCell</code>) is another kind of smart pointer.</p><p>It implements the &#8220;interior mutability&#8221; pattern. This means that you can mutate the contents of a <code>RefCell</code> using an immutable reference.</p><p>Borrowing rules with <code>RefCell</code> are enforced at runtime, not compile time.</p><p><code>RefCell</code> provides two functions that allow you to get references to its contents: <code>borrow()</code> and <code>borrow_mut()</code>.</p><p>In the following example, I wrap an integer in a <code>RefCell</code>, print it using <code>borrow()</code> and alter it using <code>borrow_mut()</code>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0NiA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa898491d-1413-48e9-9f06-d25c8f526187_1108x750.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0NiA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa898491d-1413-48e9-9f06-d25c8f526187_1108x750.png 424w, https://substackcdn.com/image/fetch/$s_!0NiA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa898491d-1413-48e9-9f06-d25c8f526187_1108x750.png 848w, https://substackcdn.com/image/fetch/$s_!0NiA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa898491d-1413-48e9-9f06-d25c8f526187_1108x750.png 1272w, https://substackcdn.com/image/fetch/$s_!0NiA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa898491d-1413-48e9-9f06-d25c8f526187_1108x750.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0NiA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa898491d-1413-48e9-9f06-d25c8f526187_1108x750.png" width="498" height="337.09386281588445" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a898491d-1413-48e9-9f06-d25c8f526187_1108x750.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:750,&quot;width&quot;:1108,&quot;resizeWidth&quot;:498,&quot;bytes&quot;:103800,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/164828449?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa898491d-1413-48e9-9f06-d25c8f526187_1108x750.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!0NiA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa898491d-1413-48e9-9f06-d25c8f526187_1108x750.png 424w, https://substackcdn.com/image/fetch/$s_!0NiA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa898491d-1413-48e9-9f06-d25c8f526187_1108x750.png 848w, https://substackcdn.com/image/fetch/$s_!0NiA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa898491d-1413-48e9-9f06-d25c8f526187_1108x750.png 1272w, https://substackcdn.com/image/fetch/$s_!0NiA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa898491d-1413-48e9-9f06-d25c8f526187_1108x750.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h3>My Descent Into Madness</h3><p>At this point I found myself facing a difficult question: what is this actually for?</p><p>I read somewhere that it might be useful for implementing tree or other structures, where you want to be able to alter the nodes.</p><p>You might use <code>Rc</code> to implement multiple ownership of the nodes, so that one node can be in multiple places and still get cleaned up at the right time.</p><p>But <code>Rc</code> does not allow you to mutate its contents, so you can wrap a RefCell with an Rc.</p><p>This sounds fine and I was able to get Microsoft Copilot to implement such a program (I couldn&#8217;t quite manage it myself), but the unsatisfactory thing is that the resulting API isn&#8217;t very clean.</p><p>It ends up being necessary to call borrow methods on the nodes before calling their insert methods.</p><p>When I tried to get Copilot to implement something more to my liking, including nodes that contain strings, it couldn&#8217;t seem to manage it, producing code over and over again that just didn&#8217;t compile.</p><p></p><h3>The Problem</h3><p>Part of the problem, I realised, was that I wasn&#8217;t clear about what I actually wanted. I can&#8217;t have nodes that are owned by variables in main() but then somehow also have multiple owners within some structure, unless those nodes are actually created as Rc smart pointers.</p><p>No matter what I try, it becomes necessary to expose smart pointer methods outside of my desired API.</p><p>Another problem is, if nodes in a tree can appear at different points in a tree, those nodes will also have pointers to other nodes that are repeated within the tree, and potentially you&#8217;ll create an infinite recursive tree.</p><p></p><h3>Solution, Sort Of</h3><p>Eventually I came up with the program outlined in the video, which you can also find on GitHub.</p><p>This consists of nodes formed from Data structs, connected by Connector structs.</p><p>It&#8217;s possible to connect them into a kind of triangle, or loop, where the last in the chain connects to the first.</p><p>Because <code>RefCell</code> is used to refer to the nodes, they can be seamlessly swapped out, even though immutable <code>Rc</code> smart pointers are used to refer to them.</p><p></p>]]></content:encoded></item><item><title><![CDATA[Reference Counting in Rust]]></title><description><![CDATA[Code on GitHub: https://github.com/caveofprogramming/rust/The Cave of Programming Blog is a reader-supported publication.]]></description><link>https://blog.caveofprogramming.com/p/reference-counting-in-rust</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/reference-counting-in-rust</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Thu, 15 May 2025 15:51:56 GMT</pubDate><enclosure url="https://api.substack.com/feed/podcast/163396648/67bbbaf9440ec831d83ff87474c7719b.mp3" length="0" type="audio/mpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/rust/tree/59c741ef9a4576618f818f75cc7ba940cf422d0f/033%20Reference%20Counting/refs">https://github.com/caveofprogramming/rust/</a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.caveofprogramming.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">The Cave of Programming Blog is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>Consider a tree structure, where every node in the tree may have child nodes.</p><p>A node may appear multiple times at different points in the tree.</p><p>How do we ensure that every node is cleaned up (dropped) eventually, but is never dropped as long as at least one other node still refers to it?</p><p>This can be accomplished using reference counting.</p><p></p><h3>Example Struct</h3><p>I will use the following struct, representing person data. I&#8217;ve implemented the <code>Drop</code> trait so that we can see exactly when a <code>Person</code> object is dropped.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!a_mE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1db16f-f690-4fd3-954a-0a718315e706_950x536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!a_mE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1db16f-f690-4fd3-954a-0a718315e706_950x536.png 424w, https://substackcdn.com/image/fetch/$s_!a_mE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1db16f-f690-4fd3-954a-0a718315e706_950x536.png 848w, https://substackcdn.com/image/fetch/$s_!a_mE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1db16f-f690-4fd3-954a-0a718315e706_950x536.png 1272w, https://substackcdn.com/image/fetch/$s_!a_mE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1db16f-f690-4fd3-954a-0a718315e706_950x536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!a_mE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1db16f-f690-4fd3-954a-0a718315e706_950x536.png" width="430" height="242.61052631578949" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1a1db16f-f690-4fd3-954a-0a718315e706_950x536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:536,&quot;width&quot;:950,&quot;resizeWidth&quot;:430,&quot;bytes&quot;:63798,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/163396648?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1db16f-f690-4fd3-954a-0a718315e706_950x536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!a_mE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1db16f-f690-4fd3-954a-0a718315e706_950x536.png 424w, https://substackcdn.com/image/fetch/$s_!a_mE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1db16f-f690-4fd3-954a-0a718315e706_950x536.png 848w, https://substackcdn.com/image/fetch/$s_!a_mE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1db16f-f690-4fd3-954a-0a718315e706_950x536.png 1272w, https://substackcdn.com/image/fetch/$s_!a_mE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1db16f-f690-4fd3-954a-0a718315e706_950x536.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Managing the Object</h3><p>We can use <code>std::rc::Rc</code> to initially own the object.</p><p>The object then has a reference count of 1.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Xd5M!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f129475-bb0e-4674-810b-bc9d2a72a802_1394x246.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Xd5M!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f129475-bb0e-4674-810b-bc9d2a72a802_1394x246.png 424w, https://substackcdn.com/image/fetch/$s_!Xd5M!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f129475-bb0e-4674-810b-bc9d2a72a802_1394x246.png 848w, https://substackcdn.com/image/fetch/$s_!Xd5M!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f129475-bb0e-4674-810b-bc9d2a72a802_1394x246.png 1272w, https://substackcdn.com/image/fetch/$s_!Xd5M!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f129475-bb0e-4674-810b-bc9d2a72a802_1394x246.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Xd5M!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f129475-bb0e-4674-810b-bc9d2a72a802_1394x246.png" width="1394" height="246" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1f129475-bb0e-4674-810b-bc9d2a72a802_1394x246.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:246,&quot;width&quot;:1394,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:62368,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/163396648?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f129475-bb0e-4674-810b-bc9d2a72a802_1394x246.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Xd5M!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f129475-bb0e-4674-810b-bc9d2a72a802_1394x246.png 424w, https://substackcdn.com/image/fetch/$s_!Xd5M!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f129475-bb0e-4674-810b-bc9d2a72a802_1394x246.png 848w, https://substackcdn.com/image/fetch/$s_!Xd5M!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f129475-bb0e-4674-810b-bc9d2a72a802_1394x246.png 1272w, https://substackcdn.com/image/fetch/$s_!Xd5M!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f129475-bb0e-4674-810b-bc9d2a72a802_1394x246.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6HjC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedf976e7-095f-406e-91a0-dc226cd32665_382x40.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6HjC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedf976e7-095f-406e-91a0-dc226cd32665_382x40.png 424w, https://substackcdn.com/image/fetch/$s_!6HjC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedf976e7-095f-406e-91a0-dc226cd32665_382x40.png 848w, https://substackcdn.com/image/fetch/$s_!6HjC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedf976e7-095f-406e-91a0-dc226cd32665_382x40.png 1272w, https://substackcdn.com/image/fetch/$s_!6HjC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedf976e7-095f-406e-91a0-dc226cd32665_382x40.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6HjC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedf976e7-095f-406e-91a0-dc226cd32665_382x40.png" width="230" height="24.083769633507853" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/edf976e7-095f-406e-91a0-dc226cd32665_382x40.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:40,&quot;width&quot;:382,&quot;resizeWidth&quot;:230,&quot;bytes&quot;:3548,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/163396648?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4d91dff-7d6f-42e1-9db3-eb5605488635_382x50.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6HjC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedf976e7-095f-406e-91a0-dc226cd32665_382x40.png 424w, https://substackcdn.com/image/fetch/$s_!6HjC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedf976e7-095f-406e-91a0-dc226cd32665_382x40.png 848w, https://substackcdn.com/image/fetch/$s_!6HjC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedf976e7-095f-406e-91a0-dc226cd32665_382x40.png 1272w, https://substackcdn.com/image/fetch/$s_!6HjC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedf976e7-095f-406e-91a0-dc226cd32665_382x40.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><p>After this we can create more variables that own the object, incrementing the reference count.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yPTM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7acbceb9-0b80-44e4-9dc6-35c6723c34b5_1268x254.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yPTM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7acbceb9-0b80-44e4-9dc6-35c6723c34b5_1268x254.png 424w, https://substackcdn.com/image/fetch/$s_!yPTM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7acbceb9-0b80-44e4-9dc6-35c6723c34b5_1268x254.png 848w, https://substackcdn.com/image/fetch/$s_!yPTM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7acbceb9-0b80-44e4-9dc6-35c6723c34b5_1268x254.png 1272w, https://substackcdn.com/image/fetch/$s_!yPTM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7acbceb9-0b80-44e4-9dc6-35c6723c34b5_1268x254.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yPTM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7acbceb9-0b80-44e4-9dc6-35c6723c34b5_1268x254.png" width="1268" height="254" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7acbceb9-0b80-44e4-9dc6-35c6723c34b5_1268x254.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:254,&quot;width&quot;:1268,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:78060,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/163396648?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7acbceb9-0b80-44e4-9dc6-35c6723c34b5_1268x254.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yPTM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7acbceb9-0b80-44e4-9dc6-35c6723c34b5_1268x254.png 424w, https://substackcdn.com/image/fetch/$s_!yPTM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7acbceb9-0b80-44e4-9dc6-35c6723c34b5_1268x254.png 848w, https://substackcdn.com/image/fetch/$s_!yPTM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7acbceb9-0b80-44e4-9dc6-35c6723c34b5_1268x254.png 1272w, https://substackcdn.com/image/fetch/$s_!yPTM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7acbceb9-0b80-44e4-9dc6-35c6723c34b5_1268x254.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fFSA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb813663-4639-428b-a8e4-715edcb899cf_374x84.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fFSA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb813663-4639-428b-a8e4-715edcb899cf_374x84.png 424w, https://substackcdn.com/image/fetch/$s_!fFSA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb813663-4639-428b-a8e4-715edcb899cf_374x84.png 848w, https://substackcdn.com/image/fetch/$s_!fFSA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb813663-4639-428b-a8e4-715edcb899cf_374x84.png 1272w, https://substackcdn.com/image/fetch/$s_!fFSA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb813663-4639-428b-a8e4-715edcb899cf_374x84.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fFSA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb813663-4639-428b-a8e4-715edcb899cf_374x84.png" width="238" height="53.45454545454545" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fb813663-4639-428b-a8e4-715edcb899cf_374x84.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:84,&quot;width&quot;:374,&quot;resizeWidth&quot;:238,&quot;bytes&quot;:11570,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/163396648?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb813663-4639-428b-a8e4-715edcb899cf_374x84.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fFSA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb813663-4639-428b-a8e4-715edcb899cf_374x84.png 424w, https://substackcdn.com/image/fetch/$s_!fFSA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb813663-4639-428b-a8e4-715edcb899cf_374x84.png 848w, https://substackcdn.com/image/fetch/$s_!fFSA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb813663-4639-428b-a8e4-715edcb899cf_374x84.png 1272w, https://substackcdn.com/image/fetch/$s_!fFSA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb813663-4639-428b-a8e4-715edcb899cf_374x84.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The object will only be cleaned up and <code>drop()</code> called when all of these references have been dropped or have gone out of scope.</p><p>We can drop these references manually if desired, in any order.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EWWe!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c66f2ed-be33-48b6-aa37-1c3def4fecfc_1294x476.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EWWe!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c66f2ed-be33-48b6-aa37-1c3def4fecfc_1294x476.png 424w, https://substackcdn.com/image/fetch/$s_!EWWe!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c66f2ed-be33-48b6-aa37-1c3def4fecfc_1294x476.png 848w, https://substackcdn.com/image/fetch/$s_!EWWe!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c66f2ed-be33-48b6-aa37-1c3def4fecfc_1294x476.png 1272w, https://substackcdn.com/image/fetch/$s_!EWWe!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c66f2ed-be33-48b6-aa37-1c3def4fecfc_1294x476.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!EWWe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c66f2ed-be33-48b6-aa37-1c3def4fecfc_1294x476.png" width="548" height="201.58268933539412" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0c66f2ed-be33-48b6-aa37-1c3def4fecfc_1294x476.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:476,&quot;width&quot;:1294,&quot;resizeWidth&quot;:548,&quot;bytes&quot;:123296,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/163396648?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c66f2ed-be33-48b6-aa37-1c3def4fecfc_1294x476.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EWWe!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c66f2ed-be33-48b6-aa37-1c3def4fecfc_1294x476.png 424w, https://substackcdn.com/image/fetch/$s_!EWWe!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c66f2ed-be33-48b6-aa37-1c3def4fecfc_1294x476.png 848w, https://substackcdn.com/image/fetch/$s_!EWWe!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c66f2ed-be33-48b6-aa37-1c3def4fecfc_1294x476.png 1272w, https://substackcdn.com/image/fetch/$s_!EWWe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c66f2ed-be33-48b6-aa37-1c3def4fecfc_1294x476.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MHju!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5a140fc-129a-4224-975a-867fda0180e2_384x236.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MHju!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5a140fc-129a-4224-975a-867fda0180e2_384x236.png 424w, https://substackcdn.com/image/fetch/$s_!MHju!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5a140fc-129a-4224-975a-867fda0180e2_384x236.png 848w, https://substackcdn.com/image/fetch/$s_!MHju!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5a140fc-129a-4224-975a-867fda0180e2_384x236.png 1272w, https://substackcdn.com/image/fetch/$s_!MHju!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5a140fc-129a-4224-975a-867fda0180e2_384x236.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MHju!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5a140fc-129a-4224-975a-867fda0180e2_384x236.png" width="252" height="154.875" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b5a140fc-129a-4224-975a-867fda0180e2_384x236.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:236,&quot;width&quot;:384,&quot;resizeWidth&quot;:252,&quot;bytes&quot;:27077,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/163396648?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5a140fc-129a-4224-975a-867fda0180e2_384x236.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!MHju!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5a140fc-129a-4224-975a-867fda0180e2_384x236.png 424w, https://substackcdn.com/image/fetch/$s_!MHju!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5a140fc-129a-4224-975a-867fda0180e2_384x236.png 848w, https://substackcdn.com/image/fetch/$s_!MHju!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5a140fc-129a-4224-975a-867fda0180e2_384x236.png 1272w, https://substackcdn.com/image/fetch/$s_!MHju!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5a140fc-129a-4224-975a-867fda0180e2_384x236.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The <code>Rc</code> object keeps a count of how many references still refer to the object, and the object gets dropped when there are none left.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.caveofprogramming.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">The Cave of Programming Blog is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[Implementing the Drop Trait]]></title><description><![CDATA[How to add cleanup code in Rust]]></description><link>https://blog.caveofprogramming.com/p/implementing-the-drop-trait</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/implementing-the-drop-trait</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Tue, 06 May 2025 10:49:06 GMT</pubDate><enclosure url="https://api.substack.com/feed/podcast/162897659/b582cb604dabd1c9056b637305c996e7.mp3" length="0" type="audio/mpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/rust/tree/ec941865137803af0f3fed79b028bffecdd00b35/032%20The%20Drop%20Trait">https://github.com/caveofprogramming/rust/</a></p><p>If a Rust type implements the <code>Drop</code> trait, the drop method of any object of that type will be called automatically when the variable that owns it goes out of scope.</p><p>This can be used for cleanup, e.g. terminating database connections, freeing memory.</p><p>Here&#8217;s a struct called <code>Data</code> which stores a single value.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Lt5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e07f91f-6d63-4718-b9e6-b1ed8cec48ee_348x178.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Lt5w!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e07f91f-6d63-4718-b9e6-b1ed8cec48ee_348x178.png 424w, https://substackcdn.com/image/fetch/$s_!Lt5w!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e07f91f-6d63-4718-b9e6-b1ed8cec48ee_348x178.png 848w, https://substackcdn.com/image/fetch/$s_!Lt5w!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e07f91f-6d63-4718-b9e6-b1ed8cec48ee_348x178.png 1272w, https://substackcdn.com/image/fetch/$s_!Lt5w!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e07f91f-6d63-4718-b9e6-b1ed8cec48ee_348x178.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Lt5w!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e07f91f-6d63-4718-b9e6-b1ed8cec48ee_348x178.png" width="186" height="95.13793103448276" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5e07f91f-6d63-4718-b9e6-b1ed8cec48ee_348x178.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:178,&quot;width&quot;:348,&quot;resizeWidth&quot;:186,&quot;bytes&quot;:15592,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162897659?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e07f91f-6d63-4718-b9e6-b1ed8cec48ee_348x178.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Lt5w!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e07f91f-6d63-4718-b9e6-b1ed8cec48ee_348x178.png 424w, https://substackcdn.com/image/fetch/$s_!Lt5w!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e07f91f-6d63-4718-b9e6-b1ed8cec48ee_348x178.png 848w, https://substackcdn.com/image/fetch/$s_!Lt5w!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e07f91f-6d63-4718-b9e6-b1ed8cec48ee_348x178.png 1272w, https://substackcdn.com/image/fetch/$s_!Lt5w!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e07f91f-6d63-4718-b9e6-b1ed8cec48ee_348x178.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p>We can implement the <code>Drop</code> trait like this:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!N1OF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faeb286c1-e10a-4bdb-a080-cd36524c1266_964x282.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!N1OF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faeb286c1-e10a-4bdb-a080-cd36524c1266_964x282.png 424w, https://substackcdn.com/image/fetch/$s_!N1OF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faeb286c1-e10a-4bdb-a080-cd36524c1266_964x282.png 848w, https://substackcdn.com/image/fetch/$s_!N1OF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faeb286c1-e10a-4bdb-a080-cd36524c1266_964x282.png 1272w, https://substackcdn.com/image/fetch/$s_!N1OF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faeb286c1-e10a-4bdb-a080-cd36524c1266_964x282.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!N1OF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faeb286c1-e10a-4bdb-a080-cd36524c1266_964x282.png" width="401" height="117.30497925311204" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/aeb286c1-e10a-4bdb-a080-cd36524c1266_964x282.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:282,&quot;width&quot;:964,&quot;resizeWidth&quot;:401,&quot;bytes&quot;:42743,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162897659?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faeb286c1-e10a-4bdb-a080-cd36524c1266_964x282.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!N1OF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faeb286c1-e10a-4bdb-a080-cd36524c1266_964x282.png 424w, https://substackcdn.com/image/fetch/$s_!N1OF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faeb286c1-e10a-4bdb-a080-cd36524c1266_964x282.png 848w, https://substackcdn.com/image/fetch/$s_!N1OF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faeb286c1-e10a-4bdb-a080-cd36524c1266_964x282.png 1272w, https://substackcdn.com/image/fetch/$s_!N1OF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faeb286c1-e10a-4bdb-a080-cd36524c1266_964x282.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><p>Here I&#8217;ve made the <code>drop</code> function just output some text.</p><p>Now <code>drop()</code> is called whenever the owner of any object of type <code>Data</code> goes out of scope.</p><p>For example, the following code outputs <code>Dropping 0</code>, because here the owner of the <code>Data</code> struct immediately goes out of scope right after it&#8217;s created, at the closing bracket.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iK3N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2eff95da-fa0b-4593-a02e-5077e5cfc965_586x210.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iK3N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2eff95da-fa0b-4593-a02e-5077e5cfc965_586x210.png 424w, https://substackcdn.com/image/fetch/$s_!iK3N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2eff95da-fa0b-4593-a02e-5077e5cfc965_586x210.png 848w, https://substackcdn.com/image/fetch/$s_!iK3N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2eff95da-fa0b-4593-a02e-5077e5cfc965_586x210.png 1272w, https://substackcdn.com/image/fetch/$s_!iK3N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2eff95da-fa0b-4593-a02e-5077e5cfc965_586x210.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iK3N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2eff95da-fa0b-4593-a02e-5077e5cfc965_586x210.png" width="254" height="91.02389078498294" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2eff95da-fa0b-4593-a02e-5077e5cfc965_586x210.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:210,&quot;width&quot;:586,&quot;resizeWidth&quot;:254,&quot;bytes&quot;:17547,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162897659?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2eff95da-fa0b-4593-a02e-5077e5cfc965_586x210.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!iK3N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2eff95da-fa0b-4593-a02e-5077e5cfc965_586x210.png 424w, https://substackcdn.com/image/fetch/$s_!iK3N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2eff95da-fa0b-4593-a02e-5077e5cfc965_586x210.png 848w, https://substackcdn.com/image/fetch/$s_!iK3N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2eff95da-fa0b-4593-a02e-5077e5cfc965_586x210.png 1272w, https://substackcdn.com/image/fetch/$s_!iK3N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2eff95da-fa0b-4593-a02e-5077e5cfc965_586x210.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>As a precaution against cleanup code being called twice, the <code>drop</code> method cannot be called directly, but you can invoke it using the <code>std::mem::drop</code> function (which is part of the prelude so no need for an import).</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!AhIU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb4e7ee-76bf-4306-96aa-c151a203e337_506x136.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!AhIU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb4e7ee-76bf-4306-96aa-c151a203e337_506x136.png 424w, https://substackcdn.com/image/fetch/$s_!AhIU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb4e7ee-76bf-4306-96aa-c151a203e337_506x136.png 848w, https://substackcdn.com/image/fetch/$s_!AhIU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb4e7ee-76bf-4306-96aa-c151a203e337_506x136.png 1272w, https://substackcdn.com/image/fetch/$s_!AhIU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb4e7ee-76bf-4306-96aa-c151a203e337_506x136.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!AhIU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb4e7ee-76bf-4306-96aa-c151a203e337_506x136.png" width="268" height="72.03162055335969" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bcb4e7ee-76bf-4306-96aa-c151a203e337_506x136.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:136,&quot;width&quot;:506,&quot;resizeWidth&quot;:268,&quot;bytes&quot;:18060,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162897659?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb4e7ee-76bf-4306-96aa-c151a203e337_506x136.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!AhIU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb4e7ee-76bf-4306-96aa-c151a203e337_506x136.png 424w, https://substackcdn.com/image/fetch/$s_!AhIU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb4e7ee-76bf-4306-96aa-c151a203e337_506x136.png 848w, https://substackcdn.com/image/fetch/$s_!AhIU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb4e7ee-76bf-4306-96aa-c151a203e337_506x136.png 1272w, https://substackcdn.com/image/fetch/$s_!AhIU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb4e7ee-76bf-4306-96aa-c151a203e337_506x136.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p>]]></content:encoded></item><item><title><![CDATA[Implementing the Deref Trait]]></title><description><![CDATA[Box implements the deref trait, enabling Box variables to behave like references. Here's how.]]></description><link>https://blog.caveofprogramming.com/p/implementing-the-deref-trait</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/implementing-the-deref-trait</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Tue, 29 Apr 2025 11:35:35 GMT</pubDate><enclosure url="https://api.substack.com/feed/podcast/162349600/6bdbc66a59b47323d8a5113268be27a1.mp3" length="0" type="audio/mpeg"/><content:encoded><![CDATA[<p>This article is basically my own understanding of the Rust documentation article found <a href="https://doc.rust-lang.org/book/ch15-02-deref.html">here</a>.</p><p>Code on Github: <a href="https://github.com/caveofprogramming/rust/tree/b95c33a7cfa62f2d2ff58672a9108d2cc2cd0d8f/031%20Implementing%20Deref/deref">https://github.com/caveofprogramming/rust/</a></p><div><hr></div><h3>Why Can Box Be Dereferenced?</h3><p>The following code dereferences <code>Box</code> with <code>*</code> to get the value out of <code>Box</code>. This is what we&#8217;ll look at here. This is accomplished by implementing the <code>Deref</code> trait.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kwt6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9c2f037-edd2-40db-a7df-862b6850b344_508x134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kwt6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9c2f037-edd2-40db-a7df-862b6850b344_508x134.png 424w, https://substackcdn.com/image/fetch/$s_!kwt6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9c2f037-edd2-40db-a7df-862b6850b344_508x134.png 848w, https://substackcdn.com/image/fetch/$s_!kwt6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9c2f037-edd2-40db-a7df-862b6850b344_508x134.png 1272w, https://substackcdn.com/image/fetch/$s_!kwt6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9c2f037-edd2-40db-a7df-862b6850b344_508x134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kwt6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9c2f037-edd2-40db-a7df-862b6850b344_508x134.png" width="318" height="83.88188976377953" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b9c2f037-edd2-40db-a7df-862b6850b344_508x134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:134,&quot;width&quot;:508,&quot;resizeWidth&quot;:318,&quot;bytes&quot;:21141,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162349600?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9c2f037-edd2-40db-a7df-862b6850b344_508x134.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!kwt6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9c2f037-edd2-40db-a7df-862b6850b344_508x134.png 424w, https://substackcdn.com/image/fetch/$s_!kwt6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9c2f037-edd2-40db-a7df-862b6850b344_508x134.png 848w, https://substackcdn.com/image/fetch/$s_!kwt6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9c2f037-edd2-40db-a7df-862b6850b344_508x134.png 1272w, https://substackcdn.com/image/fetch/$s_!kwt6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9c2f037-edd2-40db-a7df-862b6850b344_508x134.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p>Actually, with Box, the following code also works and prints the value 5, even without the dereference operator.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!I28N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7fe6505-0831-4e6c-9090-0263c5064aba_490x144.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!I28N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7fe6505-0831-4e6c-9090-0263c5064aba_490x144.png 424w, https://substackcdn.com/image/fetch/$s_!I28N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7fe6505-0831-4e6c-9090-0263c5064aba_490x144.png 848w, https://substackcdn.com/image/fetch/$s_!I28N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7fe6505-0831-4e6c-9090-0263c5064aba_490x144.png 1272w, https://substackcdn.com/image/fetch/$s_!I28N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7fe6505-0831-4e6c-9090-0263c5064aba_490x144.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!I28N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7fe6505-0831-4e6c-9090-0263c5064aba_490x144.png" width="284" height="83.46122448979592" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a7fe6505-0831-4e6c-9090-0263c5064aba_490x144.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:144,&quot;width&quot;:490,&quot;resizeWidth&quot;:284,&quot;bytes&quot;:20690,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162349600?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7fe6505-0831-4e6c-9090-0263c5064aba_490x144.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!I28N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7fe6505-0831-4e6c-9090-0263c5064aba_490x144.png 424w, https://substackcdn.com/image/fetch/$s_!I28N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7fe6505-0831-4e6c-9090-0263c5064aba_490x144.png 848w, https://substackcdn.com/image/fetch/$s_!I28N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7fe6505-0831-4e6c-9090-0263c5064aba_490x144.png 1272w, https://substackcdn.com/image/fetch/$s_!I28N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7fe6505-0831-4e6c-9090-0263c5064aba_490x144.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><p>This second kind of behaviour apparently depends on special compiler support for <em>deref coercion </em>for types in the standard library, where values are automatically dereferenced. This isn&#8217;t going to work with our own types, although we could make it work by implementing the <code>Display</code> trait.</p><p></p><h3>A Tuple Struct</h3><p>We start with a simple tuple struct. Tuple structs are like structs except the fields aren&#8217;t named.</p><p>The following code defines a tuple struct that can hold one integer value. We create a tuple struct and then print its value, using <code>.0</code> to access its first field.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!XL7N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4536381-da5f-4745-80e0-8777770f372b_684x170.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!XL7N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4536381-da5f-4745-80e0-8777770f372b_684x170.png 424w, https://substackcdn.com/image/fetch/$s_!XL7N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4536381-da5f-4745-80e0-8777770f372b_684x170.png 848w, https://substackcdn.com/image/fetch/$s_!XL7N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4536381-da5f-4745-80e0-8777770f372b_684x170.png 1272w, https://substackcdn.com/image/fetch/$s_!XL7N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4536381-da5f-4745-80e0-8777770f372b_684x170.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!XL7N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4536381-da5f-4745-80e0-8777770f372b_684x170.png" width="364" height="90.46783625730994" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c4536381-da5f-4745-80e0-8777770f372b_684x170.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:170,&quot;width&quot;:684,&quot;resizeWidth&quot;:364,&quot;bytes&quot;:34432,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162349600?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4536381-da5f-4745-80e0-8777770f372b_684x170.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!XL7N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4536381-da5f-4745-80e0-8777770f372b_684x170.png 424w, https://substackcdn.com/image/fetch/$s_!XL7N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4536381-da5f-4745-80e0-8777770f372b_684x170.png 848w, https://substackcdn.com/image/fetch/$s_!XL7N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4536381-da5f-4745-80e0-8777770f372b_684x170.png 1272w, https://substackcdn.com/image/fetch/$s_!XL7N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4536381-da5f-4745-80e0-8777770f372b_684x170.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>We can make this able to handle any type via generics.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!j4ir!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85424829-c5f7-4ed3-ba6f-74ad0f8f0005_696x180.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!j4ir!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85424829-c5f7-4ed3-ba6f-74ad0f8f0005_696x180.png 424w, https://substackcdn.com/image/fetch/$s_!j4ir!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85424829-c5f7-4ed3-ba6f-74ad0f8f0005_696x180.png 848w, https://substackcdn.com/image/fetch/$s_!j4ir!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85424829-c5f7-4ed3-ba6f-74ad0f8f0005_696x180.png 1272w, https://substackcdn.com/image/fetch/$s_!j4ir!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85424829-c5f7-4ed3-ba6f-74ad0f8f0005_696x180.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!j4ir!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85424829-c5f7-4ed3-ba6f-74ad0f8f0005_696x180.png" width="370" height="95.6896551724138" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/85424829-c5f7-4ed3-ba6f-74ad0f8f0005_696x180.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:180,&quot;width&quot;:696,&quot;resizeWidth&quot;:370,&quot;bytes&quot;:35441,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162349600?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85424829-c5f7-4ed3-ba6f-74ad0f8f0005_696x180.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!j4ir!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85424829-c5f7-4ed3-ba6f-74ad0f8f0005_696x180.png 424w, https://substackcdn.com/image/fetch/$s_!j4ir!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85424829-c5f7-4ed3-ba6f-74ad0f8f0005_696x180.png 848w, https://substackcdn.com/image/fetch/$s_!j4ir!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85424829-c5f7-4ed3-ba6f-74ad0f8f0005_696x180.png 1272w, https://substackcdn.com/image/fetch/$s_!j4ir!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85424829-c5f7-4ed3-ba6f-74ad0f8f0005_696x180.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Now the type T is inferred by the compiler, and we can successfully handle a float.</p><p>It is possible to explicitly define the type of thing that the tuple struct works with like this:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!KANj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b7c50e3-942d-432d-8d87-2b183c746df7_688x166.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!KANj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b7c50e3-942d-432d-8d87-2b183c746df7_688x166.png 424w, https://substackcdn.com/image/fetch/$s_!KANj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b7c50e3-942d-432d-8d87-2b183c746df7_688x166.png 848w, https://substackcdn.com/image/fetch/$s_!KANj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b7c50e3-942d-432d-8d87-2b183c746df7_688x166.png 1272w, https://substackcdn.com/image/fetch/$s_!KANj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b7c50e3-942d-432d-8d87-2b183c746df7_688x166.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!KANj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b7c50e3-942d-432d-8d87-2b183c746df7_688x166.png" width="356" height="85.8953488372093" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2b7c50e3-942d-432d-8d87-2b183c746df7_688x166.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:166,&quot;width&quot;:688,&quot;resizeWidth&quot;:356,&quot;bytes&quot;:37836,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162349600?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b7c50e3-942d-432d-8d87-2b183c746df7_688x166.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!KANj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b7c50e3-942d-432d-8d87-2b183c746df7_688x166.png 424w, https://substackcdn.com/image/fetch/$s_!KANj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b7c50e3-942d-432d-8d87-2b183c746df7_688x166.png 848w, https://substackcdn.com/image/fetch/$s_!KANj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b7c50e3-942d-432d-8d87-2b183c746df7_688x166.png 1272w, https://substackcdn.com/image/fetch/$s_!KANj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b7c50e3-942d-432d-8d87-2b183c746df7_688x166.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><h3>Adding a Constructor-Like Method</h3><p>We can add a <code>new</code> function that allows us to construct these tuple structs.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7X8w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08ae727-42a2-448c-8458-46369590bf45_692x280.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7X8w!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08ae727-42a2-448c-8458-46369590bf45_692x280.png 424w, https://substackcdn.com/image/fetch/$s_!7X8w!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08ae727-42a2-448c-8458-46369590bf45_692x280.png 848w, https://substackcdn.com/image/fetch/$s_!7X8w!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08ae727-42a2-448c-8458-46369590bf45_692x280.png 1272w, https://substackcdn.com/image/fetch/$s_!7X8w!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08ae727-42a2-448c-8458-46369590bf45_692x280.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7X8w!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08ae727-42a2-448c-8458-46369590bf45_692x280.png" width="334" height="135.14450867052022" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f08ae727-42a2-448c-8458-46369590bf45_692x280.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:280,&quot;width&quot;:692,&quot;resizeWidth&quot;:334,&quot;bytes&quot;:36309,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162349600?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08ae727-42a2-448c-8458-46369590bf45_692x280.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7X8w!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08ae727-42a2-448c-8458-46369590bf45_692x280.png 424w, https://substackcdn.com/image/fetch/$s_!7X8w!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08ae727-42a2-448c-8458-46369590bf45_692x280.png 848w, https://substackcdn.com/image/fetch/$s_!7X8w!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08ae727-42a2-448c-8458-46369590bf45_692x280.png 1272w, https://substackcdn.com/image/fetch/$s_!7X8w!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff08ae727-42a2-448c-8458-46369590bf45_692x280.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The name <code>new</code> here is only a convention. </p><p>The first <code>&lt;T&gt;</code> after <code>impl</code> specifies that <code>T</code> is a type that can be used in the rest of the definition. The <code>&lt;T&gt;</code> after <code>Value</code> says that <code>Value</code> is actually a generic type and is going to work with whatever <code>T</code> is.</p><p>Now we can create a Value like this:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hd19!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe223a38-8181-40a7-ae8a-6021ecea4429_576x74.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hd19!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe223a38-8181-40a7-ae8a-6021ecea4429_576x74.png 424w, https://substackcdn.com/image/fetch/$s_!hd19!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe223a38-8181-40a7-ae8a-6021ecea4429_576x74.png 848w, https://substackcdn.com/image/fetch/$s_!hd19!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe223a38-8181-40a7-ae8a-6021ecea4429_576x74.png 1272w, https://substackcdn.com/image/fetch/$s_!hd19!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe223a38-8181-40a7-ae8a-6021ecea4429_576x74.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hd19!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe223a38-8181-40a7-ae8a-6021ecea4429_576x74.png" width="350" height="44.96527777777778" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fe223a38-8181-40a7-ae8a-6021ecea4429_576x74.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:74,&quot;width&quot;:576,&quot;resizeWidth&quot;:350,&quot;bytes&quot;:15644,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162349600?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe223a38-8181-40a7-ae8a-6021ecea4429_576x74.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hd19!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe223a38-8181-40a7-ae8a-6021ecea4429_576x74.png 424w, https://substackcdn.com/image/fetch/$s_!hd19!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe223a38-8181-40a7-ae8a-6021ecea4429_576x74.png 848w, https://substackcdn.com/image/fetch/$s_!hd19!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe223a38-8181-40a7-ae8a-6021ecea4429_576x74.png 1272w, https://substackcdn.com/image/fetch/$s_!hd19!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe223a38-8181-40a7-ae8a-6021ecea4429_576x74.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>But we still can&#8217;t use <code>*</code> to get the numeric or other value out of our <code>Value</code> struct.</p><p>To make that work we need to implement the <code>Deref</code> trait.</p><p></p><h3>Implementing Deref</h3><p>We can implement Deref like this:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!jc0H!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f2e48a-e240-4e23-99d0-93189015bff6_872x398.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!jc0H!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f2e48a-e240-4e23-99d0-93189015bff6_872x398.png 424w, https://substackcdn.com/image/fetch/$s_!jc0H!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f2e48a-e240-4e23-99d0-93189015bff6_872x398.png 848w, https://substackcdn.com/image/fetch/$s_!jc0H!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f2e48a-e240-4e23-99d0-93189015bff6_872x398.png 1272w, https://substackcdn.com/image/fetch/$s_!jc0H!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f2e48a-e240-4e23-99d0-93189015bff6_872x398.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!jc0H!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f2e48a-e240-4e23-99d0-93189015bff6_872x398.png" width="452" height="206.30275229357798" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f3f2e48a-e240-4e23-99d0-93189015bff6_872x398.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:398,&quot;width&quot;:872,&quot;resizeWidth&quot;:452,&quot;bytes&quot;:47033,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162349600?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f2e48a-e240-4e23-99d0-93189015bff6_872x398.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!jc0H!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f2e48a-e240-4e23-99d0-93189015bff6_872x398.png 424w, https://substackcdn.com/image/fetch/$s_!jc0H!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f2e48a-e240-4e23-99d0-93189015bff6_872x398.png 848w, https://substackcdn.com/image/fetch/$s_!jc0H!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f2e48a-e240-4e23-99d0-93189015bff6_872x398.png 1272w, https://substackcdn.com/image/fetch/$s_!jc0H!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f2e48a-e240-4e23-99d0-93189015bff6_872x398.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The <code>type Target = T </code>here provides an associated type, <code>T</code>, that will be returned by the dereference operator.</p><p>We can now do this:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZXer!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feff55d97-bb00-44a3-a172-9b6c457c5c08_588x138.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZXer!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feff55d97-bb00-44a3-a172-9b6c457c5c08_588x138.png 424w, https://substackcdn.com/image/fetch/$s_!ZXer!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feff55d97-bb00-44a3-a172-9b6c457c5c08_588x138.png 848w, https://substackcdn.com/image/fetch/$s_!ZXer!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feff55d97-bb00-44a3-a172-9b6c457c5c08_588x138.png 1272w, https://substackcdn.com/image/fetch/$s_!ZXer!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feff55d97-bb00-44a3-a172-9b6c457c5c08_588x138.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZXer!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feff55d97-bb00-44a3-a172-9b6c457c5c08_588x138.png" width="396" height="92.93877551020408" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/eff55d97-bb00-44a3-a172-9b6c457c5c08_588x138.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:138,&quot;width&quot;:588,&quot;resizeWidth&quot;:396,&quot;bytes&quot;:24914,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162349600?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feff55d97-bb00-44a3-a172-9b6c457c5c08_588x138.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ZXer!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feff55d97-bb00-44a3-a172-9b6c457c5c08_588x138.png 424w, https://substackcdn.com/image/fetch/$s_!ZXer!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feff55d97-bb00-44a3-a172-9b6c457c5c08_588x138.png 848w, https://substackcdn.com/image/fetch/$s_!ZXer!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feff55d97-bb00-44a3-a172-9b6c457c5c08_588x138.png 1272w, https://substackcdn.com/image/fetch/$s_!ZXer!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feff55d97-bb00-44a3-a172-9b6c457c5c08_588x138.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><h3>Implicit Deref Coercion</h3><p>Although we can&#8217;t pass a <code>Value</code> to <code>println!</code> without dereferencing it, there are circumstances under which Rust will actually dereference it as many times as necessary to get a value of the right type, and this happens at compile time.</p><p>Suppose we define a function like this:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4FFh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d6c877-9f8c-4f05-859a-124ca6867868_582x182.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4FFh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d6c877-9f8c-4f05-859a-124ca6867868_582x182.png 424w, https://substackcdn.com/image/fetch/$s_!4FFh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d6c877-9f8c-4f05-859a-124ca6867868_582x182.png 848w, https://substackcdn.com/image/fetch/$s_!4FFh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d6c877-9f8c-4f05-859a-124ca6867868_582x182.png 1272w, https://substackcdn.com/image/fetch/$s_!4FFh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d6c877-9f8c-4f05-859a-124ca6867868_582x182.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4FFh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d6c877-9f8c-4f05-859a-124ca6867868_582x182.png" width="354" height="110.70103092783505" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a8d6c877-9f8c-4f05-859a-124ca6867868_582x182.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:182,&quot;width&quot;:582,&quot;resizeWidth&quot;:354,&quot;bytes&quot;:25264,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162349600?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d6c877-9f8c-4f05-859a-124ca6867868_582x182.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4FFh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d6c877-9f8c-4f05-859a-124ca6867868_582x182.png 424w, https://substackcdn.com/image/fetch/$s_!4FFh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d6c877-9f8c-4f05-859a-124ca6867868_582x182.png 848w, https://substackcdn.com/image/fetch/$s_!4FFh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d6c877-9f8c-4f05-859a-124ca6867868_582x182.png 1272w, https://substackcdn.com/image/fetch/$s_!4FFh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d6c877-9f8c-4f05-859a-124ca6867868_582x182.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>And we have a <code>Value</code> containing an <code>i32</code>:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!K2mB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c611-1d48-4cde-a24f-67f7dcd08614_588x76.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!K2mB!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c611-1d48-4cde-a24f-67f7dcd08614_588x76.png 424w, https://substackcdn.com/image/fetch/$s_!K2mB!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c611-1d48-4cde-a24f-67f7dcd08614_588x76.png 848w, https://substackcdn.com/image/fetch/$s_!K2mB!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c611-1d48-4cde-a24f-67f7dcd08614_588x76.png 1272w, https://substackcdn.com/image/fetch/$s_!K2mB!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c611-1d48-4cde-a24f-67f7dcd08614_588x76.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!K2mB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c611-1d48-4cde-a24f-67f7dcd08614_588x76.png" width="322" height="41.61904761904762" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3709c611-1d48-4cde-a24f-67f7dcd08614_588x76.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:76,&quot;width&quot;:588,&quot;resizeWidth&quot;:322,&quot;bytes&quot;:15836,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162349600?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c611-1d48-4cde-a24f-67f7dcd08614_588x76.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!K2mB!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c611-1d48-4cde-a24f-67f7dcd08614_588x76.png 424w, https://substackcdn.com/image/fetch/$s_!K2mB!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c611-1d48-4cde-a24f-67f7dcd08614_588x76.png 848w, https://substackcdn.com/image/fetch/$s_!K2mB!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c611-1d48-4cde-a24f-67f7dcd08614_588x76.png 1272w, https://substackcdn.com/image/fetch/$s_!K2mB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c611-1d48-4cde-a24f-67f7dcd08614_588x76.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>We still cannot pass this value directly to <code>test()</code>, but if we pass a reference to a <code>Value</code> containing an <code>i32</code>, Rust will dereference it <em>twice</em> to coerce the <code>Value</code> into an <code>i32</code>.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VhG7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e8b1c4-4287-4474-9398-979bc91fe821_292x52.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VhG7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e8b1c4-4287-4474-9398-979bc91fe821_292x52.png 424w, https://substackcdn.com/image/fetch/$s_!VhG7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e8b1c4-4287-4474-9398-979bc91fe821_292x52.png 848w, https://substackcdn.com/image/fetch/$s_!VhG7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e8b1c4-4287-4474-9398-979bc91fe821_292x52.png 1272w, https://substackcdn.com/image/fetch/$s_!VhG7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e8b1c4-4287-4474-9398-979bc91fe821_292x52.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VhG7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e8b1c4-4287-4474-9398-979bc91fe821_292x52.png" width="180" height="32.054794520547944" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/40e8b1c4-4287-4474-9398-979bc91fe821_292x52.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:52,&quot;width&quot;:292,&quot;resizeWidth&quot;:180,&quot;bytes&quot;:11198,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/162349600?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e8b1c4-4287-4474-9398-979bc91fe821_292x52.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VhG7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e8b1c4-4287-4474-9398-979bc91fe821_292x52.png 424w, https://substackcdn.com/image/fetch/$s_!VhG7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e8b1c4-4287-4474-9398-979bc91fe821_292x52.png 848w, https://substackcdn.com/image/fetch/$s_!VhG7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e8b1c4-4287-4474-9398-979bc91fe821_292x52.png 1272w, https://substackcdn.com/image/fetch/$s_!VhG7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e8b1c4-4287-4474-9398-979bc91fe821_292x52.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This actually works!</p>]]></content:encoded></item><item><title><![CDATA[Box: Storing on the Heap]]></title><description><![CDATA[A kind of smart pointer]]></description><link>https://blog.caveofprogramming.com/p/box-storing-on-the-heap</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/box-storing-on-the-heap</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Tue, 22 Apr 2025 18:23:15 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/df041af6-a074-48d5-8206-4e49e69e54b8_1024x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p></p><p></p><div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;2b57d810-482c-43f0-88ef-613b24623154&quot;,&quot;duration&quot;:null}"></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.caveofprogramming.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">The Cave of Programming Blog is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>Code: <a href="https://github.com/caveofprogramming/rust/tree/main/030%20Boxes/boxes">https://github.com/caveofprogramming/rust/</a></p><p>The following leans heavily on the examples in the <a href="https://doc.rust-lang.org/book/ch15-01-box.html">Rust documentation</a>.</p><p>I&#8217;ve tried to enlarge on the existing examples and explanation.</p><p></p><h3>Storing on the Heap</h3><p>In Rust, <code>Box</code> can be used to store values on the heap instead of the stack.</p><p>For example, in this code, the value of the integer is stored on the stack:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tn0b!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F229c1e2c-f49a-4366-b063-be7ac9b725bf_652x130.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tn0b!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F229c1e2c-f49a-4366-b063-be7ac9b725bf_652x130.png 424w, https://substackcdn.com/image/fetch/$s_!tn0b!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F229c1e2c-f49a-4366-b063-be7ac9b725bf_652x130.png 848w, https://substackcdn.com/image/fetch/$s_!tn0b!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F229c1e2c-f49a-4366-b063-be7ac9b725bf_652x130.png 1272w, https://substackcdn.com/image/fetch/$s_!tn0b!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F229c1e2c-f49a-4366-b063-be7ac9b725bf_652x130.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tn0b!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F229c1e2c-f49a-4366-b063-be7ac9b725bf_652x130.png" width="430" height="85.7361963190184" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/229c1e2c-f49a-4366-b063-be7ac9b725bf_652x130.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:130,&quot;width&quot;:652,&quot;resizeWidth&quot;:430,&quot;bytes&quot;:21147,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161804803?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F229c1e2c-f49a-4366-b063-be7ac9b725bf_652x130.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tn0b!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F229c1e2c-f49a-4366-b063-be7ac9b725bf_652x130.png 424w, https://substackcdn.com/image/fetch/$s_!tn0b!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F229c1e2c-f49a-4366-b063-be7ac9b725bf_652x130.png 848w, https://substackcdn.com/image/fetch/$s_!tn0b!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F229c1e2c-f49a-4366-b063-be7ac9b725bf_652x130.png 1272w, https://substackcdn.com/image/fetch/$s_!tn0b!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F229c1e2c-f49a-4366-b063-be7ac9b725bf_652x130.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>A slight change, to use <code>Box</code>, means the <code>value</code> is stored on the heap instead:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8qyu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4edfb836-9991-4dd1-a2a0-aface44bb293_664x120.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8qyu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4edfb836-9991-4dd1-a2a0-aface44bb293_664x120.png 424w, https://substackcdn.com/image/fetch/$s_!8qyu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4edfb836-9991-4dd1-a2a0-aface44bb293_664x120.png 848w, https://substackcdn.com/image/fetch/$s_!8qyu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4edfb836-9991-4dd1-a2a0-aface44bb293_664x120.png 1272w, https://substackcdn.com/image/fetch/$s_!8qyu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4edfb836-9991-4dd1-a2a0-aface44bb293_664x120.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8qyu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4edfb836-9991-4dd1-a2a0-aface44bb293_664x120.png" width="432" height="78.07228915662651" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4edfb836-9991-4dd1-a2a0-aface44bb293_664x120.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:120,&quot;width&quot;:664,&quot;resizeWidth&quot;:432,&quot;bytes&quot;:25917,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161804803?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4edfb836-9991-4dd1-a2a0-aface44bb293_664x120.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8qyu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4edfb836-9991-4dd1-a2a0-aface44bb293_664x120.png 424w, https://substackcdn.com/image/fetch/$s_!8qyu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4edfb836-9991-4dd1-a2a0-aface44bb293_664x120.png 848w, https://substackcdn.com/image/fetch/$s_!8qyu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4edfb836-9991-4dd1-a2a0-aface44bb293_664x120.png 1272w, https://substackcdn.com/image/fetch/$s_!8qyu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4edfb836-9991-4dd1-a2a0-aface44bb293_664x120.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>In both cases the output is:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!g8HT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ab7d36d-c198-410b-941b-41ca2aae012a_190x42.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!g8HT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ab7d36d-c198-410b-941b-41ca2aae012a_190x42.png 424w, https://substackcdn.com/image/fetch/$s_!g8HT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ab7d36d-c198-410b-941b-41ca2aae012a_190x42.png 848w, https://substackcdn.com/image/fetch/$s_!g8HT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ab7d36d-c198-410b-941b-41ca2aae012a_190x42.png 1272w, https://substackcdn.com/image/fetch/$s_!g8HT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ab7d36d-c198-410b-941b-41ca2aae012a_190x42.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!g8HT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ab7d36d-c198-410b-941b-41ca2aae012a_190x42.png" width="190" height="42" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1ab7d36d-c198-410b-941b-41ca2aae012a_190x42.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:42,&quot;width&quot;:190,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:6609,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161804803?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ab7d36d-c198-410b-941b-41ca2aae012a_190x42.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!g8HT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ab7d36d-c198-410b-941b-41ca2aae012a_190x42.png 424w, https://substackcdn.com/image/fetch/$s_!g8HT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ab7d36d-c198-410b-941b-41ca2aae012a_190x42.png 848w, https://substackcdn.com/image/fetch/$s_!g8HT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ab7d36d-c198-410b-941b-41ca2aae012a_190x42.png 1272w, https://substackcdn.com/image/fetch/$s_!g8HT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ab7d36d-c198-410b-941b-41ca2aae012a_190x42.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Even though type of <code>value</code> in the second example is now <code>Box&lt;i32&gt;</code>, Box implements the <code>Deref</code> trait, which means when we access <code>value</code> it is automatically dereferenced without need for any new syntax.</p><p>Storing a single integer on the heap seems a bit useless, but we can also store other things on the heap, like large arrays. The following code creates an array on the stack; it has 100 elements but could of course have many more.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wtNX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3321a887-b937-4ba6-866f-5dee2d8c9adc_740x178.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wtNX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3321a887-b937-4ba6-866f-5dee2d8c9adc_740x178.png 424w, https://substackcdn.com/image/fetch/$s_!wtNX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3321a887-b937-4ba6-866f-5dee2d8c9adc_740x178.png 848w, https://substackcdn.com/image/fetch/$s_!wtNX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3321a887-b937-4ba6-866f-5dee2d8c9adc_740x178.png 1272w, https://substackcdn.com/image/fetch/$s_!wtNX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3321a887-b937-4ba6-866f-5dee2d8c9adc_740x178.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wtNX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3321a887-b937-4ba6-866f-5dee2d8c9adc_740x178.png" width="496" height="119.30810810810812" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3321a887-b937-4ba6-866f-5dee2d8c9adc_740x178.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:178,&quot;width&quot;:740,&quot;resizeWidth&quot;:496,&quot;bytes&quot;:30567,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161804803?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3321a887-b937-4ba6-866f-5dee2d8c9adc_740x178.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wtNX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3321a887-b937-4ba6-866f-5dee2d8c9adc_740x178.png 424w, https://substackcdn.com/image/fetch/$s_!wtNX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3321a887-b937-4ba6-866f-5dee2d8c9adc_740x178.png 848w, https://substackcdn.com/image/fetch/$s_!wtNX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3321a887-b937-4ba6-866f-5dee2d8c9adc_740x178.png 1272w, https://substackcdn.com/image/fetch/$s_!wtNX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3321a887-b937-4ba6-866f-5dee2d8c9adc_740x178.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>With a slight adjustment, the array exists on the heap instead:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gMTS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe0bea943-929f-429c-be90-0868b7c13783_758x178.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gMTS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe0bea943-929f-429c-be90-0868b7c13783_758x178.png 424w, https://substackcdn.com/image/fetch/$s_!gMTS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe0bea943-929f-429c-be90-0868b7c13783_758x178.png 848w, https://substackcdn.com/image/fetch/$s_!gMTS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe0bea943-929f-429c-be90-0868b7c13783_758x178.png 1272w, https://substackcdn.com/image/fetch/$s_!gMTS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe0bea943-929f-429c-be90-0868b7c13783_758x178.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gMTS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe0bea943-929f-429c-be90-0868b7c13783_758x178.png" width="488" height="114.59630606860158" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e0bea943-929f-429c-be90-0868b7c13783_758x178.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:178,&quot;width&quot;:758,&quot;resizeWidth&quot;:488,&quot;bytes&quot;:35885,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161804803?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe0bea943-929f-429c-be90-0868b7c13783_758x178.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gMTS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe0bea943-929f-429c-be90-0868b7c13783_758x178.png 424w, https://substackcdn.com/image/fetch/$s_!gMTS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe0bea943-929f-429c-be90-0868b7c13783_758x178.png 848w, https://substackcdn.com/image/fetch/$s_!gMTS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe0bea943-929f-429c-be90-0868b7c13783_758x178.png 1272w, https://substackcdn.com/image/fetch/$s_!gMTS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe0bea943-929f-429c-be90-0868b7c13783_758x178.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><h3>Recursive Types</h3><p>The following code creates an enum called <code>List</code>, with two variants: <code>A</code> and <code>B</code>.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Q3CG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F833e907c-a417-479c-99b7-73c5f51ab40b_402x336.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Q3CG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F833e907c-a417-479c-99b7-73c5f51ab40b_402x336.png 424w, https://substackcdn.com/image/fetch/$s_!Q3CG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F833e907c-a417-479c-99b7-73c5f51ab40b_402x336.png 848w, https://substackcdn.com/image/fetch/$s_!Q3CG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F833e907c-a417-479c-99b7-73c5f51ab40b_402x336.png 1272w, https://substackcdn.com/image/fetch/$s_!Q3CG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F833e907c-a417-479c-99b7-73c5f51ab40b_402x336.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Q3CG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F833e907c-a417-479c-99b7-73c5f51ab40b_402x336.png" width="250" height="208.955223880597" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/833e907c-a417-479c-99b7-73c5f51ab40b_402x336.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:336,&quot;width&quot;:402,&quot;resizeWidth&quot;:250,&quot;bytes&quot;:18476,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161804803?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F833e907c-a417-479c-99b7-73c5f51ab40b_402x336.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Q3CG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F833e907c-a417-479c-99b7-73c5f51ab40b_402x336.png 424w, https://substackcdn.com/image/fetch/$s_!Q3CG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F833e907c-a417-479c-99b7-73c5f51ab40b_402x336.png 848w, https://substackcdn.com/image/fetch/$s_!Q3CG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F833e907c-a417-479c-99b7-73c5f51ab40b_402x336.png 1272w, https://substackcdn.com/image/fetch/$s_!Q3CG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F833e907c-a417-479c-99b7-73c5f51ab40b_402x336.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>It&#8217;s possible to associate one or more values with each variant. These can then be extracted using a <code>match</code>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ww3O!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F167e3eba-29fd-4358-b194-58e884f7346e_802x700.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ww3O!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F167e3eba-29fd-4358-b194-58e884f7346e_802x700.png 424w, https://substackcdn.com/image/fetch/$s_!ww3O!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F167e3eba-29fd-4358-b194-58e884f7346e_802x700.png 848w, https://substackcdn.com/image/fetch/$s_!ww3O!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F167e3eba-29fd-4358-b194-58e884f7346e_802x700.png 1272w, https://substackcdn.com/image/fetch/$s_!ww3O!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F167e3eba-29fd-4358-b194-58e884f7346e_802x700.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ww3O!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F167e3eba-29fd-4358-b194-58e884f7346e_802x700.png" width="386" height="336.90773067331673" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/167e3eba-29fd-4358-b194-58e884f7346e_802x700.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:700,&quot;width&quot;:802,&quot;resizeWidth&quot;:386,&quot;bytes&quot;:69426,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161804803?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F167e3eba-29fd-4358-b194-58e884f7346e_802x700.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ww3O!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F167e3eba-29fd-4358-b194-58e884f7346e_802x700.png 424w, https://substackcdn.com/image/fetch/$s_!ww3O!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F167e3eba-29fd-4358-b194-58e884f7346e_802x700.png 848w, https://substackcdn.com/image/fetch/$s_!ww3O!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F167e3eba-29fd-4358-b194-58e884f7346e_802x700.png 1272w, https://substackcdn.com/image/fetch/$s_!ww3O!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F167e3eba-29fd-4358-b194-58e884f7346e_802x700.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In this case, the <code>match</code> extracts both values associated with the variant <code>A</code> as a tuple. This code prints:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!98Hu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e6ae5f8-7bab-46e0-81d4-8355b1fdaa66_92x54.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!98Hu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e6ae5f8-7bab-46e0-81d4-8355b1fdaa66_92x54.png 424w, https://substackcdn.com/image/fetch/$s_!98Hu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e6ae5f8-7bab-46e0-81d4-8355b1fdaa66_92x54.png 848w, https://substackcdn.com/image/fetch/$s_!98Hu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e6ae5f8-7bab-46e0-81d4-8355b1fdaa66_92x54.png 1272w, https://substackcdn.com/image/fetch/$s_!98Hu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e6ae5f8-7bab-46e0-81d4-8355b1fdaa66_92x54.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!98Hu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e6ae5f8-7bab-46e0-81d4-8355b1fdaa66_92x54.png" width="68" height="39.91304347826087" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8e6ae5f8-7bab-46e0-81d4-8355b1fdaa66_92x54.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:54,&quot;width&quot;:92,&quot;resizeWidth&quot;:68,&quot;bytes&quot;:5302,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161804803?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e6ae5f8-7bab-46e0-81d4-8355b1fdaa66_92x54.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!98Hu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e6ae5f8-7bab-46e0-81d4-8355b1fdaa66_92x54.png 424w, https://substackcdn.com/image/fetch/$s_!98Hu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e6ae5f8-7bab-46e0-81d4-8355b1fdaa66_92x54.png 848w, https://substackcdn.com/image/fetch/$s_!98Hu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e6ae5f8-7bab-46e0-81d4-8355b1fdaa66_92x54.png 1272w, https://substackcdn.com/image/fetch/$s_!98Hu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e6ae5f8-7bab-46e0-81d4-8355b1fdaa66_92x54.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>In Lisp, it&#8217;s possible to create a list using a &#8220;cons cell&#8221;, which is a pair of two values, not unlike our <code>enum</code>. &#8220;Cons&#8221; comes from the word &#8220;construct&#8221;. This can be done via a recursive definition.</p><p>You might think the equivalent in Rust would be as follows:</p><p><strong>(This does not work)</strong></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FJFD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3921ccd-ad2a-41ea-844e-50f57021a717_898x338.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FJFD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3921ccd-ad2a-41ea-844e-50f57021a717_898x338.png 424w, https://substackcdn.com/image/fetch/$s_!FJFD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3921ccd-ad2a-41ea-844e-50f57021a717_898x338.png 848w, https://substackcdn.com/image/fetch/$s_!FJFD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3921ccd-ad2a-41ea-844e-50f57021a717_898x338.png 1272w, https://substackcdn.com/image/fetch/$s_!FJFD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3921ccd-ad2a-41ea-844e-50f57021a717_898x338.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FJFD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3921ccd-ad2a-41ea-844e-50f57021a717_898x338.png" width="390" height="146.79287305122494" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c3921ccd-ad2a-41ea-844e-50f57021a717_898x338.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:338,&quot;width&quot;:898,&quot;resizeWidth&quot;:390,&quot;bytes&quot;:33798,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161804803?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3921ccd-ad2a-41ea-844e-50f57021a717_898x338.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!FJFD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3921ccd-ad2a-41ea-844e-50f57021a717_898x338.png 424w, https://substackcdn.com/image/fetch/$s_!FJFD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3921ccd-ad2a-41ea-844e-50f57021a717_898x338.png 848w, https://substackcdn.com/image/fetch/$s_!FJFD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3921ccd-ad2a-41ea-844e-50f57021a717_898x338.png 1272w, https://substackcdn.com/image/fetch/$s_!FJFD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3921ccd-ad2a-41ea-844e-50f57021a717_898x338.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The error when we compile this code is:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!RSVC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3068f1dc-714c-40ee-a1a4-ba7daa4eed12_900x188.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!RSVC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3068f1dc-714c-40ee-a1a4-ba7daa4eed12_900x188.png 424w, https://substackcdn.com/image/fetch/$s_!RSVC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3068f1dc-714c-40ee-a1a4-ba7daa4eed12_900x188.png 848w, https://substackcdn.com/image/fetch/$s_!RSVC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3068f1dc-714c-40ee-a1a4-ba7daa4eed12_900x188.png 1272w, https://substackcdn.com/image/fetch/$s_!RSVC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3068f1dc-714c-40ee-a1a4-ba7daa4eed12_900x188.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!RSVC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3068f1dc-714c-40ee-a1a4-ba7daa4eed12_900x188.png" width="400" height="83.55555555555556" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3068f1dc-714c-40ee-a1a4-ba7daa4eed12_900x188.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:188,&quot;width&quot;:900,&quot;resizeWidth&quot;:400,&quot;bytes&quot;:20567,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161804803?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3068f1dc-714c-40ee-a1a4-ba7daa4eed12_900x188.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!RSVC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3068f1dc-714c-40ee-a1a4-ba7daa4eed12_900x188.png 424w, https://substackcdn.com/image/fetch/$s_!RSVC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3068f1dc-714c-40ee-a1a4-ba7daa4eed12_900x188.png 848w, https://substackcdn.com/image/fetch/$s_!RSVC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3068f1dc-714c-40ee-a1a4-ba7daa4eed12_900x188.png 1272w, https://substackcdn.com/image/fetch/$s_!RSVC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3068f1dc-714c-40ee-a1a4-ba7daa4eed12_900x188.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><p>The problem is apparently that Rust cannot determine the size of a <code>List</code>. However, if we insert a Box in there, this does work. Since the content of the <code>Box</code> is stored on the heap, the size of the <code>enum</code> now only involves the size of a pointer to that data, not the data itself.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!LGPr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdddc201f-4cef-48ef-9311-a8859f76045b_1306x346.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!LGPr!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdddc201f-4cef-48ef-9311-a8859f76045b_1306x346.png 424w, https://substackcdn.com/image/fetch/$s_!LGPr!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdddc201f-4cef-48ef-9311-a8859f76045b_1306x346.png 848w, https://substackcdn.com/image/fetch/$s_!LGPr!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdddc201f-4cef-48ef-9311-a8859f76045b_1306x346.png 1272w, https://substackcdn.com/image/fetch/$s_!LGPr!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdddc201f-4cef-48ef-9311-a8859f76045b_1306x346.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!LGPr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdddc201f-4cef-48ef-9311-a8859f76045b_1306x346.png" width="599" height="158.6937212863706" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dddc201f-4cef-48ef-9311-a8859f76045b_1306x346.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:346,&quot;width&quot;:1306,&quot;resizeWidth&quot;:599,&quot;bytes&quot;:46115,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161804803?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdddc201f-4cef-48ef-9311-a8859f76045b_1306x346.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!LGPr!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdddc201f-4cef-48ef-9311-a8859f76045b_1306x346.png 424w, https://substackcdn.com/image/fetch/$s_!LGPr!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdddc201f-4cef-48ef-9311-a8859f76045b_1306x346.png 848w, https://substackcdn.com/image/fetch/$s_!LGPr!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdddc201f-4cef-48ef-9311-a8859f76045b_1306x346.png 1272w, https://substackcdn.com/image/fetch/$s_!LGPr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdddc201f-4cef-48ef-9311-a8859f76045b_1306x346.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>We can also change the names of the variants to something inspired by Lisp.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EP0c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe154ce28-e1c9-4b21-afb7-2f8b3dc9a015_1468x330.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EP0c!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe154ce28-e1c9-4b21-afb7-2f8b3dc9a015_1468x330.png 424w, https://substackcdn.com/image/fetch/$s_!EP0c!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe154ce28-e1c9-4b21-afb7-2f8b3dc9a015_1468x330.png 848w, https://substackcdn.com/image/fetch/$s_!EP0c!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe154ce28-e1c9-4b21-afb7-2f8b3dc9a015_1468x330.png 1272w, https://substackcdn.com/image/fetch/$s_!EP0c!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe154ce28-e1c9-4b21-afb7-2f8b3dc9a015_1468x330.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!EP0c!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe154ce28-e1c9-4b21-afb7-2f8b3dc9a015_1468x330.png" width="618" height="138.79532967032966" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e154ce28-e1c9-4b21-afb7-2f8b3dc9a015_1468x330.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:327,&quot;width&quot;:1456,&quot;resizeWidth&quot;:618,&quot;bytes&quot;:55587,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161804803?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe154ce28-e1c9-4b21-afb7-2f8b3dc9a015_1468x330.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EP0c!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe154ce28-e1c9-4b21-afb7-2f8b3dc9a015_1468x330.png 424w, https://substackcdn.com/image/fetch/$s_!EP0c!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe154ce28-e1c9-4b21-afb7-2f8b3dc9a015_1468x330.png 848w, https://substackcdn.com/image/fetch/$s_!EP0c!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe154ce28-e1c9-4b21-afb7-2f8b3dc9a015_1468x330.png 1272w, https://substackcdn.com/image/fetch/$s_!EP0c!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe154ce28-e1c9-4b21-afb7-2f8b3dc9a015_1468x330.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The size of the List comes out at 16 bytes, as against 8 bytes for just a Box.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vsUU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F539d6d22-d111-44f4-98f0-9b1cea10e718_1438x118.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vsUU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F539d6d22-d111-44f4-98f0-9b1cea10e718_1438x118.png 424w, https://substackcdn.com/image/fetch/$s_!vsUU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F539d6d22-d111-44f4-98f0-9b1cea10e718_1438x118.png 848w, https://substackcdn.com/image/fetch/$s_!vsUU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F539d6d22-d111-44f4-98f0-9b1cea10e718_1438x118.png 1272w, https://substackcdn.com/image/fetch/$s_!vsUU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F539d6d22-d111-44f4-98f0-9b1cea10e718_1438x118.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vsUU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F539d6d22-d111-44f4-98f0-9b1cea10e718_1438x118.png" width="1438" height="118" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/539d6d22-d111-44f4-98f0-9b1cea10e718_1438x118.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:118,&quot;width&quot;:1438,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:50902,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161804803?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F539d6d22-d111-44f4-98f0-9b1cea10e718_1438x118.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!vsUU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F539d6d22-d111-44f4-98f0-9b1cea10e718_1438x118.png 424w, https://substackcdn.com/image/fetch/$s_!vsUU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F539d6d22-d111-44f4-98f0-9b1cea10e718_1438x118.png 848w, https://substackcdn.com/image/fetch/$s_!vsUU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F539d6d22-d111-44f4-98f0-9b1cea10e718_1438x118.png 1272w, https://substackcdn.com/image/fetch/$s_!vsUU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F539d6d22-d111-44f4-98f0-9b1cea10e718_1438x118.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8lVw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9206a28f-22a7-4220-9751-8da3b3e22bac_406x72.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8lVw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9206a28f-22a7-4220-9751-8da3b3e22bac_406x72.png 424w, https://substackcdn.com/image/fetch/$s_!8lVw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9206a28f-22a7-4220-9751-8da3b3e22bac_406x72.png 848w, https://substackcdn.com/image/fetch/$s_!8lVw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9206a28f-22a7-4220-9751-8da3b3e22bac_406x72.png 1272w, https://substackcdn.com/image/fetch/$s_!8lVw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9206a28f-22a7-4220-9751-8da3b3e22bac_406x72.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8lVw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9206a28f-22a7-4220-9751-8da3b3e22bac_406x72.png" width="310" height="54.97536945812808" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9206a28f-22a7-4220-9751-8da3b3e22bac_406x72.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:72,&quot;width&quot;:406,&quot;resizeWidth&quot;:310,&quot;bytes&quot;:8126,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161804803?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e2a4f29-a026-4bcf-bd86-e7215b3501a8_406x86.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8lVw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9206a28f-22a7-4220-9751-8da3b3e22bac_406x72.png 424w, https://substackcdn.com/image/fetch/$s_!8lVw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9206a28f-22a7-4220-9751-8da3b3e22bac_406x72.png 848w, https://substackcdn.com/image/fetch/$s_!8lVw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9206a28f-22a7-4220-9751-8da3b3e22bac_406x72.png 1272w, https://substackcdn.com/image/fetch/$s_!8lVw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9206a28f-22a7-4220-9751-8da3b3e22bac_406x72.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This size is determined by several factor: the size of the largest variant, data associated with the variants, and the size of the discriminant that specifies which variant is being used (one byte for up to 256 variants).</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.caveofprogramming.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">The Cave of Programming Blog is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[More Rust Iterators]]></title><description><![CDATA[A program using iterators to process command-line args.]]></description><link>https://blog.caveofprogramming.com/p/more-rust-iterators</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/more-rust-iterators</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Fri, 11 Apr 2025 12:35:05 GMT</pubDate><enclosure url="https://api.substack.com/feed/podcast/161090542/3554451c286b7e034f528712f95807bf.mp3" length="0" type="audio/mpeg"/><content:encoded><![CDATA[<p>GitHub source code: <a href="https://github.com/caveofprogramming/rust/tree/main/029%20More%20Iterators/iterate">https://github.com/caveofprogramming/rust/</a></p><p>In this video I&#8217;ve put together a program that parses command-line args (a string and an integer) using iterators, rather than collecting them in a vector.</p><p>You can see the full program via the GitHub link above, or in the video.</p><p>I&#8217;ve used the following techniques in the program.</p><h3>Retrieving Arguments</h3><p>You can specify program arguments when you run via cargo like this:</p><p><code>cargo run &#8212; hello to you</code></p><p>hello, to and you become arguments to the program being run.</p><p>To retrieve these arguments in the program we can use:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7nC8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc50d1c12-899c-4823-8ffd-39ab52dbe5b9_622x146.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7nC8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc50d1c12-899c-4823-8ffd-39ab52dbe5b9_622x146.png 424w, https://substackcdn.com/image/fetch/$s_!7nC8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc50d1c12-899c-4823-8ffd-39ab52dbe5b9_622x146.png 848w, https://substackcdn.com/image/fetch/$s_!7nC8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc50d1c12-899c-4823-8ffd-39ab52dbe5b9_622x146.png 1272w, https://substackcdn.com/image/fetch/$s_!7nC8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc50d1c12-899c-4823-8ffd-39ab52dbe5b9_622x146.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7nC8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc50d1c12-899c-4823-8ffd-39ab52dbe5b9_622x146.png" width="334" height="78.39871382636656" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c50d1c12-899c-4823-8ffd-39ab52dbe5b9_622x146.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:146,&quot;width&quot;:622,&quot;resizeWidth&quot;:334,&quot;bytes&quot;:22916,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161090542?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc50d1c12-899c-4823-8ffd-39ab52dbe5b9_622x146.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7nC8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc50d1c12-899c-4823-8ffd-39ab52dbe5b9_622x146.png 424w, https://substackcdn.com/image/fetch/$s_!7nC8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc50d1c12-899c-4823-8ffd-39ab52dbe5b9_622x146.png 848w, https://substackcdn.com/image/fetch/$s_!7nC8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc50d1c12-899c-4823-8ffd-39ab52dbe5b9_622x146.png 1272w, https://substackcdn.com/image/fetch/$s_!7nC8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc50d1c12-899c-4823-8ffd-39ab52dbe5b9_622x146.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p><code>args</code> gets set to an object which implements the <code>Iterator</code> trait.</p><p></p><h3>Passing Arguments to a Function</h3><p>To pass this variable to a function, we can specify that the argument implements the <code>Iterator</code> trait, and specifically, an <code>Iterator</code> over strings.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dDhP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9ae7781-b098-482e-b17f-a88044502cee_1508x222.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dDhP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9ae7781-b098-482e-b17f-a88044502cee_1508x222.png 424w, https://substackcdn.com/image/fetch/$s_!dDhP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9ae7781-b098-482e-b17f-a88044502cee_1508x222.png 848w, https://substackcdn.com/image/fetch/$s_!dDhP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9ae7781-b098-482e-b17f-a88044502cee_1508x222.png 1272w, https://substackcdn.com/image/fetch/$s_!dDhP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9ae7781-b098-482e-b17f-a88044502cee_1508x222.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dDhP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9ae7781-b098-482e-b17f-a88044502cee_1508x222.png" width="1456" height="214" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d9ae7781-b098-482e-b17f-a88044502cee_1508x222.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:214,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:59602,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161090542?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9ae7781-b098-482e-b17f-a88044502cee_1508x222.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!dDhP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9ae7781-b098-482e-b17f-a88044502cee_1508x222.png 424w, https://substackcdn.com/image/fetch/$s_!dDhP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9ae7781-b098-482e-b17f-a88044502cee_1508x222.png 848w, https://substackcdn.com/image/fetch/$s_!dDhP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9ae7781-b098-482e-b17f-a88044502cee_1508x222.png 1272w, https://substackcdn.com/image/fetch/$s_!dDhP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9ae7781-b098-482e-b17f-a88044502cee_1508x222.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p><code>next()</code> actually returns an <code>Option&lt;T&gt;</code>, so we have to unwrap it to get at the actual string argument.</p><p>Since the first argument is always the name of the program, and my program is called &#8220;iterator&#8221;, this prints</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yiD0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32e6a9c0-3a1e-4152-9e35-25a43233869e_1316x70.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yiD0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32e6a9c0-3a1e-4152-9e35-25a43233869e_1316x70.png 424w, https://substackcdn.com/image/fetch/$s_!yiD0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32e6a9c0-3a1e-4152-9e35-25a43233869e_1316x70.png 848w, https://substackcdn.com/image/fetch/$s_!yiD0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32e6a9c0-3a1e-4152-9e35-25a43233869e_1316x70.png 1272w, https://substackcdn.com/image/fetch/$s_!yiD0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32e6a9c0-3a1e-4152-9e35-25a43233869e_1316x70.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yiD0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32e6a9c0-3a1e-4152-9e35-25a43233869e_1316x70.png" width="490" height="26.06382978723404" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/32e6a9c0-3a1e-4152-9e35-25a43233869e_1316x70.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:70,&quot;width&quot;:1316,&quot;resizeWidth&quot;:490,&quot;bytes&quot;:18204,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161090542?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32e6a9c0-3a1e-4152-9e35-25a43233869e_1316x70.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yiD0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32e6a9c0-3a1e-4152-9e35-25a43233869e_1316x70.png 424w, https://substackcdn.com/image/fetch/$s_!yiD0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32e6a9c0-3a1e-4152-9e35-25a43233869e_1316x70.png 848w, https://substackcdn.com/image/fetch/$s_!yiD0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32e6a9c0-3a1e-4152-9e35-25a43233869e_1316x70.png 1272w, https://substackcdn.com/image/fetch/$s_!yiD0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32e6a9c0-3a1e-4152-9e35-25a43233869e_1316x70.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>When I run it with cargo.</p><p></p><h3>Unwrapping with Match and Returning a Result</h3><p>Here I&#8217;ve written some code that retrieves the first user-supplied argument and returns it from a function, in the <code>Ok</code> value of the <code>Result</code> enum.</p><p>If there&#8217;s no such argument, the function returns <code>Err</code>.</p><p>The <code>Ok</code> value wraps a String, because it&#8217;s supplied by the user via the command line.</p><p>The <code>Err</code> value returns a <code>&amp;str</code>, because it&#8217;s hardcoded into the program. It has a lifetime of <code>&#8216;static</code>, for the same reason.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CdA4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1913cef3-7757-46de-be90-752e74c7b920_1884x582.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CdA4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1913cef3-7757-46de-be90-752e74c7b920_1884x582.png 424w, https://substackcdn.com/image/fetch/$s_!CdA4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1913cef3-7757-46de-be90-752e74c7b920_1884x582.png 848w, https://substackcdn.com/image/fetch/$s_!CdA4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1913cef3-7757-46de-be90-752e74c7b920_1884x582.png 1272w, https://substackcdn.com/image/fetch/$s_!CdA4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1913cef3-7757-46de-be90-752e74c7b920_1884x582.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CdA4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1913cef3-7757-46de-be90-752e74c7b920_1884x582.png" width="1456" height="450" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1913cef3-7757-46de-be90-752e74c7b920_1884x582.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:450,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:136934,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161090542?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1913cef3-7757-46de-be90-752e74c7b920_1884x582.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CdA4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1913cef3-7757-46de-be90-752e74c7b920_1884x582.png 424w, https://substackcdn.com/image/fetch/$s_!CdA4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1913cef3-7757-46de-be90-752e74c7b920_1884x582.png 848w, https://substackcdn.com/image/fetch/$s_!CdA4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1913cef3-7757-46de-be90-752e74c7b920_1884x582.png 1272w, https://substackcdn.com/image/fetch/$s_!CdA4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1913cef3-7757-46de-be90-752e74c7b920_1884x582.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Retrieving a Numerical Argument</h3><p>The following function does the same thing, except it expect the argument (now an age) to be an unsigned integer.</p><p>As before, <code>next()</code> gets us the next argument from the <code>args</code> iterator. This returns a <code>Option&lt;T&gt;</code>, having the values either <code>Some</code> or <code>None</code>.</p><p><code>ok_or()</code> then translates the <code>Option&lt;T&gt;</code> into a <code>Result&lt;T&gt;</code>. If the <code>Option</code> was set to <code>Some</code>, it gets the value from <code>Some</code> and wraps it with <code>Ok</code>. Otherwise it wraps the supplied message with <code>Err</code>.</p><p>The question mark after <code>ok_or() </code>then returns from the <code>get_age_arg()</code> function with the <code>Err</code> if no argument could be retrieved, otherwise it unwraps the value contained in <code>Ok</code>.</p><p><code>parse::&lt;u32&gt;()</code> then tries to parse the string argument into an unsigned 32-bit integer. This returns a <code>Result</code>: <code>Ok</code> wraps the value if the parse was successful, otherwise <code>Err</code> wraps an error value.</p><p>However, the error value isn&#8217;t just a <code>&amp;str</code>, so we can&#8217;t add a question mark to just return from the function in case of error.</p><p>Instead, we use <code>map_err()</code> with a closure to translate the <code>Err</code> into an <code>Err</code> containing a string.</p><p>That&#8217;s now compatible with the return type of <code>get_age_args()</code>, so we can use a question mark to return from the function in case of an error.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3Mjh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74e7c701-cf78-4c15-b3e9-e7e14b2e664a_1822x598.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3Mjh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74e7c701-cf78-4c15-b3e9-e7e14b2e664a_1822x598.png 424w, https://substackcdn.com/image/fetch/$s_!3Mjh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74e7c701-cf78-4c15-b3e9-e7e14b2e664a_1822x598.png 848w, https://substackcdn.com/image/fetch/$s_!3Mjh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74e7c701-cf78-4c15-b3e9-e7e14b2e664a_1822x598.png 1272w, https://substackcdn.com/image/fetch/$s_!3Mjh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74e7c701-cf78-4c15-b3e9-e7e14b2e664a_1822x598.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3Mjh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74e7c701-cf78-4c15-b3e9-e7e14b2e664a_1822x598.png" width="1456" height="478" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/74e7c701-cf78-4c15-b3e9-e7e14b2e664a_1822x598.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:478,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:113225,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/161090542?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74e7c701-cf78-4c15-b3e9-e7e14b2e664a_1822x598.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!3Mjh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74e7c701-cf78-4c15-b3e9-e7e14b2e664a_1822x598.png 424w, https://substackcdn.com/image/fetch/$s_!3Mjh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74e7c701-cf78-4c15-b3e9-e7e14b2e664a_1822x598.png 848w, https://substackcdn.com/image/fetch/$s_!3Mjh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74e7c701-cf78-4c15-b3e9-e7e14b2e664a_1822x598.png 1272w, https://substackcdn.com/image/fetch/$s_!3Mjh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74e7c701-cf78-4c15-b3e9-e7e14b2e664a_1822x598.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p>]]></content:encoded></item><item><title><![CDATA[Rust Iterators]]></title><description><![CDATA[Github: https://github.com/caveofprogramming/rustThanks for reading The Cave of Programming Blog!]]></description><link>https://blog.caveofprogramming.com/p/rust-iterators</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/rust-iterators</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Tue, 01 Apr 2025 18:19:42 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/7d20b885-49a5-4a67-b4be-9ebefd9d1df4_1024x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;3151d68f-d1d7-4e17-92fa-017eaf2d1165&quot;,&quot;duration&quot;:null}"></div><p>Github: <a href="https://github.com/caveofprogramming/rust/tree/main/028%20Iterators/iterate">https://github.com/caveofprogramming/rust</a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.caveofprogramming.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading The Cave of Programming Blog! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h3>Iterators, and the Iterator Interface</h3><p>The <code>iter()</code> method of vectors in Rust returns an immutable iterator. This is an instance of the <code>Iter</code> struct and it implements the Iterator trait. This means we can use it to iterate over the vector.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tE-T!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96f8d6ee-20d1-498a-bee2-e13127dec27b_878x365.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tE-T!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96f8d6ee-20d1-498a-bee2-e13127dec27b_878x365.png 424w, https://substackcdn.com/image/fetch/$s_!tE-T!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96f8d6ee-20d1-498a-bee2-e13127dec27b_878x365.png 848w, https://substackcdn.com/image/fetch/$s_!tE-T!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96f8d6ee-20d1-498a-bee2-e13127dec27b_878x365.png 1272w, https://substackcdn.com/image/fetch/$s_!tE-T!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96f8d6ee-20d1-498a-bee2-e13127dec27b_878x365.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tE-T!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96f8d6ee-20d1-498a-bee2-e13127dec27b_878x365.png" width="468" height="194.55580865603645" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/96f8d6ee-20d1-498a-bee2-e13127dec27b_878x365.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:365,&quot;width&quot;:878,&quot;resizeWidth&quot;:468,&quot;bytes&quot;:48388,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/160339869?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F392a9c46-d129-4f1d-b305-b3a6d406bdd7_878x398.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tE-T!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96f8d6ee-20d1-498a-bee2-e13127dec27b_878x365.png 424w, https://substackcdn.com/image/fetch/$s_!tE-T!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96f8d6ee-20d1-498a-bee2-e13127dec27b_878x365.png 848w, https://substackcdn.com/image/fetch/$s_!tE-T!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96f8d6ee-20d1-498a-bee2-e13127dec27b_878x365.png 1272w, https://substackcdn.com/image/fetch/$s_!tE-T!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96f8d6ee-20d1-498a-bee2-e13127dec27b_878x365.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p>The <code>Iterator</code> trait defines various methods, including <code>next()</code>. We can use this to retrieve items one at a time.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!pwDo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b1e4728-2c5b-4ae7-934b-4ba6f4713a12_1284x306.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!pwDo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b1e4728-2c5b-4ae7-934b-4ba6f4713a12_1284x306.png 424w, https://substackcdn.com/image/fetch/$s_!pwDo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b1e4728-2c5b-4ae7-934b-4ba6f4713a12_1284x306.png 848w, https://substackcdn.com/image/fetch/$s_!pwDo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b1e4728-2c5b-4ae7-934b-4ba6f4713a12_1284x306.png 1272w, https://substackcdn.com/image/fetch/$s_!pwDo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b1e4728-2c5b-4ae7-934b-4ba6f4713a12_1284x306.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!pwDo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b1e4728-2c5b-4ae7-934b-4ba6f4713a12_1284x306.png" width="556" height="132.50467289719626" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2b1e4728-2c5b-4ae7-934b-4ba6f4713a12_1284x306.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:306,&quot;width&quot;:1284,&quot;resizeWidth&quot;:556,&quot;bytes&quot;:73912,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/160339869?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71b66466-10ac-4a28-9167-f31a4be1b061_1284x340.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!pwDo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b1e4728-2c5b-4ae7-934b-4ba6f4713a12_1284x306.png 424w, https://substackcdn.com/image/fetch/$s_!pwDo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b1e4728-2c5b-4ae7-934b-4ba6f4713a12_1284x306.png 848w, https://substackcdn.com/image/fetch/$s_!pwDo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b1e4728-2c5b-4ae7-934b-4ba6f4713a12_1284x306.png 1272w, https://substackcdn.com/image/fetch/$s_!pwDo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b1e4728-2c5b-4ae7-934b-4ba6f4713a12_1284x306.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><p>Prints:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ctwD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F622ef65c-b0ec-4d6e-b38b-a4e5a41be088_340x86.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ctwD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F622ef65c-b0ec-4d6e-b38b-a4e5a41be088_340x86.png 424w, https://substackcdn.com/image/fetch/$s_!ctwD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F622ef65c-b0ec-4d6e-b38b-a4e5a41be088_340x86.png 848w, https://substackcdn.com/image/fetch/$s_!ctwD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F622ef65c-b0ec-4d6e-b38b-a4e5a41be088_340x86.png 1272w, https://substackcdn.com/image/fetch/$s_!ctwD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F622ef65c-b0ec-4d6e-b38b-a4e5a41be088_340x86.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ctwD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F622ef65c-b0ec-4d6e-b38b-a4e5a41be088_340x86.png" width="212" height="53.62352941176471" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/622ef65c-b0ec-4d6e-b38b-a4e5a41be088_340x86.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:86,&quot;width&quot;:340,&quot;resizeWidth&quot;:212,&quot;bytes&quot;:7121,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/160339869?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d118b0-f39c-4cf0-85e2-369eab1131ca_340x100.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ctwD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F622ef65c-b0ec-4d6e-b38b-a4e5a41be088_340x86.png 424w, https://substackcdn.com/image/fetch/$s_!ctwD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F622ef65c-b0ec-4d6e-b38b-a4e5a41be088_340x86.png 848w, https://substackcdn.com/image/fetch/$s_!ctwD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F622ef65c-b0ec-4d6e-b38b-a4e5a41be088_340x86.png 1272w, https://substackcdn.com/image/fetch/$s_!ctwD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F622ef65c-b0ec-4d6e-b38b-a4e5a41be088_340x86.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>In the code above, <code>iter</code> has to be declared <code>mut</code>, since it stores its position and we change it by calling next. <code>for</code> does this behind the scenes.</p><p>The <code>Iterator</code> trait defines various methods, including <code>count()</code> and <code>sum()</code></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PqAy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca41170d-a403-4218-9b95-f44da1eb4d7d_1050x344.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PqAy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca41170d-a403-4218-9b95-f44da1eb4d7d_1050x344.png 424w, https://substackcdn.com/image/fetch/$s_!PqAy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca41170d-a403-4218-9b95-f44da1eb4d7d_1050x344.png 848w, https://substackcdn.com/image/fetch/$s_!PqAy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca41170d-a403-4218-9b95-f44da1eb4d7d_1050x344.png 1272w, https://substackcdn.com/image/fetch/$s_!PqAy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca41170d-a403-4218-9b95-f44da1eb4d7d_1050x344.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PqAy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca41170d-a403-4218-9b95-f44da1eb4d7d_1050x344.png" width="432" height="141.53142857142856" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ca41170d-a403-4218-9b95-f44da1eb4d7d_1050x344.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:344,&quot;width&quot;:1050,&quot;resizeWidth&quot;:432,&quot;bytes&quot;:69688,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/160339869?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca41170d-a403-4218-9b95-f44da1eb4d7d_1050x344.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!PqAy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca41170d-a403-4218-9b95-f44da1eb4d7d_1050x344.png 424w, https://substackcdn.com/image/fetch/$s_!PqAy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca41170d-a403-4218-9b95-f44da1eb4d7d_1050x344.png 848w, https://substackcdn.com/image/fetch/$s_!PqAy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca41170d-a403-4218-9b95-f44da1eb4d7d_1050x344.png 1272w, https://substackcdn.com/image/fetch/$s_!PqAy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca41170d-a403-4218-9b95-f44da1eb4d7d_1050x344.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>In the above code, we have to declare the type of the things being iterated over somewhere; either after <code>sum() </code>or else on the variable itself.</p><p>We can use <code>iter_mut()</code> instead of <code>iter()</code> to get a mutable reference to the items in the vector. Then we can dereference the reference with <code>*</code> to change the item.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WIag!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e2facbf-ef9e-42e2-a0c9-2ee525bff466_982x464.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WIag!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e2facbf-ef9e-42e2-a0c9-2ee525bff466_982x464.png 424w, https://substackcdn.com/image/fetch/$s_!WIag!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e2facbf-ef9e-42e2-a0c9-2ee525bff466_982x464.png 848w, https://substackcdn.com/image/fetch/$s_!WIag!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e2facbf-ef9e-42e2-a0c9-2ee525bff466_982x464.png 1272w, https://substackcdn.com/image/fetch/$s_!WIag!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e2facbf-ef9e-42e2-a0c9-2ee525bff466_982x464.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WIag!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e2facbf-ef9e-42e2-a0c9-2ee525bff466_982x464.png" width="464" height="219.24236252545825" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0e2facbf-ef9e-42e2-a0c9-2ee525bff466_982x464.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:464,&quot;width&quot;:982,&quot;resizeWidth&quot;:464,&quot;bytes&quot;:59743,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/160339869?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e2facbf-ef9e-42e2-a0c9-2ee525bff466_982x464.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WIag!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e2facbf-ef9e-42e2-a0c9-2ee525bff466_982x464.png 424w, https://substackcdn.com/image/fetch/$s_!WIag!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e2facbf-ef9e-42e2-a0c9-2ee525bff466_982x464.png 848w, https://substackcdn.com/image/fetch/$s_!WIag!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e2facbf-ef9e-42e2-a0c9-2ee525bff466_982x464.png 1272w, https://substackcdn.com/image/fetch/$s_!WIag!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e2facbf-ef9e-42e2-a0c9-2ee525bff466_982x464.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p><code>number</code> here is not an actual number, but a mutable reference to one, so it doesn&#8217;t have to be declared with <code>mut</code>.</p><p></p><h3>Iterator Adapters</h3><p>Iterator methods that wind the iterator forwards by calling next are called consuming adapters. There are also iterator adapters that don&#8217;t consume the iterator.</p><p>For example, we can use map with a closure to change the items in the iterator.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PQkS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcedee69a-5bf4-4eb1-bc49-80cc314795c9_1562x230.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PQkS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcedee69a-5bf4-4eb1-bc49-80cc314795c9_1562x230.png 424w, https://substackcdn.com/image/fetch/$s_!PQkS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcedee69a-5bf4-4eb1-bc49-80cc314795c9_1562x230.png 848w, https://substackcdn.com/image/fetch/$s_!PQkS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcedee69a-5bf4-4eb1-bc49-80cc314795c9_1562x230.png 1272w, https://substackcdn.com/image/fetch/$s_!PQkS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcedee69a-5bf4-4eb1-bc49-80cc314795c9_1562x230.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PQkS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcedee69a-5bf4-4eb1-bc49-80cc314795c9_1562x230.png" width="634" height="93.18406593406593" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cedee69a-5bf4-4eb1-bc49-80cc314795c9_1562x230.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:214,&quot;width&quot;:1456,&quot;resizeWidth&quot;:634,&quot;bytes&quot;:62873,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/160339869?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcedee69a-5bf4-4eb1-bc49-80cc314795c9_1562x230.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!PQkS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcedee69a-5bf4-4eb1-bc49-80cc314795c9_1562x230.png 424w, https://substackcdn.com/image/fetch/$s_!PQkS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcedee69a-5bf4-4eb1-bc49-80cc314795c9_1562x230.png 848w, https://substackcdn.com/image/fetch/$s_!PQkS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcedee69a-5bf4-4eb1-bc49-80cc314795c9_1562x230.png 1272w, https://substackcdn.com/image/fetch/$s_!PQkS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcedee69a-5bf4-4eb1-bc49-80cc314795c9_1562x230.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Prints:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!105o!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F821b3c5e-8fbc-4055-bdfc-5a85796c1e02_458x54.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!105o!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F821b3c5e-8fbc-4055-bdfc-5a85796c1e02_458x54.png 424w, https://substackcdn.com/image/fetch/$s_!105o!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F821b3c5e-8fbc-4055-bdfc-5a85796c1e02_458x54.png 848w, https://substackcdn.com/image/fetch/$s_!105o!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F821b3c5e-8fbc-4055-bdfc-5a85796c1e02_458x54.png 1272w, https://substackcdn.com/image/fetch/$s_!105o!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F821b3c5e-8fbc-4055-bdfc-5a85796c1e02_458x54.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!105o!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F821b3c5e-8fbc-4055-bdfc-5a85796c1e02_458x54.png" width="280" height="33.01310043668122" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/821b3c5e-8fbc-4055-bdfc-5a85796c1e02_458x54.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:54,&quot;width&quot;:458,&quot;resizeWidth&quot;:280,&quot;bytes&quot;:8513,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/160339869?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F821b3c5e-8fbc-4055-bdfc-5a85796c1e02_458x54.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!105o!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F821b3c5e-8fbc-4055-bdfc-5a85796c1e02_458x54.png 424w, https://substackcdn.com/image/fetch/$s_!105o!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F821b3c5e-8fbc-4055-bdfc-5a85796c1e02_458x54.png 848w, https://substackcdn.com/image/fetch/$s_!105o!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F821b3c5e-8fbc-4055-bdfc-5a85796c1e02_458x54.png 1272w, https://substackcdn.com/image/fetch/$s_!105o!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F821b3c5e-8fbc-4055-bdfc-5a85796c1e02_458x54.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The <code>Vec&lt;_&gt;</code> here is needed. The underscore tells Rust to infer the type of the items in the vector.</p><p>Another example is the filter method, which creates a filtered copy of the vector.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GqHk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85583f10-34ab-4b0e-b8e6-c7f0d632109d_1966x220.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GqHk!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85583f10-34ab-4b0e-b8e6-c7f0d632109d_1966x220.png 424w, https://substackcdn.com/image/fetch/$s_!GqHk!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85583f10-34ab-4b0e-b8e6-c7f0d632109d_1966x220.png 848w, https://substackcdn.com/image/fetch/$s_!GqHk!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85583f10-34ab-4b0e-b8e6-c7f0d632109d_1966x220.png 1272w, https://substackcdn.com/image/fetch/$s_!GqHk!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85583f10-34ab-4b0e-b8e6-c7f0d632109d_1966x220.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GqHk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85583f10-34ab-4b0e-b8e6-c7f0d632109d_1966x220.png" width="1456" height="163" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/85583f10-34ab-4b0e-b8e6-c7f0d632109d_1966x220.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:163,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:74705,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/160339869?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85583f10-34ab-4b0e-b8e6-c7f0d632109d_1966x220.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!GqHk!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85583f10-34ab-4b0e-b8e6-c7f0d632109d_1966x220.png 424w, https://substackcdn.com/image/fetch/$s_!GqHk!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85583f10-34ab-4b0e-b8e6-c7f0d632109d_1966x220.png 848w, https://substackcdn.com/image/fetch/$s_!GqHk!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85583f10-34ab-4b0e-b8e6-c7f0d632109d_1966x220.png 1272w, https://substackcdn.com/image/fetch/$s_!GqHk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85583f10-34ab-4b0e-b8e6-c7f0d632109d_1966x220.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Prints:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7UNs!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad5535fa-26a0-4c18-a707-bfdf3287f7c7_564x43.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7UNs!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad5535fa-26a0-4c18-a707-bfdf3287f7c7_564x43.png 424w, https://substackcdn.com/image/fetch/$s_!7UNs!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad5535fa-26a0-4c18-a707-bfdf3287f7c7_564x43.png 848w, https://substackcdn.com/image/fetch/$s_!7UNs!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad5535fa-26a0-4c18-a707-bfdf3287f7c7_564x43.png 1272w, https://substackcdn.com/image/fetch/$s_!7UNs!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad5535fa-26a0-4c18-a707-bfdf3287f7c7_564x43.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7UNs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad5535fa-26a0-4c18-a707-bfdf3287f7c7_564x43.png" width="352" height="26.836879432624112" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ad5535fa-26a0-4c18-a707-bfdf3287f7c7_564x43.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:43,&quot;width&quot;:564,&quot;resizeWidth&quot;:352,&quot;bytes&quot;:4707,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/160339869?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1d61649-0c1a-45bd-b6f5-219ce2fb5aed_564x58.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7UNs!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad5535fa-26a0-4c18-a707-bfdf3287f7c7_564x43.png 424w, https://substackcdn.com/image/fetch/$s_!7UNs!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad5535fa-26a0-4c18-a707-bfdf3287f7c7_564x43.png 848w, https://substackcdn.com/image/fetch/$s_!7UNs!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad5535fa-26a0-4c18-a707-bfdf3287f7c7_564x43.png 1272w, https://substackcdn.com/image/fetch/$s_!7UNs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad5535fa-26a0-4c18-a707-bfdf3287f7c7_564x43.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.caveofprogramming.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading The Cave of Programming Blog! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Rust Closures]]></title><description><![CDATA[Basically, lambda expressions]]></description><link>https://blog.caveofprogramming.com/p/rust-closures</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/rust-closures</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Tue, 18 Mar 2025 01:08:14 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/99ea5c01-9e2e-4e87-9f18-0324a4ee46b1_1024x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;1ee81acc-1318-46fe-acdb-8c899fbe491c&quot;,&quot;duration&quot;:null}"></div><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.caveofprogramming.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Cave of Programming! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p><p>Git repo: <a href="https://github.com/caveofprogramming/rust/tree/main/027%20Closures/closures">https://github.com/caveofprogramming/rust/</a></p><h3>Basic Closures</h3><p>The thing that the Rust docs call closures, at least as far as I can tell, are the same things that programmers in other languages call lambda expressions: simply-defined anonymous functions that can access the scope in which they&#8217;re defined.</p><p></p><p>Here&#8217;s an example of a closure that, when called, prints &#8220;Hello&#8221;.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Wt2Y!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23431cf0-6f8b-4a2c-a2e8-50bd9dcfd409_1306x250.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Wt2Y!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23431cf0-6f8b-4a2c-a2e8-50bd9dcfd409_1306x250.png 424w, https://substackcdn.com/image/fetch/$s_!Wt2Y!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23431cf0-6f8b-4a2c-a2e8-50bd9dcfd409_1306x250.png 848w, https://substackcdn.com/image/fetch/$s_!Wt2Y!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23431cf0-6f8b-4a2c-a2e8-50bd9dcfd409_1306x250.png 1272w, https://substackcdn.com/image/fetch/$s_!Wt2Y!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23431cf0-6f8b-4a2c-a2e8-50bd9dcfd409_1306x250.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Wt2Y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23431cf0-6f8b-4a2c-a2e8-50bd9dcfd409_1306x250.png" width="540" height="103.36906584992343" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/23431cf0-6f8b-4a2c-a2e8-50bd9dcfd409_1306x250.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1306,&quot;resizeWidth&quot;:540,&quot;bytes&quot;:35646,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/159223875?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23431cf0-6f8b-4a2c-a2e8-50bd9dcfd409_1306x250.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Wt2Y!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23431cf0-6f8b-4a2c-a2e8-50bd9dcfd409_1306x250.png 424w, https://substackcdn.com/image/fetch/$s_!Wt2Y!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23431cf0-6f8b-4a2c-a2e8-50bd9dcfd409_1306x250.png 848w, https://substackcdn.com/image/fetch/$s_!Wt2Y!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23431cf0-6f8b-4a2c-a2e8-50bd9dcfd409_1306x250.png 1272w, https://substackcdn.com/image/fetch/$s_!Wt2Y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23431cf0-6f8b-4a2c-a2e8-50bd9dcfd409_1306x250.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The two vertical pipes provide a space where parameters can be defined.</p><p>This closure adds two numbers together. The expression provides the return value.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IqFP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb4330e9-3df3-4714-b964-f7e5d99d655f_1320x222.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IqFP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb4330e9-3df3-4714-b964-f7e5d99d655f_1320x222.png 424w, https://substackcdn.com/image/fetch/$s_!IqFP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb4330e9-3df3-4714-b964-f7e5d99d655f_1320x222.png 848w, https://substackcdn.com/image/fetch/$s_!IqFP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb4330e9-3df3-4714-b964-f7e5d99d655f_1320x222.png 1272w, https://substackcdn.com/image/fetch/$s_!IqFP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb4330e9-3df3-4714-b964-f7e5d99d655f_1320x222.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IqFP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb4330e9-3df3-4714-b964-f7e5d99d655f_1320x222.png" width="538" height="90.48181818181818" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fb4330e9-3df3-4714-b964-f7e5d99d655f_1320x222.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:222,&quot;width&quot;:1320,&quot;resizeWidth&quot;:538,&quot;bytes&quot;:43033,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/159223875?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb4330e9-3df3-4714-b964-f7e5d99d655f_1320x222.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IqFP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb4330e9-3df3-4714-b964-f7e5d99d655f_1320x222.png 424w, https://substackcdn.com/image/fetch/$s_!IqFP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb4330e9-3df3-4714-b964-f7e5d99d655f_1320x222.png 848w, https://substackcdn.com/image/fetch/$s_!IqFP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb4330e9-3df3-4714-b964-f7e5d99d655f_1320x222.png 1272w, https://substackcdn.com/image/fetch/$s_!IqFP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb4330e9-3df3-4714-b964-f7e5d99d655f_1320x222.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>It is possible to supply explicit parameter and return types.</p><p>Now I&#8217;m forced to add in braces to get it to work.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!REzE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fecdebb3f-da61-4177-a02e-1a64a28fbe5e_1624x214.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!REzE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fecdebb3f-da61-4177-a02e-1a64a28fbe5e_1624x214.png 424w, https://substackcdn.com/image/fetch/$s_!REzE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fecdebb3f-da61-4177-a02e-1a64a28fbe5e_1624x214.png 848w, https://substackcdn.com/image/fetch/$s_!REzE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fecdebb3f-da61-4177-a02e-1a64a28fbe5e_1624x214.png 1272w, https://substackcdn.com/image/fetch/$s_!REzE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fecdebb3f-da61-4177-a02e-1a64a28fbe5e_1624x214.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!REzE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fecdebb3f-da61-4177-a02e-1a64a28fbe5e_1624x214.png" width="496" height="65.4065934065934" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ecdebb3f-da61-4177-a02e-1a64a28fbe5e_1624x214.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:192,&quot;width&quot;:1456,&quot;resizeWidth&quot;:496,&quot;bytes&quot;:55919,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/159223875?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fecdebb3f-da61-4177-a02e-1a64a28fbe5e_1624x214.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!REzE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fecdebb3f-da61-4177-a02e-1a64a28fbe5e_1624x214.png 424w, https://substackcdn.com/image/fetch/$s_!REzE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fecdebb3f-da61-4177-a02e-1a64a28fbe5e_1624x214.png 848w, https://substackcdn.com/image/fetch/$s_!REzE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fecdebb3f-da61-4177-a02e-1a64a28fbe5e_1624x214.png 1272w, https://substackcdn.com/image/fetch/$s_!REzE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fecdebb3f-da61-4177-a02e-1a64a28fbe5e_1624x214.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Otherwise, the parameter and return types are inferred when you first call the closure.</p><p></p><h3>Closure Type Inference</h3><p>Here, I define a closure and pass an int to it. When I try to call it again with a float, I get an error, because the first call caused i32 to be inferred for the type of the parameter.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kFOW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcd37a44-cc77-4765-a4e4-136c85167f5b_1368x338.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kFOW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcd37a44-cc77-4765-a4e4-136c85167f5b_1368x338.png 424w, https://substackcdn.com/image/fetch/$s_!kFOW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcd37a44-cc77-4765-a4e4-136c85167f5b_1368x338.png 848w, https://substackcdn.com/image/fetch/$s_!kFOW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcd37a44-cc77-4765-a4e4-136c85167f5b_1368x338.png 1272w, https://substackcdn.com/image/fetch/$s_!kFOW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcd37a44-cc77-4765-a4e4-136c85167f5b_1368x338.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kFOW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcd37a44-cc77-4765-a4e4-136c85167f5b_1368x338.png" width="544" height="134.4093567251462" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dcd37a44-cc77-4765-a4e4-136c85167f5b_1368x338.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:338,&quot;width&quot;:1368,&quot;resizeWidth&quot;:544,&quot;bytes&quot;:62221,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/159223875?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcd37a44-cc77-4765-a4e4-136c85167f5b_1368x338.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!kFOW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcd37a44-cc77-4765-a4e4-136c85167f5b_1368x338.png 424w, https://substackcdn.com/image/fetch/$s_!kFOW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcd37a44-cc77-4765-a4e4-136c85167f5b_1368x338.png 848w, https://substackcdn.com/image/fetch/$s_!kFOW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcd37a44-cc77-4765-a4e4-136c85167f5b_1368x338.png 1272w, https://substackcdn.com/image/fetch/$s_!kFOW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcd37a44-cc77-4765-a4e4-136c85167f5b_1368x338.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><h3>Accessing Local Variables within Closures</h3><p>Closures can access variables in the enclosing scope (hence the name &#8220;closure&#8221;), either by immutable borrow, mutable borrow or by taking ownership.</p><p>This closure performs an immutable borrow:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wguN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F640ea1bc-3352-40e0-9160-d0d13476efc9_1450x308.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wguN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F640ea1bc-3352-40e0-9160-d0d13476efc9_1450x308.png 424w, https://substackcdn.com/image/fetch/$s_!wguN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F640ea1bc-3352-40e0-9160-d0d13476efc9_1450x308.png 848w, https://substackcdn.com/image/fetch/$s_!wguN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F640ea1bc-3352-40e0-9160-d0d13476efc9_1450x308.png 1272w, https://substackcdn.com/image/fetch/$s_!wguN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F640ea1bc-3352-40e0-9160-d0d13476efc9_1450x308.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wguN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F640ea1bc-3352-40e0-9160-d0d13476efc9_1450x308.png" width="472" height="100.25931034482758" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/640ea1bc-3352-40e0-9160-d0d13476efc9_1450x308.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:308,&quot;width&quot;:1450,&quot;resizeWidth&quot;:472,&quot;bytes&quot;:56873,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/159223875?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F640ea1bc-3352-40e0-9160-d0d13476efc9_1450x308.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wguN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F640ea1bc-3352-40e0-9160-d0d13476efc9_1450x308.png 424w, https://substackcdn.com/image/fetch/$s_!wguN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F640ea1bc-3352-40e0-9160-d0d13476efc9_1450x308.png 848w, https://substackcdn.com/image/fetch/$s_!wguN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F640ea1bc-3352-40e0-9160-d0d13476efc9_1450x308.png 1272w, https://substackcdn.com/image/fetch/$s_!wguN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F640ea1bc-3352-40e0-9160-d0d13476efc9_1450x308.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Here&#8217;s a closure that performs a mutable borrow:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BjOJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2733d5e7-3a07-4a2f-ad52-79e12a5e5a22_1320x402.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BjOJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2733d5e7-3a07-4a2f-ad52-79e12a5e5a22_1320x402.png 424w, https://substackcdn.com/image/fetch/$s_!BjOJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2733d5e7-3a07-4a2f-ad52-79e12a5e5a22_1320x402.png 848w, https://substackcdn.com/image/fetch/$s_!BjOJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2733d5e7-3a07-4a2f-ad52-79e12a5e5a22_1320x402.png 1272w, https://substackcdn.com/image/fetch/$s_!BjOJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2733d5e7-3a07-4a2f-ad52-79e12a5e5a22_1320x402.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BjOJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2733d5e7-3a07-4a2f-ad52-79e12a5e5a22_1320x402.png" width="524" height="159.5818181818182" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2733d5e7-3a07-4a2f-ad52-79e12a5e5a22_1320x402.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:402,&quot;width&quot;:1320,&quot;resizeWidth&quot;:524,&quot;bytes&quot;:69289,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/159223875?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2733d5e7-3a07-4a2f-ad52-79e12a5e5a22_1320x402.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BjOJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2733d5e7-3a07-4a2f-ad52-79e12a5e5a22_1320x402.png 424w, https://substackcdn.com/image/fetch/$s_!BjOJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2733d5e7-3a07-4a2f-ad52-79e12a5e5a22_1320x402.png 848w, https://substackcdn.com/image/fetch/$s_!BjOJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2733d5e7-3a07-4a2f-ad52-79e12a5e5a22_1320x402.png 1272w, https://substackcdn.com/image/fetch/$s_!BjOJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2733d5e7-3a07-4a2f-ad52-79e12a5e5a22_1320x402.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1Cya!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4a068d-08ff-4d55-9ddc-b51cd04d12c4_390x72.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1Cya!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4a068d-08ff-4d55-9ddc-b51cd04d12c4_390x72.png 424w, https://substackcdn.com/image/fetch/$s_!1Cya!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4a068d-08ff-4d55-9ddc-b51cd04d12c4_390x72.png 848w, https://substackcdn.com/image/fetch/$s_!1Cya!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4a068d-08ff-4d55-9ddc-b51cd04d12c4_390x72.png 1272w, https://substackcdn.com/image/fetch/$s_!1Cya!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4a068d-08ff-4d55-9ddc-b51cd04d12c4_390x72.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1Cya!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4a068d-08ff-4d55-9ddc-b51cd04d12c4_390x72.png" width="228" height="42.09230769230769" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1c4a068d-08ff-4d55-9ddc-b51cd04d12c4_390x72.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:72,&quot;width&quot;:390,&quot;resizeWidth&quot;:228,&quot;bytes&quot;:7425,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/159223875?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4a068d-08ff-4d55-9ddc-b51cd04d12c4_390x72.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1Cya!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4a068d-08ff-4d55-9ddc-b51cd04d12c4_390x72.png 424w, https://substackcdn.com/image/fetch/$s_!1Cya!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4a068d-08ff-4d55-9ddc-b51cd04d12c4_390x72.png 848w, https://substackcdn.com/image/fetch/$s_!1Cya!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4a068d-08ff-4d55-9ddc-b51cd04d12c4_390x72.png 1272w, https://substackcdn.com/image/fetch/$s_!1Cya!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4a068d-08ff-4d55-9ddc-b51cd04d12c4_390x72.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><h3>Passing Closures to Functions</h3><p>To define a function parameters type that will accept a function or other closure, we can use the fn keyword.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Jw0s!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F708456da-eedc-42f7-b948-83b3b57a666a_3140x450.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Jw0s!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F708456da-eedc-42f7-b948-83b3b57a666a_3140x450.png 424w, https://substackcdn.com/image/fetch/$s_!Jw0s!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F708456da-eedc-42f7-b948-83b3b57a666a_3140x450.png 848w, https://substackcdn.com/image/fetch/$s_!Jw0s!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F708456da-eedc-42f7-b948-83b3b57a666a_3140x450.png 1272w, https://substackcdn.com/image/fetch/$s_!Jw0s!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F708456da-eedc-42f7-b948-83b3b57a666a_3140x450.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Jw0s!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F708456da-eedc-42f7-b948-83b3b57a666a_3140x450.png" width="1456" height="209" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/708456da-eedc-42f7-b948-83b3b57a666a_3140x450.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:209,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:87050,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/159223875?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F708456da-eedc-42f7-b948-83b3b57a666a_3140x450.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Jw0s!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F708456da-eedc-42f7-b948-83b3b57a666a_3140x450.png 424w, https://substackcdn.com/image/fetch/$s_!Jw0s!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F708456da-eedc-42f7-b948-83b3b57a666a_3140x450.png 848w, https://substackcdn.com/image/fetch/$s_!Jw0s!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F708456da-eedc-42f7-b948-83b3b57a666a_3140x450.png 1272w, https://substackcdn.com/image/fetch/$s_!Jw0s!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F708456da-eedc-42f7-b948-83b3b57a666a_3140x450.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This function accepts three parameters: a function that accepts two integers and returns an integers, and two integers. </p><p>We can call it like this:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!21gM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F50dbcbb9-3215-49b4-a669-455557f7dd31_2278x330.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!21gM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F50dbcbb9-3215-49b4-a669-455557f7dd31_2278x330.png 424w, https://substackcdn.com/image/fetch/$s_!21gM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F50dbcbb9-3215-49b4-a669-455557f7dd31_2278x330.png 848w, https://substackcdn.com/image/fetch/$s_!21gM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F50dbcbb9-3215-49b4-a669-455557f7dd31_2278x330.png 1272w, https://substackcdn.com/image/fetch/$s_!21gM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F50dbcbb9-3215-49b4-a669-455557f7dd31_2278x330.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!21gM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F50dbcbb9-3215-49b4-a669-455557f7dd31_2278x330.png" width="644" height="93.32692307692308" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/50dbcbb9-3215-49b4-a669-455557f7dd31_2278x330.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:211,&quot;width&quot;:1456,&quot;resizeWidth&quot;:644,&quot;bytes&quot;:73356,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://blog.caveofprogramming.com/i/159223875?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F50dbcbb9-3215-49b4-a669-455557f7dd31_2278x330.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!21gM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F50dbcbb9-3215-49b4-a669-455557f7dd31_2278x330.png 424w, https://substackcdn.com/image/fetch/$s_!21gM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F50dbcbb9-3215-49b4-a669-455557f7dd31_2278x330.png 848w, https://substackcdn.com/image/fetch/$s_!21gM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F50dbcbb9-3215-49b4-a669-455557f7dd31_2278x330.png 1272w, https://substackcdn.com/image/fetch/$s_!21gM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F50dbcbb9-3215-49b4-a669-455557f7dd31_2278x330.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This prints &#8220;11&#8221;.</p><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.caveofprogramming.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Cave of Programming! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Rust: the "Minigrep" Example Program]]></title><description><![CDATA[Thanks for reading The Cave of Programming Blog!]]></description><link>https://blog.caveofprogramming.com/p/rust-the-minigrep-example-program</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/rust-the-minigrep-example-program</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Thu, 27 Feb 2025 22:18:20 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/468aa337-508c-49ea-9581-78372ea07951_1024x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;d1bbdf40-f936-4249-97b7-19a9bfe47869&quot;,&quot;duration&quot;:null}"></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.caveofprogramming.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading The Cave of Programming Blog! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p><p>I won&#8217;t provide a writeup for this, because it&#8217;s a lot of code, and the Rust docs already comments on it extensively.</p><p>However here&#8217;s my finished minigrep program: <a href="https://github.com/caveofprogramming/rust/tree/main/026%20Rust%20Example%20Program/minigrep">https://github.com/caveofprogramming/rust/</a></p><p></p><p>And here are the corresponding pages from the Rust docs: <a href="https://doc.rust-lang.org/book/ch12-00-an-io-project.html">https://doc.rust-lang.org/book/ch12-00-an-io-project.html</a></p>]]></content:encoded></item><item><title><![CDATA[Testing Rust Code]]></title><description><![CDATA[Finally something easy!]]></description><link>https://blog.caveofprogramming.com/p/testing-rust-code</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/testing-rust-code</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Sun, 16 Feb 2025 22:39:55 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/ccfbb87b-c619-485b-9d02-0ef2001b235b_1024x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;1156f8c8-9595-4697-9dd7-2fd20874fe72&quot;,&quot;duration&quot;:null}"></div><p></p><p>Github repository: <a href="https://github.com/caveofprogramming/rust/tree/main/025%20Tests/numerical">https://github.com/caveofprogramming/rust</a></p><h3>A Library Function That Calculates Factorials</h3><p>It&#8217;s only possible to create tests in Rust for library code.</p><p>Therefore I&#8217;m going to create a library called &#8216;numerical&#8217; like this:</p><p><code>cargo new numerical --lib </code></p><p>Now, since I&#8217;d like to also use the library code from a binary, I create a file called <code>main.rs</code> in the src folder, and add a <code>main</code> function into it.</p><p>It&#8217;s now possible to run this code as a binary executable with <code>cargo run</code>, or to just build the binary and the library using <code>cargo build</code>. Since the library includes dummy tests, we can run the tests with <code>cargo test</code>.</p><p>I&#8217;ll create another file in src/ called numeric.rs, and in there I&#8217;ll add a recursive function for calculating factorials.</p><p>Then I&#8217;ll make main.rs use the factorial function. I also need to make lib.rs load the <em>numeric</em> module.</p><p>We now have the following files:</p><h4>main.rs:</h4><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!i9LQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9098ab1-f30e-4f9a-be8f-9af461c392f3_1302x474.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!i9LQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9098ab1-f30e-4f9a-be8f-9af461c392f3_1302x474.png 424w, https://substackcdn.com/image/fetch/$s_!i9LQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9098ab1-f30e-4f9a-be8f-9af461c392f3_1302x474.png 848w, https://substackcdn.com/image/fetch/$s_!i9LQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9098ab1-f30e-4f9a-be8f-9af461c392f3_1302x474.png 1272w, https://substackcdn.com/image/fetch/$s_!i9LQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9098ab1-f30e-4f9a-be8f-9af461c392f3_1302x474.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!i9LQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9098ab1-f30e-4f9a-be8f-9af461c392f3_1302x474.png" width="512" height="186.3963133640553" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f9098ab1-f30e-4f9a-be8f-9af461c392f3_1302x474.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:474,&quot;width&quot;:1302,&quot;resizeWidth&quot;:512,&quot;bytes&quot;:59348,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!i9LQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9098ab1-f30e-4f9a-be8f-9af461c392f3_1302x474.png 424w, https://substackcdn.com/image/fetch/$s_!i9LQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9098ab1-f30e-4f9a-be8f-9af461c392f3_1302x474.png 848w, https://substackcdn.com/image/fetch/$s_!i9LQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9098ab1-f30e-4f9a-be8f-9af461c392f3_1302x474.png 1272w, https://substackcdn.com/image/fetch/$s_!i9LQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9098ab1-f30e-4f9a-be8f-9af461c392f3_1302x474.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h4>lib.rs:</h4><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1kS8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9f3ed5c-f5fc-4843-8ea7-6de67d444755_1618x476.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1kS8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9f3ed5c-f5fc-4843-8ea7-6de67d444755_1618x476.png 424w, https://substackcdn.com/image/fetch/$s_!1kS8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9f3ed5c-f5fc-4843-8ea7-6de67d444755_1618x476.png 848w, https://substackcdn.com/image/fetch/$s_!1kS8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9f3ed5c-f5fc-4843-8ea7-6de67d444755_1618x476.png 1272w, https://substackcdn.com/image/fetch/$s_!1kS8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9f3ed5c-f5fc-4843-8ea7-6de67d444755_1618x476.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1kS8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9f3ed5c-f5fc-4843-8ea7-6de67d444755_1618x476.png" width="532" height="156.3846153846154" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d9f3ed5c-f5fc-4843-8ea7-6de67d444755_1618x476.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:428,&quot;width&quot;:1456,&quot;resizeWidth&quot;:532,&quot;bytes&quot;:60042,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1kS8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9f3ed5c-f5fc-4843-8ea7-6de67d444755_1618x476.png 424w, https://substackcdn.com/image/fetch/$s_!1kS8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9f3ed5c-f5fc-4843-8ea7-6de67d444755_1618x476.png 848w, https://substackcdn.com/image/fetch/$s_!1kS8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9f3ed5c-f5fc-4843-8ea7-6de67d444755_1618x476.png 1272w, https://substackcdn.com/image/fetch/$s_!1kS8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9f3ed5c-f5fc-4843-8ea7-6de67d444755_1618x476.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h4>numeric.rs:</h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fgG_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7160c5de-a620-478d-84b1-bcf9e6b91859_1176x748.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fgG_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7160c5de-a620-478d-84b1-bcf9e6b91859_1176x748.png 424w, https://substackcdn.com/image/fetch/$s_!fgG_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7160c5de-a620-478d-84b1-bcf9e6b91859_1176x748.png 848w, https://substackcdn.com/image/fetch/$s_!fgG_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7160c5de-a620-478d-84b1-bcf9e6b91859_1176x748.png 1272w, https://substackcdn.com/image/fetch/$s_!fgG_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7160c5de-a620-478d-84b1-bcf9e6b91859_1176x748.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fgG_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7160c5de-a620-478d-84b1-bcf9e6b91859_1176x748.png" width="442" height="281.13605442176873" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7160c5de-a620-478d-84b1-bcf9e6b91859_1176x748.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:748,&quot;width&quot;:1176,&quot;resizeWidth&quot;:442,&quot;bytes&quot;:66585,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fgG_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7160c5de-a620-478d-84b1-bcf9e6b91859_1176x748.png 424w, https://substackcdn.com/image/fetch/$s_!fgG_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7160c5de-a620-478d-84b1-bcf9e6b91859_1176x748.png 848w, https://substackcdn.com/image/fetch/$s_!fgG_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7160c5de-a620-478d-84b1-bcf9e6b91859_1176x748.png 1272w, https://substackcdn.com/image/fetch/$s_!fgG_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7160c5de-a620-478d-84b1-bcf9e6b91859_1176x748.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>When I run this as a binary with <code>cargo run</code>, the output is <code>6</code>.</p><p></p><h3>Adding Unit Tests</h3><p>According to the documentation, unless my brain has failed to grasp it properly, unit tests (tests that test individual units of the code such as functions) are typically placed in the same file where they are defined.</p><p>Therefore I&#8217;ll add a submodule into numeric.rs.</p><p>The <code>#[cfg(test)]</code> annotation on the submodule says that it should be loaded when <code>cargo</code> is run with test configuration, i.e. when <code>cargo test</code> is executed.</p><p>Individual unit tests are marked with <code>#[test]</code>.</p><p><code>assert_eq!</code> and <code>assert!</code> can be used to check the results of tests.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iGpn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b91e1bc-09d5-48e0-bffa-6e76285d038d_1390x1674.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iGpn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b91e1bc-09d5-48e0-bffa-6e76285d038d_1390x1674.png 424w, https://substackcdn.com/image/fetch/$s_!iGpn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b91e1bc-09d5-48e0-bffa-6e76285d038d_1390x1674.png 848w, https://substackcdn.com/image/fetch/$s_!iGpn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b91e1bc-09d5-48e0-bffa-6e76285d038d_1390x1674.png 1272w, https://substackcdn.com/image/fetch/$s_!iGpn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b91e1bc-09d5-48e0-bffa-6e76285d038d_1390x1674.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iGpn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b91e1bc-09d5-48e0-bffa-6e76285d038d_1390x1674.png" width="548" height="659.9654676258992" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0b91e1bc-09d5-48e0-bffa-6e76285d038d_1390x1674.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1674,&quot;width&quot;:1390,&quot;resizeWidth&quot;:548,&quot;bytes&quot;:167385,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!iGpn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b91e1bc-09d5-48e0-bffa-6e76285d038d_1390x1674.png 424w, https://substackcdn.com/image/fetch/$s_!iGpn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b91e1bc-09d5-48e0-bffa-6e76285d038d_1390x1674.png 848w, https://substackcdn.com/image/fetch/$s_!iGpn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b91e1bc-09d5-48e0-bffa-6e76285d038d_1390x1674.png 1272w, https://substackcdn.com/image/fetch/$s_!iGpn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b91e1bc-09d5-48e0-bffa-6e76285d038d_1390x1674.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>.We can run this test with <code>cargo test</code>, and it passes.</p><p></p><h3>Multiple Tests</h3><p>I&#8217;ve modified the test submodule by adding two more tests:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2eIJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F085aa2d3-e41e-44a4-991c-7d2771931a59_1430x1880.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2eIJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F085aa2d3-e41e-44a4-991c-7d2771931a59_1430x1880.png 424w, https://substackcdn.com/image/fetch/$s_!2eIJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F085aa2d3-e41e-44a4-991c-7d2771931a59_1430x1880.png 848w, https://substackcdn.com/image/fetch/$s_!2eIJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F085aa2d3-e41e-44a4-991c-7d2771931a59_1430x1880.png 1272w, https://substackcdn.com/image/fetch/$s_!2eIJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F085aa2d3-e41e-44a4-991c-7d2771931a59_1430x1880.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2eIJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F085aa2d3-e41e-44a4-991c-7d2771931a59_1430x1880.png" width="496" height="652.083916083916" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/085aa2d3-e41e-44a4-991c-7d2771931a59_1430x1880.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1880,&quot;width&quot;:1430,&quot;resizeWidth&quot;:496,&quot;bytes&quot;:218504,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!2eIJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F085aa2d3-e41e-44a4-991c-7d2771931a59_1430x1880.png 424w, https://substackcdn.com/image/fetch/$s_!2eIJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F085aa2d3-e41e-44a4-991c-7d2771931a59_1430x1880.png 848w, https://substackcdn.com/image/fetch/$s_!2eIJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F085aa2d3-e41e-44a4-991c-7d2771931a59_1430x1880.png 1272w, https://substackcdn.com/image/fetch/$s_!2eIJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F085aa2d3-e41e-44a4-991c-7d2771931a59_1430x1880.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>One of these tests is marked with #[ignore]. It won&#8217;t be run by default.</p><p>To run all tests, including the ignored tests, we can use: </p><p><code>cargo test -- --include-ignored</code></p><p>To run only the ignored tests, we can use: </p><p><code>cargo test -- --ignored</code></p><p></p><h3>Testing Panic</h3><p>We can test code that should panic using the should_panic specification. This optionally allows us to specify a substring that should be found in the panic message.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uKyz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2ac3245-dede-40c5-935a-e124673cf9ef_1688x594.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uKyz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2ac3245-dede-40c5-935a-e124673cf9ef_1688x594.png 424w, https://substackcdn.com/image/fetch/$s_!uKyz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2ac3245-dede-40c5-935a-e124673cf9ef_1688x594.png 848w, https://substackcdn.com/image/fetch/$s_!uKyz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2ac3245-dede-40c5-935a-e124673cf9ef_1688x594.png 1272w, https://substackcdn.com/image/fetch/$s_!uKyz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2ac3245-dede-40c5-935a-e124673cf9ef_1688x594.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uKyz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2ac3245-dede-40c5-935a-e124673cf9ef_1688x594.png" width="614" height="215.9120879120879" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a2ac3245-dede-40c5-935a-e124673cf9ef_1688x594.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:512,&quot;width&quot;:1456,&quot;resizeWidth&quot;:614,&quot;bytes&quot;:113648,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uKyz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2ac3245-dede-40c5-935a-e124673cf9ef_1688x594.png 424w, https://substackcdn.com/image/fetch/$s_!uKyz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2ac3245-dede-40c5-935a-e124673cf9ef_1688x594.png 848w, https://substackcdn.com/image/fetch/$s_!uKyz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2ac3245-dede-40c5-935a-e124673cf9ef_1688x594.png 1272w, https://substackcdn.com/image/fetch/$s_!uKyz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2ac3245-dede-40c5-935a-e124673cf9ef_1688x594.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Here I&#8217;ve artificially added a panic! into the test itself, which would normally be expected to come from the code under test. Unless a panic occurs and the text contains the string &#8220;bad&#8221;, this test fails.</p><p></p><h3>Returning Result</h3><p>Instead of using assert! or assert_eq! it&#8217;s also possible to return a <code>Result</code>. The following test passes if it returns <code>Ok</code> and fails if it returns <code>Error</code>.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hGYb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13855506-77b5-4f8d-8c14-7442ad0cd0e6_1626x488.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hGYb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13855506-77b5-4f8d-8c14-7442ad0cd0e6_1626x488.png 424w, https://substackcdn.com/image/fetch/$s_!hGYb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13855506-77b5-4f8d-8c14-7442ad0cd0e6_1626x488.png 848w, https://substackcdn.com/image/fetch/$s_!hGYb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13855506-77b5-4f8d-8c14-7442ad0cd0e6_1626x488.png 1272w, https://substackcdn.com/image/fetch/$s_!hGYb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13855506-77b5-4f8d-8c14-7442ad0cd0e6_1626x488.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hGYb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13855506-77b5-4f8d-8c14-7442ad0cd0e6_1626x488.png" width="611" height="183.38392857142858" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/13855506-77b5-4f8d-8c14-7442ad0cd0e6_1626x488.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:437,&quot;width&quot;:1456,&quot;resizeWidth&quot;:611,&quot;bytes&quot;:76363,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hGYb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13855506-77b5-4f8d-8c14-7442ad0cd0e6_1626x488.png 424w, https://substackcdn.com/image/fetch/$s_!hGYb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13855506-77b5-4f8d-8c14-7442ad0cd0e6_1626x488.png 848w, https://substackcdn.com/image/fetch/$s_!hGYb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13855506-77b5-4f8d-8c14-7442ad0cd0e6_1626x488.png 1272w, https://substackcdn.com/image/fetch/$s_!hGYb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13855506-77b5-4f8d-8c14-7442ad0cd0e6_1626x488.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3>Integration Tests</h3><p>The above tests are all unit tests, that test software units. It&#8217;s also possible to create integration tests: tests external to a library that test how the library functions work together.</p><p>I&#8217;ve created a file called test_numeric.rs (but the name isn&#8217;t important) and put it in a folder called <code>tests/</code>, parallel to src/.</p><p>In this case there&#8217;s no need to put the tests in a submodule, and the library functions can be imported normally, using the package name.</p><h4>test_numeric.rs (in a tests folder parallel to src/):</h4><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!aGtM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca15f458-39fc-4702-bd3e-83a6b08d84cf_1310x596.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!aGtM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca15f458-39fc-4702-bd3e-83a6b08d84cf_1310x596.png 424w, https://substackcdn.com/image/fetch/$s_!aGtM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca15f458-39fc-4702-bd3e-83a6b08d84cf_1310x596.png 848w, https://substackcdn.com/image/fetch/$s_!aGtM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca15f458-39fc-4702-bd3e-83a6b08d84cf_1310x596.png 1272w, https://substackcdn.com/image/fetch/$s_!aGtM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca15f458-39fc-4702-bd3e-83a6b08d84cf_1310x596.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!aGtM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca15f458-39fc-4702-bd3e-83a6b08d84cf_1310x596.png" width="494" height="224.75114503816795" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ca15f458-39fc-4702-bd3e-83a6b08d84cf_1310x596.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:596,&quot;width&quot;:1310,&quot;resizeWidth&quot;:494,&quot;bytes&quot;:74667,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!aGtM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca15f458-39fc-4702-bd3e-83a6b08d84cf_1310x596.png 424w, https://substackcdn.com/image/fetch/$s_!aGtM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca15f458-39fc-4702-bd3e-83a6b08d84cf_1310x596.png 848w, https://substackcdn.com/image/fetch/$s_!aGtM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca15f458-39fc-4702-bd3e-83a6b08d84cf_1310x596.png 1272w, https://substackcdn.com/image/fetch/$s_!aGtM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca15f458-39fc-4702-bd3e-83a6b08d84cf_1310x596.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>These tests will also run automatically when you run <code>cargo test</code>.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.caveofprogramming.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading The Cave of Programming Blog! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[Rust Lifetimes]]></title><description><![CDATA[And also, a starfield program]]></description><link>https://blog.caveofprogramming.com/p/rust-lifetimes</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/rust-lifetimes</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Mon, 10 Feb 2025 13:32:48 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/ca5afbe2-e089-4a9d-a271-0a70e6be7581_1024x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;9bdcfb71-a941-4bfa-98ec-ba2c08b7ad74&quot;,&quot;duration&quot;:null}"></div><p></p><p>Git repository: <a href="https://github.com/caveofprogramming/rust/tree/main/024%20Lifetimes">https://github.com/caveofprogramming/rust/</a></p><h3>Functions and Lifetimes</h3><p>In rust, the following program doesn&#8217;t compile unless we move the <code>println!()</code> inside the brackets</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!LTkJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f5810c3-f2fb-46a8-99c5-e3ffbaadf916_656x746.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!LTkJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f5810c3-f2fb-46a8-99c5-e3ffbaadf916_656x746.png 424w, https://substackcdn.com/image/fetch/$s_!LTkJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f5810c3-f2fb-46a8-99c5-e3ffbaadf916_656x746.png 848w, https://substackcdn.com/image/fetch/$s_!LTkJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f5810c3-f2fb-46a8-99c5-e3ffbaadf916_656x746.png 1272w, https://substackcdn.com/image/fetch/$s_!LTkJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f5810c3-f2fb-46a8-99c5-e3ffbaadf916_656x746.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!LTkJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f5810c3-f2fb-46a8-99c5-e3ffbaadf916_656x746.png" width="244" height="277.4756097560976" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0f5810c3-f2fb-46a8-99c5-e3ffbaadf916_656x746.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:746,&quot;width&quot;:656,&quot;resizeWidth&quot;:244,&quot;bytes&quot;:44797,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!LTkJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f5810c3-f2fb-46a8-99c5-e3ffbaadf916_656x746.png 424w, https://substackcdn.com/image/fetch/$s_!LTkJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f5810c3-f2fb-46a8-99c5-e3ffbaadf916_656x746.png 848w, https://substackcdn.com/image/fetch/$s_!LTkJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f5810c3-f2fb-46a8-99c5-e3ffbaadf916_656x746.png 1272w, https://substackcdn.com/image/fetch/$s_!LTkJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f5810c3-f2fb-46a8-99c5-e3ffbaadf916_656x746.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>All references have a lifetime, and in this case, the variable <code>y</code> is dropped at the closing bracket, meaning the reference <code>x</code> doesn&#8217;t &#8220;live long enough&#8221; to be printed outside the brackets.</p><p>Lifetime parameters are generic parameters and begin with an apostrophe. Usually single lower-case letters are used to name them.</p><p>The most common function of them is to connect the lifetime of references returned from functions to the lifetimes of arguments upon which they depend.</p><p>The following program, which just returns its argument, compiles fine.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Op9k!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F683b7c87-0b6b-49e5-a7de-7f6ac32344b8_1248x682.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Op9k!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F683b7c87-0b6b-49e5-a7de-7f6ac32344b8_1248x682.png 424w, https://substackcdn.com/image/fetch/$s_!Op9k!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F683b7c87-0b6b-49e5-a7de-7f6ac32344b8_1248x682.png 848w, https://substackcdn.com/image/fetch/$s_!Op9k!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F683b7c87-0b6b-49e5-a7de-7f6ac32344b8_1248x682.png 1272w, https://substackcdn.com/image/fetch/$s_!Op9k!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F683b7c87-0b6b-49e5-a7de-7f6ac32344b8_1248x682.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Op9k!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F683b7c87-0b6b-49e5-a7de-7f6ac32344b8_1248x682.png" width="452" height="247.00641025641025" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/683b7c87-0b6b-49e5-a7de-7f6ac32344b8_1248x682.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:682,&quot;width&quot;:1248,&quot;resizeWidth&quot;:452,&quot;bytes&quot;:77653,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Op9k!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F683b7c87-0b6b-49e5-a7de-7f6ac32344b8_1248x682.png 424w, https://substackcdn.com/image/fetch/$s_!Op9k!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F683b7c87-0b6b-49e5-a7de-7f6ac32344b8_1248x682.png 848w, https://substackcdn.com/image/fetch/$s_!Op9k!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F683b7c87-0b6b-49e5-a7de-7f6ac32344b8_1248x682.png 1272w, https://substackcdn.com/image/fetch/$s_!Op9k!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F683b7c87-0b6b-49e5-a7de-7f6ac32344b8_1248x682.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The generic parameter <code>&#8216;a</code> is a lifetime annotation. It specifies that the returned reference lives as long as the s parameter. </p><p>In this case, the program also compiles without the <code>&#8216;a </code>lifetime specification, because this situation is simple enough for the Rust compiler to not require it.</p><p>In the following program, the lifetime annotations are necessary for the longest function to compile.</p><p>Here&#8217;s my understanding of this program: the generic parameter &#8216;a is inferred from the arguments passed to the function. <code>&#8216;a</code> will end up specifying that the lifetime of the returned reference is as long as the shortest lifetime of any of the input parameter references.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Foyn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95bf8668-fd56-4990-b346-4c9b71cd9a98_2238x1408.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Foyn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95bf8668-fd56-4990-b346-4c9b71cd9a98_2238x1408.png 424w, https://substackcdn.com/image/fetch/$s_!Foyn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95bf8668-fd56-4990-b346-4c9b71cd9a98_2238x1408.png 848w, https://substackcdn.com/image/fetch/$s_!Foyn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95bf8668-fd56-4990-b346-4c9b71cd9a98_2238x1408.png 1272w, https://substackcdn.com/image/fetch/$s_!Foyn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95bf8668-fd56-4990-b346-4c9b71cd9a98_2238x1408.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Foyn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95bf8668-fd56-4990-b346-4c9b71cd9a98_2238x1408.png" width="597" height="375.58516483516485" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/95bf8668-fd56-4990-b346-4c9b71cd9a98_2238x1408.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:916,&quot;width&quot;:1456,&quot;resizeWidth&quot;:597,&quot;bytes&quot;:187270,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Foyn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95bf8668-fd56-4990-b346-4c9b71cd9a98_2238x1408.png 424w, https://substackcdn.com/image/fetch/$s_!Foyn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95bf8668-fd56-4990-b346-4c9b71cd9a98_2238x1408.png 848w, https://substackcdn.com/image/fetch/$s_!Foyn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95bf8668-fd56-4990-b346-4c9b71cd9a98_2238x1408.png 1272w, https://substackcdn.com/image/fetch/$s_!Foyn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95bf8668-fd56-4990-b346-4c9b71cd9a98_2238x1408.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Struct Lifetime Parameters</h3><p>Structs can also contain references, but then you also need to specify lifetime parameters.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!z7Xh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4278d3-a846-401d-8f35-19fe0cbfd406_1138x680.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!z7Xh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4278d3-a846-401d-8f35-19fe0cbfd406_1138x680.png 424w, https://substackcdn.com/image/fetch/$s_!z7Xh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4278d3-a846-401d-8f35-19fe0cbfd406_1138x680.png 848w, https://substackcdn.com/image/fetch/$s_!z7Xh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4278d3-a846-401d-8f35-19fe0cbfd406_1138x680.png 1272w, https://substackcdn.com/image/fetch/$s_!z7Xh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4278d3-a846-401d-8f35-19fe0cbfd406_1138x680.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!z7Xh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4278d3-a846-401d-8f35-19fe0cbfd406_1138x680.png" width="540" height="322.6713532513181" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ee4278d3-a846-401d-8f35-19fe0cbfd406_1138x680.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:680,&quot;width&quot;:1138,&quot;resizeWidth&quot;:540,&quot;bytes&quot;:59311,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!z7Xh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4278d3-a846-401d-8f35-19fe0cbfd406_1138x680.png 424w, https://substackcdn.com/image/fetch/$s_!z7Xh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4278d3-a846-401d-8f35-19fe0cbfd406_1138x680.png 848w, https://substackcdn.com/image/fetch/$s_!z7Xh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4278d3-a846-401d-8f35-19fe0cbfd406_1138x680.png 1272w, https://substackcdn.com/image/fetch/$s_!z7Xh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4278d3-a846-401d-8f35-19fe0cbfd406_1138x680.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Starfields</h3><p>In the video I also demonstrate that you can create an animated starfield program using Microsoft Hotspot, and with a few slight tweaks no warnings even appear when you compile it!</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5N4Q!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88bada07-c9cb-468e-b8c2-9bd1d0c71a5c_1584x1236.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5N4Q!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88bada07-c9cb-468e-b8c2-9bd1d0c71a5c_1584x1236.png 424w, https://substackcdn.com/image/fetch/$s_!5N4Q!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88bada07-c9cb-468e-b8c2-9bd1d0c71a5c_1584x1236.png 848w, https://substackcdn.com/image/fetch/$s_!5N4Q!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88bada07-c9cb-468e-b8c2-9bd1d0c71a5c_1584x1236.png 1272w, https://substackcdn.com/image/fetch/$s_!5N4Q!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88bada07-c9cb-468e-b8c2-9bd1d0c71a5c_1584x1236.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5N4Q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88bada07-c9cb-468e-b8c2-9bd1d0c71a5c_1584x1236.png" width="1456" height="1136" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/88bada07-c9cb-468e-b8c2-9bd1d0c71a5c_1584x1236.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1136,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:49138,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5N4Q!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88bada07-c9cb-468e-b8c2-9bd1d0c71a5c_1584x1236.png 424w, https://substackcdn.com/image/fetch/$s_!5N4Q!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88bada07-c9cb-468e-b8c2-9bd1d0c71a5c_1584x1236.png 848w, https://substackcdn.com/image/fetch/$s_!5N4Q!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88bada07-c9cb-468e-b8c2-9bd1d0c71a5c_1584x1236.png 1272w, https://substackcdn.com/image/fetch/$s_!5N4Q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88bada07-c9cb-468e-b8c2-9bd1d0c71a5c_1584x1236.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.caveofprogramming.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Traits as Function Parameters and Return Types]]></title><description><![CDATA[Still very similar to Java interfaces!]]></description><link>https://blog.caveofprogramming.com/p/traits-as-function-parameters-and</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/traits-as-function-parameters-and</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Tue, 04 Feb 2025 15:41:55 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/6a5abcc9-fb04-4432-ae06-47cb84f878a7_1024x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;8c9c24c9-f15b-4c94-9eaa-226e27b30e88&quot;,&quot;duration&quot;:null}"></div><p></p><p>Github: <a href="https://github.com/caveofprogramming/rust/tree/main/023%20Traits%20as%20Function%20Parameters/traits/src">https://github.com/caveofprogramming/rust/</a></p><h3>Using Traits in Return Types</h3><p></p><p>I created a file called <strong>book.rs</strong> and put a struct in it:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%20Parameters/traits/src/book.rs" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1nYe!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e96c042-0616-4c87-b056-4b94f415dce0_922x384.png 424w, https://substackcdn.com/image/fetch/$s_!1nYe!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e96c042-0616-4c87-b056-4b94f415dce0_922x384.png 848w, https://substackcdn.com/image/fetch/$s_!1nYe!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e96c042-0616-4c87-b056-4b94f415dce0_922x384.png 1272w, https://substackcdn.com/image/fetch/$s_!1nYe!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e96c042-0616-4c87-b056-4b94f415dce0_922x384.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1nYe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e96c042-0616-4c87-b056-4b94f415dce0_922x384.png" width="354" height="147.43600867678958" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5e96c042-0616-4c87-b056-4b94f415dce0_922x384.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:384,&quot;width&quot;:922,&quot;resizeWidth&quot;:354,&quot;bytes&quot;:43851,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%20Parameters/traits/src/book.rs&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1nYe!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e96c042-0616-4c87-b056-4b94f415dce0_922x384.png 424w, https://substackcdn.com/image/fetch/$s_!1nYe!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e96c042-0616-4c87-b056-4b94f415dce0_922x384.png 848w, https://substackcdn.com/image/fetch/$s_!1nYe!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e96c042-0616-4c87-b056-4b94f415dce0_922x384.png 1272w, https://substackcdn.com/image/fetch/$s_!1nYe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e96c042-0616-4c87-b056-4b94f415dce0_922x384.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p>The struct and its fields all have to be public if I want to use it outside of the file it&#8217;s in.</p><p>To make the Rust compiler process this file, I need a <code>mod</code> statement in <strong>main.rs</strong> to tell Rust about the file.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%2Parameters/traits/src/main.rs" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7f-G!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3d6e201-e86b-4154-94ea-d2e3a727b9f4_388x88.png 424w, https://substackcdn.com/image/fetch/$s_!7f-G!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3d6e201-e86b-4154-94ea-d2e3a727b9f4_388x88.png 848w, https://substackcdn.com/image/fetch/$s_!7f-G!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3d6e201-e86b-4154-94ea-d2e3a727b9f4_388x88.png 1272w, https://substackcdn.com/image/fetch/$s_!7f-G!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3d6e201-e86b-4154-94ea-d2e3a727b9f4_388x88.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7f-G!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3d6e201-e86b-4154-94ea-d2e3a727b9f4_388x88.png" width="200" height="45.36082474226804" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c3d6e201-e86b-4154-94ea-d2e3a727b9f4_388x88.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:88,&quot;width&quot;:388,&quot;resizeWidth&quot;:200,&quot;bytes&quot;:10864,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%2Parameters/traits/src/main.rs&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7f-G!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3d6e201-e86b-4154-94ea-d2e3a727b9f4_388x88.png 424w, https://substackcdn.com/image/fetch/$s_!7f-G!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3d6e201-e86b-4154-94ea-d2e3a727b9f4_388x88.png 848w, https://substackcdn.com/image/fetch/$s_!7f-G!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3d6e201-e86b-4154-94ea-d2e3a727b9f4_388x88.png 1272w, https://substackcdn.com/image/fetch/$s_!7f-G!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3d6e201-e86b-4154-94ea-d2e3a727b9f4_388x88.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>I also want to be able to use the new Book type I&#8217;ve created in <strong>main.rs</strong>, so I also add a <code>use</code> statement:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%2Parameters/traits/src/main.rs" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MYx9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d8ebb9-c626-4952-a9cb-f03303694bcd_608x98.png 424w, https://substackcdn.com/image/fetch/$s_!MYx9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d8ebb9-c626-4952-a9cb-f03303694bcd_608x98.png 848w, https://substackcdn.com/image/fetch/$s_!MYx9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d8ebb9-c626-4952-a9cb-f03303694bcd_608x98.png 1272w, https://substackcdn.com/image/fetch/$s_!MYx9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d8ebb9-c626-4952-a9cb-f03303694bcd_608x98.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MYx9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d8ebb9-c626-4952-a9cb-f03303694bcd_608x98.png" width="312" height="50.28947368421053" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/61d8ebb9-c626-4952-a9cb-f03303694bcd_608x98.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:98,&quot;width&quot;:608,&quot;resizeWidth&quot;:312,&quot;bytes&quot;:13846,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%2Parameters/traits/src/main.rs&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!MYx9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d8ebb9-c626-4952-a9cb-f03303694bcd_608x98.png 424w, https://substackcdn.com/image/fetch/$s_!MYx9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d8ebb9-c626-4952-a9cb-f03303694bcd_608x98.png 848w, https://substackcdn.com/image/fetch/$s_!MYx9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d8ebb9-c626-4952-a9cb-f03303694bcd_608x98.png 1272w, https://substackcdn.com/image/fetch/$s_!MYx9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d8ebb9-c626-4952-a9cb-f03303694bcd_608x98.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>I want to create a couple of traits that my book will implement, so I put those in a file I called <strong>details.rs</strong>. Here is the complete <strong>details.rs</strong>:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%20Parameters/traits/src/details.rs" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3Uyk!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65104752-e29e-4d78-9ed5-cbbc1da8d318_1280x650.png 424w, https://substackcdn.com/image/fetch/$s_!3Uyk!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65104752-e29e-4d78-9ed5-cbbc1da8d318_1280x650.png 848w, https://substackcdn.com/image/fetch/$s_!3Uyk!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65104752-e29e-4d78-9ed5-cbbc1da8d318_1280x650.png 1272w, https://substackcdn.com/image/fetch/$s_!3Uyk!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65104752-e29e-4d78-9ed5-cbbc1da8d318_1280x650.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3Uyk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65104752-e29e-4d78-9ed5-cbbc1da8d318_1280x650.png" width="393" height="199.5703125" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/65104752-e29e-4d78-9ed5-cbbc1da8d318_1280x650.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:650,&quot;width&quot;:1280,&quot;resizeWidth&quot;:393,&quot;bytes&quot;:77815,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%20Parameters/traits/src/details.rs&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!3Uyk!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65104752-e29e-4d78-9ed5-cbbc1da8d318_1280x650.png 424w, https://substackcdn.com/image/fetch/$s_!3Uyk!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65104752-e29e-4d78-9ed5-cbbc1da8d318_1280x650.png 848w, https://substackcdn.com/image/fetch/$s_!3Uyk!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65104752-e29e-4d78-9ed5-cbbc1da8d318_1280x650.png 1272w, https://substackcdn.com/image/fetch/$s_!3Uyk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65104752-e29e-4d78-9ed5-cbbc1da8d318_1280x650.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>I load details.rs by adding a mod statement to <strong>main.rs</strong>:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%2Parameters/traits/src/main.rs" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kfO4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59c35c0c-ac17-4558-a1a0-982f53caa430_496x96.png 424w, https://substackcdn.com/image/fetch/$s_!kfO4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59c35c0c-ac17-4558-a1a0-982f53caa430_496x96.png 848w, https://substackcdn.com/image/fetch/$s_!kfO4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59c35c0c-ac17-4558-a1a0-982f53caa430_496x96.png 1272w, https://substackcdn.com/image/fetch/$s_!kfO4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59c35c0c-ac17-4558-a1a0-982f53caa430_496x96.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kfO4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59c35c0c-ac17-4558-a1a0-982f53caa430_496x96.png" width="246" height="47.61290322580645" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/59c35c0c-ac17-4558-a1a0-982f53caa430_496x96.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:96,&quot;width&quot;:496,&quot;resizeWidth&quot;:246,&quot;bytes&quot;:12231,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%2Parameters/traits/src/main.rs&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!kfO4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59c35c0c-ac17-4558-a1a0-982f53caa430_496x96.png 424w, https://substackcdn.com/image/fetch/$s_!kfO4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59c35c0c-ac17-4558-a1a0-982f53caa430_496x96.png 848w, https://substackcdn.com/image/fetch/$s_!kfO4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59c35c0c-ac17-4558-a1a0-982f53caa430_496x96.png 1272w, https://substackcdn.com/image/fetch/$s_!kfO4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59c35c0c-ac17-4558-a1a0-982f53caa430_496x96.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><p>Now in <strong>book.rs</strong> I can say that <code>Book</code> implements these traits, by importing the traits and then adding implementations. Here is the <strong>complete book.rs</strong>. Notice I had to import the traits via absolute paths, not relative paths.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%20Parameters/traits/src/book.rs" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TGC0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F036869f9-8a09-453b-9912-ea9381201951_1326x1772.png 424w, https://substackcdn.com/image/fetch/$s_!TGC0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F036869f9-8a09-453b-9912-ea9381201951_1326x1772.png 848w, https://substackcdn.com/image/fetch/$s_!TGC0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F036869f9-8a09-453b-9912-ea9381201951_1326x1772.png 1272w, https://substackcdn.com/image/fetch/$s_!TGC0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F036869f9-8a09-453b-9912-ea9381201951_1326x1772.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TGC0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F036869f9-8a09-453b-9912-ea9381201951_1326x1772.png" width="388" height="518.5037707390649" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/036869f9-8a09-453b-9912-ea9381201951_1326x1772.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1772,&quot;width&quot;:1326,&quot;resizeWidth&quot;:388,&quot;bytes&quot;:202438,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%20Parameters/traits/src/book.rs&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!TGC0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F036869f9-8a09-453b-9912-ea9381201951_1326x1772.png 424w, https://substackcdn.com/image/fetch/$s_!TGC0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F036869f9-8a09-453b-9912-ea9381201951_1326x1772.png 848w, https://substackcdn.com/image/fetch/$s_!TGC0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F036869f9-8a09-453b-9912-ea9381201951_1326x1772.png 1272w, https://substackcdn.com/image/fetch/$s_!TGC0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F036869f9-8a09-453b-9912-ea9381201951_1326x1772.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Now in <strong>main.rs</strong> I can define a function that returns an object that implements both traits:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%20Parameters/traits/src/main.rs" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zyan!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd36eeb47-2f81-44ed-aded-4e689f81692e_1916x586.png 424w, https://substackcdn.com/image/fetch/$s_!zyan!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd36eeb47-2f81-44ed-aded-4e689f81692e_1916x586.png 848w, https://substackcdn.com/image/fetch/$s_!zyan!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd36eeb47-2f81-44ed-aded-4e689f81692e_1916x586.png 1272w, https://substackcdn.com/image/fetch/$s_!zyan!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd36eeb47-2f81-44ed-aded-4e689f81692e_1916x586.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!zyan!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd36eeb47-2f81-44ed-aded-4e689f81692e_1916x586.png" width="518" height="158.31730769230768" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d36eeb47-2f81-44ed-aded-4e689f81692e_1916x586.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:445,&quot;width&quot;:1456,&quot;resizeWidth&quot;:518,&quot;bytes&quot;:104995,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%20Parameters/traits/src/main.rs&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zyan!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd36eeb47-2f81-44ed-aded-4e689f81692e_1916x586.png 424w, https://substackcdn.com/image/fetch/$s_!zyan!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd36eeb47-2f81-44ed-aded-4e689f81692e_1916x586.png 848w, https://substackcdn.com/image/fetch/$s_!zyan!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd36eeb47-2f81-44ed-aded-4e689f81692e_1916x586.png 1272w, https://substackcdn.com/image/fetch/$s_!zyan!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd36eeb47-2f81-44ed-aded-4e689f81692e_1916x586.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><p>I can then use this to obtain a book object, and since I know it must have methods <code>author()</code> and <code>title()</code>, I can use those to print details. Here is the <strong>complete main.rs</strong>:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%20Parameters/traits/src/main.rs" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rXo4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6024d7d2-80a0-486a-9b5d-0eff61f79be7_2110x1796.png 424w, https://substackcdn.com/image/fetch/$s_!rXo4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6024d7d2-80a0-486a-9b5d-0eff61f79be7_2110x1796.png 848w, https://substackcdn.com/image/fetch/$s_!rXo4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6024d7d2-80a0-486a-9b5d-0eff61f79be7_2110x1796.png 1272w, https://substackcdn.com/image/fetch/$s_!rXo4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6024d7d2-80a0-486a-9b5d-0eff61f79be7_2110x1796.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rXo4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6024d7d2-80a0-486a-9b5d-0eff61f79be7_2110x1796.png" width="572" height="486.75" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6024d7d2-80a0-486a-9b5d-0eff61f79be7_2110x1796.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1239,&quot;width&quot;:1456,&quot;resizeWidth&quot;:572,&quot;bytes&quot;:272279,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%20Parameters/traits/src/main.rs&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rXo4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6024d7d2-80a0-486a-9b5d-0eff61f79be7_2110x1796.png 424w, https://substackcdn.com/image/fetch/$s_!rXo4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6024d7d2-80a0-486a-9b5d-0eff61f79be7_2110x1796.png 848w, https://substackcdn.com/image/fetch/$s_!rXo4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6024d7d2-80a0-486a-9b5d-0eff61f79be7_2110x1796.png 1272w, https://substackcdn.com/image/fetch/$s_!rXo4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6024d7d2-80a0-486a-9b5d-0eff61f79be7_2110x1796.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h3>Traits as Function Parameters</h3><p></p><p>A very similar syntax can be used to specify that a function argument must implement certain traits, again using the <code>impl</code> keyword.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%2Parameters/traits/src/main.rs" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cXIV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff25a89c2-c4bc-4827-a56d-6e0211c8d73e_2058x290.png 424w, https://substackcdn.com/image/fetch/$s_!cXIV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff25a89c2-c4bc-4827-a56d-6e0211c8d73e_2058x290.png 848w, https://substackcdn.com/image/fetch/$s_!cXIV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff25a89c2-c4bc-4827-a56d-6e0211c8d73e_2058x290.png 1272w, https://substackcdn.com/image/fetch/$s_!cXIV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff25a89c2-c4bc-4827-a56d-6e0211c8d73e_2058x290.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!cXIV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff25a89c2-c4bc-4827-a56d-6e0211c8d73e_2058x290.png" width="1456" height="205" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f25a89c2-c4bc-4827-a56d-6e0211c8d73e_2058x290.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:205,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:71614,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%2Parameters/traits/src/main.rs&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cXIV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff25a89c2-c4bc-4827-a56d-6e0211c8d73e_2058x290.png 424w, https://substackcdn.com/image/fetch/$s_!cXIV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff25a89c2-c4bc-4827-a56d-6e0211c8d73e_2058x290.png 848w, https://substackcdn.com/image/fetch/$s_!cXIV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff25a89c2-c4bc-4827-a56d-6e0211c8d73e_2058x290.png 1272w, https://substackcdn.com/image/fetch/$s_!cXIV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff25a89c2-c4bc-4827-a56d-6e0211c8d73e_2058x290.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This function accepts a reference to an object that implements the <code>Author</code> and <code>Title</code> traits. If only one interface needs to be implemented, of course neither the <code>+</code> nor the brackets after the <code>&amp;</code> are needed.</p><p>This is apparently syntactic sugar for a slightly different syntax:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%2Parameters/traits/src/main.rs" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!t4Md!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d846578-2365-432b-8427-790e4b6501b5_2102x308.png 424w, https://substackcdn.com/image/fetch/$s_!t4Md!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d846578-2365-432b-8427-790e4b6501b5_2102x308.png 848w, https://substackcdn.com/image/fetch/$s_!t4Md!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d846578-2365-432b-8427-790e4b6501b5_2102x308.png 1272w, https://substackcdn.com/image/fetch/$s_!t4Md!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d846578-2365-432b-8427-790e4b6501b5_2102x308.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!t4Md!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d846578-2365-432b-8427-790e4b6501b5_2102x308.png" width="1456" height="213" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8d846578-2365-432b-8427-790e4b6501b5_2102x308.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:213,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:70034,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%2Parameters/traits/src/main.rs&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!t4Md!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d846578-2365-432b-8427-790e4b6501b5_2102x308.png 424w, https://substackcdn.com/image/fetch/$s_!t4Md!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d846578-2365-432b-8427-790e4b6501b5_2102x308.png 848w, https://substackcdn.com/image/fetch/$s_!t4Md!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d846578-2365-432b-8427-790e4b6501b5_2102x308.png 1272w, https://substackcdn.com/image/fetch/$s_!t4Md!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d846578-2365-432b-8427-790e4b6501b5_2102x308.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>There is another syntax which is designed to be helpful if you need to specify lots of interfaces or have lots of generic parameter types:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%2Parameters/traits/src/main.rs" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ydv_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdea0a67-083b-46af-9606-27933398939c_2288x598.png 424w, https://substackcdn.com/image/fetch/$s_!ydv_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdea0a67-083b-46af-9606-27933398939c_2288x598.png 848w, https://substackcdn.com/image/fetch/$s_!ydv_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdea0a67-083b-46af-9606-27933398939c_2288x598.png 1272w, https://substackcdn.com/image/fetch/$s_!ydv_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdea0a67-083b-46af-9606-27933398939c_2288x598.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ydv_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdea0a67-083b-46af-9606-27933398939c_2288x598.png" width="1456" height="381" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fdea0a67-083b-46af-9606-27933398939c_2288x598.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:381,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:97313,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://github.com/caveofprogramming/rust/blob/main/023%20Traits%20as%20Function%2Parameters/traits/src/main.rs&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ydv_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdea0a67-083b-46af-9606-27933398939c_2288x598.png 424w, https://substackcdn.com/image/fetch/$s_!ydv_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdea0a67-083b-46af-9606-27933398939c_2288x598.png 848w, https://substackcdn.com/image/fetch/$s_!ydv_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdea0a67-083b-46af-9606-27933398939c_2288x598.png 1272w, https://substackcdn.com/image/fetch/$s_!ydv_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdea0a67-083b-46af-9606-27933398939c_2288x598.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.caveofprogramming.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item></channel></rss>