<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Andrew Lilley Brinker — Rust</title>
  <subtitle>Posts about Rust</subtitle>
  <id>https://www.alilleybrinker.com/topics/rust/atom.xml</id>
  <link rel="self" type="application/atom+xml" href="https://www.alilleybrinker.com/topics/rust/atom.xml"/>
  <link rel="alternate" type="text/html" href="https://www.alilleybrinker.com/topics/rust/"/>
  <updated>2026-01-16T12:00:00.000Z</updated>
  <author><name>Andrew Lilley Brinker</name><uri>https://www.alilleybrinker.com/</uri></author>
  <entry>
    <title>Rust’s Culture of Semantic Precision</title>
    <id>https://www.alilleybrinker.com/blog/rusts-culture-of-semantic-precision/</id>
    <link rel="alternate" type="text/html" href="https://www.alilleybrinker.com/blog/rusts-culture-of-semantic-precision/"/>
    <published>2026-01-16T12:00:00.000Z</published>
    <updated>2026-01-16T12:00:00.000Z</updated>
    <summary>Explaining how Rust’s culture of semantic precision pays dividends for system robustness while complicating adoption in projects like Linux.</summary>
    <content type="html" xml:base="https://www.alilleybrinker.com/blog/rusts-culture-of-semantic-precision/">&lt;p&gt;Last week, LWN published &lt;a href=&quot;https://lwn.net/SubscriberLink/1053142/8ec93e58d5d3cc06/&quot;&gt;“&lt;code&gt;READ_ONCE()&lt;/code&gt;, &lt;code&gt;WRITE_ONCE()&lt;/code&gt;, but not for Rust”&lt;/a&gt;,
which described an issue encountered as part of integrating more Rust into
the Linux kernel: namely that the &lt;code&gt;READ_ONCE&lt;/code&gt; and &lt;code&gt;WRITE_ONCE&lt;/code&gt; macros are in
practice used for diverging purposes in different contexts, implicitly encoding
an inconsistent set of semantic guarantees. The article then describes how the
Rust contributors to Linux are working to determine how to more accurately
express the &lt;em&gt;specific&lt;/em&gt; semantic guarantees associated with the Rust-side of
APIs which use these macros on the C-side.&lt;/p&gt;
&lt;p&gt;This situation, of identifying an API which is implicitly encoding multiple
distinct semantic guarantees, and thereafter wanting to disentangle them, is
a familiar one for Rust developers. One little-discussed cultural norm in the
Rust ecosystem is that APIs should be designed to encode semantic guarantees
&lt;em&gt;as precisely as possible&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This is shown in things like the design of container types, such as Rust’s
various “cell” types (&lt;code&gt;Cell&lt;/code&gt;, &lt;code&gt;RefCell&lt;/code&gt;, &lt;code&gt;OnceCell&lt;/code&gt;, etc.), as well as
pointer-associated types such as &lt;code&gt;Unique&lt;/code&gt; (internal to the Rust standard
library, where it is used heavily) or &lt;code&gt;NonNull&lt;/code&gt; (part of the &lt;code&gt;std::ptr&lt;/code&gt; API).
As Manish Goregaokar described in &lt;a href=&quot;https://manishearth.github.io/blog/2015/05/27/wrapper-types-in-rust-choosing-your-guarantees/&quot;&gt;“Wrapper Types in Rust: Choosing Your
Guarantees”&lt;/a&gt;, Rust tries to disentangle unique semantic guarantees
which users can then compose together to express &lt;em&gt;precisely&lt;/em&gt; what they need.&lt;/p&gt;
&lt;p&gt;It also appears in Rust’s ongoing discussion around &lt;a href=&quot;https://smallcultfollowing.com/babysteps/blog/2025/10/07/the-handle-trait/&quot;&gt;amending the &lt;code&gt;Copy&lt;/code&gt; /
&lt;code&gt;Clone&lt;/code&gt; trait hierarchy to enable more precision&lt;/a&gt;. &lt;code&gt;Copy&lt;/code&gt; is a trait
intended for types which are trivially-copyable, meaning their full contents
can be copied with a single &lt;code&gt;memcpy&lt;/code&gt;. &lt;code&gt;Clone&lt;/code&gt; is intended for more “expensive”
copies, for example copies which require duplication of data on the heap and
adjustment of pointers to point to the copied data. Over time, Rust developers
have identified types with a different sort of copy semantics, such as &lt;code&gt;Rc&lt;/code&gt; or
&lt;code&gt;Arc&lt;/code&gt;, where cloning the data in fact means cloning &lt;em&gt;the handle&lt;/em&gt; to the
underlying data, not the data itself.&lt;/p&gt;
&lt;p&gt;The Rust community’s desire for precise semantics, in the long run, leads to
more robust software systems. In the short run, as the Linux devs are
encountering, it can be challenging to introduce greater semantic precision in
systems which were previously more ambiguous about guarantees and requirements.
Personally, I’m glad Rust has this norm, and it’s something I find appealing
about Rust as a &lt;em&gt;culture&lt;/em&gt;, not just a language, and I remain optimistic that
Linux will be better off because of it.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>The Best Monad Tutorial Ever Written</title>
    <id>https://www.alilleybrinker.com/blog/the-best-monad-tutorial/</id>
    <link rel="alternate" type="text/html" href="https://www.alilleybrinker.com/blog/the-best-monad-tutorial/"/>
    <published>2025-02-19T12:00:00.000Z</published>
    <updated>2025-02-19T12:00:00.000Z</updated>
    <summary>Rain Paharia has done it, they’ve explained monads with real, practical examples.</summary>
    <content type="html" xml:base="https://www.alilleybrinker.com/blog/the-best-monad-tutorial/">&lt;p&gt;When &lt;a href=&quot;https://bsky.app/profile/sunshowers.io&quot;&gt;Rain&lt;/a&gt; mentioned recently they had a draft monad tutorial that explains
them &lt;em&gt;without mathematical jargon&lt;/em&gt;, with &lt;em&gt;practical examples&lt;/em&gt;, and as a
&lt;em&gt;design pattern&lt;/em&gt;, I knew it was going to be good. Of course, it is &lt;strong&gt;even
better than I hoped&lt;/strong&gt;.&lt;/p&gt;
&lt;p class=&quot;hang-double&quot;&gt;&lt;a href=&quot;https://sunshowers.io/posts/monads-through-pbt/&quot;&gt;“Demystifying monads in Rust through property-based testing”&lt;/a&gt; is an
undersell of a title. If you’ve floated around the world of functional
programming long, you’ve probably seen one of the many attempts at explaining
monads. Many are from the perspective of a formalism&lt;span class=&quot;em-dash&quot;&gt;—&lt;/span&gt;the monad laws&lt;span class=&quot;em-dash&quot;&gt;—&lt;/span&gt;and
written with Haskell&lt;span class=&quot;em-dash&quot;&gt;—&lt;/span&gt;the language that made them a central feature with
syntactic sugar and a direct to how IO is done.&lt;/p&gt;
&lt;p&gt;Rain instead introduces monads with a practical example of how their
flexibility actually makes some work harder, motivated with a concrete example
of how “shrinking,” a core operation for doing practical property-based
testing, becomes much more difficult when monadic operations are used to
generate test inputs. Instead of Haskell, the tutorial uses Rust.&lt;/p&gt;
&lt;p&gt;Thanks Rain for the best monad tutorial I’ve ever read!&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Safety Hygiene</title>
    <id>https://www.alilleybrinker.com/blog/safety-hygiene/</id>
    <link rel="alternate" type="text/html" href="https://www.alilleybrinker.com/blog/safety-hygiene/"/>
    <published>2025-02-10T12:00:00.000Z</published>
    <updated>2025-02-10T12:00:00.000Z</updated>
    <summary>Explaining how to properly contain and document unsafe code to make it maintainable over time.</summary>
    <content type="html" xml:base="https://www.alilleybrinker.com/blog/safety-hygiene/">&lt;p&gt;I really like Jack Wrenn’s new article &lt;a href=&quot;https://jack.wrenn.fyi/blog/safety-hygiene/&quot;&gt;“The Three Basic Rules of Safety
Hygiene,”&lt;/a&gt; which covers how to handle unsafe code when you have to write it,
through a hygiene checklist for expressing and validating compliance with
safety conditions in the code.&lt;/p&gt;
&lt;p&gt;Rust’s goal is not to eliminate unsafe code, but to &lt;a href=&quot;https://doc.rust-lang.org/nomicon/meet-safe-and-unsafe.html&quot;&gt;contain it in isolated
modules&lt;/a&gt; you &lt;a href=&quot;https://github.com/rust-secure-code/safety-dance&quot;&gt;can audit&lt;/a&gt; and which provide safe interaces to
external users. Sometimes unsafety is necessary, and having a &lt;em&gt;process&lt;/em&gt; for
how to handle that unsafety is a key development.&lt;/p&gt;
&lt;p&gt;Research on how to reduce things like industrial accidents, medical incidents,
and airplane crashes all consistently find that checklists and procedures
&lt;em&gt;work&lt;/em&gt;. Humans are fallible, and being accountable to a checklist or a process
defends against our propensity to make mistakes.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Renegotiating C</title>
    <id>https://www.alilleybrinker.com/blog/renegotiating-c/</id>
    <link rel="alternate" type="text/html" href="https://www.alilleybrinker.com/blog/renegotiating-c/"/>
    <published>2024-02-19T12:00:00.000Z</published>
    <updated>2024-02-19T12:00:00.000Z</updated>
    <summary>Expressing empathy for C programmers as they grapple with fear and uncertainty amid the rise of Rust.</summary>
    <content type="html" xml:base="https://www.alilleybrinker.com/blog/renegotiating-c/">&lt;p&gt;Amid the move to memory safe languages, the people who build and build on C and
C++ ought to be recognized. Any of us could be hit by an upheaval of technology
in which we’ve invested.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;I am tying myself in a knot.&lt;/p&gt;
&lt;p&gt;It all started when I read “&lt;a href=&quot;https://nedbatchelder.com/blog/202401/you_probably_dont_need_to_learn_c.html&quot;&gt;You (Probably) Don’t Need to Learn C&lt;/a&gt;,” a good article
which raises good points about the poor arguments people tend to make when defending
the choice of learning C in this day and age. First and foremost: “you should learn
C so you know how a computer really works.” The article itself refers then to David
Chisnall’s “&lt;a href=&quot;https://dl.acm.org/doi/pdf/10.1145/3212477.3212479&quot;&gt;C is Not a Low-Level Language&lt;/a&gt;,” so I’ll do the same because it makes the
case better than I can. C is not the computer. C is (maybe) the only interface to
syscalls exposed by the operating system, depending on the system. C is (definitely)
an important part of computing history and computing today.&lt;/p&gt;
&lt;p&gt;And yet… “&lt;a href=&quot;https://www.humprog.org/~stephen/research/papers/kell17some-preprint.pdf&quot;&gt;Some Were Meant for C&lt;/a&gt;.” When I was an undergraduate I had a classmate
who insisted on doing every assignment, regardless of the class we were taking, in C.
This was in a program which used C++ almost exclusively, except for the “Software
Engineering” class in Java, the web development classes in the obligatory JavaScript,
or the delightful programming languages class which covered Java, Prolog, Scheme, and…
C.&lt;/p&gt;
&lt;p&gt;Anyway, this classmate insisted C was the &lt;strong&gt;best&lt;/strong&gt; language, the most elegant, the one
in which all of his ideas could be put to the machine in exactly the way he intended.
I didn’t understand it at the time, why he would think that, how he &lt;em&gt;could&lt;/em&gt; think that.
I toiled in the sweatshop of C++, found slight solace in Haskell code I could appreciate
but neither write nor understand. Eventually I found Rust, and… well… &lt;a href=&quot;https://github.com/alilleybrinker&quot;&gt;I like Rust&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Several years after I graduated, I was invited back to teach that same old programming
languages class after the prior professor, Dr. Richard Botting, had passed away. It was
essentially an emergency, as the Computer Science department didn’t have enough faculty to
cover the number of sessions of the class they needed. They were hiring more, but it would
take time, and they needed someone fast.&lt;/p&gt;
&lt;p&gt;By this point I was thoroughly Rust-brained. I’d written my fair share of Rust code, become
the sole Rust evangelist at work, presented at &lt;a href=&quot;https://www.youtube.com/watch?v=Wz2oFEDwiOk&quot;&gt;Rust Belt Rust&lt;/a&gt;, and written most of the
&lt;a href=&quot;https://prev.rust-lang.org/en-US/faq.html&quot;&gt;Rust Frequently Asked Questions&lt;/a&gt; answers. I was, through and through, a Rustacean. So
when I was given the opportunity to teach a class, a class that included &lt;em&gt;C&lt;/em&gt; in its curriculum,
my only question was: “can I change the languages?”&lt;/p&gt;
&lt;p&gt;I was told “yes,” and so I swapped out Haskell for Scheme, and Rust for C.&lt;/p&gt;
&lt;p&gt;The full details of this experience became the focus of &lt;a href=&quot;https://www.youtube.com/watch?v=0PhfaFkzdBA&quot;&gt;my talk at RustConf&lt;/a&gt;. The important
bit is this… during the talk I made a joke: “I replaced C with Rust, because it’s 2017.”
Good joke. Everybody laugh. It was an easy jab at a language against which Rust was clearly
positioned as an alternative, said to an audience all too certain to appreciate it. After all,
who doesn’t love being told they’re doing the better thing?&lt;/p&gt;
&lt;p&gt;However, in the intervening years, some context in the broader ecosystem has changed.
First, a number of companies, their materials dutifully collected by &lt;a href=&quot;https://alexgaynor.net/2020/may/27/science-on-memory-unsafety-and-security/&quot;&gt;Alex Gaynor&lt;/a&gt;
and then &lt;a href=&quot;https://advocacy.consumerreports.org/research/report-future-of-memory-safety/&quot;&gt;reported further by Consumer Reports&lt;/a&gt;, started to report about the prevalence of
memory safety vulnerabilities in their software. The numbers break down to roughly 70%
across many large organizations. That’s a big number!&lt;/p&gt;
&lt;p&gt;Consumer Reports bumping the news was also a big deal. It helped take the topic across the
barrier of “things the computer nerds talk about” and over to “things the policy world
talks about.”&lt;/p&gt;
&lt;p&gt;There also started to be bigger, more noticeable cyber security events where software
security in particular was in question (not just security from the interconnection of systems
and their surprising interactions, but literal “the software did an oopsie with memory”
kinds of vulnerabilities leading to exploitation). Heartbleed, a major vulnerability in OpenSSL.
Log4Shell, where people suddenly realized how &lt;em&gt;omnipresent&lt;/em&gt; and hard to identify or update some
of this software is.&lt;/p&gt;
&lt;p&gt;Not &lt;em&gt;all&lt;/em&gt; of this is memory safety related, but I think the memory safety angle seems
appealing to policymakers because it seems both achievable, and relatively clear. If someone
says they have a new building technique for houses that means a substantial portion of houses
that would fall over today wouldn’t fall over in the future, you as a policy maker would
probably find that pretty appealing?&lt;/p&gt;
&lt;p&gt;So why did memory safety suddenly feel &lt;em&gt;possible&lt;/em&gt;? I think the answer, unintentionally,
&lt;em&gt;is&lt;/em&gt; Rust.&lt;/p&gt;
&lt;p&gt;Memory safety isn’t &lt;em&gt;new&lt;/em&gt;. Even in the &lt;a href=&quot;https://www.cisa.gov/case-memory-safe-roadmaps&quot;&gt;latest guidance from CISA&lt;/a&gt; they list a number of
non-Rust memory safe languages, used on all sorts of widespread platforms. But crucially,
lots of the “big,” “important,” real-world stuff is written in C or C++. They’re what
the grown-ups use. They weren’t memory safe, and there wasn’t really a &lt;em&gt;language&lt;/em&gt; which
bundled up all the assurance guarantees you might want into a package which is easy to use.&lt;/p&gt;
&lt;p&gt;Yeah, some people would write secure C, they’d follow the &lt;a href=&quot;https://misra.org.uk/&quot;&gt;MISRA guidelines&lt;/a&gt; or they’d run
a bunch of analyzers or they’d hire some consultants to audit the code and give them the
okay. At the end of the day, none of those things had the ease of what you get with a new
language that plays in the same sandbox and has memory safety right out of the gate. So
C and C++ retained their distinction, and their cool, by existing in a little exurb
outside of the megalopolis of memory safety, secure in the knowledge that no one could
&lt;em&gt;really&lt;/em&gt; do better without them.&lt;/p&gt;
&lt;p&gt;Rust coming along doesn’t just challenge that security, it upends that security. I think
this is part of why Bjarne Stroustrup and the C++ Directions Group have &lt;a href=&quot;https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2759r0.pdf&quot;&gt;written things
about security and safety&lt;/a&gt; that have seemed, at best, difficult to follow, and at worst
filled with anxiety and defensive about the essential nature and continuing compelling
pitch of C++. They haven’t been at risk before! To reference an old film, “Oh! I’m in
pain. I think this is what &lt;em&gt;pain&lt;/em&gt; feels like.”&lt;/p&gt;
&lt;p&gt;This is not to say that either language has been resting on their laurels, or that Rust
is set to sweep them off the stage. Despite the imagined lurking of a Rust Evangelism
Strike Force prepared to assist C and C++ in shuffling off their mortal coils, I do
not think it is the expectation of technical leaders at major tech companies, nor
policy makers or thinktankers, to fully eliminate C and C++. There’s too much of both
languages around today to get rid of, and we know that replacing old code with new code
has a tendency to introduce new vulnerabilities anyway. In the field of a settled
graveyard, we must be careful digging things up.&lt;/p&gt;
&lt;p&gt;Instead, and you can see this borne out in &lt;a href=&quot;https://www.regulations.gov/docket/ONCD-2023-0002/comments&quot;&gt;the responses to the recent ONCD Request
for Input on open source software security&lt;/a&gt;, many if not most of the respondents
recommend a transition of new code efforts from C and C++ into memory safe languages,
with caution, careful prioritization, and accompanied by other assurance techniques
when such a transition is not feasible or prudent, along with careful protection
and further assurance of the C and C++ code we already have.&lt;/p&gt;
&lt;p&gt;Nor, as I think you’ll find, do the policy documents, academic papers, and thinktank
recommendations floating around recommend that everyone transition to &lt;em&gt;Rust&lt;/em&gt; specifically.
Rust might be the language that helped break the dam on memory safety, but it is
not the &lt;em&gt;only&lt;/em&gt; memory safe language. There are many still who recall the mess of
Ada becoming a mandatory programming language in some contexts, and who do not
wish to repeat such an enforced monoculture.&lt;/p&gt;
&lt;p&gt;So, to bring it back around, why am I in a knot? I have, through personal preference
and a happy accident of history, invested substantial personal time and energy
into building my knowledge and establishing a degree of personal branding around
a programming language that is now after many years &lt;em&gt;winning&lt;/em&gt;. Rust is &lt;em&gt;winning&lt;/em&gt;. It’s
&lt;a href=&quot;https://www.c-span.org/video/?c4808083/user-clip-rust-language-chosen&quot;&gt;being mentioned in Congressional hearings&lt;/a&gt; and recommended by federal agencies.
It’s used all over the globe. You can get real jobs in it now. Why am I troubled?&lt;/p&gt;
&lt;p&gt;I think we’re in the middle of a grand renegotiation of &lt;em&gt;what C means&lt;/em&gt; and
&lt;em&gt;what C++ means&lt;/em&gt;, or what they are in the context of programming, now that they’re
no longer hegemonic powers. To borrow from geopolitics a tad, this is the slow
decline of twin empires and the entrance of a new multipolar world. It is new,
and different, and exciting, but also… sad?&lt;/p&gt;
&lt;p&gt;I think I’m in knots because I look at the people who have invested so much in C
or C++, who now wrestle with the challenges of what this new world means for them,
and I think that could well be me, down the line. None of us are guaranteed that we
back the right horse in our selections of technologies to learn or “teams” to
invest in. None of us are guaranteed that we win.&lt;/p&gt;
&lt;p&gt;Which brings me back to the joke, and my discomfort with it now. Back then, I wanted
to win. I wanted to be superior; to be on the best side, with the best language.
I wanted to be better.&lt;/p&gt;
&lt;p&gt;I’d like to &lt;a href=&quot;https://www.youtube.com/watch?v=iSmkqocn0oQ&quot;&gt;share a video&lt;/a&gt;. It’s of Simon Peyton Jones, a major contributor to Haskell,
sitting with other programming language geeks at a conference years ago. In the video
he explains, happily, that Haskell is useless, but is slowly becoming more useful.
In the video, the other participants, themselves major contributors to other languages,
jump in with their own funny anecdotes and comments. When I first watched this in my
early 20s I thought it was kinda silly, kinda funny. What I see now is that all of
these people were &lt;em&gt;friends&lt;/em&gt;. They weren’t competitors; in fact they readily copy
each other and learn from each other. In the video they talk about how Haskell
learned things from C#!&lt;/p&gt;
&lt;p&gt;It’s easy to form cliques, its easy to form monocultures. It’s easy to want to win,
and when you win it’s easy to gloat.&lt;/p&gt;
&lt;p&gt;As the march toward memory safety policy moves closer, I see more and more &lt;em&gt;angst&lt;/em&gt;
in the comments from C and C++ programmers who are fighting tooth and nail to
preserve the best parts of the thing they love, or to defend themselves and their
own prior choices. I see fear and concern that Rust will become the one true way
they will be forced to take.&lt;/p&gt;
&lt;p&gt;Even as we admit that C and C++’s guarantees are no longer sufficient for the
security needs of the day, given the vast important of the software systems
we use and maintain, I don’t think we need to condemn the people who have invested
in it, or further mock the C and C++ languages. There are good people involved in
them who are trying hard to improve things, and any work we do today is built on
the shoulders of people who came before and tried other things which may or may not
have worked. There wouldn’t be Rust without C++, I imagine, to have paved the way
with many of its own design considerations and choices.&lt;/p&gt;
&lt;p&gt;I don’t know how this transition will work out. We’re in the middle of a grand
renegotiation whose outcome is uncertain. I’m excited to see where it goes.&lt;/p&gt;
&lt;p&gt;Yet as I look out across the vista, and see the shore and opportunity that
lies out of sight beyond it, as I board a ship and begin to sail, I can
look below me and remember, “&lt;a href=&quot;https://www.humprog.org/~stephen/research/papers/kell17some-preprint.pdf&quot;&gt;Some Were Meant for C&lt;/a&gt;”.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>“Implementation Selection” in Rust</title>
    <id>https://www.alilleybrinker.com/blog/implementation-selection/</id>
    <link rel="alternate" type="text/html" href="https://www.alilleybrinker.com/blog/implementation-selection/"/>
    <published>2024-01-23T12:00:00.000Z</published>
    <updated>2024-01-23T12:00:00.000Z</updated>
    <summary>Exploring options for selecting branching paths in Rust code at compile time and runtime.</summary>
    <content type="html" xml:base="https://www.alilleybrinker.com/blog/implementation-selection/">&lt;p&gt;Rust types offer four ways to decide what happens next, along two axes:
deciding at runtime vs. compile time, and permitting a closed or open set of
types.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;A common problem in programming is when you want the path of options for “what happens
next” to split. In structured programming this would generally come in the form of
a series of &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; (or whatever the syntax is in your preferred language),
or a &lt;code&gt;switch&lt;/code&gt; if your language supports it. Maybe you have a ternary operator for a more
compact &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt;. &lt;em&gt;Maybe&lt;/em&gt; you go whole-hog and have some array of function pointers
to embed the selection into data. Whatever way you slice it, you have paths of control
flow, and you want to choose between them in a structured fashion.&lt;/p&gt;
&lt;p&gt;Another way to enable this kind of selection is through the use of polymorphism. In an
Object-Oriented framing, this might mean subtype polymorphism via the use of child classes
of some shared parent class. This notion is in some way an extension of the “array of
function pointers” approach, except instead of an explicit array of function pointers like
you might find in C, you instead have, for each object, an implicit set of function pointers
for the method implementations in each subclass, and the language dispatches to those
based on the concrete type being handled.&lt;/p&gt;
&lt;p&gt;Depending on the language, the nature of overriding, method lookup, etc. may differ, but the
general notion is the same. Objects have some associated set of functions to which function
calls in the caller are dispatched according to the concrete type. We’ve elevated
implementation from the value level to the type level. This is convenient!&lt;/p&gt;
&lt;p&gt;In Rust, my own preferred language, you have instead a nice little grid of four options
for “implementation selection,” which vary based on whether dispatch is done at compile
time or run time, and whether the set of types which may be used is “open” (meaning
third-parties can use their own types) or “closed” (meaning only the types intended by the
original creator may be used).&lt;/p&gt;




















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Resolved at compile time&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Resolved at runtime&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Open set of types&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Generics params with trait&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Trait objects&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Closed set of types&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Generic params with sealed traits&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enum&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Let’s cover each of these in turn:&lt;/p&gt;
&lt;h2 id=&quot;1-generic-parameters-with-traits&quot;&gt;1. Generic Parameters with Traits&lt;/h2&gt;
&lt;p&gt;This is Rust’s standard mechanism for parametric polymorphism. You have a generic function,
with the generic type bounded by some trait(s). In this case, the calls to the generic
function are resolved at compile time, meaning the generic function is monomorphized to produce
a non-generic copy of it specialized for each concrete type the function is called with,
and the newly-generated concrete functions are swapped in for the generic function at the callsite.&lt;/p&gt;
&lt;p&gt;This gives you fast run time behavior (no dispatching / pointer chasing to do), but you
spend a bit more time on code generation during compilation, and the object file size generally
gets bigger. It also limits separate compilation, because the generic code needs to know the
concrete type(s) it’s getting called with.&lt;/p&gt;
&lt;p&gt;You also, crucially, don’t control the types which can be passed to the generic function. In
this category, the trait(s) involved are public, so third-parties can implement them for their
own types. This is &lt;em&gt;usually&lt;/em&gt; desirable, as it lets you write flexible APIs that expand to fit
future needs or the needs of others which you can’t predict.&lt;/p&gt;
&lt;h2 id=&quot;2-generic-parameters-with-sealed-traits&quot;&gt;2. Generic Parameters with Sealed Traits&lt;/h2&gt;
&lt;p&gt;In this quadrant, the trade-offs are the same as for generics with unsealed traits, with the
exception that you’ve now limited the set of types to be “closed.” So now, the &lt;em&gt;only&lt;/em&gt; types
which can be passed to the generic function are &lt;em&gt;your own&lt;/em&gt; types which have implemented the
relevant trait.&lt;/p&gt;
&lt;p&gt;This lets you have the benefits of generics in your own code without exposing a generic API
you don’t actually intend to be generic.&lt;/p&gt;
&lt;p&gt;On the other hand, consumers of your code may be put off by seeing a generic mechanism which
they can’t actually use (we often want whatever we can’t have), and it also means more complex
function signatures versus manually (or with the help of macros) writing out each version of
the function for the concrete types by hand.&lt;/p&gt;
&lt;h2 id=&quot;3-trait-objects&quot;&gt;3. Trait Objects&lt;/h2&gt;
&lt;p&gt;Trait objects work like generic parameters with traits, in that a function can be polymorphic
over the concrete type they’re handling, but it moves function dispatch from compile time to
run time. There are some limitations here, namely that not all traits in Rust can be made into
trait objects (this limitation is based on “object safety,” and the specific rules basically
codify that only traits whose dispatch &lt;em&gt;can&lt;/em&gt; be handled at run time can be made into trait
objects).&lt;/p&gt;
&lt;p&gt;Trait objects are powerful, and I’ve actually written about them before. On Possible Rust I
write &lt;a href=&quot;https://www.possiblerust.com/pattern/3-things-to-try-when-you-can-t-make-a-trait-object&quot;&gt;“3 Things to Try When You Can’t Make a Trait Object,”&lt;/a&gt;. Looking back
at that article now, I realize the first two suggestions are literally suggestions to move
on step in either direction on the grid I shared at the start of this article. Option 1 is
“Try an enum” (meaning close the set of types which is currently open), Option 2 is
“try type erasure” (meaning use a trick from David Tolnay to span a non-object safe trait
into an object-safe equivalent trait so you &lt;em&gt;can&lt;/em&gt; make a trait object). So option 1 is
about moving down one in the quadrant, and option 2 is about moving to the left.&lt;/p&gt;
&lt;p&gt;At any rate, trait objects are great, and powerful, though you pay a cost with run time
dispatch. You should benchmark and see if that cost is relevant for you.&lt;/p&gt;
&lt;h2 id=&quot;4-enumerations&quot;&gt;4. Enumerations&lt;/h2&gt;
&lt;p&gt;Finally, &lt;code&gt;enum&lt;/code&gt;s, one of Rust’s most powerful features. Enums let you list a set of
data-carrying variants, one of which will be present at runtime. Then, when using
the enum type, you &lt;code&gt;match&lt;/code&gt; (or use a &lt;code&gt;match&lt;/code&gt;-equivalent) to select the right behavior.&lt;/p&gt;
&lt;p&gt;This is, conceptually, similar to the good-old &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; chaining, or to
&lt;code&gt;switch&lt;/code&gt;, but with the added benefit of exhaustiveness checking. The compiler makes sure
you cover &lt;em&gt;all&lt;/em&gt; possible variants of the enum when selecting the right behavior, so you
don’t miss any possibilities.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Nothing said in this post is revelatory, but hopefully it provides a helpful description
of the option space here when you want to be able to have some “implementation selection”
in your program. Just remember the table:&lt;/p&gt;




















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Resolved at compile time&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Resolved at runtime&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Open set of types&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Generics params with trait&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Trait objects&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Closed set of types&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Generic params with sealed traits&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enum&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</content>
  </entry>
  <entry>
    <title>Technostructuralism</title>
    <id>https://www.alilleybrinker.com/blog/technostructuralism/</id>
    <link rel="alternate" type="text/html" href="https://www.alilleybrinker.com/blog/technostructuralism/"/>
    <published>2019-04-03T12:00:00.000Z</published>
    <updated>2019-04-03T12:00:00.000Z</updated>
    <summary>The systems we build are manifestations of our politics, so what is the philosophy of Rust?</summary>
    <content type="html" xml:base="https://www.alilleybrinker.com/blog/technostructuralism/">&lt;p&gt;The systems we build are manifestations of our politics. So what is the
philosophy of Rust and what does it offer for the state of software?&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;So Steve Klabnik posted a really thoughtful piece on open source and free software
(&lt;a href=&quot;https://words.steveklabnik.com/what-comes-after-open-source&quot;&gt;read it if you haven’t&lt;/a&gt;),
and the responses on places like Hacker News made an error that seems common in these
circles: believing working harder will solve a problem.&lt;/p&gt;
&lt;aside class=&quot;info-callout&quot; aria-labelledby=&quot;twitter-thread-reproduction&quot;&gt;
  &lt;p id=&quot;twitter-thread-reproduction&quot; class=&quot;info-callout-title&quot;&gt;This is a reproduction of a Twitter thread&lt;/p&gt;
  &lt;p&gt;The following is a reproduction of a thread I’d posted on Twitter back in 2019. I am sharing it here because Twitter has increasingly locked content behind a login prompt and I’d like to be able to refer people to this material without them having to have a Twitter/X account. The original thread can be found at &lt;a href=&quot;https://twitter.com/alilleybrinker/status/1113538427855757313&quot;&gt;Twitter&lt;/a&gt;. The version reproduced here is done without the thread-markers, @-references, and hashtags which were used in the original.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;This comes up with people defending C, who argue “people should write better C.” Of
course, this is nonsense. If a person can write better C consistently, we should
assume they will. If a person can, with extreme focus, write better C, that doesn’t
mean they can sustain it.&lt;/p&gt;
&lt;p&gt;It comes up with free software too. When someone points out the failures of the free
software movement, the response is “more people should believe in free software,
THEN we can deliver our goals.” This is nonsense. You don’t get more adherents by
demanding more adherents.&lt;/p&gt;
&lt;p&gt;In both scenarios, people refuse to re-examine structures. It’s not that C isn’t
meeting our needs, it’s that we’re not good enough to meet them ourselves. It’s not
that free software isn’t achieving its goals, it’s that not enough people believe in
it for it to succeed.&lt;/p&gt;
&lt;p&gt;This is all tied to the underlying politics of the software developers, often
characterized as “technolibertarian,” which believes strongly in the goodness of
technology, and the primacy of the individual in determining outcomes.&lt;/p&gt;
&lt;p&gt;This ideology is diametrically opposed to the structuralist view that Steve appears to
be taking in his piece, or that tech critics are often taking in their critiques of
tech. It plays out in the thousand externalities of tech which go unaddressed.&lt;/p&gt;
&lt;p&gt;What’s frustrating about this ideology is it opposes resolution of structural problems,
against mounting evidence of their existence and the failure of existing solutions to
fix them. It also connects to a point about programming languages as political objects.&lt;/p&gt;
&lt;p&gt;Steve’s a leader in the Rust community, and that’s no accident. Rust is a structural
response to C. Where a technolibertarian says “write better C,” a Rustacean says
“write Rust, which changes the structure so it, and not you, carries the burden of
correctness.”&lt;/p&gt;
&lt;p&gt;This is not a small point. It’s a microcosm of the fact that the technologies we build
are manifestations of our politics. It is not possible to keep politics out of tech,
because our politics shape how we approach problems, and what we build to fix them.&lt;/p&gt;
&lt;p&gt;We have, as I see it, two competing cultures in tech. The dominant culture,
technolibertarianism, and the counter-culture, which I’ll call technostructuralism.
These two cultures are often talking past each other, but they’re building systems
that match their perspectives.&lt;/p&gt;
&lt;p&gt;I know which side I prefer. Hopefully (maybe with a name, mine or someone else’s)
the technostructuralists can come out of the closet and unify their technology with
their politics. At the very least, let’s move the conversation.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Monomorphization Bloat</title>
    <id>https://www.alilleybrinker.com/blog/monomorphization-bloat/</id>
    <link rel="alternate" type="text/html" href="https://www.alilleybrinker.com/blog/monomorphization-bloat/"/>
    <published>2016-12-03T12:00:00.000Z</published>
    <updated>2016-12-03T12:00:00.000Z</updated>
    <summary>Rust generics can trade off binary size for expressiveness. Learning to identify when the trade off isn’t worth it is a valuable skill.</summary>
    <content type="html" xml:base="https://www.alilleybrinker.com/blog/monomorphization-bloat/">&lt;p&gt;Rust generics can trade off binary size for expressiveness. Learning to
identify when the trade isn’t worth it is a valuable skill that can help you
write better code.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;In this post I will discuss monomorphization: what it is, why it’s useful, and
what problems can potentially arise from it, with a particular focus on the
problem of monomorphization bloat. I will also look at options for dealing with
monomorphization bloat in your codebase.&lt;/p&gt;
&lt;aside class=&quot;info-callout&quot; aria-labelledby=&quot;repost-notice&quot;&gt;
  &lt;p id=&quot;repost-notice&quot; class=&quot;info-callout-title&quot;&gt;This is a repost&lt;/p&gt;
  &lt;p&gt;The following is a reproduction of a blog post originally shared on my now-defunct blog Suspect Semantics. It was originally published on December 3rd, 2016.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;Monomorphization is a compilation strategy to allow polymorphism with static
dispatch. To explain how it works, we’ll look at the following function:&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; do_the_thing&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&gt;(x&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // ... does the thing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see, this function takes in some type &lt;code&gt;T&lt;/code&gt;. This means we can call
the function with any type, like so:&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    do_the_thing&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;42&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    do_the_thing&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;8385&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;35&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    do_the_thing&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&quot;a string!&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At compile time, the compiler sees that &lt;code&gt;do_the_thing&lt;/code&gt; is called with three
different types: &lt;code&gt;i32&lt;/code&gt;, &lt;code&gt;f64&lt;/code&gt;, and &lt;code&gt;&amp;#x26;str&lt;/code&gt;. So it creates three different
functions based on &lt;code&gt;do_the_thing&lt;/code&gt;, replacing the general &lt;code&gt;T&lt;/code&gt; type with
each of the three concrete types the function is actually called with. In
the end, you get something like this:&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; do_the_thing_1&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(x&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; i32&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // ... do the thing, with an i32!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; do_the_thing_2&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(x&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; f64&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // ... do the thing, with an f64!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; do_the_thing_3&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(x&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // ... do the thing, with a &amp;#x26;str!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    do_the_thing_1&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;42&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    do_the_thing_2&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;8385&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;35&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    do_the_thing_3&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&quot;a string!&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is monomorphization! The generated code is equivalent to you having
written three different functions, but you didn’t have to actually write
three functions! This is really cool, as writing the general version is
a lot less work than writing a version for every concrete type you’ll
use, and a lot more flexible too.&lt;/p&gt;
&lt;h2 id=&quot;bloat&quot;&gt;Bloat&lt;/h2&gt;
&lt;p&gt;The downside is that, by generating these functions, you potentially add
bloat to the resulting binary. If you have a function with size &lt;code&gt;n&lt;/code&gt;, and
it’s called with &lt;code&gt;m&lt;/code&gt; number of concrete types, it’ll have a size of
&lt;code&gt;n×m&lt;/code&gt; in the resulting binary! In some contexts, a large binary size can
be a problem.&lt;/p&gt;
&lt;p&gt;Thankfully, there are several options to deal with this. In my last post,
I talked about a small conversion trick using &lt;code&gt;Into&lt;/code&gt;. This is a fairly
common trick in the Rust world, and can if you’re not careful cause
the monomorphization bloat problem we’re discussing here. But imagine
you have something like the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;pub&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; fn&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; big_function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; Into&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;i32&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(x&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // This is a giant function with hundreds of lines!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // And it gets called with a lot of concrete types!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // Oh no!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The way monomorphization works, the entire body of the function gets copied.
But in the case of conversion traits like &lt;code&gt;Into&amp;#x3C;T&gt;&lt;/code&gt;, &lt;code&gt;From&amp;#x3C;T&gt;&lt;/code&gt;, &lt;code&gt;AsRef&amp;#x3C;T&gt;&lt;/code&gt;,
&lt;code&gt;Borrow&amp;#x3C;T&gt;&lt;/code&gt;, and &lt;code&gt;ToString&lt;/code&gt;, you can actually separate the part that needs to
be monomorphized from the rest of the function like so:&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;pub&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; fn&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; big_function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; Into&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;i32&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&gt;&gt;(x&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    let&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; x&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; i32&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; x&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;into&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    _big_function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(x)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; _big_function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(x&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; i32&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // This is where all the rest of the original function body is now!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By splitting out the conversion code from the function, you keep the part that
the compiler will monomorphize, and thereby duplicate, quite small. This helps
to keep the size of the resulting binary down.&lt;/p&gt;
&lt;p&gt;Alternatively, you can remove the conversion trait abstraction, and instead
require callers to do the conversion themselves before calling the function.
This may make the API a little more tedious to use, but it avoids the potential
problem of bloat by not requiring monomorphization at all.&lt;/p&gt;
&lt;p&gt;Additionally, you can always try to shrink the sizes of your functions by
refactoring. A big function is probably a sign of something gone wrong anyway.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I don’t say any of this to encourage you not to use trait bounds or not to
use some of the conversion trait niceties in your API. Generics and static
dispatch are an important part of Rust, and it would be silly not to take
advantage of these features. Just keep in mind the potential for bloat, and
keep an eye on the size of your binary. If it grows too large and becomes a
problem, hopefully you will be better equipped to understand what is happening,
and to correct it.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>String Types in Rust</title>
    <id>https://www.alilleybrinker.com/blog/string-types-in-rust/</id>
    <link rel="alternate" type="text/html" href="https://www.alilleybrinker.com/blog/string-types-in-rust/"/>
    <published>2016-03-27T12:00:00.000Z</published>
    <updated>2016-03-27T12:00:00.000Z</updated>
    <summary>Rust has many pairs of string types, each with a distinct purpose. Understanding them helps you understand Rust itself.</summary>
    <content type="html" xml:base="https://www.alilleybrinker.com/blog/string-types-in-rust/">&lt;p&gt;Rust has many pairs of string types, each with a distinct purpose. Understanding
those purposes helps with understanding Rust itself.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;This seems to be a common issue, so let’s talk about the different string types
in the Rust programming language. In this post I’m going to explain the
organization of Rust’s string types with &lt;a href=&quot;https://doc.rust-lang.org/std/string/struct.String.html&quot; title=&quot;Rustdoc for the String type&quot;&gt;&lt;code&gt;String&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://doc.rust-lang.org/std/primitive.str.html&quot; title=&quot;Rustdoc for the str type&quot;&gt;&lt;code&gt;str&lt;/code&gt;&lt;/a&gt; as
examples, then get into the lesser-used string types---&lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.CString.html&quot; title=&quot;Rustdoc for the CString type&quot;&gt;&lt;code&gt;CString&lt;/code&gt;&lt;/a&gt;,
&lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.CStr.html&quot; title=&quot;Rustdoc for the CStr type&quot;&gt;&lt;code&gt;CStr&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.OsString.html&quot; title=&quot;Rustdoc for the OsString type&quot;&gt;&lt;code&gt;OsString&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.OsStr.html&quot; title=&quot;Rustdoc for the OsStr type&quot;&gt;&lt;code&gt;OsStr&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://doc.rust-lang.org/std/path/struct.PathBuf.html&quot; title=&quot;Rustdoc for the PathBuf type&quot;&gt;&lt;code&gt;PathBuf&lt;/code&gt;&lt;/a&gt;,
and &lt;a href=&quot;https://doc.rust-lang.org/std/path/struct.Path.html&quot; title=&quot;Rustdoc for the Path type&quot;&gt;&lt;code&gt;Path&lt;/code&gt;&lt;/a&gt;---and how the &lt;a href=&quot;https://doc.rust-lang.org/std/borrow/enum.Cow.html&quot; title=&quot;Rustdoc for the Cow type&quot;&gt;&lt;code&gt;Cow&lt;/code&gt;&lt;/a&gt; container can make working with
Rust strings easier.&lt;/p&gt;
&lt;aside class=&quot;info-callout&quot; aria-labelledby=&quot;originally-published-elsewhere&quot;&gt;
  &lt;p id=&quot;originally-published-elsewhere&quot; class=&quot;info-callout-title&quot;&gt;This was originally published on a different site.&lt;/p&gt;
  &lt;p&gt;This is a reproduction of a post of mine which was originally published on a now-defunct programming blog called “Suspect Semantics”. I am reproducing it here to give me something to refer to, as there have been times where I wanted to point people to this article. I don’t have any intent of bringing the original site back in full, but at least some of the content was good and is still reasonably correct and relevant to be worth preserving.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;The most important thing to understand is that string types in Rust come in
pairs, which I’ll call the “owned” variety and “slice” variety (&lt;em&gt;the term
“variety” is being used here to mean roughly “set of types”, to make clear that
“owned” and “slice” are not actual Rust types, but a notion of organization for
Rust’s string types&lt;/em&gt;). The “owned” variety of strings---&lt;a href=&quot;https://doc.rust-lang.org/std/string/struct.String.html&quot; title=&quot;Rustdoc for the String type&quot;&gt;&lt;code&gt;String&lt;/code&gt;&lt;/a&gt;,
&lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.CString.html&quot; title=&quot;Rustdoc for the CString type&quot;&gt;&lt;code&gt;CString&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.OsString.html&quot; title=&quot;Rustdoc for the OsString type&quot;&gt;&lt;code&gt;OsString&lt;/code&gt;&lt;/a&gt;, and &lt;a href=&quot;https://doc.rust-lang.org/std/path/struct.PathBuf.html&quot; title=&quot;Rustdoc for the PathBuf type&quot;&gt;&lt;code&gt;PathBuf&lt;/code&gt;&lt;/a&gt;---have
ownership over their contents (hence the name!), and can grow or shrink. The
“slice” variety of strings---&lt;a href=&quot;https://doc.rust-lang.org/std/primitive.str.html&quot; title=&quot;Rustdoc for the str type&quot;&gt;&lt;code&gt;str&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.CStr.html&quot; title=&quot;Rustdoc for the CStr type&quot;&gt;&lt;code&gt;CStr&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.OsStr.html&quot; title=&quot;Rustdoc for the OsStr type&quot;&gt;&lt;code&gt;OsStr&lt;/code&gt;&lt;/a&gt;, and
&lt;a href=&quot;https://doc.rust-lang.org/std/path/struct.Path.html&quot; title=&quot;Rustdoc for the Path type&quot;&gt;&lt;code&gt;Path&lt;/code&gt;&lt;/a&gt;---are views into some collection of characters. There’s also the
&lt;a href=&quot;https://doc.rust-lang.org/std/borrow/enum.Cow.html&quot; title=&quot;Rustdoc for the Cow type&quot;&gt;&lt;code&gt;Cow&lt;/code&gt;&lt;/a&gt; wrapper type, which can make working with the two varieties of
strings easier while retaining good performance characteristics.&lt;/p&gt;
&lt;h2 id=&quot;owned-vs-slice-varieties-of-strings&quot;&gt;“Owned” vs. “Slice” varieties of Strings&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://doc.rust-lang.org/std/string/struct.String.html&quot; title=&quot;Rustdoc for the String type&quot;&gt;&lt;code&gt;String&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://doc.rust-lang.org/std/primitive.str.html&quot; title=&quot;Rustdoc for the str type&quot;&gt;&lt;code&gt;str&lt;/code&gt;&lt;/a&gt; are, by far, the most common string types
in Rust, so I will use them to illustrate the difference between the two
varieties of string types. &lt;a href=&quot;https://doc.rust-lang.org/std/string/struct.String.html&quot; title=&quot;Rustdoc for the String type&quot;&gt;&lt;code&gt;String&lt;/code&gt;&lt;/a&gt; (the “owned” variety of string
type) is a wrapper for a heap-allocated buffer of UTF-8–encoded unicode
codepoints. &lt;a href=&quot;https://doc.rust-lang.org/std/primitive.str.html&quot; title=&quot;Rustdoc for the str type&quot;&gt;&lt;code&gt;str&lt;/code&gt;&lt;/a&gt; (the “slice” variety of string type) is a buffer of
UTF-8–encoded unicode codepoints that may be on the heap or in the
program memory itself.&lt;/p&gt;
&lt;p&gt;When you create a string literal in Rust, it is by default of type &lt;code&gt;&amp;#x26;str&lt;/code&gt;.
There are two things this could mean, depending on how that buffer was
created.&lt;/p&gt;
&lt;h3 id=&quot;slice-on-the-heap&quot;&gt;“Slice” on the Heap&lt;/h3&gt;
&lt;p&gt;If the reference is taken from a &lt;a href=&quot;https://doc.rust-lang.org/std/string/struct.String.html&quot; title=&quot;Rustdoc for the String type&quot;&gt;&lt;code&gt;String&lt;/code&gt;&lt;/a&gt;, it will be a reference to
the contents of the &lt;a href=&quot;https://doc.rust-lang.org/std/string/struct.String.html&quot; title=&quot;Rustdoc for the String type&quot;&gt;&lt;code&gt;String&lt;/code&gt;&lt;/a&gt;’s internal buffer, which is on the heap.
Handing out these references is a common pattern for effectively using “owned”
variety strings in Rust. For convenience’s sake, all references to “owned”
variety strings coerce to references to their “slice” variety equivalent. That
is, &lt;code&gt;&amp;#x26;String&lt;/code&gt; becomes &lt;code&gt;&amp;#x26;str&lt;/code&gt;. It is considered good practice to use the latter
type for function parameters taking a reference to a string.&lt;/p&gt;
&lt;h3 id=&quot;slice-in-program-memory&quot;&gt;“Slice” in Program Memory&lt;/h3&gt;
&lt;p&gt;If the buffer is a string literal, with or without an explicit lifetime, then
the buffer will be located in program memory. In fact, all string literals have
the &lt;code&gt;'static&lt;/code&gt; lifetime by default, meaning they can be safely referenced from
anywhere in the program. Note though that having a buffer with a &lt;code&gt;'static&lt;/code&gt;
lifetime is &lt;em&gt;not&lt;/em&gt; the same as declaring a &lt;code&gt;static&lt;/code&gt; variable. A buffer with a
&lt;code&gt;'static&lt;/code&gt; lifetime may be safely referenced from anywhere, but must still be
passed to functions explicitly; it is not globally visible. A variable must be
declared with the &lt;code&gt;static&lt;/code&gt; keyword to be globally visible.&lt;/p&gt;
&lt;p&gt;There is some nuance here though. When returning a &lt;code&gt;&amp;#x26;str&lt;/code&gt; from a function, the
lifetime of the reference must be tied to either a single input lifetime, or
a method taking either &lt;code&gt;&amp;#x26;self&lt;/code&gt; or &lt;code&gt;&amp;#x26;mut self&lt;/code&gt;. In the first case, the lifetime
of the returned reference will be the lifetime of the single input reference.
In the second case, the lifetime will be the lifetime of the reference to
&lt;code&gt;self&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If your function does not fit either of these cases, &lt;code&gt;rustc&lt;/code&gt; will return an
error, as in the following example:&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; get_string&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;-&gt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;    &quot;A string!&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    let&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; string &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; get_string&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    println!&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&quot;{}&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;, string);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can avoid the lifetime elision problems entirely by explicitly annotating
the function in one of the following ways:&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; get_string_static&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;-&gt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;    &quot;A string!&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; get_string_function_call&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;#x3C;'&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&gt;() &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;-&gt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;    &quot;A string!&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    let&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; string_1 &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; get_string_static&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    let&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; string_2 &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; get_string_function_call&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    println!&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&quot;{}&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;, string_1);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    println!&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&quot;{}&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;, string_2);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So what do you do if you want to return a string from a function without
giving it a &lt;code&gt;'static&lt;/code&gt; lifetime, or want to return a string whose contents
aren’t known at compile time? You use the “owned” variety of string type, in
this case: &lt;a href=&quot;https://doc.rust-lang.org/std/string/struct.String.html&quot; title=&quot;Rustdoc for the String type&quot;&gt;&lt;code&gt;String&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;why-owned-strings-exist&quot;&gt;Why “Owned” Strings Exist&lt;/h3&gt;
&lt;p&gt;The “owned” varities of strings are allocated on the heap, and do not suffer the
same limitations of the “slice” varities. They may be freely moved around
without safety issues, handing out slices of their internal buffer as needed.
The “slice” varities do not have the same guarantees, and so can’t be used as
freely.&lt;/p&gt;
&lt;p&gt;If you want to avoid dealing with ownership and borrowing for strings, you
can just turn every “slice” variety into an “owned” variety via the
&lt;a href=&quot;https://doc.rust-lang.org/std/borrow/trait.ToOwned.html#tymethod.to_owned&quot; title=&quot;Rustdoc for the to_owned function&quot;&gt;&lt;code&gt;to_owned()&lt;/code&gt;&lt;/a&gt; method (provided by the &lt;a href=&quot;https://doc.rust-lang.org/std/borrow/trait.ToOwned.html&quot; title=&quot;Rustdoc for the ToOwned trait&quot;&gt;&lt;code&gt;ToOwned&lt;/code&gt;&lt;/a&gt; trait,
which all “owned” varities of string types implement), and only work with the
“owned” varities, but doing so would incur performance penalties when you
unecessarily allocate string buffers on the heap, and would be considered poor
Rust style. Instead, it’s best to develop an understanding of when the two
varities of strings are needed.&lt;/p&gt;
&lt;p&gt;Another important thing to note is that because the “owned” varities of strings
abstract away the underlying buffer, they can grow or shrink, possibly
allocating a new underlying buffer and copying their contents to this new
buffer. The “slice” varities of strings cannot be resized, as they may not even
be on the heap.&lt;/p&gt;
&lt;p&gt;The “slice” variety strings can only be accessed via what’s called a “fat
pointer.” This is because slices are “dynamically-sized types,” meaning they
do not carry information about their own length. They are simply some
collection of contiguous memory. A “fat pointer” to a slice stores both a
pointer to the memory in question and the length of the data stored at that
memory location. This is all handled automatically by Rust, but it means that
the “slice” variety of strings are interacted with via references, rather than
being handled directly. For more detail about dynamically-sized types, check
out “The Rustonomicon,” which &lt;a href=&quot;https://doc.rust-lang.org/nomicon/exotic-sizes.html#dynamically-sized-types-dsts&quot; title=&quot;Rustonomicon section on dynamically-sized types&quot;&gt;covers them in detail&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-string-type-pairs&quot;&gt;The String Type Pairs&lt;/h2&gt;
&lt;p&gt;The pairs of string types are differentiated from each other by the guarantees
provided by the underlying buffer.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://doc.rust-lang.org/std/string/struct.String.html&quot; title=&quot;Rustdoc for the String type&quot;&gt;&lt;code&gt;String&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://doc.rust-lang.org/std/primitive.str.html&quot; title=&quot;Rustdoc for the str type&quot;&gt;&lt;code&gt;str&lt;/code&gt;&lt;/a&gt;
: &lt;a href=&quot;https://doc.rust-lang.org/std/string/struct.String.html&quot; title=&quot;Rustdoc for the String type&quot;&gt;&lt;code&gt;String&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://doc.rust-lang.org/std/primitive.str.html&quot; title=&quot;Rustdoc for the str type&quot;&gt;&lt;code&gt;str&lt;/code&gt;&lt;/a&gt; are guaranteed to be valid UTF-8 encoded
Unicode strings. If you’re wondering why UTF-8 is the standard encoding for
Rust strings, check out the &lt;a href=&quot;https://www.rust-lang.org/faq.html#why-are-strings-utf-8&quot; title=&quot;Rust FAQ answer about why UTF-8 is the standard encoding for Rust strings&quot;&gt;Rust FAQ’s answer to that question&lt;/a&gt;.
&lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.CString.html&quot; title=&quot;Rustdoc for the CString type&quot;&gt;&lt;code&gt;CString&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.CStr.html&quot; title=&quot;Rustdoc for the CStr type&quot;&gt;&lt;code&gt;CStr&lt;/code&gt;&lt;/a&gt;
: &lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.CString.html&quot; title=&quot;Rustdoc for the CString type&quot;&gt;&lt;code&gt;CString&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.CStr.html&quot; title=&quot;Rustdoc for the CStr type&quot;&gt;&lt;code&gt;CStr&lt;/code&gt;&lt;/a&gt; are guaranteed to be compatible with C
strings, and are usually used in FFI code.
&lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.OsString.html&quot; title=&quot;Rustdoc for the OsString type&quot;&gt;&lt;code&gt;OsString&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.OsStr.html&quot; title=&quot;Rustdoc for the OsStr type&quot;&gt;&lt;code&gt;OsStr&lt;/code&gt;&lt;/a&gt;
: &lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.OsString.html&quot; title=&quot;Rustdoc for the OsString type&quot;&gt;&lt;code&gt;OsString&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.OsStr.html&quot; title=&quot;Rustdoc for the OsStr type&quot;&gt;&lt;code&gt;OsStr&lt;/code&gt;&lt;/a&gt; are guaranteed to be
platform-native strings (that is, they use the encoding of the current
platform) that can be cheaply converted into &lt;a href=&quot;https://doc.rust-lang.org/std/string/struct.String.html&quot; title=&quot;Rustdoc for the String type&quot;&gt;&lt;code&gt;String&lt;/code&gt;&lt;/a&gt; and
&lt;a href=&quot;https://doc.rust-lang.org/std/primitive.str.html&quot; title=&quot;Rustdoc for the str type&quot;&gt;&lt;code&gt;str&lt;/code&gt;&lt;/a&gt; types. They are usually used when interacting directly with the
operating system.
&lt;a href=&quot;https://doc.rust-lang.org/std/path/struct.PathBuf.html&quot; title=&quot;Rustdoc for the PathBuf type&quot;&gt;&lt;code&gt;PathBuf&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://doc.rust-lang.org/std/path/struct.Path.html&quot; title=&quot;Rustdoc for the Path type&quot;&gt;&lt;code&gt;Path&lt;/code&gt;&lt;/a&gt;
: &lt;a href=&quot;https://doc.rust-lang.org/std/path/struct.PathBuf.html&quot; title=&quot;Rustdoc for the PathBuf type&quot;&gt;&lt;code&gt;PathBuf&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://doc.rust-lang.org/std/path/struct.Path.html&quot; title=&quot;Rustdoc for the Path type&quot;&gt;&lt;code&gt;Path&lt;/code&gt;&lt;/a&gt; are wrappers around
&lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.OsString.html&quot; title=&quot;Rustdoc for the OsString type&quot;&gt;&lt;code&gt;OsString&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.OsStr.html&quot; title=&quot;Rustdoc for the OsStr type&quot;&gt;&lt;code&gt;OsStr&lt;/code&gt;&lt;/a&gt; that provide convenient methods
for operating on paths according to the rules of the current system. They are
usually used when interacting with system paths.&lt;/p&gt;
&lt;h2 id=&quot;cow&quot;&gt;&lt;a href=&quot;https://doc.rust-lang.org/std/borrow/enum.Cow.html&quot; title=&quot;Rustdoc for the Cow type&quot;&gt;&lt;code&gt;Cow&lt;/code&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;There are times when working with strings in Rust that you want to cleanly
abstract over the two varities of string types. Maybe you want a container that
may hold either “owned” variety or “slice” variety strings, or you want to use
slices except for cases where an “owned” variety is absolutely necessary. In
these situations, use &lt;a href=&quot;https://doc.rust-lang.org/std/borrow/enum.Cow.html&quot; title=&quot;Rustdoc for the Cow type&quot;&gt;&lt;code&gt;Cow&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://doc.rust-lang.org/std/borrow/enum.Cow.html&quot; title=&quot;Rustdoc for the Cow type&quot;&gt;&lt;code&gt;Cow&lt;/code&gt;&lt;/a&gt; (which stands for “Clone on Write”) is a container that can take in
a “slice” variety of string type, and only convert that “slice” variety into an
“owned” variety when absolutely necessary (when something attempts to write to
the slice). This has the advantage of reducing the complexity of reasoning about
ownership, while also helping keep unecessary allocations down. It is not a
panacea, but it can be very helpful sometimes.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;That was a relatively quick explanation of string types in Rust. Hopefully that
helped to clarify a bit of why the different string types exist, and why they
are useful. As a final reference, here is a table of the different types:&lt;/p&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Guarantees&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;“Slice” variety&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;“Owned” variety&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;UTF-8&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;a href=&quot;https://doc.rust-lang.org/std/primitive.str.html&quot; title=&quot;Rustdoc for the str type&quot;&gt;&lt;code&gt;str&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;a href=&quot;https://doc.rust-lang.org/std/string/struct.String.html&quot; title=&quot;Rustdoc for the String type&quot;&gt;&lt;code&gt;String&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;C-compatible&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.CStr.html&quot; title=&quot;Rustdoc for the CStr type&quot;&gt;&lt;code&gt;CStr&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.CString.html&quot; title=&quot;Rustdoc for the CString type&quot;&gt;&lt;code&gt;CString&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;OS-compatible&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.OsStr.html&quot; title=&quot;Rustdoc for the OsStr type&quot;&gt;&lt;code&gt;OsStr&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;a href=&quot;https://doc.rust-lang.org/std/ffi/struct.OsString.html&quot; title=&quot;Rustdoc for the OsString type&quot;&gt;&lt;code&gt;OsString&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;System path&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;a href=&quot;https://doc.rust-lang.org/std/path/struct.Path.html&quot; title=&quot;Rustdoc for the Path type&quot;&gt;&lt;code&gt;Path&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;a href=&quot;https://doc.rust-lang.org/std/path/struct.PathBuf.html&quot; title=&quot;Rustdoc for the PathBuf type&quot;&gt;&lt;code&gt;PathBuf&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;hr&gt;
&lt;p&gt;Updated (3/28/2016):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This post has been updated based on corrections from &lt;a href=&quot;https://www.reddit.com/r/rust/comments/4c7wq2/string_types_in_rust/d1fsxd4&quot; title=&quot;Feedback from CryZe92 on the Rust subreddit&quot;&gt;CryZe92&lt;/a&gt; and
&lt;a href=&quot;https://www.reddit.com/r/rust/comments/4c7wq2/string_types_in_rust/d1fyvjo&quot; title=&quot;Feedback from Ilogiq on the Rust subreddit&quot;&gt;Ilogiq&lt;/a&gt; on the Rust subreddit. Thanks to both of them for the help!&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Updated (3/29/2016):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Replaced the word “sort” with “variety” based on feedback from &lt;a href=&quot;https://news.ycombinator.com/item?id=11380530&quot; title=&quot;Feedback from rkangel on Hacker News&quot;&gt;rkangel&lt;/a&gt;
on Hacker News.&lt;/li&gt;
&lt;li&gt;Clarified the wording of the unicode explanation, based on feedback from
&lt;a href=&quot;https://news.ycombinator.com/item?id=11379308&quot; title=&quot;Feedback from nothrabannosir on Hacker News&quot;&gt;nothrabannosir&lt;/a&gt;, also on Hacker News.&lt;/li&gt;
&lt;li&gt;Fixed a typo based on feedback from &lt;a href=&quot;https://twitter.com/respeccing/status/714551241615998976&quot; title=&quot;Feedback from respeccing on Twitter&quot;&gt;respeccing&lt;/a&gt; on Twitter.&lt;/li&gt;
&lt;li&gt;Reworded the transition into the explanation of “owned” string types, based
on feedback from &lt;a href=&quot;https://www.reddit.com/r/rust/comments/4c7wq2/string_types_in_rust/d1hbnmp&quot; title=&quot;Feedback from bjzaba on the Rust subreddit&quot;&gt;bjzaba&lt;/a&gt; from the Rust subreddit.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Updated (3/31/2016):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Replaced “three” with “two,” fixing an ommission from earlier changes.&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry>
</feed>
