<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>It&#x27;s all about the bit</title>
    <link rel="self" type="application/atom+xml" href="https://itsallaboutthebit.com/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://itsallaboutthebit.com"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-10-11T13:02:00+00:00</updated>
    <id>https://itsallaboutthebit.com/atom.xml</id>
    <entry xml:lang="en">
        <title>I&#x27;m sorry, but you still have to think</title>
        <published>2026-10-11T13:02:00+00:00</published>
        <updated>2026-10-11T13:02:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://itsallaboutthebit.com/i-am-sorry-but-you-still-have-to-think/"/>
        <id>https://itsallaboutthebit.com/i-am-sorry-but-you-still-have-to-think/</id>
        
        <content type="html" xml:base="https://itsallaboutthebit.com/i-am-sorry-but-you-still-have-to-think/">&lt;p&gt;DHH, the creator of the Ruby on Rails framework,
decided to rewrite &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;basecamp&#x2F;once-campfire&quot;&gt;Campfire Once&lt;&#x2F;a&gt; from Ruby on Rails to Rust. Or rather
ask a clanker to do it for him, as he can&#x27;t stand reading or writing the Rust code himself. After that he added
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;x.com&#x2F;dhh&#x2F;status&#x2F;2106810173683851564?s=20&quot;&gt;rewrites in other languages&lt;&#x2F;a&gt;, like Elixir and Go.&lt;&#x2F;p&gt;
&lt;p&gt;This is very interesting to me, as it&#x27;s a very good insight into the results of someone
using AI agents without reading the code. In this case, that someone has also a lot of prior programming experience.
And the results are... well, not very encouraging if you are hoping you can stop reading the code, or stop thinking altogether
anytime soon. There are multiple problems with the rewrites, but let&#x27;s start with non-functional differences cause
they nicely show something that a lot of people seem to not realize: if your prompt is not specific enough,
many decisions are a coin flip. This is because a whole lot of questions don&#x27;t
have a single correct answer. Do we want to keep backwards compatibility? Do we care more about latency or throughput?
How much memory can the system use under load? Is losing new content notifications after a crash acceptable?
You may not care about them, or at least some of them,
but they will be implicitly answered when an LLM implements what you think you want.&lt;&#x2F;p&gt;
&lt;p&gt;If you look into the rewrites closer, you will quickly see that they treat backwards compatibility and other constraints
differently. The Rust version doesn&#x27;t maintain 100% backwards compatibility. For example, it dropped the CSRF token so
that caching is easier, and also replaced Redis with in-process queues.
Elixir rewrite is much closer to the Rails original. This already makes the comparison pretty much useless,
as these differences are language independent. It&#x27;s not like a clanker heard &quot;rewrite in Elixir&quot; and chose to keep
100% backwards compatibility because of the language that has been used. But it gets even better!&lt;&#x2F;p&gt;
&lt;p&gt;If you looked at the code, and yeah, I know, we should not be reading code anymore, you would quickly notice a lot
of the things are not great. For example, I&#x27;ve seen people complaining that Elixir&#x27;s version got a single process
sequentially processing all of the SQL queries, even if these were reads that could be running concurrently. That&#x27;s
a fair complaint, but DHH &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;x.com&#x2F;dhh&#x2F;status&#x2F;2106864061832991134?s=20&quot;&gt;seems to think&lt;&#x2F;a&gt; it&#x27;s not the best look for Elixir that the clanker couldn&#x27;t write
performant code. Not sure if I agree when I look at the Rust version. Cause in the Rust version some of the database
operations are not async, which in some cases may be worse. You see, in Rust, when using an async runtime, the scheduling
is not preemptive, but rather cooperative. If a task doesn&#x27;t yield, no other task can run on the same worker thread.
What this means, in practice, is that the time spent in the task should be as short as possible. For example, when
you run an SQL query that takes 100ms, you don&#x27;t want all the other tasks to wait as the task issuing the query is
waiting anyway. Thus, you should ideally use an async I&#x2F;O operation that yields to the runtime
while waiting for a response from the database. In the Rust rewrite some queries run on worker threads, but some run as
blocking operations in async tasks. Same with locks. When using
an async runtime, the safest bet is to use async locks like &lt;code&gt;tokio::sync::Mutex&lt;&#x2F;code&gt;. It&#x27;s fine to use a non-async
version if you&#x27;re sure that the lock is held for a very short time, but if you hold a sync lock for 10ms,
all the tasks on the same thread wait for it, while the blocking task itself is not doing any work. Thus, I regret
to inform you, that no, coding is not likely &quot;solved&quot;, and you still need to know what you&#x27;re doing.&lt;&#x2F;p&gt;
&lt;p&gt;Going further, it turned out that slop benchmarks are also, well, slop, as they only measure throughput, disregarding other properties of the system. For example, &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;x.com&#x2F;ZachSDaniel1&#x2F;status&#x2F;2106876400196059200&quot;&gt;Zach Daniels measured new post notifications
delivery rate under load&lt;&#x2F;a&gt;, and discovered a 1% successful
delivery rate in the Rust version under heavy load. Not a great look. Benchmarks are hard.
But even the improved benchmark may not be actually testing what you want to test, depending on the characteristics of your system.
You see, when you test a system you may want to emphasize different properties under load.
Both DHH&#x27;s and Zach&#x27;s benchmarks were closed-loop benchmarks, so they were testing &quot;how many requests can the system handle in a specific amount of time?&quot;.
The test was using N clients, each sending a new message as soon as it gets a response for the previous one.
In many cases, though, the increased load on the system
may come from a big number of users performing an action at the same time, who will not wait for other users to finish their actions. In this case you would prefer
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;grafana.com&#x2F;docs&#x2F;k6&#x2F;latest&#x2F;using-k6&#x2F;scenarios&#x2F;executors&#x2F;constant-arrival-rate&#x2F;&quot;&gt;constant arrival rate&lt;&#x2F;a&gt;, ie. you want to send requests at a constant
rate rather than making the rate depend on how fast the system can respond. If you want to check how reliable the events delivery is,
you would ideally want to compare the same number of events.
Here, if you look at the results, it shows that Rust delivered 1% of notifications out of 6-7k, whereas Elixir delivered 100%
of notifications out of ~1.7k. What it shows is that the Elixir version handles backpressure better, but this only holds as long as the number
of requests don&#x27;t overload the system.&lt;&#x2F;p&gt;
&lt;p&gt;But let&#x27;s leave benchmarks just for a moment and talk about trade-offs, cause programming is all about trade-offs. Sure, there are situations
where a tool or a solution is clearly better, without any downsides, but it&#x27;s quite rare, especially when you get to complex systems
that have to be reliable. I&#x27;ve seen quite a lot of
people from the Elixir community coming to various conclusions after they&#x27;ve seen the 1% delivery rate of the Rust version,
without really trying to understand why it happens. The general consensus? Elixir is just better at concurrency! In Rust the
scheduler is cooperative, how can you even live like this? I like Elixir, and I&#x27;ve successfully used it in production, but it&#x27;s not a silver bullet. Yes, Elixir (or other BEAM based languages) is very good at concurrency, and admittedly
writing concurrent code in Elixir is, in general, easier than in Rust, but it comes at a cost of not having low level control,
higher memory usage, and oftentimes, speed. There is a reason why &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rusterlium&#x2F;rustler&quot;&gt;rustler&lt;&#x2F;a&gt; exists. And proclaiming
language&#x27;s superiority based on a single metric without knowing the root cause, may be misleading.&lt;&#x2F;p&gt;
&lt;p&gt;Remember how I mentioned the closed-loop stress test shows just one of the properties of the system as Rust processed ~4 times more requests and thus
had to handle more events sent to the client through a WebSocket? I&#x27;ve rerun the test with constant delivery rate using the DHH&#x27;s Rust version and Zach&#x27;s Elixir version with various fixes. At 100 POSTs&#x2F;s
the clients received ~14% events in Rust. In Elixir it was ~60%. Still better, right? Not so fast! At this traffic level Rust didn&#x27;t have any
HTTP errors. Elixir timed out on ~23% of HTTP POST requests. And what about latency? The worst event delivery latency was close to
180s. In Rust when the deliveries are lagging, clients get disconnected. On reconnect, a browser client would fetch the latest messages,
which largely invalidates the need for the missed events.
What do you think is better UX: the client silently reconnecting in the background and fetching the new updates, or waiting for an
update about a new message for 3 minutes? Which just shows that, again, a single metric doesn&#x27;t tell the whole story.&lt;&#x2F;p&gt;
&lt;p&gt;Getting back to trade-offs. Do you know &lt;em&gt;why&lt;&#x2F;em&gt; the Rust version drops so many messages under heavy load? It uses &lt;code&gt;tokio::sync::broadcast&lt;&#x2F;code&gt; channel
to broadcast events to connected clients. An event informing about a new chat message may need to be sent to multiple connections,
so that makes total sense. One of the properties of the broadcast channel is that it&#x27;s bounded by a set capacity. If a receiver can&#x27;t
handle the messages fast enough, the receiver will get a &lt;code&gt;RecvError::Lagged&lt;&#x2F;code&gt; error (refer to the docs to learn more about &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;latest&#x2F;tokio&#x2F;sync&#x2F;broadcast&#x2F;index.html#lagging&quot;&gt;lagging&lt;&#x2F;a&gt;). The broadcast capacity in the Rust rewrite was set to 256. In Elixir, the &lt;code&gt;GenServer&lt;&#x2F;code&gt; process is handling events
delivery and by default &lt;code&gt;GenServer&lt;&#x2F;code&gt; mailboxes don&#x27;t have a limit. A single line change in Rust:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;diff&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B31D28;background-color: #FFEEF0;&quot;&gt;- stream_capacity: 256&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #22863A;background-color: #F0FFF4;&quot;&gt;+ stream_capacity: 16384&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;increases the delivery rate at 100 req&#x2F;s from ~14% to ~90%. It&#x27;s better than Elixir now, isn&#x27;t it? Not really. I think that
this version is actually worse because in this case it&#x27;s better to fail fast and force the client to reconnect rather than handle things extremely slowly. You know
what else changed after raising the capacity? The pMAX event delivery latency went up from 11s to &amp;gt;130s, similarly to how Elixir behaved, which I think
is strictly worse than disconnecting the lagging clients. If you ask me, even 11s is too long and if the receiver can&#x27;t pass the event faster
than that, it&#x27;s better to drop the event. Which shows how important it is to set sensible constraints in the system, and that, in fact,
Elixir isn&#x27;t just automatically fixing all of the concurrency issues out of the box. Also, if you don&#x27;t set bounds yourself, you will likely run into
external limits. During the 100 req&#x2F;s stress test the Elixir version reached 1.8GB memory usage. Another thing to ponder on: is it better to drop some messages or
get OOM killed? Trade-offs all the way. Elixir is great, but it won&#x27;t magically solve all of your problems. No matter the language you&#x27;re using, you have to think
about failure modes and trade-offs.&lt;&#x2F;p&gt;
&lt;p&gt;So what have we learned today? You still have to think critically. It&#x27;s good to know what you&#x27;re doing. Don&#x27;t
make hasty assumptions based on a single metric. If you want a reliable system you should know when to fail. Also, benchmarks are hard.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Memory Safety Absolutists</title>
        <published>2026-07-28T10:02:00+00:00</published>
        <updated>2026-07-28T10:02:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://itsallaboutthebit.com/memory-safety-absolutists/"/>
        <id>https://itsallaboutthebit.com/memory-safety-absolutists/</id>
        
        <content type="html" xml:base="https://itsallaboutthebit.com/memory-safety-absolutists/">&lt;p&gt;When seeing the title of this post, I bet in some people&#x27;s minds, the first thought was &quot;Rust devs!&quot;. This connection is not unfounded. Rust devs tend to be passionate about memory safety. Some of it, I&#x27;m sure, may have been caused by a classic language wars attitude: my language is better than yours, and here is why. I&#x27;m hoping, though, that most of the Rust devs, who say they care about memory safety, genuinely care about making software safer, and not just criticizing languages that compete with Rust.&lt;&#x2F;p&gt;
&lt;p&gt;The article, surprisingly, is not about Rust devs.&lt;&#x2F;p&gt;
&lt;p&gt;Until recently, most of the memory safety related discourse had a relatively simple basis, at least when considering non-GC, systems programming languages, like C, C++, Zig, and Rust. Rust aims to disallow compiling programs that may introduce memory safety issues (at a cost of sometimes disallowing a program that would have been safe), with an escape hatch in the form of &lt;code&gt;unsafe&lt;&#x2F;code&gt; that allows, among other things, dereferencing raw pointers. C and others leave ensuring memory safety to the programmer. The amount of help from the language differs, for example there is RAII and smart pointers in C++, or defer in Zig, but for the most part nothing stops you from violating memory access.&lt;&#x2F;p&gt;
&lt;p&gt;The situation today is a bit different with a new way to make C, C++, and in the future maybe also Zig, code memory safe: &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;fil-c.org&#x2F;&quot;&gt;Fil-C&lt;&#x2F;a&gt;. C and C++ code compiled with Fil-C will panic on invalid memory access, like out of bounds access, or use after free. It achieves it by combining GC and InvisiCaps - a way to track memory accessible by a pointer. Zig&#x27;s author recently announced &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;codeberg.org&#x2F;ziglang&#x2F;zig&#x2F;issues&#x2F;36237&quot;&gt;a new compilation mode for Zig inspired by Fil-C&lt;&#x2F;a&gt;. Fil-C is a very interesting project and I sincerely hope it will succeed and at least some popular C and C++ projects will provide Fil-C compiled releases.&lt;&#x2F;p&gt;
&lt;p&gt;In an ideal world both Rust programmers and C&#x2F;C++&#x2F;Zig programmers that care about memory safety would be happy that there are more ways to minimize memory safety vulnerabilities, but alas, we&#x27;re not living in an ideal world and I can&#x27;t shake the feeling a lot of the criticism towards Rust recently is disingenuous. If you read &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;x.com&#x2F;filpizlo&quot;&gt;Fil-C&#x27;s author opinions on Twitter&lt;&#x2F;a&gt; you can find a huge number of claims Rust is a memory unsafe language because of ability to bypass some of the Rust guarantees when using &lt;code&gt;unsafe&lt;&#x2F;code&gt;. Andrew Kelley, Zig&#x27;s author, seems to have a similar stance demonstrated in the fil compilation mode issue title: &quot;introduce an actually memory safe (unlike Rust) compilation mode inspired by Fil-C&quot;. Meaning: Rust is unsafe and only Fil-C or Zig&#x27;s &quot;fil&quot; compilation mode will deal with memory safety related vulnerabilities.&lt;&#x2F;p&gt;
&lt;p&gt;In discussions related to Rust and Fil-C, I&#x27;ve often seen a claim similar to: &quot;if Rust folks really cared about memory safety, they would promote Fil-C and ditch Rust as Fil-C is safer, otherwise they just care about their new shiny language, and not memory safety&quot;. That includes Fil-C&#x27;s author himself. I&#x27;m not sure about Andrew Kelley, but the issue title I mentioned earlier feels awfully close. These kinds of arguments are, in my opinion, ignoring reality, and feel like fanaticism and cult-like behaviour that often Rust devs are accused of.&lt;&#x2F;p&gt;
&lt;p&gt;If Fil-C was a drop in replacement with absolutely zero trade-offs, I would maybe partially agree with the sentiment, but it has trade-offs: it&#x27;s ABI incompatible with non-Fil-C compiled programs, it may be a few times slower in some cases, and it introduces GC. None of these things is a deal breaker for some programs. A lot of programs you use daily could be a few times slower than they are, and you wouldn&#x27;t even notice. A lot of them also don&#x27;t link anything dynamically, so the ABI compatibility doesn&#x27;t matter. But not every software program is a simple utility. There are lots of popular projects, where GC and ABI incompatibility are an issue and there is no way they would ever start using Fil-C like technology, or at least not in a current form. Crucially, the kind of programs that can&#x27;t use Fil-C are often a good fit for Rust.&lt;&#x2F;p&gt;
&lt;p&gt;But Rust is unsafe, isn&#x27;t it? It has &lt;code&gt;unsafe&lt;&#x2F;code&gt; after all! If you want to be that strict, or in other words, if you are a memory safety absolutist, that may well be true for you. I, and I hope most people, am more pragmatic than that. There is not a whole lot of data on how secure Rust really is in practice, but to my knowledge there hasn&#x27;t been a lot of exploitable memory safety vulnerabilities in Rust software, and there are big projects with some data, like &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;blog.google&#x2F;security&#x2F;rust-in-android-move-fast-fix-things&#x2F;#:~:text=With%20roughly%205%20million%20lines,1%20million%20lines%20(MLOC).&quot;&gt;5M+ LOC in Android&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;With roughly 5 million lines of Rust in the Android platform and one potential memory safety vulnerability found (and fixed pre-release), our estimated vulnerability density for Rust is 0.2 vuln per 1 million lines (MLOC).&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;Our historical data for C and C++ shows a density of closer to 1,000 memory safety vulnerabilities per MLOC. Our Rust code is currently tracking at a density orders of magnitude lower: a more than 1000x reduction.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;I&#x27;m sure these numbers would differ for other projects, but I think by now it is well established that, in practice, Rust minimizes the risk of introducing memory safety issues.&lt;&#x2F;p&gt;
&lt;p&gt;If you could choose a technology that prevents 99.9% of issues in 100% of programs or 100% of issues in 90% of programs, which one would you choose? I have no idea what are the real numbers, but you get the point. Thankfully, we don&#x27;t have to choose one or the other, contrary to what some people would claim. I would hope that projects written in C&#x2F;C++&#x2F;Zig, that can accept the trade-offs, will be available as Fil-C compiled binaries, and software that can&#x27;t, will be written in languages that either completely or mostly remove the risk of introducing memory safety vulnerabilities.&lt;&#x2F;p&gt;
&lt;p&gt;And I also think it is totally fine to use Rust even if you could use a GC language like Go or Fil-C. Memory safety absolutists would tell you that&#x27;s not acceptable, although some of them seem to apply memory safety absolutism only when it comes to Rust, and not C, C++, or Zig, go figure. The thing is, programs that can be also written in GC powered languages, often don&#x27;t need &lt;code&gt;unsafe&lt;&#x2F;code&gt; at all, and programs that need &lt;code&gt;unsafe&lt;&#x2F;code&gt; often couldn&#x27;t have been using GC. In my experience, people that don&#x27;t approach solving problem with total fanaticism, tend to consider trade-offs. For many of them, a very small risk of ever running into a severe memory safety issue, is outweighed by other language guarantees (like data races prevention), and other language features. What&#x27;s more, remember the 1000 memory safety related vulnerabilities per MLOC? With Fil-C they become crashes. It&#x27;s still better than introducing a security vulnerability issue, but that&#x27;s quite a lot of crashes to fix. If we&#x27;re in a realm of fearing relatively uncommon problems, it might be good to state that there were security vulnerabilities in the past that were enabled by an attacker being able to crash a program.&lt;&#x2F;p&gt;
&lt;p&gt;I will end this post saying that if someone cares so much about memory safety that even Rust&#x27;s 0.2 vulnerabilities per 1M lines of code is not acceptable, I would sure hope they will be criticizing people, who compile YOLO C&#x2F;C++ and non-fil Zig, as much, if not more, as Rust developers. After all, if you genuinely care about memory safety so strongly that even Rust is not safe enough for you, you wouldn&#x27;t want to allow people to use even less safe alternatives, would you?&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>JAWSM - a JavaScript to WASM compiler</title>
        <published>2025-04-06T20:20:00+00:00</published>
        <updated>2025-04-06T20:20:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://itsallaboutthebit.com/jawsm/"/>
        <id>https://itsallaboutthebit.com/jawsm/</id>
        
        <content type="html" xml:base="https://itsallaboutthebit.com/jawsm/">&lt;p&gt;About a year ago I open sourced a distributed load testing tool called &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;drogus&#x2F;crows&quot;&gt;Crows&lt;&#x2F;a&gt;. It runs scenarios compiled to WebAssembly,
and as much as I like writing Rust, I wanted to allow using a scripting language in Crows. There are some options to do that
in WebAssembly, like using &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.assemblyscript.org&#x2F;&quot;&gt;AssemblyScript&lt;&#x2F;a&gt;, but there is no native support for any
of the mainstream scripting languages like JavaScript or Python. And, for better or for worse, people using load testing
tools are now used to being able to write them in JavaScript through the popularity of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;k6.io&#x2F;&quot;&gt;k6&lt;&#x2F;a&gt;. While you
can run JavaScript on top of WebAssembly today, it involves compiling a JavaScript engine to WASM first, and
embedding it in the binary. While good tooling to do that exists, for example in a form of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;bytecodealliance&#x2F;StarlingMonkey&quot;&gt;SterlingMonkey&lt;&#x2F;a&gt;,
it means having to ship a multi megabyte binary for each script, and pay the price of keeping all of that code in memory.
In my rough testing with the Wasmtime runtime, each WebAssembly module generated by SterlingMonkey used about 10MBs of memory
at the start. And while it may be OK in some scenarios, it is a no go in a lot of use cases.&lt;&#x2F;p&gt;
&lt;p&gt;That&#x27;s why, about a half a year ago, I started working on a JS to WASM compiler called &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;drogus&#x2F;jawsm&quot;&gt;JAWSM&lt;&#x2F;a&gt;.
The idea is to compile scripts written in JS into WASM binaries, without relying on any existing engines, and thus
to massively decrease the binary size and the memory required to run a script. When I started the project it was simply
a way to learn more about recently standardized WASM proposals like WASM GC (garbage collected data structures) and
exception handling. Soon after I started, I realized I&#x27;m making very good progress, so I pushed forward. Now, after less
than 6 months of development, in my very limited free time, I have a proof of concept of all of the major JS semantics
like scopes, async&#x2F;await, exception handling, and generators, and the project passes about 25% of the Test262 test suite.&lt;&#x2F;p&gt;
&lt;p&gt;If you want to support further development of the project, please consider &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;sponsors&#x2F;drogus&quot;&gt;supporting me on GitHub&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;implementation-details&quot;&gt;Implementation details&lt;&#x2F;h3&gt;
&lt;p&gt;JAWSM benefits from modern WASM proposals like WASM GC and exception handling. WASM core instructions
are quite limited. You can read and write from memory, perform basic arithmetic functions, and call
host provided functions, but also limited integer or float inputs and outputs. This makes compiling lower level
code to WebAssembly fairly straightforward, but it also means any more advanced features have to be
implemented on top of WebAssembly core. WASM GC changes the game by introducing basic data structures
like structs and arrays, which are garbage collected when out of scope. This means that when implementing
a garbage collected language on top of WASM GC, you don&#x27;t have to reimplement the GC part. Of course
WebAssembly semantics are vastly different from JavaScript semantics, but it is possible to emulate
JavaScript behaviour using WASM. For example let&#x27;s consider a JavaScript object. It has a prototype,
own properties, may have a value (like objects wrappers). In WASM, it could be represented as a following
struct:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;(type $Object&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (struct&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (field $properties (mut (ref $PropertyMap)))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (field $own_prototype (mut anyref))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (field $value (mut anyref))))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we could have a function that sets a property on the object by modifying the &lt;code&gt;$properties&lt;&#x2F;code&gt; field
or a function that fetches a property and checks the prototype if it can&#x27;t find an own property.
While relatively simple, this kind of implementation allows to implement most of the prototypal inheritance
that JavaScript uses. Add a few specialized objects like &lt;code&gt;$Function&lt;&#x2F;code&gt; or &lt;code&gt;$Array&lt;&#x2F;code&gt; to the mix and you have
a working language created on top of a WASM virtual machine.&lt;&#x2F;p&gt;
&lt;p&gt;Creating a JS to WASM compiler in this style involves a few steps:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Creating objects and helpers required to implement JavaScript objects and semantics&lt;&#x2F;li&gt;
&lt;li&gt;Creating a compiler that converts JavaScript instructions into WASM instructions. For example
converting &lt;code&gt;foo.bar = 10&lt;&#x2F;code&gt; would be translated into fetching a &lt;code&gt;foo&lt;&#x2F;code&gt; variable from the scope,
creating an object holding a number &lt;code&gt;10&lt;&#x2F;code&gt;, and calling a function to set a property named &lt;code&gt;bar&lt;&#x2F;code&gt;
into the &lt;code&gt;foo&lt;&#x2F;code&gt; object&#x27;s properties.&lt;&#x2F;li&gt;
&lt;li&gt;Doing code transformations on generated WASM code in order to achieve things that are currently
not supported in WASM, like for example stack switching&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Once a significant portion of the language is implemented that way, it is possible to implement
builting types and functions implementing them in JavaScript. For example, recently I&#x27;ve implemented
a lot of the functions on the &lt;code&gt;Array.prototype&lt;&#x2F;code&gt;, like &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;drogus&#x2F;jawsm&#x2F;blob&#x2F;6b3dd22479e7bb3cec102b42d2c2c44b0a9b8666&#x2F;src&#x2F;js&#x2F;prepend.js#L544-L562&quot;&gt;shift&lt;&#x2F;a&gt;.
The implementation is fully in JavaScript.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;future-plans&quot;&gt;Future plans&lt;&#x2F;h3&gt;
&lt;p&gt;I am at a point where the project has a good chance of eventually transitioning from the proof of
concept stage into a production ready software. Sure, it will require a lot of work, and figuring
out a few more non-trivial problems, but it looks very promising already. Something that I wouldn&#x27;t
think I would be able to say anytime soon.&lt;&#x2F;p&gt;
&lt;p&gt;As the funamentals, like JavaScript semantics, are now mostly working, I think that I will be able
to gradually focus on simpler stuff and move forward with the builtin types and functions. I will
also have to invest into tooling cause there are multiple parts of the project that are no longer
cutting it at the scale the project reaches. Like, for example, most of the WASM code is generated
using a Rust macro that has 10k lines of code and at the moment there is no way to split it into
smaller pieces without changing the underlying implementation.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;m looking forward to the future and I&#x27;m hoping the WebAssembly ecosystem will continue expanding
as I think it&#x27;s a very promising technology that still hasn&#x27;t shown its&#x27; full potential.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Crows, a Rust and WASM based load testing tool</title>
        <published>2024-03-24T22:20:00+00:00</published>
        <updated>2024-03-24T22:20:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://itsallaboutthebit.com/crows/"/>
        <id>https://itsallaboutthebit.com/crows/</id>
        
        <content type="html" xml:base="https://itsallaboutthebit.com/crows/">&lt;p&gt;Today I released &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;drogus&#x2F;crows&quot;&gt;Crows&lt;&#x2F;a&gt;, a distributed load testing tool written in Rust running scenarios compiled to WebAssembly. If you are new to load testing - it&#x27;s a type of performance testing that helps you determine how your system behaves under load. Sometimes also the term &quot;stress testing&quot; is used, which usually involves testing the system with a load big enough to make the system fail and observing how the failure manifests itself. In a simple case a tool like &lt;code&gt;wrk&lt;&#x2F;code&gt; may be used to send several requests to a single URL, but on many occasions, you may want to write more advanced scenarios simulating user behavior. Crows falls under the latter category. One of the interesting things about Crows is that it aims to be a distributed tool that allows to dynamically add and remove workers to the system, but also upload scenarios dynamically. If you want to know more about the tool itself take a look at the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;drogus&#x2F;crows?tab=readme-ov-file#crows&quot;&gt;README&lt;&#x2F;a&gt;, but in this post, I wanted to focus a bit more on another interesting feature: WebAssembly support.&lt;&#x2F;p&gt;
&lt;p&gt;While I&#x27;m not saying using WebAssembly magically makes software better, I think WebAssembly is one of the most interesting things that happened in the programming world in recent years. Crows it&#x27;s still in the &quot;proof of concept&quot; phase, but there are many tools that use WebAssembly successfully: (SpacetimeDB)[https:&#x2F;&#x2F;spacetimedb.com&#x2F;] (disclaimer, it&#x27;s where I work), &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;lunatic.solutions&#x2F;&quot;&gt;Lunatic runtime&lt;&#x2F;a&gt;, &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;fluvio.io&#x2F;&quot;&gt;Fluvio&lt;&#x2F;a&gt; or other experimental or &quot;in the works&quot; software like &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;flawless.dev&#x2F;&quot;&gt;Flawless&lt;&#x2F;a&gt;. If you are new to WebAssembly or to WebAssembly outside of the browser, you might be wondering: what&#x27;s the appeal?&lt;&#x2F;p&gt;
&lt;p&gt;I feel like writing applications using WebAssembly to run code is a bit like having at least part of the power that comes with writing your language without actually having to design and implement the language. Instead, you connect a runtime to a language or languages of your choice. You can use Rust and execute I&#x2F;O operations asynchronously and yet write the code as if it was a regular synchronous program. You can control how I&#x2F;O functions are performed. You can support running sandboxed programs compiled from multiple languages. Ideally, in the future, you could easily re-use libraries written in one language when coding in another language. And a lot more. I don&#x27;t want to dive into all of these features, but I&#x27;d like to talk about two things that are very appealing to me personally: many language features working out of the box and sandboxing.&lt;&#x2F;p&gt;
&lt;p&gt;One of the libraries that was an inspiration for Crows is a well-known load testing tool - &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;k6.io&#x2F;&quot;&gt;K6&lt;&#x2F;a&gt;. It&#x27;s a great tool and interestingly it kinda uses a similar model to Crows: the host (written in Go) provides the functions needed to interpret the scenarios, make HTTP requests, etc. while scenarios are written in another language (JS). Doing it this way makes a lot of sense. JavaScript is a fairly fast-interpreted language (which means no compilation is required) that works well in the context of handling and sending HTTP requests, but it&#x27;s usually slower than Go and in most implementations, it runs only on one thread. The latter part makes it trickier to write tools that use all of your cores. It&#x27;s not impossible, but usually, it involves some form of creating multiple processes like forking and then we get into the inter-process communication. Not my favorite style of programming to be honest. One of the issues of the model K6 uses, though, is the need to implement a lot of plumbing in between JS scenarios and the Go host. Let&#x27;s take &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;k6.io&#x2F;docs&#x2F;javascript-api&#x2F;k6-experimental&#x2F;timers&#x2F;&quot;&gt;timers&lt;&#x2F;a&gt; as an example. Javascript allows you to use functions like &lt;code&gt;setTimeout&lt;&#x2F;code&gt; and K6 also has an equivalent, but the implementation involves &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;grafana&#x2F;k6&#x2F;blob&#x2F;0af62a245781989a50d9b3f884a9875fcacffd23&#x2F;js&#x2F;modules&#x2F;k6&#x2F;timers&#x2F;timers.go&quot;&gt;a few hundred lines of code&lt;&#x2F;a&gt;. It&#x27;s not an issue for the users of the library, of course, but it&#x27;s a burden on the maintainers. For comparison when writing Crows, I didn&#x27;t have to do anything special to make something like &lt;code&gt;std::thread::sleep()&lt;&#x2F;code&gt; in Rust work. Of course, it&#x27;s not entirely free, someone had to implement it, ie. Rust needs to correctly compile this code to WASM format and a runtime, like &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;bytecodealliance&#x2F;wasmtime&quot;&gt;Wasmtime&lt;&#x2F;a&gt;, has to correctly interpret it. But then it&#x27;s a part of the standard and work we can use as a foundation. In other words, it gives a lot of power to programmers without the need to reimplement most of the glue code over and over again.&lt;&#x2F;p&gt;
&lt;p&gt;Another thing that is very interesting for me is sandboxing. Sandboxing, ie. running a program in a sandboxed environment thus also controlling the resources the program has access to and things it can do, is not a trivial problem to solve. It differs across different languages and in some cases, it&#x27;s either very hard or impossible to achieve. WebAssembly changes that. While it&#x27;s not something crucial for implementing tools like Crows, it comes in very handy in many situations. For example, imagine allowing users to write server side plugins to your hosted service. Doesn&#x27;t it sound powerful?&lt;&#x2F;p&gt;
&lt;p&gt;People usually suck at predicting the future, so I hope I&#x27;m not wrong here, but I think WebAssemby usage outside of the browser will grow in the next few years. It has its disadvantages and may be rough on the edges, but I think it opens a whole new world of possibilities that we didn&#x27;t have before. I&#x27;m hoping I picked your interest and be sure to check out &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;drogus&#x2F;crows&quot;&gt;Crows&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Can Rust prevent logic errors?</title>
        <published>2023-10-30T22:20:00+00:00</published>
        <updated>2023-10-30T22:20:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://itsallaboutthebit.com/logic-errors-in-rust/"/>
        <id>https://itsallaboutthebit.com/logic-errors-in-rust/</id>
        
        <content type="html" xml:base="https://itsallaboutthebit.com/logic-errors-in-rust/">&lt;p&gt;I&#x27;m a Rust &lt;s&gt;fanboi&lt;&#x2F;s&gt;enthusiast and as a Rust enthusiast I often talk about
things that make Rust appealing to me. Sometimes these conversations revolve
around reliability and correctness and then I usually mention how I feel Rust
helps me write software with fewer bugs, which usually is brushed off as something
improbable. My experience tells me otherwise, so facing a lack of
research, I&#x27;d like to at least offer my point of view in this context.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s start with a definition of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Software_bug&quot;&gt;a software
bug&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;A software bug is an error, flaw or fault in the design, development,
or operation of computer software that causes it to produce an incorrect or
unexpected result, or to behave in unintended ways&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;When coding in Rust you quickly notice that whole &lt;em&gt;categories of bugs&lt;&#x2F;em&gt; are
impossible to introduce in (safe) Rust:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;there are no nulls, so there are no things like &lt;code&gt;undefined method foo for nil:NilClass&lt;&#x2F;code&gt; in Ruby or &lt;code&gt;NullPointerException&lt;&#x2F;code&gt; in Java. A possibility of a missing
value is always explicit (through &lt;code&gt;Option&lt;&#x2F;code&gt;)&lt;&#x2F;li&gt;
&lt;li&gt;errors returned from a function have to be handled. You can&#x27;t access a
returned value unless you handle the &lt;code&gt;Result&lt;&#x2F;code&gt; type&lt;&#x2F;li&gt;
&lt;li&gt;data races are impossible (race conditions and deadlocks are
still possible, though)&lt;&#x2F;li&gt;
&lt;li&gt;Rust is a memory safe language and thus safe Rust shouldn&#x27;t result in an
undefined behavior&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Additionaly Rust requires explicitly marking variables as mutable and
it distinguishes between mutable and immutable borrowed values, so it&#x27;s typically
harder to introduce problems caused by unexpected mutations.&lt;&#x2F;p&gt;
&lt;p&gt;In combination all of these features, and more, let you write applications that
almost never crash. Or in other words they have no runtime
exceptions at all, except maybe things like running out of memory.&lt;&#x2F;p&gt;
&lt;p&gt;I spent a large part of my career writing Ruby and runtime exceptions are not only
common, but in many big applications they become practically impossible to deal
with. Pretty much every big application I&#x27;ve seen had an error tracker full of
errors. Each single exception in a tracker is of course fixable, but when you have
hundreds of them you need a dedicated effort to go to &quot;inbox zero&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;The problem is, bugs are not equal. Some bugs don&#x27;t have too much
impact on customer experience. A common thing happening in Rails apps is not
handling exceptions properly that leads to a 500 error instead of a 404 error.
Is it a big problem? Not really, a customer wouldn&#x27;t see what they expected
anyway, so not much harm done. It&#x27;s similar in many other cases. Maybe it&#x27;s not
a big deal, maybe it happens very rarely, maybe it&#x27;s a bot hitting a
non-existent page. Whatever the reason is, if a bug doesn&#x27;t have much impact
on user experience, it&#x27;s often ignored. Maybe a ticket is opened to fix it,
but then it often lands on the bottom of a never ending backlog.&lt;&#x2F;p&gt;
&lt;p&gt;At some point it turns out that there are too many errors in an error tracker
and fixing them would require considerable effort. But there is no time, and besides, those are minor
problems anyway. When a new error shows up in the tracker, you don&#x27;t
deal with it right away, you first check if it&#x27;s something new or if it&#x27;s even
something actually problematic. You can&#x27;t rely
on an error tracker anymore.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s not a hypothetical problem, too. I&#x27;ve seen multiple outages in my life that could have
been detected much sooner if only error tracker notifications meant anything.
If you have hundreds of errors, happening thousands of times a day, you
can&#x27;t really turn on notifications for every single error. You have to
periodically check if anything worth looking into showed up there.&lt;&#x2F;p&gt;
&lt;p&gt;Imagine how much nicer it feels when you don&#x27;t have runtime exceptions &lt;em&gt;at all&lt;&#x2F;em&gt;.
And again, it is not hypothetical. I co-wrote a real-time production app in Rust able to
handle more than a million websocket connections on one server that had
literally zero runtime errors for more than a year. Something that I absolutely
never achieved in any other language.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-about-logic-errors-though&quot;&gt;What about logic errors, though?&lt;&#x2F;h3&gt;
&lt;p&gt;So far I&#x27;ve spent quite a bit of time talking about runtime errors. And while
for me, it is a serious issue, many people brush it off with a statement similar
to:
&quot;whatever, most of the bugs are logic errors anyway, so the fact that Rust
prevents a few simple issues doesn&#x27;t matter&quot;. The argument is
the following: there is no research showing that statically typed languages, or
even Rust specifically, result in fewer bugs. Yes, you may have fewer bugs of a
certain type, but most bugs are caused by logic errors anyway.&lt;&#x2F;p&gt;
&lt;p&gt;At this point it&#x27;s worth differentiating between logic errors and runtime
errors. When a runtime error happens, an application either crashes or returns an
error to a user. Logic erors are a bit sneakier as they result in an incorrect
result, but without crashing a program. Look at this function for
example:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; add&lt;&#x2F;span&gt;&lt;span&gt;(a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; u64&lt;&#x2F;span&gt;&lt;span&gt;, b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; u64&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; u64&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;    a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There is clearly a logic error here, which can&#x27;t be easily found by conventional tools.
The function will not result in an error, but it will not do what it&#x27;s supposed
to do, ie. add two unsigned integers. That, plus also the fact that it can overflow.
If you do &lt;code&gt;add(0, u64::MAX)&lt;&#x2F;code&gt; it will panic in the development mode
and return &lt;code&gt;1&lt;&#x2F;code&gt; in release mode (you can prevent this kind of stuff with a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;rust-lang.github.io&#x2F;rust-clippy&#x2F;master&#x2F;#integer_arithmetic&quot;&gt;clippy
lint&lt;&#x2F;a&gt;, btw).&lt;&#x2F;p&gt;
&lt;p&gt;One could argue that this specific case could still be prevented if we used a
generic type instead of &lt;code&gt;u64&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;ops&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; add&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Add&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; A&lt;&#x2F;span&gt;&lt;span&gt;, b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; A&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; A&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;    a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This example will fail, because the &lt;code&gt;A&lt;&#x2F;code&gt; type is only expected to implement the
&lt;code&gt;Add&lt;&#x2F;code&gt; trait, which allows using the &lt;code&gt;+&lt;&#x2F;code&gt; operator. It quickly looses its
strength, though, if the calculation is a bit more complex. We might need to use
more traits and who knows if it&#x27;s correct or not:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;ops&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Sub&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Mul&lt;&#x2F;span&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; calculate&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Sub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Mul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; A&lt;&#x2F;span&gt;&lt;span&gt;, b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; A&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; A&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;    a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;calculate&lt;&#x2F;code&gt; function may require a few operations like addition, subtraction
or multiplication, and thus we can&#x27;t ensure correctness just by specifying the
arguments type.&lt;&#x2F;p&gt;
&lt;p&gt;So as we can clearly see Rust can&#x27;t prevent all the bugs (and I&#x27;m not sure if
there&#x27;s anyone that says otherwise!). But now the question is: can it prevent at
least &lt;em&gt;some&lt;&#x2F;em&gt; logic bugs?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;expressiveness&quot;&gt;Expressiveness&lt;&#x2F;h3&gt;
&lt;p&gt;I would argue that an expressive language can sometimes help you prevent logic errors
that you would otherwise introduce. Let&#x27;s take a simple &lt;code&gt;for&lt;&#x2F;code&gt; loop as an example.
Consider this JavaScript code:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; animals&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;dog&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;cat&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;fish&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;=&lt;&#x2F;span&gt;&lt;span&gt; animals.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;length&lt;&#x2F;span&gt;&lt;span&gt;; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;++&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  console.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(animals[i]);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Can you spot the problem? Instead of writing &lt;code&gt;i &amp;lt; animals.length&lt;&#x2F;code&gt; I wrote &lt;code&gt;i &amp;lt;= animals.length&lt;&#x2F;code&gt;, which would run the loop 4 times, including the case where &lt;code&gt;i&lt;&#x2F;code&gt;
is equal to 3. Of course there is another way to loop through an array in
JavaScript, which prevents &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Off-by-one_error&quot;&gt;&quot;off by one&quot;
errors&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; animal&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; of&lt;&#x2F;span&gt;&lt;span&gt; animals) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  console.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(animal);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As simple as this example is, I hope it clearly shows that, in fact, language
constructs &lt;em&gt;can&lt;&#x2F;em&gt; sometimes prevent logic errors. One, of course, could argue
that
this is not a logic error, but I agree with the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Logic_error&quot;&gt;Wikipedia
definition&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;In computer programming, a logic error is a bug in a program that causes it to operate incorrectly, but not to terminate abnormally (or crash). A logic error produces unintended or undesired output or other behaviour, although it may not immediately be recognized as such.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;In the case of the above code, the &lt;code&gt;for&lt;&#x2F;code&gt; loop version would not crash, it would
just print and extra line in the console saying &lt;code&gt;undefined&lt;&#x2F;code&gt;. Of course some
variations of off by one error could result in a runtime exception, but it&#x27;s not
always the case.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;digression-outside-mainstream-languages&quot;&gt;Digression: outside mainstream languages&lt;&#x2F;h3&gt;
&lt;p&gt;This post is mainly about mainstream programming languages and Rust specifically, but if we go outside
general-purpose mainstream languages there are lots of solutions for writing formally verified software.
Two examples are &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;agda&#x2F;agda&quot;&gt;Agda&lt;&#x2F;a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;coq.inria.fr&#x2F;&quot;&gt;Coq&lt;&#x2F;a&gt;.
There are also multiple tools for model verification and other formal
verification methods that can be used on top of programming languages, like &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;lamport.azurewebsites.net&#x2F;tla&#x2F;tla.html&quot;&gt;TLA+&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;That&#x27;s outside of the scope of this post, though, so let&#x27;s get back to Rust.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;logic-errors-turned-into-type-errors&quot;&gt;Logic errors turned into type errors&lt;&#x2F;h3&gt;
&lt;p&gt;When using a language like Rust you can sometimes end up with a
type related compilation error that would otherwise be a logic error at runtime.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s consider the sqlx library in Rust. Sqlx is a library for interacting with SQL
databases and an interesting feature it has is checking for type errors between
code and a database at compile time. For example imagine the following database
schema in SQL:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;sql&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;create table&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; authors&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  id &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;bigserial primary key not null&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  name text&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and the following structure in Rust:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Serialize&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Deserialize&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Debug&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Author&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; i64&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now imagine if we tried to fetch all the authors from a database and compile
a list of their names&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; authors&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; sqlx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;query_as!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Author&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; r#&amp;quot;SELECT * FROM authors&amp;quot;#&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;fetch_all&lt;&#x2F;span&gt;&lt;span&gt;(db)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        .await?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;&#x2F;&#x2F; map through the authors list returning their name and join all of the names&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;&#x2F;&#x2F; with a coma&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; authors_list&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; authors&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;author&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; author&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Even though it might look correct, this code will not compile.
It will result in a following error:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;11 | let authors = sqlx::query_as!(Author, r#&amp;quot;SELECT * FROM authors&amp;quot;#)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From&amp;lt;std::option::Option&amp;lt;std::string::String&amp;gt;&amp;gt;` is not implemented for `std::string::String`&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Why is it happening? The error says:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;the trait `From&amp;lt;std::option::Option&amp;lt;std::string::String&amp;gt;&amp;gt;` is not implemented for `std::string::String`&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which is maybe not the best error message of all time, but what it means in Rust is
that sqlx is trying to convert from &lt;code&gt;Option&amp;lt;String&amp;gt;&lt;&#x2F;code&gt; to &lt;code&gt;String&lt;&#x2F;code&gt; and there&#x27;s no way
to do it. In Rust there is no &lt;code&gt;null&lt;&#x2F;code&gt; type, so cases which could
potentially result in a &lt;code&gt;null&lt;&#x2F;code&gt; value usually use the &lt;code&gt;Option&lt;&#x2F;code&gt; type which can
either have &lt;code&gt;Some(...)&lt;&#x2F;code&gt; value or be &lt;code&gt;None&lt;&#x2F;code&gt;. Because the schema didn&#x27;t define the
&lt;code&gt;name&lt;&#x2F;code&gt; column as a &lt;code&gt;not null&lt;&#x2F;code&gt; column, it may contain &lt;code&gt;null&lt;&#x2F;code&gt; values. Thus sqlx
is correctly trying to map the &lt;code&gt;name&lt;&#x2F;code&gt; type into &lt;code&gt;Option&amp;lt;String&amp;gt;&lt;&#x2F;code&gt;. The problem is
the &lt;code&gt;name&lt;&#x2F;code&gt; field in the &lt;code&gt;Author&lt;&#x2F;code&gt; struct is of type &lt;code&gt;String&lt;&#x2F;code&gt; and thus it can&#x27;t accept
a possibility of a non existing value. In other words
Rust forces us to decide whether we actually want the name to be optional or
not. We have to either change it to optional in the program code or add a &lt;code&gt;not null&lt;&#x2F;code&gt;
constraint to disallow &lt;code&gt;null&lt;&#x2F;code&gt; values altogether.&lt;&#x2F;p&gt;
&lt;p&gt;This is a type of error that I&#x27;ve seen countless times in my career. Someone
forgets to add a &lt;code&gt;not null&lt;&#x2F;code&gt; constraint, everywhere in the code it&#x27;s assumed
that the column will always be present and eventually a &lt;code&gt;null&lt;&#x2F;code&gt; value ends up in
the table breaking &lt;em&gt;something&lt;&#x2F;em&gt;. Sometimes it&#x27;s a runtime error, sometimes a
logic error. In this particular case if we used a language like Ruby it might
result in a logic error with a weird looking output, like:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;N.K. Jemisin, , Goerge R.R. Martin&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h3 id=&quot;concurrency&quot;&gt;Concurrency&lt;&#x2F;h3&gt;
&lt;p&gt;As I mentioned in the beginning you can&#x27;t introduce data races in safe Rust.
That alone is a huge source of preventing logic errors. When writing concurrent
code it&#x27;s very easy to forget about guarding access to a variable shared
between threads, which might introduce a data race. It also happens that data
races are often also logic errors in high level languages. In C a data race is
considered undefined behavior, so you never know what will happen, but in a
language like Ruby or Python a data race might either result in a runtime
exception or a wrong answer.&lt;&#x2F;p&gt;
&lt;p&gt;Here, again, we can see that the language can prevent logic errors. What&#x27;s
more, I think that the way Rust prevents data races (ie. by enforcing ownership rules) makes it
easier to prevent race conditions as well. Let&#x27;s take a serious real life problem that
was caused by a data race and resulted in a race condition. At some point GitHub encountered a problem with
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.blog&#x2F;2021-03-08-github-security-update-a-bug-related-to-handling-of-authenticated-sessions&#x2F;&quot;&gt;users being randomly logged in as other
users&lt;&#x2F;a&gt;.
Although it was a small percent of users, it makes for a &lt;em&gt;very&lt;&#x2F;em&gt; bad PR. Imagine
owning a company and learning that a random person could have looked and cloned
all of your source code. Kinda scary. The blog post I linked explains the
situation quite well, but hopefully I can summarize it here so you don&#x27;t have to read it all (I recommend it, though!). The problem they
encountered was caused by Unicorn, a web server for Ruby applications.
It was reusing a hash map for keeping request data between requests for performance. Whenever a
request finished, the hash map was cleared and it was reused for the next request.
As one thread can only handle one request at a time it was
perfectly safe to do. The problem was, somewhere down the line a background
thread for handling exceptions was introduced in GitHub&#x27;s code. The thread modified session data, also
stored in the request hash map, which resulted in a possibility of a data race
and a race condition. It happened because the request hash map was passed to a separate thread
while Unicorn was also holding onto it and reusing it for the next request. The biggest issue in this situation is that even if
you looked at the code in Unicorn, you couldn&#x27;t have predicted this kind of
behavior. Because once you pass data to another method in Ruby, you have no
idea where it will land and what will happen to it (and btw, this is also true
for most of the mainstream languages, even statically typed like Go, when you pass
objects by a reference). You can&#x27;t prevent downstream code from modifying the
value you pass, and you can&#x27;t prevent it from passing it to another thread.&lt;&#x2F;p&gt;
&lt;p&gt;In Rust, on the other hand, you have tools for this kind of control. If you
create a request data hash map, and you define it as a simple &lt;code&gt;HashMap&lt;&#x2F;code&gt;, one can&#x27;t
simply send it to another thread for modification. It would have to be wrapped by
something like Arc + Mutex, ie. &lt;code&gt;Arc&amp;lt;Mutex&amp;lt;HashMap&amp;gt;&amp;gt;&lt;&#x2F;code&gt;. Now, I want to be
perfectly clear; it is possible to introduce exactly the same bug in Rust if you try
hard enough, but it would be highly unlikely, because:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;a HashMap for keeping request data would probably be kept on a
stack, so no allocation would have been needed, thus it&#x27;s unlikely anyone
would want to reuse it between requests&lt;&#x2F;li&gt;
&lt;li&gt;even &lt;em&gt;if&lt;&#x2F;em&gt; someone wanted to reuse it, it would have probably stayed as a
regular variable, not behind a Mutex because it was used in a single thread
only, so no one could pass it to another thread for modification while also
using it in the request handling thread&lt;&#x2F;li&gt;
&lt;li&gt;even &lt;em&gt;if&lt;&#x2F;em&gt; another thread was introduced during handling a request, you would have to &lt;em&gt;consider&lt;&#x2F;em&gt; what it
means to allow to share the variable with other threads. Or in other words, it
would have to be a very concious decision to say: this variable can be modified
on other threads while we also use it in the request handling thread&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Most importantly libraries don&#x27;t typically share resources behind a &lt;code&gt;Mutex&lt;&#x2F;code&gt;. They may
hand you the data for ownership, but then they couldn&#x27;t be using it for modification
afterwards. Or they could borrow you the data, but with a limited lifetime, so even if it&#x27;s
mutable you wouldn&#x27;t be able to pass it to a thread. Thus errors like the one with Unicorn are almost impossible to introduce in Rust.&lt;&#x2F;p&gt;
&lt;p&gt;I think that the biggest value here lays in having to explicitly consider data ownership.
Which also makes programming in Rust harder, because you have to actually make all of
those decisions, similarly to error handling. A lot of examples in Rust use &lt;code&gt;unwrap()&lt;&#x2F;code&gt;
just because handling all of the possible
errors can get rather verbose. Which also shows how much we &quot;sweep under the rug&quot; in languages
where error handling is dynamic and based on exceptions.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;access-control&quot;&gt;Access control&lt;&#x2F;h3&gt;
&lt;p&gt;As mentioned in the previous section Rust has tools for mutability and access control.
One of the types of bugs I&#x27;ve seen in in the wild was caused by mutating
stuff that shouldn&#x27;t be mutated. It often also resulted in a logic error.&lt;&#x2F;p&gt;
&lt;p&gt;It typically goes like this:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Pass a non-primitive type to a function&lt;&#x2F;li&gt;
&lt;li&gt;The function needs to calculate something and it treats the passed value as it owns it,
typically by using it for an intermediary cache or even storing the result.&lt;&#x2F;li&gt;
&lt;li&gt;The calling function doesn&#x27;t expect any mutation to happen.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Let&#x27;s look at the following Ruby code:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; calculate_project_cost&lt;&#x2F;span&gt;&lt;span&gt;(tasks, hourly_rate)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;  total_cost&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  tasks.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;each&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; do&lt;&#x2F;span&gt;&lt;span&gt; |task|&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;    estimated_hours&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; task[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;:estimated_hours&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;    # If a task is labeled as &amp;#39;complex&amp;#39;, increase the estimated_hours by 30%&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; task[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;:complexity&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;complex&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      task[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;:estimated_hours&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (estimated_hours &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1.3&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;round&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;    total_cost&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span&gt; task[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;:estimated_hours&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; hourly_rate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  total_cost&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this result we go through the list of tasks, with each task being described by complexity
and an estimated hours value. In order to make project estimation less risky, we want
to increase each of the complex tasks budget by 30% before calculating the total project
cost at our hourly rate. This function uses the original task as a holder of temporary value
thus mutating it. Of course it would be wiser to create a separate variable for the calculation
and even better use elements of functional programming like &lt;code&gt;map&lt;&#x2F;code&gt; and &lt;code&gt;sum&lt;&#x2F;code&gt; on the array,
but we all know what happens when people are under time pressure - mistakes happen and code that should
never have been written inevitably gets written. I&#x27;ve also seen serious production issues caused by this
type of bug. Usually it&#x27;s also hard to debug as the mutation can happen multiple levels deep with
the effects of the bug not being directly related to the modifying code.&lt;&#x2F;p&gt;
&lt;p&gt;In Rust when you pass a variable to a function that calculates something you typically only
pass a read pointer, or in Rust terminology, the function borrows it. A function calculating
a value that needs an ownership on input arguments or that needs a mutable borrow would
certainly raise some eyebrows. There are functions that calculate or generate data that accept
mutable references, but it&#x27;s usually done for saving the result only (for example it&#x27;s common to pass
a mutable reference to an array as a buffer to write to).&lt;&#x2F;p&gt;
&lt;p&gt;Some of the commenters pointed out that this is a contrived example, cause people would most
likely use &lt;code&gt;map&lt;&#x2F;code&gt; + &lt;code&gt;sum&lt;&#x2F;code&gt; instead of using &lt;code&gt;each&lt;&#x2F;code&gt;. And while, yes, this is a bit of a contrived
example, I can assure you, you can absolutely find this kind of code in production.
It took me 2 minutes of using
GitHub search to find &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Shopify&#x2F;ruby&#x2F;blob&#x2F;701ca070b4c6fd334543c69e863c40c70178521c&#x2F;yjit.rb#L403&quot;&gt;an example in the Ruby repository itself&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If you want to see a bug caused by unwanted mutation in the wild, here&#x27;s &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rails&#x2F;rails&#x2F;issues&#x2F;6115&quot;&gt;an issue in Rails&lt;&#x2F;a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rails&#x2F;rails&#x2F;commit&#x2F;c5176023a0585278c533610daf1eaf6ba7d19cd8&quot;&gt;a commit&lt;&#x2F;a&gt;
where I fixed it. I know it&#x27;s old, but it was easier to look through my commits that include &lt;code&gt;.dup&lt;&#x2F;code&gt; or &lt;code&gt;.freeze&lt;&#x2F;code&gt;
rather than all commits to the framework and I knew for a fact I fixed at least a few of issues like that.&lt;&#x2F;p&gt;
&lt;p&gt;The issue is that when a default model value in ActiveRecord was modified in place, it would also
modify the default values itself, which would result in this unexpected behaviour:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;author&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Author&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;new&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;author.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; # returns &amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;author.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;N.K. Jemisin&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; # we modify the name in place&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;another_author&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Author&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;new&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;another_author.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; # returns &amp;quot;N.K. Jemisin&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What&#x27;s especially interesting about this particular issue is that the author of the original
code already &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rails&#x2F;rails&#x2F;commit&#x2F;c5176023a0585278c533610daf1eaf6ba7d19cd8#diff-4411c7993e3f8ccfd8e09f44a81f10dfac3ea7dbd44cabd60b369040a00be324L168&quot;&gt;used &lt;code&gt;dup&lt;&#x2F;code&gt; on the default attributes&lt;&#x2F;a&gt;. It just wasn&#x27;t enough, because &lt;code&gt;dup&lt;&#x2F;code&gt; used on a hash will only duplicate the hash itself, not the values:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;h&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; foo:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; h.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;dup&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;h1[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;:foo&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;bar&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;h[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;:foo&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; # returns &amp;quot;bar&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;enums&quot;&gt;Enums&lt;&#x2F;h3&gt;
&lt;p&gt;This is an easy one. In Rust whenever we deal with a number of options the chances are
very high we will use an enum. In other languages it will a lot of the time be just
a loosely defined list of possible states. Consider the following code:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; process_payment&lt;&#x2F;span&gt;&lt;span&gt;(status)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  case&lt;&#x2F;span&gt;&lt;span&gt; status&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  when&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; :pending&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;    # process a pending payment&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  when&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; :authorized&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;    # process an authorized payment&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This works great until someone comes and adds an additional status for payments and then
you have to find all of the places where you could possibly be handling the payment status.
And while you don&#x27;t necessarily have to handle all of the status types in every single case,
there is a chance you might miss a place where you need to handle all of them unless the compiler
yells at you. This is not a contrived example, either. I&#x27;d bet most of Rails applications have at least
one case of an attribute like status being a symbol.&lt;&#x2F;p&gt;
&lt;p&gt;In Rust you would typically use an enum. Using an enum in a &lt;code&gt;match&lt;&#x2F;code&gt; statement will by default
result in an error if one of the options is not handled&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; PaymentStatus&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Pending&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Authorized&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; process_payment&lt;&#x2F;span&gt;&lt;span&gt;(status&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; PaymentStatus&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    match&lt;&#x2F;span&gt;&lt;span&gt; status {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        Pending&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; todo!&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        Authorized&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; todo!&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Of course it is &lt;em&gt;possible&lt;&#x2F;em&gt; to not use an enum for this case, but it&#x27;s rather uncommon in Rust,
because enum is so easy to use. You can emulate a similar behaviour in Ruby or other languages
that lack enums, but people tend to use solutions that are readily available and popular.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;beyond-the-language&quot;&gt;Beyond the language&lt;&#x2F;h3&gt;
&lt;p&gt;Although the Rust language alone already gives you quite a lot of guarantees and tools
for writing correct software, I would argue the value lies not only in the language. In this
one of the examples I gave uses the sqlx library, but there are other libraries that are widely used
and use similar context. For example, even though it&#x27;s not in the standard library, when dealing with JSON
or other serialization formats, you would almost always use the serde library, which makes
it easy to handle serialization with types.&lt;&#x2F;p&gt;
&lt;p&gt;But even outside of everyday use libraries there is a value of having a community that cares
about correctness. There are techniques that you can use to catch more bugs, like:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;quickcheck&quot;&gt;property based testing&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;model-checking&#x2F;kani&quot;&gt;proof verification&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rust-fuzz&#x2F;cargo-fuzz&quot;&gt;fuzzing&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;llogiq&#x2F;mutagen&quot;&gt;mutation testing&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;shuttle&quot;&gt;concurrency testing&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;PLSysSec&#x2F;haybale&quot;&gt;symbolic execution engines&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;And many more.&lt;&#x2F;p&gt;
&lt;p&gt;Some popular languages don&#x27;t have even the basic tools from that list (I&#x27;m looking at you, Ruby),
but what&#x27;s more a lot of these tools can&#x27;t be implemented for a lot of the languages. For example
proof verification would be extremely hard (if not impossible) to implement for dynamic languages.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h3&gt;
&lt;p&gt;I hope now you could agree with me that Rust gives you tools that help you prevent
at least some logic errors from happening, especially when you write idiomatic Rust. Of course it&#x27;s still
very much possible to shoot yourself in the foot in Rust, but I&#x27;d argue Rust is one of the
languages with a relatively small amount of footguns.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>(async) Rust doesn&#x27;t have to be hard</title>
        <published>2022-06-03T22:20:00+00:00</published>
        <updated>2022-06-03T22:20:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://itsallaboutthebit.com/async-simple/"/>
        <id>https://itsallaboutthebit.com/async-simple/</id>
        
        <content type="html" xml:base="https://itsallaboutthebit.com/async-simple/">&lt;p&gt;An article titled &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;hirrolot.github.io&#x2F;posts&#x2F;rust-is-hard-or-the-misery-of-mainstream-programming.html&quot;&gt;Rust Is Hard, Or: The Misery of Mainstream
Programming&lt;&#x2F;a&gt; came
out today and it&#x27;s getting a lot of attention. I have a feeling that it&#x27;s viewed
in a wrong context, so I&#x27;d like to comment on the issue raised there.&lt;&#x2F;p&gt;
&lt;p&gt;A lot of the comments to the article are in a tone of &quot;this is precisely the
reason why I don&#x27;t learn Rust, it&#x27;s just too hard&quot; or &quot;just don&#x27;t use async,
it&#x27;s too hard, otherwise Rust is not that bad&quot;. So I&#x27;ll start with a note for
all the people intimidated by the techniques the author is trying to use in
the post: when writing Rust code you almost never use this kind of stuff.
Especially when you&#x27;re writing applications, as opposed to libraries. And
especially if you&#x27;re not writing anything that is traditionally systems
programming, like a database or an operating system (and yes, Rust is great for
non systems programming too). I
really wish the author clearly pointed out that they write the article from a
point of view of a library author trying to come up with generic and flexible
APIs. Then maybe I wouldn&#x27;t feel the need to write
a rebuttal and shake my head in disbielief reading some of the comments on HN or
Reddit. But here we are, I guess.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ve been coding in Rust for a few years now and although I haven&#x27;t done any
advanced stuff, I wrote quite a bit of async code. In fact I learned Rust doing
async, I wrote pretty much only async code when using Rust at work
(unfortunately only for a few months) and I still write mostly async code. In
the article I&#x27;m responding to you can read:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;“Fearless concurrency” – a formally correct but nonetheless misleading statement. Yes, you no longer have fear of data races, but you have PAIN, much pain.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;What&#x27;s going on here? Why is the language causing so much pain voted as &quot;the
most loved language&quot; for like 7 years in a row in the Stack Overflow survey. Is
it the Stockholm syndrome? Do Rust devs like pain? Or maybe it&#x27;s a huge cult:
you get in and you try to lure in as many people as you can so they feel the
pain too.&lt;&#x2F;p&gt;
&lt;p&gt;The truth is: this is probably not the experience most people have with Rust.
It&#x27;s definitely not the experience I have with Rust. In fact, it&#x27;s vastly different.
Which doesn&#x27;t mean Hirrolot is wrong, it just means they
have a different perspective and different experience, probably because of the stuff they
needed to implement in Rust.&lt;&#x2F;p&gt;
&lt;p&gt;Is Rust totally painless? Of course not, I had my fair share of fights with the
compiler to try to make code compile. I definitely get frustrated from time
to time, but as a professional developer I can stomach &lt;em&gt;a lot&lt;&#x2F;em&gt; of compile time
frustration. I have much less tolerance for runtime frustration. Or in other
words: I prefer to spend a few hours being frustrated at a compiler rather than
a few hours debugging a production issue under pressure. This is a tradeoff, for
sure, so your milage may vary, but that&#x27;s just where I&#x27;m standing after 17 years
or so in the industry.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;async-code-in-applications&quot;&gt;async code in applications&lt;&#x2F;h3&gt;
&lt;p&gt;While I definitely agree async adds complexity to Rust and if you don&#x27;t need it,
it will be most probably easier to not use it, I don&#x27;t think it&#x27;s practical
to outright recommend not using async at all. But the question is: what do you
use async for? Most of the time it would be probably something like a web
service. And then most of the time your code will look something like:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;#[get(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&#x2F;todos&#x2F;{id:\\d+}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; todos_show_handler&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;    id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; web&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Path&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;i64&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span&gt;    pool&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; web&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Data&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;PgPool&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span&gt;    routing&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; web&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Data&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;RoutingService&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;TodoPresenter&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Error&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; todo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; sqlx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;query_as!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Todo&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; r#&amp;quot;SELECT * FROM todos WHERE id = $1&amp;quot;#&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt;id)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;fetch_one&lt;&#x2F;span&gt;&lt;span&gt;(pool&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;get_ref&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        .await?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; url&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; routing&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;todo_url&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;id);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;TodoPresenter&lt;&#x2F;span&gt;&lt;span&gt; { todo, url })&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is one of the handlers from &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;drogus&#x2F;todo-backend&quot;&gt;my
implementation&lt;&#x2F;a&gt; of a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;todobackend.com&#x2F;&quot;&gt;Todo
Backend&lt;&#x2F;a&gt;. It&#x27;s what you would do in vast majority of
your handlers: get an input from a request, fetch data from the database or
otherwise do some kind of computation and return the result. Notice that there
are no explicit lifetimes here and you need only a very limited undersanding of
the borrow checker. There is shared state here (in the form of the database pool
and the router &quot;service&quot;), but it&#x27;s shared using the &lt;code&gt;web::Data&lt;&#x2F;code&gt; type, which is
a type from actix-web very similar to &lt;code&gt;Arc&lt;&#x2F;code&gt;. In fact, that&#x27;s also how I think a
lot of async code would be written, also outside of web frameworks: instead of
dealing with lifetimes just use &lt;code&gt;Arc&lt;&#x2F;code&gt; or &lt;code&gt;Arc&amp;lt;Mutex&amp;lt;...&amp;gt;&amp;gt;&lt;&#x2F;code&gt;. That&#x27;s also why my first
article on this blog is about &lt;a href=&quot;&#x2F;arc-mutex&quot;&gt;&lt;code&gt;Arc&lt;&#x2F;code&gt; and &lt;code&gt;Mutex&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; - I think &lt;code&gt;Arc&lt;&#x2F;code&gt;
should be the first thing you try when you need to share stuff between threads,
not the last. Most of the time it will be fast enough, don&#x27;t worry. Then only if you need more performance or you think you can pull
off a better API you can try something more advanced, but otherwise? Why would
you make your life harder?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;if-you-always-use-arc-why-not-just-use-gced-language&quot;&gt;If you always use &lt;code&gt;Arc&lt;&#x2F;code&gt;, why not just use GCed language&lt;&#x2F;h3&gt;
&lt;p&gt;As my response to most of the ownership and lifetimes problems is to just slap
an &lt;code&gt;Arc&lt;&#x2F;code&gt; or an &lt;code&gt;Rc&lt;&#x2F;code&gt; there, I&#x27;ve heard quite a lot of variations of the question:
If you default to using &lt;code&gt;Arc&lt;&#x2F;code&gt;, won&#x27;t it be slower than using a language with a
sophisticated garbage collector? It&#x27;s a good question and if you really put
every single thing behind an &lt;code&gt;Arc&lt;&#x2F;code&gt;, it would probably be the case. Which would
indeed defeat some of the reasons to use Rust. Not all, mind you, you would
still get data races free programs and predictable memory usage, but yes, it
wouldn&#x27;t be optimal.&lt;&#x2F;p&gt;
&lt;p&gt;The good thing is, by saying &quot;just use &lt;code&gt;Arc&lt;&#x2F;code&gt;&quot; I&#x27;m not advocating to hide every
single variable behind an &lt;code&gt;Arc&lt;&#x2F;code&gt;. You mostly need it for shared state. Remember the
example from the previous section? It had two things behind an &lt;code&gt;Arc&lt;&#x2F;code&gt;, but the
rest was pretty much regular Rust code. So while, yes, every request to your
application would have to do at least two &lt;code&gt;Arc.clone()&lt;&#x2F;code&gt; operations in this
specific case, the
performance penalty is negligable. And unless you&#x27;re writing a &quot;close to the
metal&quot; layer 4 load balancer or something, you probably won&#x27;t care. It&#x27;s fine,
really.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-dynamic-dispatcher&quot;&gt;The dynamic dispatcher&lt;&#x2F;h3&gt;
&lt;p&gt;Hirrolot tries to implement a dynamic dispatcher in Rust. Not only that - it
takes a closure as an argument. This is one of the things that is just hard to
do in Rust. There are a few things like that, you might have heard about linked
lists already. It&#x27;s just one of the tradeoffs. So, yes, if you are planning to
create an API that holds async closures and does zero extra allocations on top of it,
you&#x27;re in for a bad, bad time. Fortunately, we can compromise, it&#x27;s not like
your manager will come to you and tell you &quot;we need this feature for yesterday.
and remember, no allocations and no &lt;code&gt;Arc&lt;&#x2F;code&gt;!&quot;. Or actually they might, but in this
case you&#x27;re probably working in embedded or on something very specific that
would be challenging to write even without Rust.&lt;&#x2F;p&gt;
&lt;p&gt;If I had to implement something like that, I would do it this way:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; async_trait&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;async_trait;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Update&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;#[async_trait]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Handler&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; update&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, update&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Update&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; ();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Default&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Dispatcher&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span&gt;    handlers&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Handler&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;14&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;15&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Dispatcher&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;17&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; push_handler&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;, handler&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Handler&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;18&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;handlers&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(handler);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;19&lt;&#x2F;span&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;21&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;22&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;&#x2F;&#x2F; example handler&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;23&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Foo&lt;&#x2F;span&gt;&lt;span&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;24&lt;&#x2F;span&gt;&lt;span&gt;#[async_trait]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;25&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Handler&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Foo&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;26&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; update&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, update&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Update&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;27&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Update: {:?}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, update);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;28&lt;&#x2F;span&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;29&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;30&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;31&lt;&#x2F;span&gt;&lt;span&gt;#[tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;main]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;32&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;33&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let mut&lt;&#x2F;span&gt;&lt;span&gt; dispatcher&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Dispatcher&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;34&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; handler&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Box&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Foo&lt;&#x2F;span&gt;&lt;span&gt; {});&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;35&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;36&lt;&#x2F;span&gt;&lt;span&gt;    dispatcher&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;push_handler&lt;&#x2F;span&gt;&lt;span&gt;(handler);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;37&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Instead of using closures I would make a &lt;code&gt;Handler&lt;&#x2F;code&gt; trait. No &lt;code&gt;Arc&lt;&#x2F;code&gt;, no
lifetimes, no worries. Of course there are potential issues here:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;I&#x27;m using &lt;code&gt;async_trait&lt;&#x2F;code&gt; here (which Hirrolot doesn&#x27;t like, but I don&#x27;t mind),
so a handler needs to be &lt;code&gt;Send&lt;&#x2F;code&gt;. If this is an issue you could also implement
a &lt;code&gt;!Send&lt;&#x2F;code&gt; version by using &lt;code&gt;#[async_trait(?Send)]&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;It needs more boilerplate than the closure version or even the &lt;code&gt;fn&lt;&#x2F;code&gt; version&lt;&#x2F;li&gt;
&lt;li&gt;We need to wrap handlers in a &lt;code&gt;Box&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;The interface uses &lt;code&gt;&amp;amp;self&lt;&#x2F;code&gt;, so if you need &lt;code&gt;&amp;amp;mut self&lt;&#x2F;code&gt; you would have to
change it&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;And probably more. But does it matter? If you&#x27;re writing application code (which
most of the Rust coders do), I don&#x27;t think so. All of those things are minor
annoyances and after you write the actual handlers you can adjust the API as
needed. These problems would only matter in any meaningful way if you were
writing a library that has to work for many different scenarios for as many
users as possible. So we get back to what I wrote in the beginning - problems of
library authors are different than problems of application developers.&lt;&#x2F;p&gt;
&lt;p&gt;Also this is a theoretical problem. If it was a practical problem to be solved
the API I come up with might be vastly different, but with the amount of
information I have I don&#x27;t think it matters much.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;library-code-vs-application-code&quot;&gt;Library code vs application code&lt;&#x2F;h3&gt;
&lt;p&gt;(this section was added after posting the initial version)&lt;&#x2F;p&gt;
&lt;p&gt;One of the interesting comments I got in response to this post was asking about
a distinction between library code and application code. Usually when writing
applications you tend to extract duplicated code and the generalized version
is often something you could release as a library. Does it mean that it doesn&#x27;t
happen in Rust. It does, but I think the constraints for internal libraries are
usually less strict than for external libraries. It&#x27;s also easier to compromise - you
roughly know what you will be using the code for, so you can write
it in a way that works for your use case. And if you don&#x27;t get it right the
first time? It&#x27;s not a big deal if all of the users of your library are in the
same company.&lt;&#x2F;p&gt;
&lt;p&gt;I know some of you will frown on this. If you don&#x27;t get it right it means
updates and updates are costly! Yes and no - I find it that in Rust refactoring
and updating code is much easier than in other languages I know - the type
system will guide you through it.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;no-problem-then&quot;&gt;No problem then?&lt;&#x2F;h3&gt;
&lt;p&gt;After reading the article this far you may be thinking I&#x27;m just an ignorant who
doesn&#x27;t see any issues with Rust. A fanboy that would write anything to defend
his favourite language. That&#x27;s definitely not the case. Well, at least the
part about writing anything to defend the language. I &lt;em&gt;am&lt;&#x2F;em&gt; a fanboy and Rust &lt;em&gt;is&lt;&#x2F;em&gt;
my favourite language.&lt;&#x2F;p&gt;
&lt;p&gt;I am aware of the issues with Rust. I know it sucks for library authors like
Hirrolot and I wish things were progressing faster. If we had GATs and Polonius
and many other awaited features, stuff like this would be hopefully easier. But
it takes a long time to implement these kind of features and I doubt we will get
any of that very soon.&lt;&#x2F;p&gt;
&lt;p&gt;The issue is, I think, that without proper context, Hirrolot&#x27;s article is
discouraging people from learning Rust, especially with the following sentiment
in there:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Still think that programming with borrow checker is easy and everybody can do it after some practice?&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;This is doing a lot of harm, because now beginners reading this might think: oh
no, if this is how Rust works, I better quit now, I will never understand the
language at this level. And it&#x27;s sad, because most people won&#x27;t have to. If
you&#x27;re a beginner in Rust you don&#x27;t need to understand everything. You don&#x27;t
need to write generic flexible APIs that will work for everyone and every
workload. You don&#x27;t need to write zero allocations code.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;can-t-i-be-a-library-author&quot;&gt;Can&#x27;t I be a library author?&lt;&#x2F;h3&gt;
&lt;p&gt;(this section was added after posting the initial version)&lt;&#x2F;p&gt;
&lt;p&gt;While this article tries to show that Rust is managable for app developers, it
might paint a grim picture for library developers. It&#x27;s indeed harder to write
Rust libraries than it is to write libraries in other languages. Getting the API
write usually means you must think about types, consider allocations, sometimes
deal with advanced lifetimes or traits. But it is doable too, you may need to
make compromises, though.&lt;&#x2F;p&gt;
&lt;p&gt;As you saw in both Hirrolot&#x27;s article and mine, you can design a relatively
simple API for a dispatcher handling updates. It may not be an ideal API. It may
have some constraints that it wouldn&#x27;t have in other languages, but it is
doable. Again, it&#x27;s a tradeoff.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h3&gt;
&lt;p&gt;If there is one thing I want you to get out of this article it&#x27;s this: Rust
doesn&#x27;t have to be very hard. It is a hard language, especially comparing to
languages like Javascript or Go, but it&#x27;s not unachivable. And it will only get
easier over time. Writing Rust in 2022 is a vastly different experience than
writing Rust when it hit 1.0 version and the Rust team is working hard on making
it more approachable.&lt;&#x2F;p&gt;
&lt;p&gt;So if you&#x27;re wondering if it&#x27;s a language for you I hope you give it a try!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Arc and Mutex in Rust</title>
        <published>2022-05-28T22:20:00+00:00</published>
        <updated>2022-05-28T22:20:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://itsallaboutthebit.com/arc-mutex/"/>
        <id>https://itsallaboutthebit.com/arc-mutex/</id>
        
        <content type="html" xml:base="https://itsallaboutthebit.com/arc-mutex/">&lt;p&gt;When writing concurrent Rust you will encounter &lt;code&gt;Arc&lt;&#x2F;code&gt; and &lt;code&gt;Mutex&lt;&#x2F;code&gt; types sooner
or later. And although &lt;code&gt;Mutex&lt;&#x2F;code&gt; might already sound familiar as it&#x27;s a concept
known in many languages, chances are you haven&#x27;t heard about &lt;code&gt;Arc&lt;&#x2F;code&gt; before Rust.
What&#x27;s more, you can&#x27;t fully understand these concepts without tying them to the
ownership model in Rust. This post is my take on understanding &lt;code&gt;Arc&lt;&#x2F;code&gt; and &lt;code&gt;Mutex&lt;&#x2F;code&gt;
in Rust.&lt;&#x2F;p&gt;
&lt;p&gt;Typically when you share data in a concurrent environment you either
share memory or pass data as messages.
You might often hear that passing messages (for
example by using channels) is a preferred way to handle concurrency, but in Rust
I don&#x27;t think the safety or correctness differences are as big as in other languages due to
the ownership model in Rust. Or more specifically: you can&#x27;t have a data race in safe
Rust. That&#x27;s why when I choose between message passing or memory sharing in
Rust, I do it mostly in relation to convinience, not safety.&lt;&#x2F;p&gt;
&lt;p&gt;If you choose to share data by sharing memory you will quickly encounter that
you can&#x27;t do much without &lt;code&gt;Arc&lt;&#x2F;code&gt; and &lt;code&gt;Mutex&lt;&#x2F;code&gt;. &lt;code&gt;Arc&lt;&#x2F;code&gt; is a smart pointer that let&#x27;s
you safely share a value between multiple threads. &lt;code&gt;Mutex&lt;&#x2F;code&gt; is a wrapper over
another type, which allows safe mutability across threads. In order
to fully understand these concepts, though, let&#x27;s dive into the ownership model.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;ownership-in-rust&quot;&gt;Ownership in Rust&lt;&#x2F;h3&gt;
&lt;p&gt;If you tried to distill the ownership model in Rust, you would probably get the
following points:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;a value can have only one owner&lt;&#x2F;li&gt;
&lt;li&gt;you can have multiple shared immutable references to a value&lt;&#x2F;li&gt;
&lt;li&gt;you can have only one mutable reference to a value&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Let&#x27;s see how it plays out. Given a &lt;code&gt;User&lt;&#x2F;code&gt; struct containing a &lt;code&gt;String&lt;&#x2F;code&gt; field
named &lt;code&gt;name&lt;&#x2F;code&gt; we create a thread and print out a message for the user:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;spawn;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span&gt;    name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; String&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; { name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;drogus&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;() };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Hello from the first thread {}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span&gt;    })&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;14&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So far so good, the program compiles and prints the message. Now imagine we need
to add a second thread that also has access to the &lt;code&gt;user&lt;&#x2F;code&gt; instance:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; { name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;drogus&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;() };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; t1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Hello from the first thread {}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; t2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Hello from the second thread {}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;    t1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span&gt;    t2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;14&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With this code we get the following error:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;error[E0382]: use of moved value: `user.name`&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  --&amp;gt; src&#x2F;main.rs:15:20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;11 |     let t1 = spawn(move || {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |                    ------- value moved into closure here&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;12 |         println!(&amp;quot;Hello from the first thread {}&amp;quot;, user.name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |                                                    --------- variable moved due to use in closure&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;15 |     let t2 = spawn(move || {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |                    ^^^^^^^ value used here after move&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;16 |         println!(&amp;quot;Hello from the second thread {}&amp;quot;, user.name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |                                                    --------- use occurs due to use in closure&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   = note: move occurs because `user.name` has type `String`, which does not implement the `Copy` trait&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What does the compiler want? The error reads &quot;use of moved value &lt;code&gt;user.name&lt;&#x2F;code&gt;&quot;.
The compiler is even nice enough to point us to specific places where the
problem occurs. We first move the value to the first thread on line 11 and
then we try to do the same thing with the second thread on line 15.
If you look at the ownership rules, this shouldn&#x27;t be surprising. A value can
have only one owner. With the current version of the code we need to
&quot;move&quot; the value to the first thread if we want to use it, and thus we can&#x27;t move it to
the other thread. It already changed ownership. But we don&#x27;t mutate the data,
right? Which means we can have multiple shared references. Let&#x27;s try that.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; { name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;drogus&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;() };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; t1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Hello from the first thread {}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; t2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Hello from the second thread {}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;    t1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span&gt;    t2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;14&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I removed the &lt;code&gt;move&lt;&#x2F;code&gt; keyword in the thread closures and I made the threads
borrow the &lt;code&gt;user&lt;&#x2F;code&gt; value immutably, or in other words get a shared reference,
which is represented by the ampersand.
With this code we get the following:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;error[E0373]: closure may outlive the current function, but it borrows `user.name`, which is owned by the current function&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  --&amp;gt; src&#x2F;main.rs:15:20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;15 |     let t2 = spawn(|| {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |                    ^^ may outlive borrowed value `user.name`&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;16 |         println!(&amp;quot;Hello from the first thread {}&amp;quot;, &amp;amp;user.name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |                                                     --------- `user.name` is borrowed here&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;note: function requires argument type to outlive `&amp;#39;static`&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  --&amp;gt; src&#x2F;main.rs:15:14&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;15 |       let t2 = spawn(|| {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |  ______________^&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;16 | |         println!(&amp;quot;Hello from the second thread {}&amp;quot;, &amp;amp;user.name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;17 | |     });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   | |______^&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;help: to force the closure to take ownership of `user.name` (and any other referenced variables), use the `move` keyword&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;15 |     let t2 = spawn(move || {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |                    ++++&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now the error says that the closure can outlive the function. In other words the
Rust compiler can&#x27;t guarantee that the closure in the thread will finish before
the &lt;code&gt;main()&lt;&#x2F;code&gt; function finishes. Threads are borrowing the user struct, but
it&#x27;s still owned by the main function. In this scenario if the main function
finishes, the user struct goes out of scope and the memory is dropped. Thus if
it was allowed to share the value with threads in that manner, there could be a
scenario when a thread is trying to read freed memory.
Which is an undefined behaviour and we certainly don&#x27;t want that.&lt;&#x2F;p&gt;
&lt;p&gt;The note also says that it may help to move the variable &lt;code&gt;user&lt;&#x2F;code&gt; to the thread in
order to avoid borrowing, but we&#x27;re just coming from that scenario, so it&#x27;s
no good. Now, there are two easy solutions to fix this and one of them is to use
&lt;code&gt;Arc&lt;&#x2F;code&gt;, but let&#x27;s explore the other solution first: scoped threads.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;scoped-threads&quot;&gt;Scoped threads&lt;&#x2F;h3&gt;
&lt;p&gt;Scoped
threads is a feature available either from an excellent &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;crossbeam&#x2F;latest&#x2F;crossbeam&#x2F;fn.scope.html&quot;&gt;crossbeam
crate&lt;&#x2F;a&gt;
or as an &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;nightly&#x2F;std&#x2F;thread&#x2F;fn.scope.html&quot;&gt;experimental nightly feature&lt;&#x2F;a&gt; in Rust.
For the purpose of this article I will use crossbeam, but the API is very similar for
both versions. After adding &lt;code&gt;crossbeam = &quot;0.8&quot;&lt;&#x2F;code&gt; to dependencies in
&lt;code&gt;Cargo.toml&lt;&#x2F;code&gt; this code will work without problems&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; crossbeam&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;scope;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span&gt;    name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;        name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;drogus&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;    };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    scope&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;14&lt;&#x2F;span&gt;&lt;span&gt;        s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;15&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;            println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Hello from the first thread {}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;        });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;17&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;18&lt;&#x2F;span&gt;&lt;span&gt;        s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;19&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;            println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Hello from the second thread {}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span&gt;        });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;21&lt;&#x2F;span&gt;&lt;span&gt;    })&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;22&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;23&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The way scoped threads work is all of the threads created in the scope are
guaranteed to be finished before the &lt;code&gt;scope&lt;&#x2F;code&gt; closure finishes. Or in other words&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;before the scoped closure goes out of scope, the threads are joined and
awaited to be finished. Thanks to that the compiler knows that none of the
borrows will outlive the owner.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;One interesting thing to note here is that as a human reader we would have
interpreted both of these programs as valid. In the version that Rust
rejects we join both threads before the &lt;code&gt;main()&lt;&#x2F;code&gt; function finishes, so
it would be actually safe to share the user value with the threads. This is,
unfortunately, something that you may encounter when writing in Rust. Writing
a compiler that would accept all of the valid programs is not possible, thus
we&#x27;re left with the next best thing: a compiler that will reject all invalid
programs at a cost of being overly strict. Scoped threads is a future written
specifically to allow us to write this code in a way that compiler can accept.&lt;&#x2F;p&gt;
&lt;p&gt;As useful as the scoped threads feature is, however, you can&#x27;t always use it, for example when writing async code.
Let&#x27;s get to the &lt;code&gt;Arc&lt;&#x2F;code&gt; solution then.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;arc-to-the-rescue&quot;&gt;Arc to the rescue&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;Arc&lt;&#x2F;code&gt; is a smart pointer enabling sharing data between threads. Its
name is a shortcut for &quot;atomic reference counter&quot;. The way &lt;code&gt;Arc&lt;&#x2F;code&gt; works is
essentially to wrap a value we&#x27;re trying to share and act as a pointer to it.
&lt;code&gt;Arc&lt;&#x2F;code&gt; keeps track of all of the copies of the pointer and as soon as
the last pointer goes out of scope it can safely free the memory. The solution
to our small problem with &lt;code&gt;Arc&lt;&#x2F;code&gt; would look something like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;spawn;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sync&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;    name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; String&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user_original&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt; { name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;drogus&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;() });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; user_original&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; t1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;14&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Hello from the first thread {}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;15&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;16&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;17&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; user_original&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;18&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; t2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;19&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Hello from the first thread {}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;21&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;22&lt;&#x2F;span&gt;&lt;span&gt;    t1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;23&lt;&#x2F;span&gt;&lt;span&gt;    t2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;24&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s go through it step by step. First, on line 10, we create a user value,
but we also wrap it with an &lt;code&gt;Arc&lt;&#x2F;code&gt;. Now the value is stored in memory and &lt;code&gt;Arc&lt;&#x2F;code&gt;
acts only as a pointer. Whenever we clone the &lt;code&gt;Arc&lt;&#x2F;code&gt; we only clone the
reference, not the user value itself. On lines 12 and 17 we clone the &lt;code&gt;Arc&lt;&#x2F;code&gt;
and thus a copy of the pointer is moved to each of the threads. As you can see
&lt;code&gt;Arc&lt;&#x2F;code&gt; allows us to share the data regardless of the lifetimes. In this example
we will have three pointers to the user value. One created when an &lt;code&gt;Arc&lt;&#x2F;code&gt;
is created, one created by cloning before starting the first thread and moved to the first thread
and one created by cloning before starting the second thread and moved the first
thread. As long as any of these pointers is alive, Rust will not free the
memory. But when both the threads and the main function finish, all of the &lt;code&gt;Arc&lt;&#x2F;code&gt;
pointers will get out of scope, dropped and as soon as the last one drops, it
will also drop the user value.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;send-and-sync&quot;&gt;&lt;code&gt;Send&lt;&#x2F;code&gt; and &lt;code&gt;Sync&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;Let&#x27;s go a bit deeper, though. If you look at the &lt;code&gt;Arc&lt;&#x2F;code&gt; documentation, you will
see it &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;sync&#x2F;struct.Arc.html#impl-Send&quot;&gt;implements &lt;code&gt;Send&lt;&#x2F;code&gt; and &lt;code&gt;Sync&lt;&#x2F;code&gt; traits&lt;&#x2F;a&gt;,
but &lt;em&gt;only&lt;&#x2F;em&gt; if the wrapped type also implements both &lt;code&gt;Send&lt;&#x2F;code&gt; and &lt;code&gt;Sync&lt;&#x2F;code&gt;. In order
to understand what it means and why it&#x27;s implemented this way let&#x27;s start by
defining &lt;code&gt;Send&lt;&#x2F;code&gt; and &lt;code&gt;Sync&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The Rustonomicon &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;nomicon&#x2F;send-and-sync.html&quot;&gt;defines &lt;code&gt;Send&lt;&#x2F;code&gt; and &lt;code&gt;Sync&lt;&#x2F;code&gt; as&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;A type is Send if it is safe to send it to another thread.&lt;&#x2F;li&gt;
&lt;li&gt;A type is Sync if it is safe to share between threads (T is Sync if and only if &amp;amp;T is Send).&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Feel free to read about these traits on Rustonomicon, but I&#x27;ll also try to share
my understanding here. Both &lt;code&gt;Send&lt;&#x2F;code&gt; and &lt;code&gt;Sync&lt;&#x2F;code&gt; are traits acting as markers -
they don&#x27;t have any implemented methods nor they require you to implement
anything. What they allow is to notify the compiler about a type&#x27;s ability to be
shared or sent between threads. Let&#x27;s start with &lt;code&gt;Send&lt;&#x2F;code&gt;, which is a bit more
straightforward. What it means is that you can&#x27;t send type which is &lt;code&gt;!Send&lt;&#x2F;code&gt;
(read: not &lt;code&gt;Send&lt;&#x2F;code&gt;) to another thread. For example you can&#x27;t send it through a
channel nor can you move it to a thread. For example this code will not compile:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;#![feature(negative_impls)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Foo&lt;&#x2F;span&gt;&lt;span&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;impl !&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Send&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Foo&lt;&#x2F;span&gt;&lt;span&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; foo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Foo&lt;&#x2F;span&gt;&lt;span&gt; {};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        dbg!&lt;&#x2F;span&gt;&lt;span&gt;(foo);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;Send&lt;&#x2F;code&gt; and &lt;code&gt;Sync&lt;&#x2F;code&gt; are autoderived, meaning that for example if all of the
attributes of a type are &lt;code&gt;Send&lt;&#x2F;code&gt;, the type will also be &lt;code&gt;Send&lt;&#x2F;code&gt;. This code uses an
experimental feature called &lt;code&gt;negative_impls&lt;&#x2F;code&gt;, which lets us tell the compiler &quot;I
explicitly want to mark this type as &lt;code&gt;!Send&lt;&#x2F;code&gt;&quot;. Trying to compile this code
will result in an error:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;`Foo` cannot be sent between threads safely&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The same would happen if you created a channel to send &lt;code&gt;foo&lt;&#x2F;code&gt; to a thread. So now
what with &lt;code&gt;Arc&lt;&#x2F;code&gt;? As you might have guessed it will also not help, this will also
error out in the same way (and the same would be true for a &lt;code&gt;!Sync&lt;&#x2F;code&gt; type as
&lt;code&gt;Arc&lt;&#x2F;code&gt; needs both traits):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;#![feature(negative_impls)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Foo&lt;&#x2F;span&gt;&lt;span&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;impl !&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Send&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Foo&lt;&#x2F;span&gt;&lt;span&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; foo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Foo&lt;&#x2F;span&gt;&lt;span&gt; {});&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        dbg!&lt;&#x2F;span&gt;&lt;span&gt;(foo);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, why is that the case? Isn&#x27;t &lt;code&gt;Arc&lt;&#x2F;code&gt; supposed to be wrapping our type and give it more
capabilities? While this is certainly true, &lt;code&gt;Arc&lt;&#x2F;code&gt; can&#x27;t magically make our type
threadsafe. I will give you a more in-depth example to show you why at the end
of this article, but for now let&#x27;s continue with learning on how to use these
types.&lt;&#x2F;p&gt;
&lt;p&gt;What we learned so far is this: &lt;code&gt;Arc&lt;&#x2F;code&gt; enables us to share references to types
that are &lt;code&gt;Send + Sync&lt;&#x2F;code&gt; between threads without us having to worry about
lifetimes (because it&#x27;s not a regular reference, but rather a smart pointer).&lt;&#x2F;p&gt;
&lt;h3 id=&quot;modifying-data-with-mutex&quot;&gt;Modifying data with Mutex&lt;&#x2F;h3&gt;
&lt;p&gt;Now let&#x27;s talk about &lt;code&gt;Mutex&lt;&#x2F;code&gt;. Mutexes in many languages are treated like
semaphores. You create a mutex object and you can guard a certain piece (or
pieces) of the code with the mutex in a way that only one thread at a time can
access the guarded place. In Rust &lt;code&gt;Mutex&lt;&#x2F;code&gt; behaves more like a wrapper. It
consumes the underlying value and let&#x27;s you access it only after locking the
mutex. Typically &lt;code&gt;Mutex&lt;&#x2F;code&gt; is used with conjunction with &lt;code&gt;Arc&lt;&#x2F;code&gt; to make it easier
to share it between threads. Let&#x27;s look at the following example:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;time&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Duration&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;{thread,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;sleep};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sync&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Mutex&lt;&#x2F;span&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;    name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; String&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user_original&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Mutex&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt; { name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;drogus&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;) }));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; user_original&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; t1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;14&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        let mut&lt;&#x2F;span&gt;&lt;span&gt; locked_user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;lock&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;15&lt;&#x2F;span&gt;&lt;span&gt;        locked_user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;piotr&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;        &#x2F;&#x2F; after locked_user goes out of scope, mutex will be unlocked again,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;17&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;        &#x2F;&#x2F; but you can also explicitly unlock it with:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;18&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;        &#x2F;&#x2F; drop(locked_user);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;19&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;21&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; user_original&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;22&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; t2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;23&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        sleep&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Duration&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;from_millis&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;24&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;25&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;        &#x2F;&#x2F; it will print: Hello piotr&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;26&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Hello {}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;lock&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;27&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;28&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;29&lt;&#x2F;span&gt;&lt;span&gt;    t1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span&gt;    t2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;31&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s go over it. in the first line of the &lt;code&gt;main()&lt;&#x2F;code&gt; function we create an
instance of the &lt;code&gt;User&lt;&#x2F;code&gt; struct and we wrap it with a &lt;code&gt;Mutex&lt;&#x2F;code&gt; and an &lt;code&gt;Arc&lt;&#x2F;code&gt;. With
an &lt;code&gt;Arc&lt;&#x2F;code&gt; we can easily clone the pointer and thus share the mutex between
threads. In the 13th line you can see the mutex is locked and since that moment
the underlying value can be used exclusively by this thread. Then we modify the
value in the next line. The mutex is unlocked once the locked guard goes out of
scope or we manually drop it with &lt;code&gt;drop(locked_user)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In the second thread we wait 10ms and print the name, which should be the name
updated in the first thread. This time locking is done in one line, so the mutex
will be dropped in the same statement.&lt;&#x2F;p&gt;
&lt;p&gt;One more thing that is worth mentioning is the &lt;code&gt;unwrap()&lt;&#x2F;code&gt; method we call after
&lt;code&gt;lock()&lt;&#x2F;code&gt;. &lt;code&gt;Mutex&lt;&#x2F;code&gt; from the standard library has a notion of being poisoned.
If a thread panics while the mutex is locked we can&#x27;t be certain if the value
inside &lt;code&gt;Mutex&lt;&#x2F;code&gt; is still valid and thus the default behaviour is to return an
error instead of a guard. So &lt;code&gt;Mutex&lt;&#x2F;code&gt; can either return an &lt;code&gt;Ok()&lt;&#x2F;code&gt; variant with
the wrapped value as an argument or an error. You can read more about it &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;sync&#x2F;struct.Mutex.html#poisoning&quot;&gt;in the
docs&lt;&#x2F;a&gt;. In
general leaving &lt;code&gt;unrwap()&lt;&#x2F;code&gt; methods in the production code is not recommended,
but in case of &lt;code&gt;Mutex&lt;&#x2F;code&gt; it might be a valid strategy - if a mutex has been
poisoned we might decide that the application state is invalid and crash the
application.&lt;&#x2F;p&gt;
&lt;p&gt;Another interesting thing about &lt;code&gt;Mutex&lt;&#x2F;code&gt; is that as long as a type inside the
&lt;code&gt;Mutex&lt;&#x2F;code&gt; is &lt;code&gt;Send&lt;&#x2F;code&gt;, &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;sync&#x2F;struct.Mutex.html#impl-Sync&quot;&gt;&lt;code&gt;Mutex&lt;&#x2F;code&gt; will also be
&lt;code&gt;Sync&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. This
is because &lt;code&gt;Mutex&lt;&#x2F;code&gt; ensures that only one thread can get access to the underlying
value and thus it&#x27;s safe to share &lt;code&gt;Mutex&lt;&#x2F;code&gt; between threads.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;mutex-add-sync-to-a-send-type&quot;&gt;&lt;code&gt;Mutex&lt;&#x2F;code&gt;: add &lt;code&gt;Sync&lt;&#x2F;code&gt; to a &lt;code&gt;Send&lt;&#x2F;code&gt; type&lt;&#x2F;h3&gt;
&lt;p&gt;As you may remember from the beginning of the article, &lt;code&gt;Arc&lt;&#x2F;code&gt; needs an underlying
type to be &lt;code&gt;Send + Sync&lt;&#x2F;code&gt; in order for &lt;code&gt;Arc&lt;&#x2F;code&gt; to be &lt;code&gt;Send + Sync&lt;&#x2F;code&gt; too. &lt;code&gt;Mutex&lt;&#x2F;code&gt;
only requires and underlying type to be &lt;code&gt;Send&lt;&#x2F;code&gt; in order for &lt;code&gt;Mutex&lt;&#x2F;code&gt; to be `Send&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Sync&lt;code&gt;. In other words &lt;&#x2F;code&gt;Mutex&lt;code&gt;will make a&lt;&#x2F;code&gt;!Sync&lt;code&gt;type&lt;&#x2F;code&gt;Sync`, so you can
share it between threads and modify it too.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;mutex-without-arc&quot;&gt;&lt;code&gt;Mutex&lt;&#x2F;code&gt; without &lt;code&gt;Arc&lt;&#x2F;code&gt;?&lt;&#x2F;h3&gt;
&lt;p&gt;An interesting question that you may ask is if &lt;code&gt;Mutex&lt;&#x2F;code&gt; can be used without
&lt;code&gt;Arc&lt;&#x2F;code&gt;. I encourage you to think about it a little before reading further: what
does it mean that &lt;code&gt;Mutex&lt;&#x2F;code&gt; is &lt;code&gt;Send + Sync&lt;&#x2F;code&gt; for types that are &lt;code&gt;Send&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;If you get back to the first part of this post you can see what it means for the
&lt;code&gt;Arc&lt;&#x2F;code&gt; type and in case of &lt;code&gt;Mutex&lt;&#x2F;code&gt; it means a very similar thing. If we can use
something like scope threads it&#x27;s entirely possible to use &lt;code&gt;Mutex&lt;&#x2F;code&gt; without
&lt;code&gt;Arc&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; crossbeam&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;scope;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sync&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Mutex&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;sleep,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; time&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Duration&lt;&#x2F;span&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;    name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Mutex&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;        name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;drogus&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;13&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;14&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    scope&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;15&lt;&#x2F;span&gt;&lt;span&gt;        s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;            user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;lock&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;piotr&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;17&lt;&#x2F;span&gt;&lt;span&gt;        });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;18&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;19&lt;&#x2F;span&gt;&lt;span&gt;        s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;            sleep&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Duration&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;from_millis&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;21&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;22&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;            &#x2F;&#x2F; should print: Hello piotr&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;23&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;            println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Hello {}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;lock&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;24&lt;&#x2F;span&gt;&lt;span&gt;        });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;25&lt;&#x2F;span&gt;&lt;span&gt;    })&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;26&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;27&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this program we achieve the same goal. We are accessing a value behind a mutex in
two separate threads, but we share mutexes by reference and not by using an &lt;code&gt;Arc&lt;&#x2F;code&gt;. But
again, this is not always possible, for example in async code, so &lt;code&gt;Mutex&lt;&#x2F;code&gt; is
very often used along with &lt;code&gt;Arc&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h3&gt;
&lt;p&gt;I&#x27;m hoping that in this article I helped you understand what are &lt;code&gt;Arc&lt;&#x2F;code&gt; and
&lt;code&gt;Mutex&lt;&#x2F;code&gt; types in Rust and how to use them. To sum it up I would say you would
typically use &lt;code&gt;Arc&lt;&#x2F;code&gt; whenever you want to share data between threads and you
can&#x27;t do so using regular references. You would also use &lt;code&gt;Mutex&lt;&#x2F;code&gt; if you need to
modify data you share between threads. And then you would use
&lt;code&gt;Arc&amp;lt;Mutex&amp;lt;...&amp;gt;&amp;gt;&lt;&#x2F;code&gt; whenever you want to modify data you share between threads and
you can&#x27;t share a mutex using references.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;bonus-why-arc-needs-type-to-be-sync&quot;&gt;Bonus: Why &lt;code&gt;Arc&lt;&#x2F;code&gt; needs type to be &lt;code&gt;Sync&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;Now let&#x27;s get back to the question of &quot;why &lt;code&gt;Arc&lt;&#x2F;code&gt; needs the underlying type to be
both &lt;code&gt;Send&lt;&#x2F;code&gt; and &lt;code&gt;Sync&lt;&#x2F;code&gt; to mark it as &lt;code&gt;Send&lt;&#x2F;code&gt; and &lt;code&gt;Sync&lt;&#x2F;code&gt;). Feel free to ignore
this last section, though, it&#x27;s not really needed for you to use &lt;code&gt;Arc&lt;&#x2F;code&gt; and
&lt;code&gt;Mutex&lt;&#x2F;code&gt; in your code. It might help you understand markter traits a bit better.&lt;&#x2F;p&gt;
&lt;p&gt;Lets take &lt;code&gt;Cell&lt;&#x2F;code&gt; as an example. &lt;code&gt;Cell&lt;&#x2F;code&gt; wraps another type and
enables &quot;interior mutability&quot; or in other words it allows us to modify a value
inside an immutable struct. &lt;code&gt;Cell&lt;&#x2F;code&gt; is &lt;code&gt;Send&lt;&#x2F;code&gt;, but it&#x27;s &lt;code&gt;!Sync&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;An example of using &lt;code&gt;Cell&lt;&#x2F;code&gt; would be:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;cell&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Cell&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span&gt;    age&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Cell&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;usize&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; { age&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Cell&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span&gt;) };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;    user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;age&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;36&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;    &#x2F;&#x2F; will print: Age: 36&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Age: {}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;age&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;14&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;Cell&lt;&#x2F;code&gt; is useful in some situations, but it isn&#x27;t thread safe or in other words
it&#x27;s &lt;code&gt;!Sync&lt;&#x2F;code&gt;. If you somehow shared a value wrapped in a cell between multiple threads you could modify the
same place in memory from two threads, for example:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;&#x2F;&#x2F; this example will not compile, `Cell` is `!Sync` and thus&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;&#x2F;&#x2F; `Arc` will be `!Sync` and `!Send`&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;cell&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Cell&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;    age&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Cell&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;usize&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user_original&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt; { age&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Cell&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span&gt;) });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; user_original&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;14&lt;&#x2F;span&gt;&lt;span&gt;        user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;age&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;15&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;16&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;17&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; user_original&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;18&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;19&lt;&#x2F;span&gt;&lt;span&gt;        user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;age&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;21&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If that worked, it could result in an undefined behaviour. That&#x27;s why &lt;code&gt;Arc&lt;&#x2F;code&gt; will
not work with any type that is not &lt;code&gt;Send&lt;&#x2F;code&gt; &lt;em&gt;nor&lt;&#x2F;em&gt; &lt;code&gt;Sync&lt;&#x2F;code&gt;. At the same time &lt;code&gt;Cell&lt;&#x2F;code&gt;
is &lt;code&gt;Send&lt;&#x2F;code&gt;, meaning that you can send it between threads. Why is that? Sending,
or in other words moving, will not make a value accessible from more than one
thread, it will have to always be only one thread. Once you move it to another,
the previous thread doesn&#x27;t own the value anymore. With that in mind, we can
always mutate a &lt;code&gt;Cell&lt;&#x2F;code&gt; locally.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;bonus-why-arc-needs-type&quot;&gt;Bonus: why &lt;code&gt;Arc&lt;&#x2F;code&gt; needs type&lt;&#x2F;h3&gt;
&lt;p&gt;At this point you might also wonder why &lt;code&gt;Arc&lt;&#x2F;code&gt; will not provide the &lt;code&gt;Send&lt;&#x2F;code&gt; trait
for a &lt;code&gt;!Send&lt;&#x2F;code&gt; type, either. One of the types in Rust which is &lt;code&gt;!Send&lt;&#x2F;code&gt; is &lt;code&gt;Rc&lt;&#x2F;code&gt;.
&lt;code&gt;Rc&lt;&#x2F;code&gt; is a cousin of &lt;code&gt;Arc&lt;&#x2F;code&gt;, but it&#x27;s not &quot;atomic&quot;, &lt;code&gt;Rc&lt;&#x2F;code&gt; expands to just
&quot;reference counter&quot;. Its role is pretty much the same as &lt;code&gt;Arc&lt;&#x2F;code&gt;, but it can only
be used in a single thread. Not only it can&#x27;t be shared between threads, but
also it can&#x27;t be moved between threads. Let&#x27;s see why.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;&#x2F;&#x2F; this code won&amp;#39;t compile, Rc is !Send and !Sync&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Rc&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; foo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; foo_clone&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; foo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        dbg!&lt;&#x2F;span&gt;&lt;span&gt;(foo_clone);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; foo_clone&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; foo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;14&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        dbg!&lt;&#x2F;span&gt;&lt;span&gt;(foo_clone);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;15&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This example won&#x27;t compile, because &lt;code&gt;Rc&lt;&#x2F;code&gt; is &lt;code&gt;!Sync + !Send&lt;&#x2F;code&gt;. Its internal
counter is not atomic and thus sharing it between threads could result in an
inaccurate count of references. Now imagine that &lt;code&gt;Arc&lt;&#x2F;code&gt; would make &lt;code&gt;!Send&lt;&#x2F;code&gt; types
&lt;code&gt;Send&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Rc&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sync&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;    name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;unsafe impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Send&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;unsafe impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Sync&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; foo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span&gt;        name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;drogus&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;14&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;15&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; foo_clone&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; foo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;17&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;18&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span&gt; name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; foo_clone&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;19&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;21&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; foo_clone&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; foo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;22&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;23&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span&gt; name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; foo_clone&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;24&lt;&#x2F;span&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #1B1F234D;&quot;&gt;25&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This example will compile, but it&#x27;s wrong, please don&#x27;t do it in your actual
code! In here I define a &lt;code&gt;User&lt;&#x2F;code&gt; struct, which holds an &lt;code&gt;Rc&lt;&#x2F;code&gt; inside. Because
&lt;code&gt;Send&lt;&#x2F;code&gt; and &lt;code&gt;Sync&lt;&#x2F;code&gt; are autoderived and &lt;code&gt;Rc&lt;&#x2F;code&gt; is &lt;code&gt;!Send + !Sync&lt;&#x2F;code&gt;, the &lt;code&gt;User&lt;&#x2F;code&gt; struct
should also be &lt;code&gt;!Send + !Sync&lt;&#x2F;code&gt;, but we can explicitly tell the compiler to mark
it differently, in this case &lt;code&gt;Send + Sync&lt;&#x2F;code&gt;, using &lt;code&gt;unsafe impl&lt;&#x2F;code&gt; syntax.&lt;&#x2F;p&gt;
&lt;p&gt;Now you can clearly see what would go wrong if &lt;code&gt;Arc&lt;&#x2F;code&gt; allowed &lt;code&gt;!Send&lt;&#x2F;code&gt; types to be
moved between threads. In the example &lt;code&gt;Arc&lt;&#x2F;code&gt; clones are moved into separate
threads and then nothing is stopping us from cloning the &lt;code&gt;Rc&lt;&#x2F;code&gt; type. And because
&lt;code&gt;Rc&lt;&#x2F;code&gt; type is not thread safe, it could result in an inacurate count of
references and thus could either free memory too soon or it could not free it at
all even though it should.&lt;&#x2F;p&gt;
&lt;p&gt;I know that this article is a long one, so kudos too all of you that got it all
the way here, thanks!&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
