<?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]]></title><description><![CDATA[Videos and articles and programming and computers.]]></description><link>https://blog.caveofprogramming.com</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</title><link>https://blog.caveofprogramming.com</link></image><generator>Substack</generator><lastBuildDate>Fri, 08 May 2026 11:34:36 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[0713 Mastering C: String Exercise]]></title><description><![CDATA[Code on GitHub: https://github.com/caveofprogramming/c-for-programmers/]]></description><link>https://blog.caveofprogramming.com/p/0713-mastering-c-string-exercise</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/0713-mastering-c-string-exercise</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Sat, 12 Jul 2025 16:03:19 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/f480df45-db7d-495d-a488-8611df74c6f0_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/c-for-programmers/tree/dc6346ea9fdd9db56a6c3e412ed5e5e0a0b99b0f/07%20Strings">https://github.com/caveofprogramming/c-for-programmers/</a></p>
      <p>
          <a href="https://blog.caveofprogramming.com/p/0713-mastering-c-string-exercise">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[0712 Mastering C: Command-Line Arguments]]></title><description><![CDATA[Code on GitHub: https://github.com/caveofprogramming/c-for-programmers/]]></description><link>https://blog.caveofprogramming.com/p/0712-mastering-c-command-line-arguments</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/0712-mastering-c-command-line-arguments</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Fri, 11 Jul 2025 16:03:11 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/60817295-601a-46b3-9fe5-55db37f7ba86_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/c-for-programmers/tree/dc6346ea9fdd9db56a6c3e412ed5e5e0a0b99b0f/07%20Strings">https://github.com/caveofprogramming/c-for-programmers/</a></p>
      <p>
          <a href="https://blog.caveofprogramming.com/p/0712-mastering-c-command-line-arguments">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[0711 Mastering C: Puts]]></title><description><![CDATA[Code on GitHub: https://github.com/caveofprogramming/c-for-programmers/]]></description><link>https://blog.caveofprogramming.com/p/0711-mastering-c-puts</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/0711-mastering-c-puts</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Thu, 10 Jul 2025 16:03:18 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/806b43e8-27cd-467c-b762-80bdcf05dbc9_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/c-for-programmers/tree/dc6346ea9fdd9db56a6c3e412ed5e5e0a0b99b0f/07%20Strings">https://github.com/caveofprogramming/c-for-programmers/</a></p>
      <p>
          <a href="https://blog.caveofprogramming.com/p/0711-mastering-c-puts">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[0710 Mastering C: Arrays of Strings]]></title><description><![CDATA[Code on GitHub: https://github.com/caveofprogramming/c-for-programmers/]]></description><link>https://blog.caveofprogramming.com/p/0710-mastering-c-arrays-of-strings</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/0710-mastering-c-arrays-of-strings</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Wed, 09 Jul 2025 16:03:24 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/f512ff0e-fd2b-4cea-8270-72db5569d50e_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/c-for-programmers/tree/dc6346ea9fdd9db56a6c3e412ed5e5e0a0b99b0f/07%20Strings">https://github.com/caveofprogramming/c-for-programmers/</a></p>
      <p>
          <a href="https://blog.caveofprogramming.com/p/0710-mastering-c-arrays-of-strings">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[0709 Mastering C: Tokenizing Strings]]></title><description><![CDATA[Code on GitHub: https://github.com/caveofprogramming/c-for-programmers/]]></description><link>https://blog.caveofprogramming.com/p/0709-mastering-c-tokenizing-strings</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/0709-mastering-c-tokenizing-strings</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Tue, 08 Jul 2025 16:03:08 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/6ff85c25-c74f-4c0f-bafb-68825f9a907b_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/c-for-programmers/tree/dc6346ea9fdd9db56a6c3e412ed5e5e0a0b99b0f/07%20Strings">https://github.com/caveofprogramming/c-for-programmers/</a></p>
      <p>
          <a href="https://blog.caveofprogramming.com/p/0709-mastering-c-tokenizing-strings">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[0708 Mastering C: Copying N Characters]]></title><description><![CDATA[Code on GitHub: https://github.com/caveofprogramming/c-for-programmers/]]></description><link>https://blog.caveofprogramming.com/p/0708-mastering-c-copying-n-characters</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/0708-mastering-c-copying-n-characters</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Mon, 07 Jul 2025 16:02:51 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/c22191d9-8221-4288-8eb2-8a7094f9031c_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/c-for-programmers/tree/dc6346ea9fdd9db56a6c3e412ed5e5e0a0b99b0f/07%20Strings">https://github.com/caveofprogramming/c-for-programmers/</a></p>
      <p>
          <a href="https://blog.caveofprogramming.com/p/0708-mastering-c-copying-n-characters">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[0707 Mastering C: Copying Strings]]></title><description><![CDATA[Code on GitHub: https://github.com/caveofprogramming/c-for-programmers/]]></description><link>https://blog.caveofprogramming.com/p/0707-mastering-c-copying-strings</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/0707-mastering-c-copying-strings</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Sat, 05 Jul 2025 23:57:22 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/3573dfb5-fdf6-427d-9ef3-89de308edc72_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/c-for-programmers/tree/dc6346ea9fdd9db56a6c3e412ed5e5e0a0b99b0f/07%20Strings">https://github.com/caveofprogramming/c-for-programmers/</a></p>
      <p>
          <a href="https://blog.caveofprogramming.com/p/0707-mastering-c-copying-strings">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[0705 Mastering C: Scanning User Input]]></title><description><![CDATA[Code on GitHub: https://github.com/caveofprogramming/c-for-programmers/]]></description><link>https://blog.caveofprogramming.com/p/0705-mastering-c-scanning-user-input</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/0705-mastering-c-scanning-user-input</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Fri, 04 Jul 2025 16:01:10 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/89886194-ca91-4ddb-a241-8c70ecd55837_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/c-for-programmers/tree/dc6346ea9fdd9db56a6c3e412ed5e5e0a0b99b0f/07%20Strings">https://github.com/caveofprogramming/c-for-programmers/</a></p>
      <p>
          <a href="https://blog.caveofprogramming.com/p/0705-mastering-c-scanning-user-input">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[0704 Mastering C: Sprintf]]></title><description><![CDATA[Code on GitHub: https://github.com/caveofprogramming/c-for-programmers/]]></description><link>https://blog.caveofprogramming.com/p/0704-mastering-c-sprintf</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/0704-mastering-c-sprintf</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Thu, 03 Jul 2025 15:58:26 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/b40ded9e-068f-4350-9515-ad8c1e13a27b_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/c-for-programmers/tree/dc6346ea9fdd9db56a6c3e412ed5e5e0a0b99b0f/07%20Strings">https://github.com/caveofprogramming/c-for-programmers/</a></p>
      <p>
          <a href="https://blog.caveofprogramming.com/p/0704-mastering-c-sprintf">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[0703 Mastering C: Concatenation]]></title><description><![CDATA[Code on GitHub: https://github.com/caveofprogramming/c-for-programmers/]]></description><link>https://blog.caveofprogramming.com/p/0703-mastering-c-concatenation</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/0703-mastering-c-concatenation</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Wed, 02 Jul 2025 15:56:17 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/c65c6281-bc4a-490d-a868-2aa02beacfb2_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/c-for-programmers/tree/dc6346ea9fdd9db56a6c3e412ed5e5e0a0b99b0f/07%20Strings">https://github.com/caveofprogramming/c-for-programmers/</a></p>
      <p>
          <a href="https://blog.caveofprogramming.com/p/0703-mastering-c-concatenation">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[0702 Mastering C: Comparing Strings]]></title><description><![CDATA[Code on GitHub: https://github.com/caveofprogramming/c-for-programmers/]]></description><link>https://blog.caveofprogramming.com/p/0702-mastering-c-comparing-strings</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/0702-mastering-c-comparing-strings</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Tue, 01 Jul 2025 15:56:47 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/bb459023-8263-421f-b67a-9fb442706b7a_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/c-for-programmers/tree/dc6346ea9fdd9db56a6c3e412ed5e5e0a0b99b0f/07%20Strings">https://github.com/caveofprogramming/c-for-programmers/</a></p>
      <p>
          <a href="https://blog.caveofprogramming.com/p/0702-mastering-c-comparing-strings">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[0701 Mastering C: String Length]]></title><description><![CDATA[Code on GitHub: https://github.com/caveofprogramming/c-for-programmers/]]></description><link>https://blog.caveofprogramming.com/p/0701-mastering-c-string-length</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/0701-mastering-c-string-length</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Mon, 30 Jun 2025 15:56:10 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/72ba3795-513f-4b57-8526-e84097274872_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/c-for-programmers/tree/dc6346ea9fdd9db56a6c3e412ed5e5e0a0b99b0f/07%20Strings">https://github.com/caveofprogramming/c-for-programmers/</a></p>
      <p>
          <a href="https://blog.caveofprogramming.com/p/0701-mastering-c-string-length">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[0700 Mastering C: Strings Introduction]]></title><description><![CDATA[Code on GitHub: https://github.com/caveofprogramming/c-for-programmers/]]></description><link>https://blog.caveofprogramming.com/p/0700-mastering-c-strings-introduction</link><guid isPermaLink="false">https://blog.caveofprogramming.com/p/0700-mastering-c-strings-introduction</guid><dc:creator><![CDATA[John Purcell]]></dc:creator><pubDate>Sun, 29 Jun 2025 15:54:16 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/5a96258a-ab26-422e-ae5a-8be7037d78f8_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Code on GitHub: <a href="https://github.com/caveofprogramming/c-for-programmers/tree/dc6346ea9fdd9db56a6c3e412ed5e5e0a0b99b0f/07%20Strings">https://github.com/caveofprogramming/c-for-programmers/</a></p>
      <p>
          <a href="https://blog.caveofprogramming.com/p/0700-mastering-c-strings-introduction">
              Read more
          </a>
      </p>
   ]]></content:encoded></item></channel></rss>