<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Matt Davies</title>
    <description>The latest articles on DEV Community by Matt Davies (@cthutu).</description>
    <link>https://dev.to/cthutu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F651529%2F69464c59-cb87-494a-9d04-cbabb2b070dc.jpeg</url>
      <title>DEV Community: Matt Davies</title>
      <link>https://dev.to/cthutu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cthutu"/>
    <language>en</language>
    <item>
      <title>Rust #8: Strings</title>
      <dc:creator>Matt Davies</dc:creator>
      <pubDate>Tue, 10 Aug 2021 08:51:09 +0000</pubDate>
      <link>https://dev.to/cthutu/rust-8-strings-53o</link>
      <guid>https://dev.to/cthutu/rust-8-strings-53o</guid>
      <description>&lt;p&gt;Strings, at first glance in Rust, may seem very complicated.  First of all, there's the existence of two string types: &lt;code&gt;String&lt;/code&gt; and &lt;code&gt;str&lt;/code&gt;.  Then, there's the fact you can't easily index them.  On top of that, as you dig deeper in the language, there are more string types such as &lt;code&gt;CString&lt;/code&gt;, &lt;code&gt;OsString&lt;/code&gt;, &lt;code&gt;PathBuf&lt;/code&gt;, &lt;code&gt;CStr&lt;/code&gt;, &lt;code&gt;OsStr&lt;/code&gt; and &lt;code&gt;Path&lt;/code&gt;.  And then, if that wasn't enough, there are different ways to construct them and to convert between them because different APIs use different string types.&lt;/p&gt;

&lt;p&gt;No wonder people complain about Rust's learning curve, which I will personally argue is not as steep as C++.&lt;/p&gt;

&lt;p&gt;But, the truth is, that when you understand a few concepts like Unicode, UTF-8, ownership and slices, it is not that complicated.  In fact, necessary, and can also increase performance.&lt;/p&gt;

&lt;p&gt;In this blog article, I will talk about the whys of strings and how to convert between the various types.  I will not talk about some of the operations you can do on strings as that is a huge topic.  I do recommend you look at the Rust Standard API documentation for more information.&lt;/p&gt;

&lt;h2&gt;
  
  
  ASCII
&lt;/h2&gt;

&lt;p&gt;In the far annals of time, when computers only talked in English, there needed to be a method to map byte values in computer memory to characters that included digits, letters (upper and lower case), and punctuation.  Computers only see bytes; they do not see digits, letters and punctuation.  This meant that if you wanted to show text on printed paper or a screen, there needed to be a standard way of saying "whenever you see this byte value, print the letter A".&lt;/p&gt;

&lt;p&gt;You could, for example, say that a byte of value 0 means &lt;code&gt;A&lt;/code&gt;, a value 1 means &lt;code&gt;B&lt;/code&gt; and so on.  You could invent anything you wanted as long as your software on the computer and the hardware that printed the letter agreed.  They had to agree, because if they did not, sending &lt;code&gt;HELLO&lt;/code&gt; could come out as &lt;code&gt;JONNY&lt;/code&gt; or &lt;code&gt;$£&amp;amp;&amp;amp;!&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Two major standards appeared in the 1960s: IBM's EBCDIC and ASCII's committee's ASCII code.  EBCDIC was the abbreviation for Extended Binary Coded Decimal Interchange Code.  ASCII stood for American Standard Code for Information Interchange.  But ASCII won over, probably because it was easier to remember its name!&lt;/p&gt;

&lt;p&gt;ASCII uses 7 of the 8 bits in a byte to encode a character of code.  Values from 0-31 and 127 were character codes and used to give meta-data to printers and terminals: for example, a new line or backspace.  Values 32-126 represented all the printable characters in the English language as well as many punctuation characters.  It had some nice properties such as to convert from upper case to lower case you only needed to add 32 or set bit 5.&lt;/p&gt;

&lt;p&gt;The unused 8th bit was used for error detection when sending those bytes over networks.  But later when computers started appearing in countries other than English speaking languages, more characters needed to be added and that 8th bit was given up for 128 more characters.  Then new competing standards turned up that matched ASCII for the first 128 values, but differed in the second 128 values.  You had the concept of code pages that defined how byte values 128-255 were mapped.&lt;/p&gt;

&lt;p&gt;But eventually even 256 separate values were not enough to contain all the characters that were printable in the world.  Things got complicated very fast.  So enter the Unicode Consortium.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unicode
&lt;/h2&gt;

&lt;p&gt;It turns out there are many, MANY different characters in the world.  To support that many characters a new standard had to be created and this was run by the Unicode Consortium.  They manage the Unicode standard that, according to Wikipedia, "defines 143,859 characters covering 154 modern and historic scripts, as well as symbols, emojis and non-visual control and formatting codes.".  But the consortium doesn't just maintain those characters and codes, but how to display them, normalise, decompose, collate and render them.  It is a very, very, and dare I add one more 'very', complicated system.&lt;/p&gt;

&lt;p&gt;For example, &lt;code&gt;é&lt;/code&gt;, the accented letter 'e' that can appear in French and many other languages, can be represented by a single value, or by two values: the value of the letter &lt;code&gt;e&lt;/code&gt; and the value meaning put an acute accent on the previous character.  Normalisation is the process of making sure all the characters follow one representation or the other.  Composition is the process of reducing it to a single value, decomposition is the process of expanding it to the longer representation.&lt;/p&gt;

&lt;p&gt;Each value, or code point, is represented as a 32-bit value.  The first 128 values match ASCII exactly, which means converting ASCII to Unicode trivial, and converting Unicode to ASCII trivial as well as long as all code points lie between the values of 0 and 127.  The various values are divided into planes that represent a range of 65,536 values:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Plane&lt;/th&gt;
&lt;th&gt;Value range in hex&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0000-FFFF&lt;/td&gt;
&lt;td&gt;Basic Multilingual Plane&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;10000-1FFFF&lt;/td&gt;
&lt;td&gt;Supplementary Multilingual Plane&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;20000-2FFFF&lt;/td&gt;
&lt;td&gt;Supplementary Ideographic Plane&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;30000-3FFFF&lt;/td&gt;
&lt;td&gt;Tertiary Ideographic Plane&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4-13&lt;/td&gt;
&lt;td&gt;40000-DFFFF&lt;/td&gt;
&lt;td&gt;Unused&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;E0000-EFFFF&lt;/td&gt;
&lt;td&gt;Supplementary Special-purpose Plane&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;15-16&lt;/td&gt;
&lt;td&gt;F0000-10FFFF&lt;/td&gt;
&lt;td&gt;Supplementary Private Use Area Planes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As you can see from the table above, not all value ranges are used within the 32-bit space.  Only 21 bits are used.  I guess we need more bits when we discover aliens and learn their languages.&lt;/p&gt;

&lt;p&gt;But the downside of Unicode representation is that now all text takes 4 times as much space, irregardless or language and how common each symbol is.  Thanks to David Prosser and Ken Thompson (yes, that Ken Thompson, inventor and co-inventor of Unix, the B programming language, the grep utility and the modern programming language Go), we have a better representation.&lt;/p&gt;

&lt;h2&gt;
  
  
  UTF-8
&lt;/h2&gt;

&lt;p&gt;In 1992, Dave Prosser, working for the Unix System Laboratories, submitted a proposal for a new representation of Unicode that mapped 31 bits to 1-5 bytes in such a way that lower Unicode values could be represented with fewer bytes.  ASCII itself could be represented as normal single byte ASCII.  It worked by using bit 7 to indicate that a multibyte sequence was being used.  In the first byte of the sequence, the number of 1 bits following bit 7 represented how many bytes would follow.  The encoding worked like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Number of bytes&lt;/th&gt;
&lt;th&gt;Value range&lt;/th&gt;
&lt;th&gt;Byte 1&lt;/th&gt;
&lt;th&gt;Byte 2&lt;/th&gt;
&lt;th&gt;Byte 3&lt;/th&gt;
&lt;th&gt;Byte 4&lt;/th&gt;
&lt;th&gt;Byte 5&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0000000-0000007F&lt;/td&gt;
&lt;td&gt;0xxxxxxx&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;0000080-0000207F&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;1xxxxxxx&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;0002080-0008207F&lt;/td&gt;
&lt;td&gt;110xxxxx&lt;/td&gt;
&lt;td&gt;1xxxxxxx&lt;/td&gt;
&lt;td&gt;1xxxxxxx&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;0082080-0208207F&lt;/td&gt;
&lt;td&gt;1110xxxx&lt;/td&gt;
&lt;td&gt;1xxxxxxx&lt;/td&gt;
&lt;td&gt;1xxxxxxx&lt;/td&gt;
&lt;td&gt;1xxxxxxx&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;2082080-7FFFFFFF&lt;/td&gt;
&lt;td&gt;11110xxx&lt;/td&gt;
&lt;td&gt;1xxxxxxx&lt;/td&gt;
&lt;td&gt;1xxxxxxx&lt;/td&gt;
&lt;td&gt;1xxxxxxx&lt;/td&gt;
&lt;td&gt;1xxxxxxx&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Plane 0 of Unicode, which contained the most common characters around the world, could be encoded 1-3 bytes.  All the current Unicode characters could be encoded, at most, within 4 bytes.&lt;/p&gt;

&lt;p&gt;Ken Thompson improved it at the cost of adding a 6th byte, but allowed a program to tell the difference between an initial byte that started a sequence and an intermediate byte.  He did this by reserving &lt;code&gt;10&lt;/code&gt; for only the following bytes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Number of bytes&lt;/th&gt;
&lt;th&gt;Value range&lt;/th&gt;
&lt;th&gt;Byte 1&lt;/th&gt;
&lt;th&gt;Byte 2&lt;/th&gt;
&lt;th&gt;Byte 3&lt;/th&gt;
&lt;th&gt;Byte 4&lt;/th&gt;
&lt;th&gt;Byte 5&lt;/th&gt;
&lt;th&gt;Byte 6&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0000000-0000007F&lt;/td&gt;
&lt;td&gt;0xxxxxxx&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;0000080-000007FF&lt;/td&gt;
&lt;td&gt;110xxxxx&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;0000800-0000FFFF&lt;/td&gt;
&lt;td&gt;1110xxxx&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;0010000-001FFFFF&lt;/td&gt;
&lt;td&gt;11110xxx&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;0200000-03FFFFFF&lt;/td&gt;
&lt;td&gt;111110xx&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;4000000-7FFFFFFF&lt;/td&gt;
&lt;td&gt;1111110x&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;td&gt;10xxxxxx&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Plane 0 can still be encoded in 1-3 bytes, ASCII is still represented and all Unicode characters can still be encoded in up to 4 bytes.  UTF-8 is such an amazingly clever standard.  Well done David and Ken!&lt;/p&gt;

&lt;p&gt;Rust uses the UTF-8 standard to encode its strings in the &lt;code&gt;String&lt;/code&gt; struct.  So any character in a string will be encoded using between 1 and 4 bytes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ownership and slices
&lt;/h2&gt;

&lt;p&gt;To recap, Rust strictly defines the concept of ownership.  This is where a variable, parameter etc owns the memory it requires to store its value, and it is the ONLY variable to own that memory.  Rust's compiler guarantees that there is only a single owner of a piece of memory at any single time.  This also means that when the owner goes out of scope, the compiler can safely destroy the memory and give it back to the operating system.&lt;/p&gt;

&lt;p&gt;Secondly, the owner can dish out references to its memory and lend them to other parts of the program.  It can do so either mutably or immutably.  Rust enforces that you can only give out a single mutable reference (i.e. the reference holder can mutate the memory the owner owns) at any time.  The owner itself cannot even mutate itself while there's a mutable reference.  But, if there are no mutable references, the owner can give out any number of immutable references, which also means that the owner itself cannot mutate itself while they still exist.&lt;/p&gt;

&lt;p&gt;It is also important to note that if there are any references live when the owner goes out of scope, that is a compiler error.  Rust will just not allow you to do that.  To enforce that, there is a concept of lifetimes, but I do not need to go into that for the topic of strings.&lt;/p&gt;

&lt;p&gt;So to summarise, you can only have these 3 states in Rust:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only an owner.  If the variable is marked &lt;code&gt;mut&lt;/code&gt;, it can mutate itself.&lt;/li&gt;
&lt;li&gt;Only an owner and any number of immutable references.  Everything is read-only.  The owner cannot mutate itself and neither can the references mutate the owned memory as they are immutable.&lt;/li&gt;
&lt;li&gt;Only an owner and a single mutable reference.  Only the mutable reference can mutate the owned memory.  The owner can only read itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One type of reference, mutable or immutable, is called a slice.  It is a special reference to an array of memory or a contiguous sequence of values.  The array that contains elements of type &lt;code&gt;T&lt;/code&gt; is written as &lt;code&gt;[T]&lt;/code&gt; and the slice, because it's just a reference to an array, is written as either &lt;code&gt;&amp;amp;[T]&lt;/code&gt; or &lt;code&gt;&amp;amp;mut [T]&lt;/code&gt;.  Simply put, a slice can be thought of as a &lt;strong&gt;view&lt;/strong&gt; into some contiguous memory owned by something else.  A slice, due to being a reference, does not own any memory and so therefore requires something else to own that memory to have a view on to it.&lt;/p&gt;

&lt;p&gt;Now a slice doesn't have to view all of the owned memory.  It can view a subset of it.  And Rust guarantees both at compile-time and run-time that a slice cannot view outside the bounds of the owned memory.  This characteristic of slices is crucial to having performant operations on strings as you will see.&lt;/p&gt;

&lt;p&gt;A slice has two pieces of information.  Firstly, a pointer to the start of the memory it views, and secondly, the size of the memory it can view.&lt;/p&gt;

&lt;p&gt;And now we can talk about what the string types above are all about.  Some are owners, types that own the memory that contain the string characters, and some are array types that with the reference operator &lt;code&gt;&amp;amp;&lt;/code&gt; implement the slice.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Owner type&lt;/th&gt;
&lt;th&gt;Slice type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;String&lt;/td&gt;
&lt;td&gt;&amp;amp;str&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;OsString&lt;/td&gt;
&lt;td&gt;&amp;amp;OsStr&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PathBuf&lt;/td&gt;
&lt;td&gt;&amp;amp;Path&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;String&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;If we look at the definition of &lt;code&gt;std::string::String&lt;/code&gt; we see this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means a &lt;code&gt;String&lt;/code&gt;, internally, is an array of bytes.  However, there is one important aspect to those array of bytes.  Rust guarantees through its standard APIs that those array of bytes can only contain a valid sequence of UTF-8 characters.  Likewise, &lt;code&gt;&amp;amp;str&lt;/code&gt; guarantees that it is a view on the whole or part of a &lt;code&gt;String&lt;/code&gt; that only contains UTF-8 characters.  The APIs guarantee that the start pointer of a &lt;code&gt;&amp;amp;str&lt;/code&gt; and its implied end pointer (the start pointer plus its length) fall exactly on UTF-8 character boundaries.&lt;/p&gt;

&lt;p&gt;The fact that the data is UTF-8 means that when you ask for a character from the string, you can get back 1, 2, 3 or even 4 bytes of data due to the encoded nature of UTF-8.  This is why when you extract a character from a string you get back a &lt;code&gt;char&lt;/code&gt; type, which is 4 bytes in size.  Rust's string APIs will convert the UTF-8 sequence of bytes to a single Unicode code point or code points.&lt;/p&gt;

&lt;p&gt;It is this very complexity of how characters or codes in UTF-8 can be multiple bytes (or not) that makes it very cumbersome to use when processing these strings.  It is important to Rust that no matter what you do the UTF-8 is valid, otherwise it will panic.&lt;/p&gt;

&lt;p&gt;One more aspect to talk about is string literals.  For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All literal strings are embedded in the executable.  And because that memory is unmoving and frozen in time, you can only grab an immutable slice to that string.  Therefore, all literal strings are of types &lt;code&gt;&amp;amp;str&lt;/code&gt;.  They can never be mutated as the memory containing all the literals in an executable are always read-only on all operating systems.&lt;/p&gt;

&lt;p&gt;Technically speaking, the type is really &lt;code&gt;&amp;amp;'static str&lt;/code&gt; because the literal is available for the entire lifetime of the program.  If you don't understand lifetimes at time of reading this article then don't worry about this detail right now.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;OsString&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Different operating systems use different techniques for encoding their strings.  This is important to know when interacting with the operating system directly because UTF-8 encodings may not cut it.  Therefore, Rust introduces &lt;code&gt;std::ffi::OsString&lt;/code&gt; to abstract away the differences between operating systems.  It is defined in the FFI (Foreign Function Interface) module because it is often required when talking to C code and other languages.&lt;/p&gt;

&lt;p&gt;On Unix systems, strings are sequences of non-zero bytes, often in UTF-8 encoding.  On Windows, strings can be encoded in either 8-bit ASCII or a 16-bit representation called UTF-16 (another Unicode encoding system using 1 or 2 16-bit values).  Rust strings are always UTF-8 that may contain zeroes.  So Rust provides a new type with fallible conversion functions.&lt;/p&gt;

&lt;p&gt;Like &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;OsString&lt;/code&gt; has a companion slice type called &lt;code&gt;std::ffi::OsStr&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;PathBuf&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;std::path::PathBuf&lt;/code&gt; is a &lt;code&gt;OsString&lt;/code&gt; with a particular purpose.  It is used to represent filenames.  And because filenames are used by the operating system, they are required to be OS-specifically encoded.  The definition of &lt;code&gt;PathBuf&lt;/code&gt; is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;PathBuf&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;OsString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similar to the other string representations, there is a companion slice type called &lt;code&gt;std::fs::Path&lt;/code&gt;.  Along with this type are many useful methods for manipulating paths, like producing slices on the extension part, or the filename part.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;CString&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Finally, out of the string types that I wish to talk about in this article, there is the &lt;code&gt;std::ffi::CString&lt;/code&gt; type.  This is used to communicate with C functions because C stores strings very differently to Rust.  For starters, there is no length parameter as C uses a terminating zero byte, called the null-terminator, to mark the end of the string.  This means that C strings can never contain zero bytes and finding the length requires stepping through all the characters.  Also, C strings have no implied encoding.  Due to this, &lt;code&gt;CString&lt;/code&gt; is usually constructed from a byte array or vector.&lt;/p&gt;

&lt;p&gt;Constructing a &lt;code&gt;CString&lt;/code&gt; will make sure that there are no zero bytes in the middle of an array and will return an error if it finds them.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CStrings&lt;/code&gt; are a vector of the type &lt;code&gt;c_char&lt;/code&gt; which is an unsigned byte to match the &lt;code&gt;char&lt;/code&gt; type of the C language.  The companion type of &lt;code&gt;CString&lt;/code&gt; is &lt;code&gt;CStr&lt;/code&gt; which contains an array of &lt;code&gt;c_char&lt;/code&gt;s.&lt;/p&gt;

&lt;p&gt;When working with C code, you usually find yourself converting a &lt;code&gt;String&lt;/code&gt; to a &lt;code&gt;CString&lt;/code&gt;, which can then be passed to the function that takes a &lt;code&gt;const char*&lt;/code&gt; using &lt;code&gt;CStr&lt;/code&gt;'s &lt;code&gt;as_ptr()&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conversions
&lt;/h2&gt;

&lt;p&gt;Another source of confusion when starting with Rust is how do you convert from one type to another.  I have often found myself rushing to Stack Overflow to find code that shows me how.  First, I will cover conversion between owner types and their slice types.  Then, I will visit each string type and talk about how to convert from any other different string type.&lt;/p&gt;

&lt;h3&gt;
  
  
  From owned types to slices
&lt;/h3&gt;

&lt;p&gt;Converting from an owned type to a slice type is as simple as putting a reference operator in front:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;os_s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;OsString&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Foo"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;path_s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;PathBuf&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/home/matt/.bashrc"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;c_s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;CString&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C sucks!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;ref_s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;ref_os_s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;OsStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;os_s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;ref_path_s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;path_s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;ref_c_s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;CStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;c_s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This magic works because of the &lt;code&gt;Deref&lt;/code&gt; trait.  For example, &lt;code&gt;String&lt;/code&gt; implements &lt;code&gt;Deref&amp;lt;Target=str&amp;gt;&lt;/code&gt; and can inherit all of &lt;code&gt;str&lt;/code&gt;'s methods.  This also means that you can pass a &lt;code&gt;String&lt;/code&gt; to a function that expects a &lt;code&gt;&amp;amp;str&lt;/code&gt; or &lt;code&gt;&amp;amp;mut str&lt;/code&gt; is the owner is mutable.  This is why many functions that accept strings use &lt;code&gt;&amp;amp;str&lt;/code&gt; as the parameter type.  But when returning a string, functions often return an owned type so its lifetime isn't linked to the input parameters.  So, for example, you can often see a function like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with_capacity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;place&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;place&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because converting an owned type to a slice is just constructing a pointer and a length, there is no allocation.  Also, by accepting a &lt;code&gt;&amp;amp;str&lt;/code&gt;, the function doesn't care whether you pass a &lt;code&gt;&amp;amp;String&lt;/code&gt; or a string literal.&lt;/p&gt;

&lt;h3&gt;
  
  
  From slices to owned types
&lt;/h3&gt;

&lt;p&gt;This is a little more complicated but not much.  To construct an owned type from a slice, you have to allocate memory and copy the characters from the memory the slice is referencing.  This is because someone else owns that memory and you can't have two owners.  There are multiple ways of doing it.&lt;/p&gt;

&lt;p&gt;One way is via the &lt;code&gt;From&lt;/code&gt; trait, which declares the &lt;code&gt;from&lt;/code&gt; method.  All the owned string types described here implement &lt;code&gt;From&lt;/code&gt; for their companion slice type.  You've already seen its use in the examples above.&lt;/p&gt;

&lt;p&gt;Another way is using a method on the slice type.  Unfortunately, the different string types use different methods names:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Owned type&lt;/th&gt;
&lt;th&gt;Method to convert from slice type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;String&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;str.to_owned()&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;String&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;str.to_string()&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;OsString&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;OsStr.to_os_string()&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;PathBuf&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;Path.to_path_buf()&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CString&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;no method exists&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You will notice that &lt;code&gt;str&lt;/code&gt; has two methods: &lt;code&gt;to_owned()&lt;/code&gt; and &lt;code&gt;to_string()&lt;/code&gt;.  &lt;code&gt;to_string()&lt;/code&gt; actually comes from the trait &lt;code&gt;ToString&lt;/code&gt;.  The default version of this trait using Rust's formatting functions and ends up being slower than the &lt;code&gt;to_owned()&lt;/code&gt; method.  So I highly recommend you use &lt;code&gt;to_owned()&lt;/code&gt; or the &lt;code&gt;From&lt;/code&gt; trait methods.&lt;/p&gt;

&lt;p&gt;Unfortunately, none of the other string types implement the &lt;code&gt;ToString&lt;/code&gt; type as the conversion can fail.  &lt;code&gt;to_string()&lt;/code&gt; does not return a &lt;code&gt;Result&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conversions between string types
&lt;/h3&gt;

&lt;p&gt;There are many different functions with different naming conventions to convert between string types, sometimes the owned type, sometimes the slice type.  I thought about how I would represent this information.  I considered tables for each string type I was converting to.  But sometimes there isn't a single function you can call.  In the end I decided on graph representations.&lt;/p&gt;

&lt;p&gt;The first graph shows conversions between different types using their methods.  Where a &lt;code&gt;?&lt;/code&gt; appears after the method name, it means it is fallible and either returns an &lt;code&gt;Option&lt;/code&gt; or &lt;code&gt;Result&lt;/code&gt;.  You will need to handle that.  Red nodes are the owned types.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk3idews6bgwya9i0fo3i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk3idews6bgwya9i0fo3i.png" alt="strings-convert"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The second graph shows conversion using the &lt;code&gt;From&lt;/code&gt; trait.  If an arrow travels from type &lt;code&gt;T&lt;/code&gt; to type &lt;code&gt;U&lt;/code&gt;, it means that &lt;code&gt;U&lt;/code&gt; can be created using the &lt;code&gt;From&amp;lt;T&amp;gt;&lt;/code&gt; trait.  Again red nodes are owned types.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff1n7xtrgvzfq2y49x86g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff1n7xtrgvzfq2y49x86g.png" alt="strings-from"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope these graphs help you convert from any string type to another.&lt;/p&gt;

&lt;h2&gt;
  
  
  Holy Cow!
&lt;/h2&gt;

&lt;p&gt;There is one more string type in Rust I want to discuss and that is the &lt;code&gt;std::borrow::Cow&amp;lt;T&amp;gt;&lt;/code&gt; type where T is a slice type that implements the &lt;code&gt;ToOwned&lt;/code&gt; trait.  All the string slice types mentioned in this article implement the &lt;code&gt;ToOwned&lt;/code&gt; trait.&lt;/p&gt;

&lt;p&gt;COW is an acronym for Copy On Write.  It can either hold an immutable slice or an owned type.  It is commonly used for parameters and return types and is constructed from the multiple &lt;code&gt;From&lt;/code&gt; traits it implements.&lt;/p&gt;

&lt;p&gt;If you construct it from a slice (such as a string literal or &lt;code&gt;&amp;amp;str&lt;/code&gt;), it will hold an immutable reference to that slice.  If you construct it from an owned type, it will take ownership:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;c1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Cow&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// c1 = Owned("Hello")&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;c2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Cow&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// c2 = Borrowed("Hello")&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes &lt;code&gt;Cow&lt;/code&gt; a useful return type for a function that can return a static string or a generated string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;g1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Matt"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;   &lt;span class="c1"&gt;// g1 = Owned("Hello Matt")&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;g2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;           &lt;span class="c1"&gt;// g2 = Borrowed("Hello human!")&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Cow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;'static&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Cow&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;format!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
        &lt;span class="nb"&gt;None&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Cow&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello human!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can notice that the generic parameters passed to &lt;code&gt;Cow&lt;/code&gt; are a lifetime parameter and the slice type.  The slice type is required if &lt;code&gt;Cow&lt;/code&gt; is ever borrowed.  The owned type is derived from the slice type's &lt;code&gt;ToOwned&lt;/code&gt; trait.  The lifetime parameter is required for in the case of borrowing a reference.  If the &lt;code&gt;Cow&lt;/code&gt; owns data, a lifetime is no required.&lt;/p&gt;

&lt;p&gt;In the example above, &lt;code&gt;g1&lt;/code&gt; owns the data and &lt;code&gt;g2&lt;/code&gt; doesn't because it references a static string literal.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cow&lt;/code&gt; types can be easily cloned too.  If it owns a type, it returns a &lt;code&gt;Cow&lt;/code&gt; that borrows.  You can also obtain a mutable reference to the contents by calling &lt;code&gt;to_mut&lt;/code&gt; and that will clone if necessary.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;That concludes my article for this week.  I hope you find strings easier to understand now and realise that the reasons they are complicated are justified.  We talked about owned types and their associated slice types.  We talked about why there are different types due to differences in UTF-8 and operating system representations.  We also talked about how you can convert between them using a combination of their methods and &lt;code&gt;From&lt;/code&gt; traits.  Finally, we discussed the &lt;code&gt;Cow&lt;/code&gt; type that can represent both an owned type or a referenced type.&lt;/p&gt;

&lt;p&gt;Until next time!&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
    <item>
      <title>Rust #7: Command-Line interfaces</title>
      <dc:creator>Matt Davies</dc:creator>
      <pubDate>Sun, 01 Aug 2021 13:00:47 +0000</pubDate>
      <link>https://dev.to/cthutu/rust-7-command-line-interfaces-4084</link>
      <guid>https://dev.to/cthutu/rust-7-command-line-interfaces-4084</guid>
      <description>&lt;p&gt;I have discovered that Rust has great support for writing Command-Line Interface (CLI) tools, or tools that you interact with on the command-line.&lt;/p&gt;

&lt;p&gt;The basic requirement of CLI tools is to be able to read the arguments that the user types after the command on the terminal and process them.&lt;/p&gt;

&lt;p&gt;You may have noticed that most CLI tools are written in Rust look the same when interacting with them.  For example, invalid use of them shows some text on how you should use them.  Typing &lt;code&gt;--help&lt;/code&gt; after the command will list all sub-commands (if any) and flags (commands that start with a hyphen or double hyphen) that you can use.  Even typing &lt;code&gt;--version&lt;/code&gt; after the command will show version information.  I guarantee that none of the CLI tool developers worked hard to provide this functionality.&lt;/p&gt;

&lt;p&gt;So to provide an introduction on how to write CLI tools, let me introduce you to my demo CLI app called greeter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// in src/main.rs&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;env&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;args&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Greet&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Unknown&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Goodbye&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Greet&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Unknown&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"-n"&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"--name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nf"&gt;.clone&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Greet&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"bye"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Greet&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Goodbye&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;greet&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nn"&gt;Greet&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Unknown&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"No idea what to do!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="nn"&gt;Greet&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Hello&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;format!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="nn"&gt;Greet&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Goodbye&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;format!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Goodbye {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This program takes a sub-command (a command that follows after the program's name) and an optional flag to provide a name to use in the greeting.  Let's put it through its paces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ greet
No idea what to do!
$ greet hello
Hello World
$ greet bye
Goodbye World
$ greet --name Matt
No idea what to do!
$ greet hello --name Matt
Hello Matt
$ greet bye -n Matt
Goodbye Matt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But there are problems with this program.  For example, &lt;code&gt;greet hello bye --name Matt --name Bob&lt;/code&gt; is valid and will result in &lt;code&gt;Goodbye Bob&lt;/code&gt;.  We'd probably want to restrict that kind of confusing use.  Processing command-line parameters is tricky and full of edge cases.&lt;/p&gt;

&lt;p&gt;There are even more issues.  There is no help, no support for version information and as mentioned before, no good error handling.&lt;/p&gt;

&lt;p&gt;So the code above is not idiomatic at all and is implemented similar to how a C programmer might do so.  Processing command-lines arguments is error-prone and is code that you usually incrementally increase as you come back to it time and time again to implement more features.  C programmers can use a library called &lt;code&gt;Getopt&lt;/code&gt; to manage this and is very common in Unix CLI tools.&lt;/p&gt;

&lt;p&gt;Rust provides us with the arguments using &lt;code&gt;std::env::args()&lt;/code&gt; that provides an iterator (in true Rust fashion) that can iterate through all the commands that were given.  The very first iteration provides the name of the program, which is why the loop starts at 1 and not 0.&lt;/p&gt;

&lt;p&gt;Enter the Clap!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;clap&lt;/code&gt; crate
&lt;/h2&gt;

&lt;p&gt;Clap is a crate that was written to manage command-line options and provide a familiar interface via &lt;code&gt;--help&lt;/code&gt; and &lt;code&gt;--version&lt;/code&gt;.  Clap uses the builder pattern to declaratively describe how your tool should interact with the command-line.  So let's start rewriting our amazing CLI tool using Clap.&lt;/p&gt;

&lt;p&gt;For starters, it can provide application information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;clap&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;App&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The Amazing Greeter"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.version&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.author&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Matt Davies"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.about&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The best greeter in town!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.get_matches&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't forget to add &lt;code&gt;clap = "2.33"&lt;/code&gt; to &lt;code&gt;Cargo.toml&lt;/code&gt; in the &lt;code&gt;[dependencies]&lt;/code&gt; section.&lt;/p&gt;

&lt;p&gt;This short program already gives us plenty of functionality.  For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo run -q -- --help
The Amazing Greeter 1.0
Matt Davies
The best greeter in town!

USAGE:
    greet

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The standard help information!  The &lt;code&gt;--&lt;/code&gt; in the command &lt;code&gt;cargo run -- --help&lt;/code&gt; separates the arguments that go to Cargo from the arguments that go to our program.  So &lt;code&gt;--help&lt;/code&gt; is sent to our program and Clap dutifully follows it out.  The &lt;code&gt;-q&lt;/code&gt; flag sent to Cargo stops it from outputting build information ('q' is for quiet).  Let's try the version information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo run -q -- -V
The Amazing Greeter 1.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to add support for the name flag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;clap&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;App&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Arg&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;App&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The Amazing Greeter"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.version&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.author&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Matt Davies"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.about&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The best greeter in town!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.arg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nn"&gt;Arg&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.short&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.long&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.value_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"NAME"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.help&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Provides a name to use in the greeting"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.takes_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.default_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.get_matches&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt;&lt;span class="nf"&gt;.value_of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"NAME: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;.arg()&lt;/code&gt; takes a builder that is created with &lt;code&gt;Arg::with_name()&lt;/code&gt;.  From this builder, we can set all the properties for the option.  First, we give it a short name (a single character) and a long name.  This allows the option to be provided with &lt;code&gt;-n&lt;/code&gt; or &lt;code&gt;--name&lt;/code&gt;.  &lt;code&gt;value_name()&lt;/code&gt; and &lt;code&gt;help()&lt;/code&gt; provide extra information for the help information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo run -q -- --help
The Amazing Greeter 1.0
Matt Davies
The best greeter in town!

USAGE:
    greet [OPTIONS]

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
    -n, --name &amp;lt;NAME&amp;gt;    Provides a name to use in the greeting [default: World]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;value_name()&lt;/code&gt; gives the name between the angled brackets and &lt;code&gt;help()&lt;/code&gt; provides the text afterwards.  Of course, the information from &lt;code&gt;short()&lt;/code&gt; and &lt;code&gt;long()&lt;/code&gt; are used too.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;takes_value()&lt;/code&gt; tells clap that &lt;code&gt;--name&lt;/code&gt; requires a value to follow after it and &lt;code&gt;default_value()&lt;/code&gt; provides a value if the option is omitted.  You may also have noticed that hyphened names are called flags unless they have values following them.  In that case, they are called options.  You may have additionally noticed that the value you passed to &lt;code&gt;default_value()&lt;/code&gt; is also shown in the help information.&lt;/p&gt;

&lt;p&gt;The intent for how this option is interacted with is much, much clearer than the original naive C-like code we used before.  Let's test it further:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo run -q
NAME: World
$ cargo run -q --name Matt
NAME: Matt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's continue and add our sub-commands &lt;code&gt;hello&lt;/code&gt; and &lt;code&gt;bye&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;clap&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;App&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Arg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;App&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The Amazing Greeter"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.version&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.author&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Matt Davies"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.about&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The best greeter in town!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.arg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nn"&gt;Arg&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.short&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.long&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.value_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"NAME"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.help&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Provides a name to use in the greeting"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.takes_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.default_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.subcommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nn"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.about&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Let's meet!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.version&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.author&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Matt Davies (again!)"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.subcommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nn"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bye"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.about&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"We part ways"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.version&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.author&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Matt Davies (again!)"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.get_matches&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt;&lt;span class="nf"&gt;.value_of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt;&lt;span class="nf"&gt;.subcommand_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bye"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Goodbye {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"No idea what to do!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;interestingly, the sub-commands have their own &lt;code&gt;about()&lt;/code&gt;, &lt;code&gt;version()&lt;/code&gt; and &lt;code&gt;author()&lt;/code&gt; calls in the builder generated by &lt;code&gt;SubCommand::with_name()&lt;/code&gt;.  So let's try some help:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo r --q -- -h
The Amazing Greeter 1.0
Matt Davies
The best greeter in town!

USAGE:
    greet [OPTIONS] [SUBCOMMAND]

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
    -n, --name &amp;lt;NAME&amp;gt;    Provides a name to use in the greeting [default: World]

SUBCOMMANDS:
    bye      We part ways
    hello    Let's meet!
    help     Prints this message or the help of the given subcommand(s)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we have a subcommands section with the added subcommand &lt;code&gt;help&lt;/code&gt; added:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cargo r -q -- help hello
greet-hello 1.0
Matt Davies (again!)
Let's meet!

USAGE:
    greet hello

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the name of the application is the concatenation of the application name and the subcommand: &lt;code&gt;greet-hello&lt;/code&gt;.  I find this interesting as this implies you can add plugins by creating programs that are called &lt;code&gt;greet-&amp;lt;command&amp;gt;&lt;/code&gt; providing that &lt;code&gt;greet&lt;/code&gt; generates the filename if the subcommand is unknown, and calls it.  This is exactly how Cargo works.  I wonder if Cargo uses clap?  Looking at its &lt;a href="https://crates.io/crates/cargo/0.55.0/dependencies"&gt;dependencies&lt;/a&gt; we can see that yes, indeed, it does.  Will Clap execute plugins automatically?  Let's try it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo r -q -- test
error: Found argument 'test' which wasn't expected, or isn't valid in this context

USAGE:
    greet [OPTIONS] [SUBCOMMAND]

For more information try --help
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No, it doesn't.  But it's great to see that the usage description is generated according to the fact we have added options and subcommands.&lt;/p&gt;

&lt;p&gt;Ok, now let's try our program out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo r -q
No idea what to do!
$ cargo r -q -- --name Matt
No idea what to do!
$ cargo r -q -- --name Matt hello
Hello Matt
$ cargo r -q -- --name Bob bye
Goodbye Bob
$ cargo r -q -- hello --name Matt
error: Found argument '--name' which wasn't expected, or isn't valid in this context

USAGE:
    greet hello

For more information try --help
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, that's not good.  It seems that with Clap, the options before a subcommand are considered different than the options passed after a subcommand.  We can add arguments to the subcommands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;clap&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;App&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Arg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;App&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The Amazing Greeter"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.version&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.author&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Matt Davies"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.about&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The best greeter in town!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.arg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nn"&gt;Arg&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.short&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.long&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.value_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"NAME"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.help&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Provides a name to use in the greeting"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.takes_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.default_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.subcommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nn"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.about&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Let's meet!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.version&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.author&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Matt Davies (again!)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.arg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="nn"&gt;Arg&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="nf"&gt;.short&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="nf"&gt;.long&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="nf"&gt;.value_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"NAME"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="nf"&gt;.help&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Provides a name to use in the greeting"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="nf"&gt;.takes_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                &lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.subcommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nn"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bye"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.about&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"We part ways"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.version&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.author&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Matt Davies (again!)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;.arg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="nn"&gt;Arg&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="nf"&gt;.short&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="nf"&gt;.long&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="nf"&gt;.value_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"NAME"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="nf"&gt;.help&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Provides a name to use in the greeting"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="nf"&gt;.takes_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                &lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.get_matches&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt;&lt;span class="nf"&gt;.value_of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt;&lt;span class="nf"&gt;.subcommand_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;submatches&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt;&lt;span class="nf"&gt;.subcommand_matches&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;submatches&lt;/span&gt;&lt;span class="nf"&gt;.value_of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap_or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bye"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;submatches&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt;&lt;span class="nf"&gt;.subcommand_matches&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bye"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;submatches&lt;/span&gt;&lt;span class="nf"&gt;.value_of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap_or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"No idea what to do!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few things to take note of.  Firstly, we don't provide default values for the subcommand options as the global option can provide that.&lt;/p&gt;

&lt;p&gt;Secondly, to access the local options of a subcommand we need to grab its matches with &lt;code&gt;subcommand_matches()&lt;/code&gt; that may or may not exist.  Since we've already matched the name, we can unwrap it here with safety.  Then we fetch the value if it exists, or use the global name value.  The global name should always have a value because it has a default one.&lt;/p&gt;

&lt;p&gt;This works quite well now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo r -q -- hello --name Matt
Hello Matt
$ cargo r -q -- bye --name Matt
Goodbye Matt
$ cargo r -q -- hello
Hello World
$ cargo r -q -- --name Matt hello
Hello Matt
$ cargo r -q -- --name Matt hello --name Bob
Hello Bob
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OK, it's not perfect since it doesn't detect two &lt;code&gt;--name&lt;/code&gt; options, but perhaps we can remove the global option and ensure that the two subcommand options have default values.&lt;/p&gt;

&lt;p&gt;There is a lot more you can do with Clap and I encourage you to explore the &lt;a href="https://docs.rs/clap"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But there's a better, more concise way to interact with Clap.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;Structopt&lt;/code&gt; crate
&lt;/h2&gt;

&lt;p&gt;What problem with Clap is that it is very verbose to describe the options and to use them.  A better way to access the command line data would be to place it all in a structure hierarchy.  Something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;CommandLineData&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;subcommand&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;SubCommand&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HelloData&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;Bye&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ByeData&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;HelloData&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;ByeData&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But we still have to extract the information from Clap and insert it into that data structure.  And, you've guessed it, is exactly what &lt;code&gt;Structopt&lt;/code&gt; strives to do.&lt;/p&gt;

&lt;p&gt;Through the use of macros and a function call, &lt;code&gt;Structopt&lt;/code&gt; can generate the calls to Clap to declare the configuration and to extract the data into your structures.  It is an extremely useful crate.&lt;/p&gt;

&lt;p&gt;First, replace the &lt;code&gt;clap&lt;/code&gt; entry in &lt;code&gt;Cargo.toml&lt;/code&gt; with &lt;code&gt;structopt = "0.3"&lt;/code&gt;.  And replace &lt;code&gt;main.rs&lt;/code&gt; with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;structopt&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;StructOpt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Debug,&lt;/span&gt; &lt;span class="nd"&gt;StructOpt)]&lt;/span&gt;
&lt;span class="nd"&gt;#[structopt(&lt;/span&gt;
    &lt;span class="nd"&gt;name&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"The Amazing Greeter"&lt;/span&gt;&lt;span class="nd"&gt;,&lt;/span&gt;
    &lt;span class="nd"&gt;about&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"The best greeter in town"&lt;/span&gt;&lt;span class="nd"&gt;,&lt;/span&gt;
    &lt;span class="nd"&gt;author&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Matt Davies"&lt;/span&gt;
&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;CommandLineData&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;#[structopt(subcommand)]&lt;/span&gt;
    &lt;span class="n"&gt;subcommand&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;SubCommand&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Debug,&lt;/span&gt; &lt;span class="nd"&gt;StructOpt)]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;SubCommand&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/// Let's meet!&lt;/span&gt;
    &lt;span class="nf"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HelloData&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;

    &lt;span class="cd"&gt;/// Let's part ways&lt;/span&gt;
    &lt;span class="nf"&gt;Bye&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ByeData&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Debug,&lt;/span&gt; &lt;span class="nd"&gt;StructOpt)]&lt;/span&gt;
&lt;span class="nd"&gt;#[structopt(author&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Matt Davies"&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;HelloData&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/// Provides a name to use in the greeting.&lt;/span&gt;
    &lt;span class="nd"&gt;#[structopt(short&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"n"&lt;/span&gt;&lt;span class="nd"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;long&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="nd"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;default_value&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"World"&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Debug,&lt;/span&gt; &lt;span class="nd"&gt;StructOpt)]&lt;/span&gt;
&lt;span class="nd"&gt;#[structopt(author&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Matt Davies (again!)"&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;ByeData&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/// Provides a name to use in the greeting.&lt;/span&gt;
    &lt;span class="nd"&gt;#[structopt(short&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"n"&lt;/span&gt;&lt;span class="nd"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;long&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="nd"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;default_value&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"World"&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;opt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;CommandLineData&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_args&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;opt&lt;/span&gt;&lt;span class="py"&gt;.subcommand&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Bye&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Goodbye {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nb"&gt;None&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Not sure what to do!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every structure and enum used to house command-line data must start with &lt;code&gt;#[derive(Debug, StructOpt)]&lt;/code&gt;.  The &lt;code&gt;Debug&lt;/code&gt; trait needs to be there to provide error messages when things go wrong.  The &lt;code&gt;StructOpt&lt;/code&gt; trait is there to do the magic.  Later in the structures and enums, we have &lt;code&gt;#[structopt()]&lt;/code&gt; attribute macros and documentation comments to help with the meta-data.  For example, the document comments for the &lt;code&gt;Hello&lt;/code&gt; variant in &lt;code&gt;SubCommand&lt;/code&gt; is used to generate the string that's passed to &lt;code&gt;about()&lt;/code&gt; in Clap and is used for documentation.  Two birds with one stone (that expression seems very cruel to me!).&lt;/p&gt;

&lt;p&gt;One difference I noticed was that version information throughout the declaration is lifted directly from &lt;code&gt;Cargo.toml&lt;/code&gt; and so different subcommands cannot have different versions.  For me, this makes much more sense!&lt;/p&gt;

&lt;p&gt;Finally, a quick call to &lt;code&gt;from_args()&lt;/code&gt; method on your top-level data structure will construct an instance.  The code that uses the command line data ends up being very concise.&lt;/p&gt;

&lt;p&gt;As with Clap, StructOpt has lots of features that I encourage you to discover in the documentation.  For example, you can declare an option as being required if another option is used.  Also, you can collect all non-flag and non-option arguments into an array by just declaring a &lt;code&gt;Vec&lt;/code&gt; field in your structure.  Flags are easily produced using a &lt;code&gt;bool&lt;/code&gt; type field.  You may have noticed that &lt;code&gt;name&lt;/code&gt; didn't require explicit information that it takes an argument.  &lt;code&gt;StructOpt&lt;/code&gt; was able to figure that out all by itself because it was of type &lt;code&gt;String&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But there's one more thing we can do...&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;Paw&lt;/code&gt; crate
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Paw&lt;/code&gt; allows us to treat the command line data structure as an argument to &lt;code&gt;main()&lt;/code&gt;.  In C and C++, you access the command line arguments via arguments passed to the &lt;code&gt;main&lt;/code&gt; function but Rust handles it differently.  You have to call a function to fetch them using &lt;code&gt;std::env::args()&lt;/code&gt;.  But with Paw, you can have the same functionality.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;Cargo.toml&lt;/code&gt;, change the dependencies to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[dependencies]&lt;/span&gt;
&lt;span class="nn"&gt;structopt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="py"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="py"&gt;features&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;["paw"]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="py"&gt;paw&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1.0"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and now &lt;code&gt;main&lt;/code&gt; can be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[paw::main]&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;opt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;CommandLineData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;opt&lt;/span&gt;&lt;span class="py"&gt;.subcommand&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Bye&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Goodbye {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nb"&gt;None&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Not sure what to do!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;paw::main&lt;/code&gt; macro wraps our main and transforms it into one that can take a single argument containing our command line data.  No more calls to &lt;code&gt;from_args()&lt;/code&gt;.  You're not saving much but I thought this crate was quite cute.  Very rust-like.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I showed you a terrible naive way of writing a program that processed the command-line, and then I showed you how to improve the situation drastically using &lt;code&gt;Clap&lt;/code&gt;.  Following that, I showed you that you didn't require much code at all and &lt;code&gt;StructOpt&lt;/code&gt; allowed you to pack all your command line data into your own data structures whose very structure described the command-line use.  Finally, I showed you &lt;code&gt;Paw&lt;/code&gt; that allowed the command-line structure to be passed directly to your &lt;code&gt;main&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Hopefully, I have shown you that Rust, along with a few crates, is the best language to use for processing command-line arguments.&lt;/p&gt;

&lt;p&gt;So, journey on and write some amazing CLI programs!&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
    <item>
      <title>Rust #6: Exploring crates</title>
      <dc:creator>Matt Davies</dc:creator>
      <pubDate>Sat, 24 Jul 2021 15:33:44 +0000</pubDate>
      <link>https://dev.to/cthutu/rust-6-exploring-crates-3p6i</link>
      <guid>https://dev.to/cthutu/rust-6-exploring-crates-3p6i</guid>
      <description>&lt;p&gt;I often install tools via cargo and use crates for my code that have many dependencies.  If you're like me, you are wondering when downloading and compiling what all those crates do.  I could just look at the top N popular crates on &lt;a href="http://crates.io" rel="noopener noreferrer"&gt;http://crates.io&lt;/a&gt; but I thought that was boring.  Rather, I thought I'd clone the &lt;code&gt;exa&lt;/code&gt; command-line tool from Github and see what crates it used.  Better to look at a released tool that is out in the wild?&lt;/p&gt;

&lt;p&gt;The 'exa' tool is a Rust-built drop-in replacement for the Unix command &lt;code&gt;ls&lt;/code&gt;.  It allows me to produce listings like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7zlrjafaqqin2t3gvs9k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7zlrjafaqqin2t3gvs9k.png" alt="exa-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A simple command shows us the crate dependency hierarchy for this tool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo tree
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The top-level list of dependencies are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ansi_term&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;datetime&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;glob&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;lazy_static&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;libc&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;locale&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;log&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;natord&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;num_cpus&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;number_prefix&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;scoped_threadpool&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;term_grid&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;term_size&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;unicode-width&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;users&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;zoneinfo_compiled&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some interesting crates that I recognise and are dependencies of the above also appeared:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;bitflags&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;byteorder&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;matches&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pad&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;tinyvec&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;url&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So now I will boot up my favourite browser, go to &lt;a href="http://docs.rs" rel="noopener noreferrer"&gt;http://docs.rs&lt;/a&gt; and figure out what all these crates do and see if any are useful.  At least cursory knowledge of them will stop me from reinventing the wheel if I need their functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;ansi_term&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This is a library that allows the generation of ANSI control codes that allow for colour and formatting (i.e. bold, italic, etc.) on your terminal. If you are not sure what ANSI control codes are, this link &lt;a href="https://en.wikipedia.org/wiki/ANSI_escape_code" rel="noopener noreferrer"&gt;here at Wikipedia&lt;/a&gt; should explain it. It provides styles and colours via the builder pattern or a colour enum. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;ansi_term&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Colour&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Yellow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"My name is {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Yellow&lt;/span&gt;&lt;span class="nf"&gt;.bold&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.paint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Matt"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's support for blink, bold, italic, underline, inverse (confusingly call reverse here), 256 colours and 24-bit colours.  This is a very useful crate for wrapping strings with the ANSI control codes that you need.&lt;/p&gt;

&lt;p&gt;But I have seen crates that do this with different syntax using extension traits on &lt;code&gt;&amp;amp;str&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt;.  The one that I use frequently is &lt;code&gt;colored&lt;/code&gt;.  For example, to recreate the last snippet using &lt;code&gt;colored&lt;/code&gt;, it is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;colored&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Colorize&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"My name is {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Matt"&lt;/span&gt;&lt;span class="nf"&gt;.yellow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.bold&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I prefer the latter form, but it is totally subjective.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;datetime&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This is one crate I am familiar with.  Unfortunately, I found the standard's library time and date support lacking for a tool I was writing.  Some research unearthed this crate that provides a lot more functionality.  Be careful though, there is another crate called &lt;code&gt;date_time&lt;/code&gt; that does similar stuff.  The &lt;code&gt;datetime&lt;/code&gt; library here provides structures for representing a calendar date and time of day in both timezone and local form.  It also provides formatting functions that convert a date or time into a string and back again, but unfortunately no documentation on how to use it.  The &lt;code&gt;format&lt;/code&gt; function takes a time value but also takes a locale of type &lt;code&gt;Time&lt;/code&gt; but no information on how to generate that.  I couldn't figure it myself.&lt;/p&gt;

&lt;p&gt;My go-to date and time crate that I use is &lt;code&gt;chrono&lt;/code&gt;.  It is fully featured, efficient and better documented.  You can do UTC, local and fixed offset times (times in a certain timezone).  It can format to and parse from strings containing times and dates.  Times also have nanosecond accuracy.  Times and dates are complicated things and &lt;code&gt;chrono&lt;/code&gt; is a useful weapon in your coding arsenal.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;bitflags&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This is a very useful crate for generating enums that are bitmasks.  You often see this in low-level programming or when dealing the operating system interfaces.  Unlike normal enums, bitflag enums can be combined using logical operators.  This is trivial to do in C but is not supported in Rust.  In C, you could do something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Flags&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x01&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;C&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x04&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;ABC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;ab&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Rust, enums are distinct and cannot be combined like that.  With &lt;code&gt;bitflags&lt;/code&gt; you can:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;bitflags&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;bitflags&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;bitflags!&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Flags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u8&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0b001&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0b010&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0b100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ABC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="py"&gt;.bits&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="k"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="py"&gt;.bits&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="k"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="py"&gt;.bits&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;ab&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Flags&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nn"&gt;Flags&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not as eloquent as C, but at least the enumerations are scoped like C++'s &lt;code&gt;enum class&lt;/code&gt;.  However, Rust's &lt;code&gt;bitflags&lt;/code&gt; crate does support set difference using the &lt;code&gt;-&lt;/code&gt; operator.  This would go very wrong in C and C++ as &lt;code&gt;-&lt;/code&gt; would be treated as a normal integer subtract.  For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;ac&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Flags&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ABC&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nn"&gt;Flags&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;we_dont_want_b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ac&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nn"&gt;Flags&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;would do the right thing.  The equivalent code in C would not.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;byteorder&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This simple crate allows reading and writing values in little-endian or big-endian byte order.  The standard library does have some support with the &lt;code&gt;to_le_bytes&lt;/code&gt; et al. family of functions on the integer primitive types so a lot of this crate is redundant now.  Where this crate is useful is with implementing the &lt;code&gt;Read&lt;/code&gt; and &lt;code&gt;Write&lt;/code&gt; interfaces.&lt;/p&gt;

&lt;p&gt;If you're wondering what endian means, it refers to how computers store numeric values that require more than a byte to store.  For example, with &lt;code&gt;u32&lt;/code&gt; it takes 4 bytes of storage.  There are 2 conventional ways of storing this value.  You could put the high bytes first (big-endian) or the low bytes first (little-endian) into memory.  So for example, the number &lt;code&gt;42&lt;/code&gt; could be stored as the bytes &lt;code&gt;42, 0, 0, 0&lt;/code&gt; or the bytes &lt;code&gt;0, 0, 0, 42&lt;/code&gt;.  Most modern CPUs, by default, support the former, which is little-endian.  However, data that goes over the network is usually big-endian.  So these routines are critical for putting the data in the correct form.  There is also a third convention called &lt;code&gt;native-endian&lt;/code&gt; that is either little or big depending on the CPU's preferred form.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;git2&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This crate offers bindings over the C-based &lt;code&gt;libgit2&lt;/code&gt; library.  &lt;code&gt;exa&lt;/code&gt; uses this to implement its &lt;code&gt;.gitignore&lt;/code&gt; support.  This is a large crate and way beyond the scope of this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;glob&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;One of the main obvious jobs of &lt;code&gt;exa&lt;/code&gt; is to iterate over all the files in a directory.  &lt;code&gt;glob&lt;/code&gt; does this with the bonus that you can use the wildcards &lt;code&gt;*&lt;/code&gt; and &lt;code&gt;**&lt;/code&gt;.  It provides a single function &lt;code&gt;glob&lt;/code&gt; that takes a file pattern and gives back an iterator returning paths.  For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Read in all the markdown articles under all folders in my blogs folder.&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;glob&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;glob&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;glob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"~/blogs/**/*.md"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// path is a PathBuf&lt;/span&gt;
        &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;

        &lt;span class="c1"&gt;// e is a GlobError&lt;/span&gt;
        &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;eprintln!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course, being an iterator, you can run it through all the iteration operators, such as &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt; etc.  But there is no need to sort since paths are yielded in alphabetical order.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;lazy_static&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Now this is a crate that is often used.  Normally, static variables cannot have run-time calculated values.  Try this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Number = {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;MY_STATIC&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;MY_STATIC&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mi"&gt;42&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will be greeted with the error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
 --&amp;gt; src/main.rs:5:25
  |
5 | static MY_STATIC: u32 = foo();
  |                         ^^^^^
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;lazy_static&lt;/code&gt; it becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;lazy_static&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;lazy_static&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Number = {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;MY_STATIC&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;lazy_static!&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;MY_STATIC&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mi"&gt;42&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are three main changes to the code.  Firstly, there's the &lt;code&gt;lazy_static!&lt;/code&gt; macro to wrap the static declarations.  Secondly, there's an added &lt;code&gt;ref&lt;/code&gt; keyword.  The statics returned here are references to your type.  Using them invokes the &lt;code&gt;Deref&lt;/code&gt; trait.  This means that thirdly, I had to dereference it so that the &lt;code&gt;Display&lt;/code&gt; trait was detectable for &lt;code&gt;u32&lt;/code&gt;.  In Rust, &lt;code&gt;Deref&lt;/code&gt; is not invoked when looking for traits so I had to do it manually.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;libc&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;There is a vast sea of C code out there implementing many useful libraries.  To speed up Rust's adoption, it was required not to rewrite many of these libraries in Rust.  Fortunately, the designers of Rust realised that and made it easy to interoperate with C.  &lt;code&gt;libc&lt;/code&gt; provides more support to interoperate with C code.  It adds type definitions (like &lt;code&gt;c_int&lt;/code&gt;), constants and function headers for standard C functions (e.g. &lt;code&gt;malloc&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;locale&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This crate is documented as mostly useless as it is being rewritten for its version &lt;code&gt;0.3&lt;/code&gt;.  This provides information on how to format numbers and time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;locale&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;user_locale_factory&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;numeric_locale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="nf"&gt;.get_numeric&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;"Numbers: decimal sep: {} thousands sep: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;numeric_locale&lt;/span&gt;&lt;span class="py"&gt;.decimal_sep&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numeric_locale&lt;/span&gt;&lt;span class="py"&gt;.thousands_sep&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="nf"&gt;.get_time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Time:"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;"  January: Long: {}, Short: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="nf"&gt;.long_month_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="nf"&gt;.short_month_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;"  Monday: Long: {}, Short: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="nf"&gt;.long_day_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="nf"&gt;.short_day_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This outputs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Numbers: decimal sep: . thousands sep: ,
Time:
  January: Long: January, Short: Jan
  Monday: Long: Mon, Short: Mon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mmmm... there seems to be a bug at the time of writing.  Surely &lt;code&gt;time.long_day_name(1)&lt;/code&gt; should return &lt;code&gt;Monday&lt;/code&gt; and not &lt;code&gt;Mon&lt;/code&gt;.  Whether this is an operating system issue or a problem with &lt;code&gt;locale&lt;/code&gt;, I am not sure.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;log&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This crate provides an interface for logging.  The user is expected to provide the implementation of the logger.  This can be done through other crates such as &lt;code&gt;env_logger&lt;/code&gt;, &lt;code&gt;simple_logger&lt;/code&gt; and few other crates.  &lt;code&gt;log&lt;/code&gt; is not used directly by &lt;code&gt;exa&lt;/code&gt; itself, but rather some of its dependencies.&lt;/p&gt;

&lt;p&gt;Essentially, it provides a few macros such as &lt;code&gt;error&lt;/code&gt; and &lt;code&gt;warn!&lt;/code&gt; to pass formatted messages to a logger.  There are multiple levels of logging and they range from &lt;code&gt;trace!&lt;/code&gt; to &lt;code&gt;error!&lt;/code&gt; in order of rising priority:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;trace!&lt;/li&gt;
&lt;li&gt;debug!&lt;/li&gt;
&lt;li&gt;info!&lt;/li&gt;
&lt;li&gt;warn!&lt;/li&gt;
&lt;li&gt;error!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I think this crate is missing a &lt;code&gt;fatal!&lt;/code&gt; as most logging systems contain these levels.&lt;/p&gt;

&lt;p&gt;Log messages can be filtered by log level, with the lowest level restricting more lower-level messages and the highest level showing all messages.  This is set using the &lt;code&gt;set_max_level&lt;/code&gt; function.  By default, it is set to &lt;code&gt;Off&lt;/code&gt; and no messages are sent to loggers.  Levels can also be set at compile-time using various features.  All of this is described in the &lt;a href="https://docs.rs/log/latest/log" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Loggers implement the &lt;code&gt;Log&lt;/code&gt; trait and users install them by calling the &lt;code&gt;set_logger&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;How should you use the logging levels?  Below I provide some opinionated guidance:&lt;/p&gt;

&lt;h3&gt;
  
  
  Trace
&lt;/h3&gt;

&lt;p&gt;Very fine-grained information is provided at this level.  This is very verbose and high traffic.  You could use this to annotate each step of an algorithm.  Or for logging function parameters.&lt;/p&gt;

&lt;h3&gt;
  
  
  Debug
&lt;/h3&gt;

&lt;p&gt;Used for everyday use and diagnosing issues.  You should rarely submit code that outputs to debug level. At the very least it shouldn't output in release builds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Info
&lt;/h3&gt;

&lt;p&gt;The standard logging level for describing changes in application state.  For example, logging that a user has been created.  This should be purely informative and not contain important information.&lt;/p&gt;

&lt;h3&gt;
  
  
  Warn
&lt;/h3&gt;

&lt;p&gt;This describes that something unexpected happened in the application.  However, this does not mean that the application failed and as a result work can continue.  Perhaps a warning could be a missing file that the application tried to load but does not necessarily require it for running (e.g. a configuration file).&lt;/p&gt;

&lt;h3&gt;
  
  
  Error
&lt;/h3&gt;

&lt;p&gt;Something bad happened to stop the application from performing a task.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;matches&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Allows a check to see if an expression matches a Rust pattern via a macro:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Macro version&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;macros!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_expr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;A&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;// is the same as:&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;my_expr&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;A&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// or even:&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nn"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;A&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_expr&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It also provides assert versions as well.  It is just a small convenience crate.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;natord&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;If you were to sort these strings using normal method: &lt;code&gt;["foo169", "foo42", "foo2"]&lt;/code&gt;, you would get the sequence &lt;code&gt;["foo169", "foo2", "foo42"]&lt;/code&gt;.  This might not be the order you would prefer.  What you might want is sometimes referred to as &lt;strong&gt;normal ordering&lt;/strong&gt;.  You might prefer the order &lt;code&gt;["foo2", "foo42", "foo169"]&lt;/code&gt; where the numbers in the strings increase in value and not by ASCII ordering.&lt;/p&gt;

&lt;p&gt;This functionality is what &lt;code&gt;natord&lt;/code&gt; provides.  Natural ordering can work well for filenames with numbering inside them and IP addresses to name just a couple.  For example, natural ordering will handle strings where the numbers are in the middle (e.g. "myfile42.txt") and not just at the end.&lt;/p&gt;

&lt;p&gt;I am sure I will make use of this crate at some point in the future.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;num_cpus&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Short and sweet this one.  It provides a &lt;code&gt;get()&lt;/code&gt; function to obtain the number of logical cores on the running system, and &lt;code&gt;get_physical()&lt;/code&gt; function to obtain the number of physical cores.  Very useful if you want to set up a worker thread pool.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;number_prefix&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This crate determines the prefix of numerical units.  Given a number, it determines whether there should be a prefix such as "kilo", "mega", "K", "M" etc., or not.  It can even handle prefixes that describe binary multipliers such as 1024, something that many programmers will appreciate, but a prefix like &lt;code&gt;K&lt;/code&gt; will not be used.  Rather &lt;code&gt;Ki&lt;/code&gt; will be used.&lt;/p&gt;

&lt;p&gt;And given a prefix, it can be converted into its full name.  For example, the prefix for 1000 would be &lt;code&gt;K&lt;/code&gt;, but call &lt;code&gt;upper()&lt;/code&gt; on it and you will get &lt;code&gt;KILO&lt;/code&gt;, call &lt;code&gt;lower()&lt;/code&gt; for &lt;code&gt;kilo&lt;/code&gt; and &lt;code&gt;caps()&lt;/code&gt; for &lt;code&gt;Kilo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is very useful for listing file lengths as &lt;code&gt;exa&lt;/code&gt; is required to do.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;pad&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This crate is used for padding strings at run-time.  The standard library &lt;code&gt;format!&lt;/code&gt; function can do a lot of padding functionality but this crate can do more.&lt;/p&gt;

&lt;p&gt;For example, it can add spaces at the front to right-align a string within a set field width:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"to the right!"&lt;/span&gt;&lt;span class="nf"&gt;.pad_to_width_with_alignment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;Alignment&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Neat huh?&lt;/p&gt;

&lt;p&gt;You don't have to pad with spaces either.  It can use any character you wish.  It will also truncate strings that are too long for a particular width.&lt;/p&gt;

&lt;p&gt;If you need to format the textual output on a terminal, you may need to look this crate up.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;scoped_threadpool&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This can produce a struct that manages a pool of threads.  These threads can be used to run closures that can access variables in the original scope.  This is useful because normally threads can only access values of &lt;code&gt;'static&lt;/code&gt; lifetime or are entirely owned inside the thread.&lt;/p&gt;

&lt;p&gt;Let us look at the example in the documentation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// We create a pool of 4 threads to be utilised later.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Pool&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Some data we can do work on and reference to.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="c1"&gt;// Use the pool of threads and give them access to the scope of `main`.&lt;/span&gt;
    &lt;span class="c1"&gt;// `vec` is guaranteed to have a lifetime longer than the work we&lt;/span&gt;
    &lt;span class="c1"&gt;// will do on the threads.&lt;/span&gt;
    &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="nf"&gt;.scoped&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Now we grab a reference to each element in the vector.&lt;/span&gt;
        &lt;span class="c1"&gt;// Remember `vec` is still around during `pool.scoped()`.&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Do some work on the threads - we move the reference&lt;/span&gt;
            &lt;span class="c1"&gt;// in as its mutably borrowed.  We still cannot mutably&lt;/span&gt;
            &lt;span class="c1"&gt;// borrow a reference in the for loop and the thread.&lt;/span&gt;
            &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="nf"&gt;.execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;move&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// Mutate the elements.  This is allowed as the lifetime&lt;/span&gt;
                &lt;span class="c1"&gt;// of the element is guaranteed to be longer than the&lt;/span&gt;
                &lt;span class="c1"&gt;// work we're doing on the thread.&lt;/span&gt;
                &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This crate allows us to create works on threads that we know will not outlive other variables in the same scope.  &lt;code&gt;pool.scoped&lt;/code&gt; must block until all work is done to allow this to happen.&lt;/p&gt;

&lt;p&gt;This is very useful for quickly doing short-lived jobs in parallel.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;term_grid&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;To produce standard &lt;code&gt;ls&lt;/code&gt; output, &lt;code&gt;exa&lt;/code&gt; must show filenames in a grid formation.  Given a width, this crate provides a function &lt;code&gt;fit_into_width&lt;/code&gt; that can help to produce a grid of strings.  By working out the longest string in a collection, it can calculate how many of those strings can fit in horizontal space, like say, the line on a terminal.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;term_size&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Very simple but crucial if you want to provide textual output on a terminal in a highly formatted way.  It uses ANSI control codes to communicate with your terminal to figure out the size of your terminal view.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="nn"&gt;term_size&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;dimensions&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;None&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The terminal, possibly, may not report the dimensions (although most do) and so the result of &lt;code&gt;dimensions()&lt;/code&gt; is an &lt;code&gt;Option&amp;lt;(usize, usize)&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;tinyvec&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This mainly provides 2 vector types: &lt;code&gt;ArrayVec&lt;/code&gt; and &lt;code&gt;TinyVec&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ArrayVec&lt;/code&gt; is a safe array-backed drop-in replacement for &lt;code&gt;Vec&lt;/code&gt;.  This means that a fixed array is allocated to act as storage for &lt;code&gt;ArrayVec&lt;/code&gt;.  In any other way, &lt;code&gt;ArrayVec&lt;/code&gt; acts like a &lt;code&gt;Vec&lt;/code&gt; but it cannot reallocate that storage.  If the array becomes too big a panic will occur.&lt;/p&gt;

&lt;p&gt;This is very useful to avoid reallocations all the time.  &lt;code&gt;ArrayVec&lt;/code&gt; even allows access to the unused space in the backing array that is currently being used for elements.&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;ArrayVec&lt;/code&gt; uses a fixed array, a heap allocation does not occur.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;TinyVec&lt;/code&gt; is a drop-in replacement for &lt;code&gt;Vec&lt;/code&gt; too.  However, for small arrays, it starts life as an &lt;code&gt;ArrayVec&lt;/code&gt; using a fixed array as the backing store.  As soon as it becomes too big, it will automatically revert to use a normal &lt;code&gt;Vec&lt;/code&gt;, hence using the heap to store the array.&lt;/p&gt;

&lt;p&gt;This requires the &lt;code&gt;alloc&lt;/code&gt; feature to be activated.&lt;/p&gt;

&lt;p&gt;It is basically an enum that can be an &lt;code&gt;ArrayVec&lt;/code&gt; or a &lt;code&gt;Vec&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;TinyVec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Inline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ArrayVec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;Heap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nn"&gt;A&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You have got to love algebraic types!  You have to provide the backing store type and a macro helps you do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_vec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;tiny_vec!&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example can only store the first 16 elements on the stack before it switches to a more regular &lt;code&gt;Vec&amp;lt;u8&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;unicode-width&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Unicode characters can be wide, and on the terminal, it's important to know how many characters a Unicode character or string might take up.  There is a standard for working out that information and it is called the &lt;a href="http://www.unicode.org/reports/tr11/" rel="noopener noreferrer"&gt;Unicode Standard Annex #11&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is very useful when displaying filenames with international characters, which is exactly what &lt;code&gt;exa&lt;/code&gt; needs to deal with.  Fortunately, there is a crate that can provide that information.&lt;/p&gt;

&lt;p&gt;The documentation for &lt;code&gt;unicode-width&lt;/code&gt; does remind us that the character width may not match the rendered width.  So be careful but I don't think there is much you can do in these situations.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;url&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;A typical URL can code much information including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the protocol scheme (e.g. &lt;code&gt;http&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;the host (e.g. &lt;code&gt;docs.rs&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;a port number (e.g. the number in &lt;code&gt;http://docs.rs:8080&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;a username and password&lt;/li&gt;
&lt;li&gt;a path (e.g. &lt;code&gt;foo/bar&lt;/code&gt; in the URL &lt;code&gt;http://docs.rs/foo/bar&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;a query (e.g. everything after the &lt;code&gt;?&lt;/code&gt; in &lt;code&gt;myurl.com/foo?var=value&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A lot of these are optional and so a URL parsing library will need to handle this too.&lt;/p&gt;

&lt;p&gt;This crate provides a &lt;code&gt;Url::parse&lt;/code&gt; function that constructs an object describing the various parts of an URL.  Various methods can then be called to provide string slices into the URL such as &lt;code&gt;scheme()&lt;/code&gt; or &lt;code&gt;host()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This crate also provides &lt;code&gt;serde&lt;/code&gt; support too so if you know what that is, you will understand what this means.  Maybe I will write about &lt;code&gt;serde&lt;/code&gt; in a future article.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;users&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This is a very Unixy thing and so is useful on Mac OSX too.  To handle various permissions in a Unix operating system, a process is assigned an effective user ID.  This user ID and the various group IDs the user belongs to determines the permissions a process has within the operating system, including file and directory access.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;users&lt;/code&gt; crate provides this functionality by wrapping the standard C library functions that can obtain user and group information.&lt;/p&gt;

&lt;p&gt;If you have trace level logging on, this crate will log all interactions with the system.&lt;/p&gt;

&lt;p&gt;This crate also has a mocking feature to use pretend users and groups that you can set up for purposes of development.  You do not necessarily want to access real IDs.&lt;/p&gt;

&lt;p&gt;Even though this is a very Unixy thing, Windows does have similar permission features.  So this crate should work on Windows too.  Although I haven't tested this and the documentation does not say so.  But I have seen &lt;code&gt;exa&lt;/code&gt; work on Windows so I am assuming.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;zoneinfo_compiled&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This crate seems to get information directly from the operating system via &lt;code&gt;zoneinfo&lt;/code&gt; files.    This information allows you to obtain information on leap seconds and daylight savings time for your current time zone.&lt;/p&gt;

&lt;p&gt;This information is maintained by a single person and distributed and stored on your harddisks if you use Unix-based systems.  You can find this data in the &lt;code&gt;/usr/share/zoneinfo&lt;/code&gt; folder.  Each timezone has a binary file and this crate can parse this file and extract the information it holds.&lt;/p&gt;

&lt;p&gt;The whole proper handling of time and time zones is beyond me at this moment in time.  So I am not sure how &lt;code&gt;exa&lt;/code&gt; would be using this crate.  It's a very complex topic and something I would love to dig in deeper with and hopefully not go insane at the same time.&lt;/p&gt;

&lt;p&gt;Another name for the database is the &lt;code&gt;tz database&lt;/code&gt; and you can find more information about it, if you so desire, at &lt;a href="https://en.wikipedia.org/wiki/Tz_database" rel="noopener noreferrer"&gt;Wikipedia&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope you enjoyed this little trip into a few crates used by a single project.  I encourage all of you to try this simple exercise.  I learnt a lot by researching for this article and I still feel I have not even scratched the surface.&lt;/p&gt;

&lt;p&gt;Please write in the discussion below about interesting crates that you have found and used.  I would love to hear about them.&lt;/p&gt;

&lt;p&gt;Until next time!&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
    <item>
      <title>Rust #5: Naming conventions</title>
      <dc:creator>Matt Davies</dc:creator>
      <pubDate>Sat, 17 Jul 2021 12:58:37 +0000</pubDate>
      <link>https://dev.to/cthutu/rust-5-naming-conventions-3cjf</link>
      <guid>https://dev.to/cthutu/rust-5-naming-conventions-3cjf</guid>
      <description>&lt;p&gt;This week, I wanted to clarify in my head what the naming conventions are in the standard library if any, and to see if they help with communicating concepts in the API.&lt;/p&gt;

&lt;p&gt;A good API has well-established concepts and paradigms and a good language to communicate that. When you see a certain proposition in a name or verb, you understand what that API is trying to do. Good examples are MacOSX's Cocoa API. Bad examples are the Windows Win32 API. For me, the most important thing is consistency.&lt;/p&gt;

&lt;p&gt;I feel that Rust's &lt;code&gt;std&lt;/code&gt; library API is trying to achieve the same thing, and I want to explore what that language is.&lt;/p&gt;

&lt;p&gt;I have already seen some of this with the prefixes and suffixes of some methods of &lt;code&gt;Option&lt;/code&gt; and &lt;code&gt;Result&lt;/code&gt;, such as &lt;code&gt;or&lt;/code&gt; and &lt;code&gt;or_else&lt;/code&gt; and more simply the use of &lt;code&gt;is&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let us go through the tools that the &lt;code&gt;std&lt;/code&gt; library uses to convey information to us.&lt;/p&gt;

&lt;h2&gt;
  
  
  Casing
&lt;/h2&gt;

&lt;p&gt;The Rust compiler is very opinionated about what casing and style you use to name things, even giving warnings when you break its rules. You can disable those warnings if you wish. Initially, I HATED the casing rules as I was used to my styles with my C++ background, and I have to admit, was a contention point for me in using Rust. But after a week of writing Rust code, I soon got used to it and appreciate there is consistency if not exactly what I'd prefer.&lt;/p&gt;

&lt;p&gt;Here is a quick overview of the rules:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Convention&lt;/th&gt;
&lt;th&gt;Types that use it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;snake_case&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Crates, modules, functions, methods, local variables and parameters, lifetimes.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CamelCase&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Types (including traits and enums), type parameters in generics.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;SCREAMING_SNAKE_CASE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Constant and static variables.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This means that when you have a type name and you want to refer to it in a function name, you have to convert. So &lt;code&gt;YourType&lt;/code&gt; will become &lt;code&gt;your_type&lt;/code&gt;. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;BankAccount&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;find_bank_account&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Rc&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;BankAccount&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, this is general Rust stuff and not specific to the &lt;code&gt;std&lt;/code&gt; library, but I thought it was worth going over.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic types
&lt;/h2&gt;

&lt;p&gt;The standard library naming convention documentation lists various pieces of text that are in method names based on the types that they operate on:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type name&lt;/th&gt;
&lt;th&gt;Text in methods&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&amp;amp;T&lt;/td&gt;
&lt;td&gt;ref&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;amp;mut T&lt;/td&gt;
&lt;td&gt;mut&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;amp;[T]&lt;/td&gt;
&lt;td&gt;slice&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;amp;mut [T]&lt;/td&gt;
&lt;td&gt;mut_slice&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;amp;[u8]&lt;/td&gt;
&lt;td&gt;bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;amp;str&lt;/td&gt;
&lt;td&gt;str&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;*const T&lt;/td&gt;
&lt;td&gt;ptr&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;*mut T&lt;/td&gt;
&lt;td&gt;mut_ptr&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Get and Set methods
&lt;/h2&gt;

&lt;p&gt;Methods that fetch or set a value within their struct are usually called Getter and Setter methods.&lt;/p&gt;

&lt;p&gt;For Setter methods, the prefix &lt;code&gt;set_&lt;/code&gt; is used with the field name. For Getter methods, no prefix is used and just the field's name is. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;Self&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Getters&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;u16&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.age&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Setters&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;set_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="nf"&gt;.to_string&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;set_age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Suffixes
&lt;/h2&gt;

&lt;p&gt;Suffixes are pieces of text that appear at the end of a name. And the &lt;code&gt;std&lt;/code&gt; library has a few. Below is a list I've discovered.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Suffix&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;err&lt;/td&gt;
&lt;td&gt;Deals with the error part of a result&lt;/td&gt;
&lt;td&gt;Result::map_err()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;in&lt;/td&gt;
&lt;td&gt;This function uses a given allocator&lt;/td&gt;
&lt;td&gt;Vec::new_in&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;move&lt;/td&gt;
&lt;td&gt;Owned value version for a method that normally returns a referenced value&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;mut&lt;/td&gt;
&lt;td&gt;Mutable borrow version of method that normally returns owned or referenced value&lt;/td&gt;
&lt;td&gt;&amp;amp;str::split_at_mut()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ref&lt;/td&gt;
&lt;td&gt;Reference version for a method that normally returns an owned value&lt;/td&gt;
&lt;td&gt;Chunks::from_ref()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unchecked&lt;/td&gt;
&lt;td&gt;This function is unsafe - there may be dragons beyond!&lt;/td&gt;
&lt;td&gt;&amp;amp;str::get_unchecked()&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Due to the nature of Rust, many methods have variants. If a method &lt;code&gt;foo&lt;/code&gt; returns an immutably borrowed value, then there are sometimes variants that return a mutably borrowed or an ownable copy. The suffixes &lt;code&gt;mut&lt;/code&gt;, &lt;code&gt;move&lt;/code&gt; and &lt;code&gt;ref&lt;/code&gt; are used for this. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// original method&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;foo_mut&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// mutable version&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;foo_move&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// owned version&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// original version&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;bar_ref&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// immutably borrowed version&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;bar_mut&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// mutably borrowed version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, there are exceptions. For conversion to an iterator returning owned values, &lt;code&gt;into_iter()&lt;/code&gt; is used instead of &lt;code&gt;iter_move()&lt;/code&gt;. Also, if the method contains a type name and it returns a mutable borrow, the &lt;code&gt;mut&lt;/code&gt; appears before the type. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;as_bar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Bar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;as_mut_bar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;Bar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;err&lt;/code&gt; is used with &lt;code&gt;Results&lt;/code&gt; for methods that work with the error part instead of the OK part.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;unchecked&lt;/code&gt; states that this method or function is unsafe. Make sure you add your protections around it. This suffix should be a BIG red flag to any user wanting to stay within Rust's safety. Usually, the documentation contains information on what you should do around this function to make sure it's still safe. For example, on &lt;code&gt;&amp;amp;str::get_unchecked(i)&lt;/code&gt; the documentation asks that you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The starting index must not exceed the ending index.&lt;/li&gt;
&lt;li&gt;Indices must be within bounds of the original slice.&lt;/li&gt;
&lt;li&gt;Indices must lie on UTF-8 sequence boundaries.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, it may be that given index &lt;code&gt;i&lt;/code&gt;, your program has already ensured that these conditions are true; in which case you can go ahead and use &lt;code&gt;get_unchecked&lt;/code&gt; instead of &lt;code&gt;get&lt;/code&gt; because it will be more efficient. However, as a programmer, whenever you see the &lt;code&gt;unchecked&lt;/code&gt; suffix, you should be reaching for the documentation to see what those conditions are.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;in&lt;/code&gt; is not stable yet and Standard Library methods that use this suffix are only in the nightly compiler as of the time of writing. This indicates a variant that allows the user to pass in their allocator for allocation. For example, whereas &lt;code&gt;Vec::new&lt;/code&gt; creates a vector that will use the default allocator when adding elements, &lt;code&gt;Vec::new_in&lt;/code&gt; requires an allocator object that implements the &lt;code&gt;Allocator&lt;/code&gt; trait that will be responsible for allocations.&lt;/p&gt;

&lt;p&gt;Occasionally, there are times where you want to have multiple suffixes.  If you do and one of them is &lt;code&gt;mut&lt;/code&gt;, the convention is that &lt;code&gt;mut&lt;/code&gt; should be last.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prefixes
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Prefix&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;as&lt;/td&gt;
&lt;td&gt;Free conversion from original&lt;/td&gt;
&lt;td&gt;&amp;amp;[u8]::as_bytes()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;into&lt;/td&gt;
&lt;td&gt;Conversion that consumes original value&lt;/td&gt;
&lt;td&gt;String::into_bytes()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;is&lt;/td&gt;
&lt;td&gt;Query method that returns a bool&lt;/td&gt;
&lt;td&gt;Vec::is_empty()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;new&lt;/td&gt;
&lt;td&gt;Indicates a constructor&lt;/td&gt;
&lt;td&gt;Box::new_zeroed()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;to&lt;/td&gt;
&lt;td&gt;Expensive conversion from original&lt;/td&gt;
&lt;td&gt;&amp;amp;str::to_owned()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;try&lt;/td&gt;
&lt;td&gt;This variant returns a Result instead panicking&lt;/td&gt;
&lt;td&gt;Box::try_new()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;with&lt;/td&gt;
&lt;td&gt;The variant uses a function to provide a value instead of directly&lt;/td&gt;
&lt;td&gt;RefCell::replace_with()&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;as&lt;/code&gt;, &lt;code&gt;to&lt;/code&gt; and &lt;code&gt;into&lt;/code&gt; prefixes indicate conversions. But there are different types of conversions. There are conversions that are free because they are basically no-ops. There are conversions that are expensive because a new type is constructed from the old. And there are conversions that consume the original value to produce the new one. Rust differentiates between these conversions by using prefixes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;as&lt;/code&gt; essentially exposes a view to an underlying representation that is present in the original value. For example, a slice from a vector. &lt;code&gt;as&lt;/code&gt; methods do not affect the original value and usually produce a borrowed reference. This means that the conversion's lifetime must be shorter than the original value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;to&lt;/code&gt; is an expensive conversion that constructs a new value from the old one. For example, creating a new &lt;code&gt;String&lt;/code&gt; from a string slice. These methods usually produce an ownable value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;into&lt;/code&gt; can be expensive and can be a no-op depending on what it does. However, it consumes the original value. An example of this is converting a &lt;code&gt;String&lt;/code&gt; to a &lt;code&gt;Vec&amp;lt;u8&amp;gt;&lt;/code&gt; using &lt;code&gt;into_bytes()&lt;/code&gt;. The &lt;code&gt;String&lt;/code&gt; contains a &lt;code&gt;Vec&amp;lt;u8&amp;gt;&lt;/code&gt; under the bonnet, and so producing it is effectively a no-op. But because it's given away the underlying structure, the &lt;code&gt;String&lt;/code&gt; cannot exist anymore.&lt;/p&gt;

&lt;p&gt;Another example is &lt;code&gt;into_iter()&lt;/code&gt;, which consumes the original container and produces an iterator that can give back its values. It can do this because the new iterator value owns the values and not the original container.&lt;/p&gt;

&lt;p&gt;These prefixes are essential in understanding the semantics of the operations their methods provide.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;new&lt;/code&gt; is often the complete name of a static method for constructing an instance of a struct. However, sometimes there are different ways to do construction and therefore multiple variants of &lt;code&gt;new&lt;/code&gt;. These all have the &lt;code&gt;new&lt;/code&gt; prefix.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;try&lt;/code&gt;, often used with &lt;code&gt;new&lt;/code&gt; for the &lt;code&gt;try_new&lt;/code&gt; prefix indicates a version of a method that can fail gracefully. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This function will panic if the id does not exist.&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get_config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Config&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// This will not panic and will return a Result instead.&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;try_get_config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;u3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ConfigError&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the &lt;code&gt;Result&lt;/code&gt; version is the only version, then there is no need for the prefix &lt;code&gt;try&lt;/code&gt;. It's only used for variants.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with&lt;/code&gt; indicates a method that whereas the original function passed a value as a parameter, this variant will be passed a function that provides that value instead. To be fair, I have not seen this suffix used that often. I have even seen other suffixes used to mean the same thing. For example, should &lt;code&gt;Option::ok_or_else&lt;/code&gt; really be &lt;code&gt;Option::ok_or_with&lt;/code&gt;?&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I found this research to be enlightening and helpful in understanding the Standard Library better.&lt;/p&gt;

&lt;p&gt;Please comment below about any other prefixes or suffixes that I might have missed - I am sure that there are many.&lt;/p&gt;

&lt;p&gt;Also, if you've been enjoying my series of random articles, please let me know what you want me to look into next.&lt;/p&gt;

&lt;p&gt;Until next week!&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
    <item>
      <title>Rust #4: Options and Results (Part 2)</title>
      <dc:creator>Matt Davies</dc:creator>
      <pubDate>Sat, 10 Jul 2021 15:54:54 +0000</pubDate>
      <link>https://dev.to/cthutu/rust-4-options-and-results-part-2-5aca</link>
      <guid>https://dev.to/cthutu/rust-4-options-and-results-part-2-5aca</guid>
      <description>&lt;p&gt;Last week I wrote about basic use of &lt;code&gt;Option&lt;/code&gt;, &lt;code&gt;Result&lt;/code&gt; and creating and using errors derived from &lt;code&gt;std::error::Error&lt;/code&gt;. But both &lt;code&gt;Option&lt;/code&gt; and &lt;code&gt;Result&lt;/code&gt; have a host of methods that I wanted to explore and describe in today's post. Below is an overview of some of the &lt;code&gt;Option&lt;/code&gt; methods I want to talk about:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Use Description&lt;/th&gt;
&lt;th&gt;Return Type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;and&lt;/td&gt;
&lt;td&gt;Testing two &lt;code&gt;Option&lt;/code&gt;s are not &lt;code&gt;None&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Option&amp;lt;U&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;and_then&lt;/td&gt;
&lt;td&gt;Chaining &lt;code&gt;Option&lt;/code&gt;s&lt;/td&gt;
&lt;td&gt;Option&amp;lt;U&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;expect&lt;/td&gt;
&lt;td&gt;Panic if None&lt;/td&gt;
&lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;filter&lt;/td&gt;
&lt;td&gt;Filter the &lt;code&gt;Option&lt;/code&gt; with a predicate&lt;/td&gt;
&lt;td&gt;Option&amp;lt;T&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;flatten&lt;/td&gt;
&lt;td&gt;Removes nested &lt;code&gt;Option&lt;/code&gt;s&lt;/td&gt;
&lt;td&gt;Option&amp;lt;T&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;is_none&lt;/td&gt;
&lt;td&gt;Test the &lt;code&gt;Option&lt;/code&gt; type&lt;/td&gt;
&lt;td&gt;bool&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;is_some&lt;/td&gt;
&lt;td&gt;Test the &lt;code&gt;Option&lt;/code&gt; type&lt;/td&gt;
&lt;td&gt;bool&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;iter&lt;/td&gt;
&lt;td&gt;Iterate over its single or no value&lt;/td&gt;
&lt;td&gt;an iterator&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;iter_mut&lt;/td&gt;
&lt;td&gt;Iterate over its single or no value&lt;/td&gt;
&lt;td&gt;an iterator&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;map&lt;/td&gt;
&lt;td&gt;Transform the value into another&lt;/td&gt;
&lt;td&gt;Option&amp;lt;U&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;map_or&lt;/td&gt;
&lt;td&gt;Transform the value into another&lt;/td&gt;
&lt;td&gt;U&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;map_or_else&lt;/td&gt;
&lt;td&gt;Transform the value into another&lt;/td&gt;
&lt;td&gt;U&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ok_or&lt;/td&gt;
&lt;td&gt;Transform the &lt;code&gt;Option&lt;/code&gt; to a &lt;code&gt;Result&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Result&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ok_or_else&lt;/td&gt;
&lt;td&gt;Transform the &lt;code&gt;Option&lt;/code&gt; to a &lt;code&gt;Result&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Result&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;or&lt;/td&gt;
&lt;td&gt;Provide a new value if &lt;code&gt;None&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Option&amp;lt;T&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;or_else&lt;/td&gt;
&lt;td&gt;Provide a value if &lt;code&gt;None&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Option&amp;lt;T&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;replace&lt;/td&gt;
&lt;td&gt;Change the value to a &lt;code&gt;Some&lt;/code&gt; while returning previous value&lt;/td&gt;
&lt;td&gt;Option&amp;lt;T&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;take&lt;/td&gt;
&lt;td&gt;Change the value to &lt;code&gt;None&lt;/code&gt; while returning original&lt;/td&gt;
&lt;td&gt;Option&amp;lt;T&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;transpose&lt;/td&gt;
&lt;td&gt;Change &lt;code&gt;Option&lt;/code&gt; of &lt;code&gt;Result&lt;/code&gt; to &lt;code&gt;Result&lt;/code&gt; of &lt;code&gt;Option&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Result, E&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unwrap&lt;/td&gt;
&lt;td&gt;Extract value&lt;/td&gt;
&lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unwrap_or&lt;/td&gt;
&lt;td&gt;Extract value&lt;/td&gt;
&lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unwrap_or_default&lt;/td&gt;
&lt;td&gt;Extract value&lt;/td&gt;
&lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unwrap_or_else&lt;/td&gt;
&lt;td&gt;Extract value&lt;/td&gt;
&lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;xor&lt;/td&gt;
&lt;td&gt;Return one of the contained values&lt;/td&gt;
&lt;td&gt;Option&amp;lt;T&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;zip&lt;/td&gt;
&lt;td&gt;Merge &lt;code&gt;Options&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Option&amp;lt;(T, U)&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This is a non-exhaustive list. And below is an overview of the &lt;code&gt;Result&lt;/code&gt; methods I want to talk about:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Use Description&lt;/th&gt;
&lt;th&gt;Return Type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;and&lt;/td&gt;
&lt;td&gt;Testing two &lt;code&gt;Results&lt;/code&gt; are not errors&lt;/td&gt;
&lt;td&gt;Result&lt;u&gt;&lt;/u&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;and_then&lt;/td&gt;
&lt;td&gt;Chaining &lt;code&gt;Results&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Result&lt;u&gt;&lt;/u&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;err&lt;/td&gt;
&lt;td&gt;Extract the error&lt;/td&gt;
&lt;td&gt;Option&amp;lt;E&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;expect&lt;/td&gt;
&lt;td&gt;Panic if &lt;code&gt;Err&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;expect_err&lt;/td&gt;
&lt;td&gt;Panic if &lt;code&gt;Ok&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;E&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;is_err&lt;/td&gt;
&lt;td&gt;Test the &lt;code&gt;Result&lt;/code&gt; type&lt;/td&gt;
&lt;td&gt;bool&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;is_ok&lt;/td&gt;
&lt;td&gt;Test the &lt;code&gt;Result&lt;/code&gt; type&lt;/td&gt;
&lt;td&gt;bool&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;iter&lt;/td&gt;
&lt;td&gt;Iterate over its single or no value&lt;/td&gt;
&lt;td&gt;an iterator&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;iter_mut&lt;/td&gt;
&lt;td&gt;Iterate over its single or no vlaue&lt;/td&gt;
&lt;td&gt;an iterator&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;map&lt;/td&gt;
&lt;td&gt;Transform the value into another&lt;/td&gt;
&lt;td&gt;Result&lt;u&gt;&lt;/u&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;map_err&lt;/td&gt;
&lt;td&gt;Transform the value into another&lt;/td&gt;
&lt;td&gt;Result&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;map_or&lt;/td&gt;
&lt;td&gt;Transform the value into another&lt;/td&gt;
&lt;td&gt;U&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;map_or_else&lt;/td&gt;
&lt;td&gt;Transform the value into another&lt;/td&gt;
&lt;td&gt;U&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ok&lt;/td&gt;
&lt;td&gt;Converts &lt;code&gt;Result&lt;/code&gt; into &lt;code&gt;Option&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Option&amp;lt;T&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;or&lt;/td&gt;
&lt;td&gt;Provide a new &lt;code&gt;Result&lt;/code&gt; if &lt;code&gt;Err&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Result&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;or_else&lt;/td&gt;
&lt;td&gt;Provide a new &lt;code&gt;Result&lt;/code&gt; if &lt;code&gt;Err&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Result&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;transpose&lt;/td&gt;
&lt;td&gt;Change &lt;code&gt;Result&lt;/code&gt; of &lt;code&gt;Option&lt;/code&gt; to &lt;code&gt;Option&lt;/code&gt; of &lt;code&gt;Result&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Option&amp;lt;Result&amp;lt;T,E&amp;gt;&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unwrap&lt;/td&gt;
&lt;td&gt;Extract value&lt;/td&gt;
&lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unwrap_err&lt;/td&gt;
&lt;td&gt;Extract error&lt;/td&gt;
&lt;td&gt;E&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unwrap_or&lt;/td&gt;
&lt;td&gt;Extract value&lt;/td&gt;
&lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unwrap_or_default&lt;/td&gt;
&lt;td&gt;Extract value&lt;/td&gt;
&lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unwrap_or_else&lt;/td&gt;
&lt;td&gt;Extract value&lt;/td&gt;
&lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can see from the above tables thit's the absense of a result &lt;code&gt;Option&lt;/code&gt; and &lt;code&gt;Result&lt;/code&gt; have similar methods and act in similar ways. This is not surprising if you think about it because both can return results or a non-result. In &lt;code&gt;Option&lt;/code&gt;'s case, the non-result is an absence of a result (&lt;code&gt;None&lt;/code&gt;) and in &lt;code&gt;Result&lt;/code&gt;'s case, the non-result is an error.&lt;/p&gt;

&lt;p&gt;Both have logical operations that combine each other: &lt;code&gt;and&lt;/code&gt;, &lt;code&gt;or&lt;/code&gt; and in &lt;code&gt;Option&lt;/code&gt;'s case, &lt;code&gt;xor&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Both can be treated like an iterator with one or no values.&lt;/p&gt;

&lt;p&gt;Both can transform their values via the &lt;code&gt;map&lt;/code&gt; family of methods and extract values via the &lt;code&gt;unwrap&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;Also, you may notice some common suffixes on the functions, particularly &lt;code&gt;or&lt;/code&gt; versus &lt;code&gt;or_else&lt;/code&gt;. The difference between these functions is how they provide a default value. The &lt;code&gt;or&lt;/code&gt; signifies that if there is no result (i.e. &lt;code&gt;None&lt;/code&gt; or &lt;code&gt;Err&lt;/code&gt;), then here is one I provide for you! The &lt;code&gt;else&lt;/code&gt; part signifies that I will provide the default value, but via a function. This allows a default result to be provided lazily. Passing a value to an &lt;code&gt;or&lt;/code&gt; type method will mean it is evaluated regardless of the original value. This is because all parameters in Rust are evaluated before &lt;code&gt;or&lt;/code&gt; is called. Because &lt;code&gt;or_else&lt;/code&gt; uses a function to provide the default value, this means it is not evaluated unless the original value is an &lt;code&gt;None&lt;/code&gt; or &lt;code&gt;Err&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Good and Bad
&lt;/h2&gt;

&lt;p&gt;Because I will be talking about &lt;code&gt;Option&lt;/code&gt; and &lt;code&gt;Result&lt;/code&gt; generically as they share so much in common, I will use different terminology for the types of values they can have.&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;Some&lt;/code&gt; and &lt;code&gt;Ok&lt;/code&gt; values, I will call them good values.&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;None&lt;/code&gt; and &lt;code&gt;Err&lt;/code&gt; values, I will call them bad values.&lt;/p&gt;

&lt;p&gt;There is no semantic meaning to the terms other than to distinguish between them. I could use positive and negative, or result and non-result. But the words good and bad are easier to type!&lt;/p&gt;

&lt;h2&gt;
  
  
  Extracting Good Values and Result Errors
&lt;/h2&gt;

&lt;p&gt;A common use pattern is to extract the good value regardless of whether it is good. This is done by the &lt;code&gt;unwrap&lt;/code&gt; family of methods. How they differ is what happens when the value is not good. If it is a good value, they just convert the value into the contained value. That is, an &lt;code&gt;Option&amp;lt;T&amp;gt;&lt;/code&gt; or &lt;code&gt;Result&amp;lt;T,E&amp;gt;&lt;/code&gt; becomes a &lt;code&gt;T&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you want to panic on a bad value, just use &lt;code&gt;unwrap&lt;/code&gt;. I wouldn't recommend using this as panicking is so user unfriendly, but it's good for prototype work and for during development.&lt;/p&gt;

&lt;p&gt;If you want to provide a default value, then there are two ways to do this: eagerly; and lazily. The &lt;code&gt;unwrap_or&lt;/code&gt; and &lt;code&gt;unwrap_or_else&lt;/code&gt; do this and I've already explained what &lt;code&gt;or&lt;/code&gt; and &lt;code&gt;or_else&lt;/code&gt; means above.&lt;/p&gt;

&lt;p&gt;If you want to provide the type's default value via the &lt;code&gt;Default&lt;/code&gt; trait, use &lt;code&gt;unwrap_or_default&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, &lt;code&gt;Result&lt;/code&gt; has a couple more unwrapping methods for errors: &lt;code&gt;err&lt;/code&gt; and &lt;code&gt;unwrap_err&lt;/code&gt;. &lt;code&gt;err&lt;/code&gt; will return an &lt;code&gt;Option&amp;lt;E&amp;gt;&lt;/code&gt; value, which is None if no error occurred. &lt;code&gt;unwrap_err&lt;/code&gt; extracts the error value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;maybe_name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_dog_name&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;maybe_name&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;maybe_name&lt;/span&gt;&lt;span class="nf"&gt;.unwrap_or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Fido"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;maybe_name&lt;/span&gt;&lt;span class="nf"&gt;.unwrap_or_else&lt;/span&gt;&lt;span class="p"&gt;(||&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Fido"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;maybe_name&lt;/span&gt;&lt;span class="nf"&gt;.unwrap_or_default&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;maybe_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"foo.txt"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;file_error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;maybe_file&lt;/span&gt;&lt;span class="nf"&gt;.unwrap_err&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;maybe_file_error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;maybe_file&lt;/span&gt;&lt;span class="nf"&gt;.err&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Iteration and Transforming Values
&lt;/h2&gt;

&lt;p&gt;Both &lt;code&gt;Option&lt;/code&gt; and &lt;code&gt;Result&lt;/code&gt; can be treated as containers that have one or zero values in it. By iterating over a single &lt;code&gt;Ok&lt;/code&gt; or &lt;code&gt;Some&lt;/code&gt; value, this opens up &lt;code&gt;Option&lt;/code&gt;s and &lt;code&gt;Result&lt;/code&gt;s to all the iterator functionality.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;iter&lt;/code&gt; provides the &lt;code&gt;&amp;amp;T&lt;/code&gt; and &lt;code&gt;iter_mut&lt;/code&gt; provides the &lt;code&gt;&amp;amp;mut T&lt;/code&gt; on the "collection".&lt;/p&gt;

&lt;p&gt;Some of the iterator methods have been brought into &lt;code&gt;Option&lt;/code&gt; and &lt;code&gt;Result&lt;/code&gt;, removing the requirement to convert to an iterator first. This is the &lt;code&gt;map&lt;/code&gt; family of methods and for &lt;code&gt;Option&lt;/code&gt; there is &lt;code&gt;filter&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;map&lt;/code&gt; calls a function that converts the good value of one type to a good value of another. The function moves the wrapped value into a function that returns a new one, which doesn't even have to share the same type. So it converts an &lt;code&gt;Option&amp;lt;T&amp;gt;&lt;/code&gt; to a &lt;code&gt;Option&amp;lt;U&amp;gt;&lt;/code&gt; or a &lt;code&gt;Result&amp;lt;T,E&amp;gt;&lt;/code&gt; to a &lt;code&gt;Result&amp;lt;U,E&amp;gt;&lt;/code&gt;. For bad values, it remains the bad value with no transformation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;map_or&lt;/code&gt; and &lt;code&gt;map_or_else&lt;/code&gt; extends &lt;code&gt;map&lt;/code&gt;'s functionality by provided a default value in the case of a bad value. This follows the same &lt;code&gt;or&lt;/code&gt; and &lt;code&gt;or_else&lt;/code&gt; functionality as described above. However, the function you provide for the default in the &lt;code&gt;Result&lt;/code&gt;'s case is provided with the error value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Result&lt;/code&gt; has one more version for transforming errors. This is &lt;code&gt;map_err&lt;/code&gt; and is often used, for example, to adapt standard errors to your own.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Breed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;maybe_name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_dog_name&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;dog1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Dog&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;maybe_name&lt;/span&gt;&lt;span class="nf"&gt;.map&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;dog_name&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;dog_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;Breed&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Labrador&lt;/span&gt; 
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;dog2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;maybe_name&lt;/span&gt;&lt;span class="nf"&gt;.map_or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Fido"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;Breed&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Labrador&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="n"&gt;dog_name&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;dog_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;Breed&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Labrador&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;dog3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;maybe_name&lt;/span&gt;&lt;span class="nf"&gt;.map_or_else&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;    &lt;span class="c1"&gt;// This lambda is passed an error argument in Result case.&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Fido"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;Breed&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Labrador&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="n"&gt;dog_name&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;dog_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;Breed&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Labrador&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;maybe_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"foo.txt"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;new_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;MyError&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;maybe_file&lt;/span&gt;&lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;_error&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;MyError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;BadStuff&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Querying Value Types
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Option&lt;/code&gt; provides &lt;code&gt;is_some&lt;/code&gt; and &lt;code&gt;is_none&lt;/code&gt; to quickly determine if it contains a good or bad value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Result&lt;/code&gt; also provides &lt;code&gt;is_ok&lt;/code&gt; and &lt;code&gt;is_err&lt;/code&gt; for the same reasons.&lt;/p&gt;

&lt;p&gt;There really isn't much to say about this. I would add that these are not used that often because the tests are implicit with the &lt;code&gt;if let&lt;/code&gt; and &lt;code&gt;match&lt;/code&gt; syntaxes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;maybe_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_dog_name&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;maybe_name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;Breed&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Labrador&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nb"&gt;None&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// instead of:&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;maybe_name&lt;/span&gt;&lt;span class="nf"&gt;.is_some&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// You still need to deconstruct the value here so the &lt;/span&gt;
    &lt;span class="c1"&gt;// is_some check is redundant.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;maybe_name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;Breed&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;GermanShepherd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nn"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They are really only useful if you need to convert an option or result to a boolean.&lt;/p&gt;

&lt;h2&gt;
  
  
  Logical combinations
&lt;/h2&gt;

&lt;p&gt;Both &lt;code&gt;Option&lt;/code&gt; and &lt;code&gt;Result&lt;/code&gt; provide &lt;code&gt;and&lt;/code&gt; and &lt;code&gt;or&lt;/code&gt; methods that combine two values according to their logical rules.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;and&lt;/code&gt;, both values need to be good, otherwise the result is bad. Also, when all values are good, the result is the final good value. This makes &lt;code&gt;and&lt;/code&gt; a really good way of chaining operations if you don't care about the results of anything but the last operation. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// All these operations must be good&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;clone_person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// We only create a new clone if the first clone has a name.  For some&lt;/span&gt;
&lt;span class="c1"&gt;// reason unnamed clones are not allowed to be cloned again!&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;new_person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;old_person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.and&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;clone_person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;old_person&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Brad"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;// new_person will either be None or Some(Person)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But even more useful is the method &lt;code&gt;and_then&lt;/code&gt;. This allows you to chain operations together but passing the result of one to the function of the next.&lt;/p&gt;

&lt;p&gt;For example, perhaps I want to try to open a file, and read the first line. Both of these operations can fail with a bad &lt;code&gt;Result&lt;/code&gt;. &lt;code&gt;and_then&lt;/code&gt; is perfect for this, because I only care about the first line and none of the intermediate data such as the open file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;result&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;read_first_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;first_line&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"foo.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.and_then&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;read_first_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;or&lt;/code&gt; method will check the first result and will return that result only if it is good. If it is bad, it will return the second result. This is useful for finding alternative operations. If one fails, then let's try the other.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Disease&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get_doctors_diagnosis&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Disease&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get_vets_diagnosis&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Disease&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// We're desperate!  Let's ask a vet if the doctor doesn't know.&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;disease&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Disease&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_doctors_diagnosis&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get_vets_diagnosis&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how &lt;code&gt;or&lt;/code&gt; differs to &lt;code&gt;and&lt;/code&gt; in that all operations must wrap the same type. In the &lt;code&gt;and&lt;/code&gt; example, the first operation wrapped a &lt;code&gt;File&lt;/code&gt;, then resulte in a wrapped &lt;code&gt;String&lt;/code&gt;. This is possible due to the nature of the logical AND. As soon as one operation is bad, all results are bad. This is not so for logical OR. Any operation could be bad and we will still get a good result if at least one of them is good. This means that all wrapped values must be the same. In the example, all wrapped values were of type &lt;code&gt;Disease&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Option&lt;/code&gt; provides one more logical combination and that is the logical XOR operation. The result is good if and only if only one of the operations is good. You can either take this result or the other, but not a combination of both.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;and_then&lt;/code&gt; versus &lt;code&gt;map&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;It may not be obvious, but &lt;code&gt;and_then&lt;/code&gt; and &lt;code&gt;map&lt;/code&gt; do very similar things. They can transform a wrapped value into another. In the example above, &lt;code&gt;and_then&lt;/code&gt; changed an &lt;code&gt;Option&amp;lt;File&amp;gt;&lt;/code&gt; into an &lt;code&gt;Option&amp;lt;String&amp;gt;&lt;/code&gt;. With the &lt;code&gt;map&lt;/code&gt; example, we changed an &lt;code&gt;Option&amp;lt;String&amp;gt;&lt;/code&gt; into an &lt;code&gt;Option&amp;lt;Dog&amp;gt;&lt;/code&gt;. But they do differ and I want to talk about how they differ.&lt;/p&gt;

&lt;p&gt;Both methods take a function that receives the wrapped value if good but the difference is in what those functions return.&lt;/p&gt;

&lt;p&gt;In the case of &lt;code&gt;and_then&lt;/code&gt; it returns a value of type &lt;code&gt;Option&amp;lt;U&amp;gt;&lt;/code&gt;, whereas &lt;code&gt;map&lt;/code&gt; returns a value of type &lt;code&gt;U&lt;/code&gt;. This means that &lt;code&gt;and_then&lt;/code&gt; can change a value from a good one to a bad one but &lt;code&gt;map&lt;/code&gt; cannot; once good always good. &lt;code&gt;map&lt;/code&gt; is purely for the transformation of a wrapped value to another value and even type but not goodness. That is, that transformation step cannot fail by becoming &lt;code&gt;Err&lt;/code&gt; in the case of &lt;code&gt;Result&lt;/code&gt;, or &lt;code&gt;None&lt;/code&gt; in the case of &lt;code&gt;Option&lt;/code&gt;. With &lt;code&gt;and_then&lt;/code&gt; the transformation can fail. The function passed can return &lt;code&gt;None&lt;/code&gt; or an &lt;code&gt;Err&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It took a while for it to click for me why you use one over the other. I hopes this helps to clarify the reasons.&lt;/p&gt;

&lt;h2&gt;
  
  
  Converting between Option and Results
&lt;/h2&gt;

&lt;p&gt;Because these types are used in very similar circumstances, it is often useful to convert between them. Let's talk about how we do this.&lt;/p&gt;

&lt;h3&gt;
  
  
  Some(T) -&amp;gt; Ok(T), None -&amp;gt; Err(E)
&lt;/h3&gt;

&lt;p&gt;We have an option and we want to convert to a result.  You could use a &lt;code&gt;match&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;opt&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;None&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;MyError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a little verbose, but you can use &lt;code&gt;ok_or&lt;/code&gt; and &lt;code&gt;ok_or_else&lt;/code&gt; to provide the error if the option is &lt;code&gt;None&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;opt&lt;/span&gt;&lt;span class="nf"&gt;.ok_or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;MyError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;opt&lt;/span&gt;&lt;span class="nf"&gt;.ok_or_else&lt;/span&gt;&lt;span class="p"&gt;(||&lt;/span&gt; &lt;span class="nn"&gt;MyError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Ok(T) -&amp;gt; Some(T), Err(E) -&amp;gt; None
&lt;/h3&gt;

&lt;p&gt;Now we have a result and we want to convert to an option.  Again with &lt;code&gt;match&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Result&lt;/code&gt; uses a method &lt;code&gt;ok&lt;/code&gt; to do the conversion:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;opt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="nf"&gt;.ok&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Transposition and Flattening
&lt;/h3&gt;

&lt;p&gt;Both &lt;code&gt;Result&lt;/code&gt; and &lt;code&gt;Option&lt;/code&gt; are container types that wrap a value of type &lt;code&gt;T&lt;/code&gt;. But that type &lt;code&gt;T&lt;/code&gt; can just as well be a &lt;code&gt;Result&lt;/code&gt; and an &lt;code&gt;Option&lt;/code&gt; too.&lt;/p&gt;

&lt;p&gt;Transposition is the operation to swap a &lt;code&gt;Result&lt;/code&gt; and &lt;code&gt;Option&lt;/code&gt; in a nested type. So, for example, a &lt;code&gt;Result&amp;lt;Option&amp;lt;T&amp;gt;&amp;gt;&lt;/code&gt; becomes a &lt;code&gt;Option&amp;lt;Result&amp;lt;T&amp;gt;&amp;gt;&lt;/code&gt; or vice versa. Both &lt;code&gt;Result&lt;/code&gt; and &lt;code&gt;Option&lt;/code&gt; offer a &lt;code&gt;transpose&lt;/code&gt; method to do that swapping.&lt;/p&gt;

&lt;p&gt;Also, it is useful to convert a &lt;code&gt;Result&amp;lt;Result&amp;lt;T,E&amp;gt;,E&amp;gt;&lt;/code&gt; into a &lt;code&gt;Result&amp;lt;T,E&amp;gt;&lt;/code&gt;, or convert an &lt;code&gt;Option&amp;lt;Option&amp;lt;T&amp;gt;&amp;gt;&lt;/code&gt; into an &lt;code&gt;Option&amp;lt;T&amp;gt;&lt;/code&gt;. This operation is called flattening and both methods offer a &lt;code&gt;flatten&lt;/code&gt; method to do so. But, as of writing, &lt;code&gt;Result::flatten&lt;/code&gt; is only available in nightly builds and so therefore, it has not been stablised yet. This is why this article only shows &lt;code&gt;flatten&lt;/code&gt; in the &lt;code&gt;Option&lt;/code&gt; table at the beginning.&lt;/p&gt;

&lt;h2&gt;
  
  
  Miscellaneous Operations
&lt;/h2&gt;

&lt;p&gt;We've covered the lion's share of the methods that are used with &lt;code&gt;Option&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Firstly, &lt;code&gt;replace&lt;/code&gt; and &lt;code&gt;take&lt;/code&gt; are used to move values in and out of the &lt;code&gt;Option&lt;/code&gt;. &lt;code&gt;replace&lt;/code&gt; transfers ownership of a value into the &lt;code&gt;Option&lt;/code&gt; and returns the old value so something else can own it. This is useful to make sure the option is not in a valid state. You cannot just move its value out without replacing it with a new value. The borrow checker will not let you do that. Quite often this is used for options within structures.&lt;/p&gt;

&lt;p&gt;It is very common that when you do a replace, you set the option to &lt;code&gt;None&lt;/code&gt; as you extract the value you want. That is, you want to do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Extract the 42 to another owner, but now make x own `None`.&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;extracted_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="nf"&gt;.replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, unfortunately, &lt;code&gt;replace&lt;/code&gt; takes a value of type &lt;code&gt;T&lt;/code&gt;, meaning you can only replace it with a &lt;code&gt;Some&lt;/code&gt; variant. &lt;code&gt;Option&lt;/code&gt; provides another method &lt;code&gt;take&lt;/code&gt; to do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;extracted_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="nf"&gt;.take&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// x will be None after this.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;I hope I have provided a reasonable guide to using &lt;code&gt;Option&lt;/code&gt; and &lt;code&gt;Result&lt;/code&gt; effectively over the past two articles. It's a lot to take in and I will certainly be referring to my own articles to help me remember how I should use them.&lt;/p&gt;

&lt;p&gt;We looked at what they are and how to create and use errors. We looked at methods to transform values, extract values, logically combine, manipulate structure and convert between them. Rust standard library provides a lot of functionality. There are more methods that I didn't go into, mainly because they were with advanced or unstable.&lt;/p&gt;

&lt;p&gt;Next week I will write about naming conventions in the Rust Standard Library. As you have seen here, there are common suffixes that describe what the function may do. In the Standard Library there are prefixes too that help convey concepts to functions that share them. I'd like to understand them better and so I can understand what the function might be doing at a glance and perhaps reused them for my own code so that others can understand it too.&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
    <item>
      <title>Rust #3: Options, Results and Errors (Part 1)</title>
      <dc:creator>Matt Davies</dc:creator>
      <pubDate>Sat, 03 Jul 2021 18:16:34 +0000</pubDate>
      <link>https://dev.to/cthutu/rust-3-options-results-and-errors-part-1-4d52</link>
      <guid>https://dev.to/cthutu/rust-3-options-results-and-errors-part-1-4d52</guid>
      <description>&lt;h2&gt;
  
  
  Options and Results
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;Option&lt;/code&gt; and &lt;code&gt;Result&lt;/code&gt; types in Rust will be two of the most used types you will have at your disposal when writing your programs. Their concepts are simple but their use can be confusing at times for beginners. It was for me. This blog entry is an attempt to help explain how to use them effectively.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Result&lt;/code&gt; can also wrap a Rust error and this blog article will cover how to create those easily too.&lt;/p&gt;

&lt;p&gt;Let's look at the basics.&lt;/p&gt;

&lt;h3&gt;
  
  
  Option
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;Option&lt;/code&gt; type allows you to have a variable that may or may not contain a value. This is useful for passing optional parameters or as a return value from a function that may or may not succeed.&lt;/p&gt;

&lt;p&gt;Its definition is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So it can either contain a single value via the &lt;code&gt;Some&lt;/code&gt; variant or no value at all via the &lt;code&gt;None&lt;/code&gt; variant.&lt;/p&gt;

&lt;p&gt;Enums in Rust are implemented with a discriminant value that tells Rust what type of variant is stored and a union of all the data in the variants. So, if you were to implement the same thing in C, it would look something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Option&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;union&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Some&lt;/span&gt; 
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;None&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, the size of the enum is usually the size of the largest variant plus the size of the type value.&lt;/p&gt;

&lt;p&gt;But Rust has a neat optimisation. If one variant has no data and the other has a single value that is a non-null pointer (such as references, boxes, function pointers), Rust will optimise the enum type so that its size is the same as the type T. It accomplishes this by representing the no-value variant (e.g. None) as a null pointer. This means something like &lt;code&gt;Option&amp;lt;&amp;amp;T&amp;gt;&lt;/code&gt; is the same size as &lt;code&gt;&amp;amp;T&lt;/code&gt;. Effectively, this is like normal C pointers with the extra type safety.&lt;/p&gt;

&lt;p&gt;For more information about this check out the documentation of &lt;code&gt;Option&lt;/code&gt; &lt;a href="https://doc.rust-lang.org/std/option/"&gt;here&lt;/a&gt; under the section titled 'Representation'.&lt;/p&gt;

&lt;p&gt;Below is an example of how we can use &lt;code&gt;Option&lt;/code&gt; for a generic function that returns the first item:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;first_item&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Clone&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nf"&gt;.clone&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;None&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function &lt;code&gt;first_item&lt;/code&gt; can only return a value if the vector being passed is not empty. This is a good candidate for &lt;code&gt;Option&lt;/code&gt;. If the vector is empty, we return &lt;code&gt;None&lt;/code&gt;, otherwise we return a copy of the value via &lt;code&gt;Some&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;None&lt;/code&gt; variant forces the programmer to consider the case where the information required is not forthcoming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Result&lt;/code&gt; is similar to &lt;code&gt;Option&lt;/code&gt; in that it can either return a value or it doesn't and is usually used as a return value from a function. But instead of returning a &lt;code&gt;None&lt;/code&gt; value, it returns an error value that hopefully encapsulates the information of why it went wrong.&lt;/p&gt;

&lt;p&gt;Its form is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything goes well, a function can return an &lt;code&gt;Ok&lt;/code&gt; variant along with the final result of the function. However, if something fails within the function it can return an &lt;code&gt;Err&lt;/code&gt; variant along with the error value.&lt;/p&gt;

&lt;p&gt;Let's look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;BufRead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;first_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="nf"&gt;.read_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;std::fs::File::open&lt;/code&gt; will return a &lt;code&gt;Result&amp;lt;std::fs::File, std::io::Error&amp;gt;&lt;/code&gt;. That is, it will either return a file handle if everything goes OK, or it will return an I/O error if it doesn't. We can match on this. If it's an error, we just return it immediately. Otherwise, we try to read the first line of that file via the &lt;code&gt;std::io::BufReader&lt;/code&gt; type. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;read_line&lt;/code&gt; method returns a &lt;code&gt;Result&amp;lt;String, std::io:Error&amp;gt;&lt;/code&gt; and once again we match on this. If it was an error, we return it immediately. Notice that the error type for both the &lt;code&gt;open&lt;/code&gt; and &lt;code&gt;read_line&lt;/code&gt; methods is &lt;code&gt;std::io::Error&lt;/code&gt;. If they were different, this function wouldn't compile. We will deal with differing error types later.&lt;/p&gt;

&lt;p&gt;However, if we were successful, we return the first line as a string via the &lt;code&gt;Ok&lt;/code&gt; variant.&lt;/p&gt;

&lt;h3&gt;
  
  
  The ? operator
&lt;/h3&gt;

&lt;p&gt;Rust introduced an operator &lt;code&gt;?&lt;/code&gt; that made handling errors less verbose. Basically, it turns code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function_that_may_fail&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;into:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function_that_may_fail&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;?&lt;/code&gt; operator changes the &lt;code&gt;Result&amp;lt;T,E&amp;gt;&lt;/code&gt; value into one of type &lt;code&gt;T&lt;/code&gt;. However, if the result was an error, the current function exits immediately with the same 'Err' variant. It unwraps the result if everything went OK, or it causes the function to return with an error if not.&lt;/p&gt;

&lt;p&gt;With this in mind, we can simplify the &lt;code&gt;first_line&lt;/code&gt; demo function above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;BufRead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;first_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="nf"&gt;.read_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think we can all agree this is a lot easier to read.&lt;/p&gt;

&lt;h3&gt;
  
  
  Errors
&lt;/h3&gt;

&lt;p&gt;The error type in a &lt;code&gt;Result&lt;/code&gt; can be any type, like, for example, a &lt;code&gt;String&lt;/code&gt;. However, it is recommended to use a type that implements the trait &lt;code&gt;std::error::Error&lt;/code&gt;. By using this standard trait, users can handle your errors better and even aggregate them.&lt;/p&gt;

&lt;p&gt;Traits are interfaces that structures can implement as methods to extend them. I might write a blog article about traits in the future, but if you are not sure what they are, please read this &lt;a href="https://blog.rust-lang.org/2015/05/11/traits.html"&gt;article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here is the &lt;code&gt;Error&lt;/code&gt; trait in all its glory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Debug&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;Display&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;source&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="n"&gt;Error&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;'static&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;backtrace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Backtrace&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;backtrace&lt;/code&gt; method is only defined in the nightly version of the compiler, at time of writing, and so only &lt;code&gt;source&lt;/code&gt; is defined for the stable version. &lt;code&gt;source&lt;/code&gt; can be implemented to return an earlier error that this current error would be chained to. But if there is no previous error, &lt;code&gt;None&lt;/code&gt; is returned. Returning &lt;code&gt;None&lt;/code&gt; is the default implementation of this method.&lt;/p&gt;

&lt;p&gt;A type that implements &lt;code&gt;Error&lt;/code&gt; must also implement &lt;code&gt;Debug&lt;/code&gt; and &lt;code&gt;Display&lt;/code&gt; traits.&lt;/p&gt;

&lt;p&gt;Errors can be enums too. Below is an example of possible errors that can occur when reading from a file-based database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Formatter&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;MyError&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;DatabaseNotFound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;CannotOpenDatabase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;CannotReadDatabase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;MyError&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Display&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;MyError&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;Formatter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'_&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;DatabaseNotFound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;write!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"File `{}` not found"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="k"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;CannotOpenDatabase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;write!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Cannot open database: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="k"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;CannotReadDatabase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;write!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Cannot read database: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we define the enum with the possible error states and their associative data (e.g. filename of file that was not found). Notice the &lt;code&gt;derive&lt;/code&gt; macro that implements the &lt;code&gt;Debug&lt;/code&gt; trait for us. Unfortunately, we cannot do that for &lt;code&gt;Display&lt;/code&gt; traits.&lt;/p&gt;

&lt;p&gt;Secondly, we implement the &lt;code&gt;Error&lt;/code&gt; trait for compatibility with other error systems. Since we're not chaining errors, the default implementation will do.&lt;/p&gt;

&lt;p&gt;Finally, we implement the &lt;code&gt;Display&lt;/code&gt; trait, which is a requirement of the &lt;code&gt;Error&lt;/code&gt;trait.&lt;/p&gt;

&lt;p&gt;This is a lot to write for an error type, but fortunately there are some popular crates that allow us to write and use errors more easily.&lt;/p&gt;

&lt;h2&gt;
  
  
  Error Crates
&lt;/h2&gt;

&lt;p&gt;As just shown, implementing an error type to be passed with a &lt;code&gt;Result&lt;/code&gt;'s &lt;code&gt;Err&lt;/code&gt; variant can be tedious to write. Some consider the &lt;code&gt;Error&lt;/code&gt; trait lacking in functionality too. Various crates have been written to combat the boilerplate and to increase the usefulness of the types of error values you can generate.&lt;/p&gt;

&lt;p&gt;I will explore a few of them in this article that I have found to be the most popular.&lt;/p&gt;

&lt;p&gt;Let us imagine we want to implement this function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;first_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FirstLineError&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will investigate how we implement &lt;code&gt;FirstLineError&lt;/code&gt; in each of these crates. The basic foundation of the error will be this enum:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;FirstLineError&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;CannotOpenFile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;NoLines&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;failure&lt;/code&gt; crate
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;failure&lt;/code&gt; provides 2 major concepts: the &lt;code&gt;Fail&lt;/code&gt; trait and an &lt;code&gt;Error&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Fail&lt;/code&gt; trait is a new custom error type specifically to hold better error information.  This trait is used by libraries to define new error types.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Error&lt;/code&gt; trait is a wrapper around the &lt;code&gt;Fail&lt;/code&gt; types that can be used to compose higher-level errors. For example, a file open error can be linked to a database open error. The user would deal with the database open error, and could dig down further and obtain the original file error if they wanted.&lt;/p&gt;

&lt;p&gt;Generally, crate writers would use &lt;code&gt;Fail&lt;/code&gt; and crate users would interact with the &lt;code&gt;Error&lt;/code&gt; types.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Failure&lt;/code&gt; also supports backtraces if the crate feature &lt;code&gt;backtrace&lt;/code&gt; is enabled and the &lt;code&gt;RUST_BACKTRACE&lt;/code&gt; environment variable is set to &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is how we would create the &lt;code&gt;FirstLineError&lt;/code&gt; error type using this crate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;BufRead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Fail&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Fail,&lt;/span&gt; &lt;span class="nd"&gt;Debug)]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;FirstLineError&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;#[fail(display&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Cannot open file `{}`"&lt;/span&gt;&lt;span class="nd"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;name)]&lt;/span&gt;
    &lt;span class="n"&gt;CannotOpenFile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nd"&gt;#[fail(display&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"No lines found"&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
    &lt;span class="n"&gt;NoLines&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;first_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FirstLineError&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nn"&gt;FirstLineError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;CannotOpenFile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="nf"&gt;.read_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nn"&gt;FirstLineError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NoLines&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The derive macro implements the &lt;code&gt;Fail&lt;/code&gt; and &lt;code&gt;Display&lt;/code&gt; traits automatically for us.  It uses the &lt;code&gt;fail&lt;/code&gt; attributes to help it construct those traits.&lt;/p&gt;

&lt;p&gt;But we do have another problem. The &lt;code&gt;File::open&lt;/code&gt; and &lt;code&gt;BufRead::read_line&lt;/code&gt; methods return a result based on the &lt;code&gt;std::io::Error&lt;/code&gt; type and not the &lt;code&gt;FirstLineError&lt;/code&gt; type that we require. We use the &lt;code&gt;Result&lt;/code&gt;'s &lt;code&gt;map_err&lt;/code&gt; method to convert one error type to another.&lt;/p&gt;

&lt;p&gt;I will cover &lt;code&gt;map_err&lt;/code&gt; and other methods for &lt;code&gt;Option&lt;/code&gt; and &lt;code&gt;Result&lt;/code&gt; in my next blog article, but for now I will describe this one. If the result is an error, &lt;code&gt;map_err&lt;/code&gt; will call the closure given with the error value allowing us an opportunity to replace it with a different error value.&lt;/p&gt;

&lt;p&gt;So, recall that &lt;code&gt;File::open&lt;/code&gt; returns a &lt;code&gt;Result&amp;lt;(), std::io::Error&lt;/code&gt; value. By calling &lt;code&gt;map_err&lt;/code&gt; we now return a &lt;code&gt;Result&amp;lt;(), FirstLineError&amp;gt;&lt;/code&gt; value. This is because the closure given returns a &lt;code&gt;FirstLineError&lt;/code&gt; value and through type inference, we get the new result type. If the result is an error, that closure will provide the value to associate with the &lt;code&gt;Err&lt;/code&gt; variant.&lt;/p&gt;

&lt;p&gt;But the value returned from &lt;code&gt;File::open&lt;/code&gt; is still a &lt;code&gt;Result&lt;/code&gt; type so we use the &lt;code&gt;?&lt;/code&gt; operator to exit immediately if an error occurs.&lt;/p&gt;

&lt;p&gt;Now we can do things like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="nf"&gt;first_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"foo.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"First line: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Error occurred: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Failure&lt;/code&gt; can even allow you to create errors on the fly that are compatible with &lt;code&gt;failure::Error&lt;/code&gt;.  For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;ensure&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;check_even&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;ensure!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Number is not even"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(())&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="nf"&gt;check_even&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;41&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(())&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"It's even!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This program will output &lt;code&gt;Number is not even&lt;/code&gt; as expected via the &lt;code&gt;Display&lt;/code&gt; trait of the error.&lt;/p&gt;

&lt;p&gt;There are other ways to create errors on the fly too with &lt;code&gt;failure&lt;/code&gt;.  &lt;code&gt;format_err!&lt;/code&gt; will create a string based error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;format_err!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"File not found: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file_name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally, there's a macro that combines &lt;code&gt;format_err!&lt;/code&gt; with a return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;bail!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"File not found: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file_name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;snafu&lt;/code&gt; crate
&lt;/h3&gt;

&lt;p&gt;This is similar to &lt;code&gt;failure&lt;/code&gt; but solves the issue where the actual error that occurred is not the error we want to report.&lt;/p&gt;

&lt;p&gt;If you recall above, we use &lt;code&gt;map_err&lt;/code&gt; to convert the &lt;code&gt;std::io::Error&lt;/code&gt; into one of our &lt;code&gt;FirstLineError&lt;/code&gt; variants. &lt;code&gt;snafu&lt;/code&gt; makes this easier by providing a &lt;code&gt;context&lt;/code&gt; method that allows the programmer to pass in the actual error they wish to report.&lt;/p&gt;

&lt;p&gt;Let's redefine our error type and function using &lt;code&gt;snafu&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;BufRead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;snafu&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;Snafu&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ResultExt&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Snafu,&lt;/span&gt; &lt;span class="nd"&gt;Debug)]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;FirstLineError&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;#[snafu(display(&lt;/span&gt;&lt;span class="s"&gt;"Cannot open file {} because: {}"&lt;/span&gt;&lt;span class="nd"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;name,&lt;/span&gt; &lt;span class="nd"&gt;source))]&lt;/span&gt;
    &lt;span class="n"&gt;CannotOpenFile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nd"&gt;#[snafu(display(&lt;/span&gt;&lt;span class="s"&gt;"No lines found because: {}"&lt;/span&gt;&lt;span class="nd"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;source))]&lt;/span&gt;
    &lt;span class="n"&gt;NoLines&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;first_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FirstLineError&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.context&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CannotOpenFile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="nf"&gt;.read_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.context&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NoLines&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use &lt;code&gt;context()&lt;/code&gt;, there needs to be a &lt;code&gt;source&lt;/code&gt; field in the variant. Notice that the enum type &lt;code&gt;FirstLineError&lt;/code&gt; is not included. We wrote &lt;code&gt;CannotOpenFile&lt;/code&gt;, not &lt;code&gt;FirstLineError::CannotOpenFile&lt;/code&gt;. And the source field is automatically set! There's some black magic going on there!&lt;/p&gt;

&lt;p&gt;If you don't want to use the name &lt;code&gt;source&lt;/code&gt; for your underlying cause, you can rename it by marking the field you do want to be the source with &lt;code&gt;#[snafu(source)]&lt;/code&gt;. Also, if there is a field called &lt;code&gt;source&lt;/code&gt; that you don't want to be treated as &lt;code&gt;snafu&lt;/code&gt;'s source field, mark it with &lt;code&gt;#[snafu(source(false))]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Similarly, &lt;code&gt;snafu&lt;/code&gt; supports the &lt;code&gt;backtrace&lt;/code&gt; field too to store a backtrace at point of error. &lt;code&gt;#[snafu(backtrace)]&lt;/code&gt; et al. controls those fields like the source.&lt;/p&gt;

&lt;p&gt;On top of this, you have the &lt;code&gt;ensure!&lt;/code&gt; macro that functions like &lt;code&gt;failure&lt;/code&gt;'s.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;anyhow&lt;/code&gt; crate
&lt;/h3&gt;

&lt;p&gt;This crate provides dynamic error support via its &lt;code&gt;anyhow::Result&amp;lt;T&amp;gt;&lt;/code&gt;. This type can receive any error.  It can create an ad-hoc error from a string using &lt;code&gt;anyhow!&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;anyhow!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"File not found: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file_name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It also defines &lt;code&gt;bail!&lt;/code&gt; and &lt;code&gt;ensure!&lt;/code&gt; like other crates. &lt;code&gt;anyhow&lt;/code&gt; results can extend the errors using a &lt;code&gt;context()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;anyhow!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"File not found: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;.context&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tried to load the configuration file"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's the &lt;code&gt;first_line&lt;/code&gt; method implemented using &lt;code&gt;anyhow&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;anyhow&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;FirstLineError&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;CannotOpenFile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;NoLines&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;FirstLineError&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Display&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;FirstLineError&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Formatter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'_&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Result&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nn"&gt;FirstLineError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;CannotOpenFile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nd"&gt;write!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Cannot open file `{}` because: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="nn"&gt;FirstLineError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NoLines&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nd"&gt;write!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Cannot find line in file because: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;first_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nn"&gt;FirstLineError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;CannotOpenFile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="nf"&gt;.read_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nn"&gt;FirstLineError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NoLines&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;anyhow&lt;/code&gt; doesn't define the &lt;code&gt;Display&lt;/code&gt; trait for us so we have to do that ourselves. Also &lt;code&gt;map_err&lt;/code&gt; has to come back if we want to convert error values from one domain to another. But, this time we use &lt;code&gt;Result&amp;lt;String&amp;gt;&lt;/code&gt; and we don't need to define which error is returned.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;thiserror&lt;/code&gt; crate
&lt;/h3&gt;

&lt;p&gt;This crate makes it easier to define the error type, and can be used in conjunction with &lt;code&gt;anyhow&lt;/code&gt;. It uses &lt;code&gt;#[derive(thiserror::Error)]&lt;/code&gt; to generate all the &lt;code&gt;Display&lt;/code&gt; and &lt;code&gt;std::error::Error&lt;/code&gt; boilerplate like other crates do.&lt;/p&gt;

&lt;p&gt;But &lt;code&gt;thiserror&lt;/code&gt; makes it easier to chain lower-level errors using the &lt;code&gt;#[from]&lt;/code&gt; attribute.  For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[derive(Error,&lt;/span&gt; &lt;span class="nd"&gt;Debug)]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;MyError&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;#[error(&lt;/span&gt;&lt;span class="s"&gt;"Everything blew up!"&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
    &lt;span class="n"&gt;BlewUp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="nd"&gt;#[error(transparent)]&lt;/span&gt;
    &lt;span class="nf"&gt;IoError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;#[from]&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will allow auto-casting from &lt;code&gt;std::io::Error&lt;/code&gt; to &lt;code&gt;MyError::IoError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's look at our demo with &lt;code&gt;anyhow&lt;/code&gt; for results, and &lt;code&gt;thiserror&lt;/code&gt; for errors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;BufRead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;anyhow&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;thiserror&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Debug,&lt;/span&gt; &lt;span class="nd"&gt;Error)]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;FirstLineError&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;#[error(&lt;/span&gt;&lt;span class="s"&gt;"Cannot open file `{name}` because: {source}"&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
    &lt;span class="n"&gt;CannotOpenFile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nd"&gt;#[error(&lt;/span&gt;&lt;span class="s"&gt;"Cannot find line in file because: {source}"&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
    &lt;span class="n"&gt;NoLines&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;first_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nn"&gt;FirstLineError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;CannotOpenFile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="nf"&gt;.read_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nn"&gt;FirstLineError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NoLines&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the neat embedded field names in the strings on the &lt;code&gt;#[error(...)]&lt;/code&gt; lines.&lt;/p&gt;

&lt;h2&gt;
  
  
  Main function
&lt;/h2&gt;

&lt;p&gt;A quick note on the main function. Rust Edition 2018 as added a feature that allows &lt;code&gt;main&lt;/code&gt; to return a &lt;code&gt;Result&lt;/code&gt;. If main returns an &lt;code&gt;Err&lt;/code&gt; variant, it will return an error code other than 0 to the operating system (signifying a fail condition), and output the error using the &lt;code&gt;Debug&lt;/code&gt; trait.&lt;/p&gt;

&lt;p&gt;If you wanted the &lt;code&gt;Display&lt;/code&gt; trait, then you have to put your main function in another, then have your new &lt;code&gt;main&lt;/code&gt; call it and &lt;code&gt;println!&lt;/code&gt; the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the &lt;code&gt;Debug&lt;/code&gt; trait is good enough for printing out your error, you can use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How does the program know what error code to return?  It uses the new &lt;code&gt;Termination&lt;/code&gt; trait:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;Termination&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;report&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler will call &lt;code&gt;report()&lt;/code&gt; on the type you return from &lt;code&gt;main&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  And there's much more...
&lt;/h2&gt;

&lt;p&gt;But not this week...&lt;/p&gt;

&lt;p&gt;I wanted to talk about the methods on &lt;code&gt;Option&lt;/code&gt; and &lt;code&gt;Result&lt;/code&gt;, like &lt;code&gt;map_err&lt;/code&gt; but this article is already too long. I will cover them next time.&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
    <item>
      <title>Rust #2: Lifetimes, Owners and Borrowers, OH MY!</title>
      <dc:creator>Matt Davies</dc:creator>
      <pubDate>Sat, 26 Jun 2021 13:22:22 +0000</pubDate>
      <link>https://dev.to/cthutu/rust-2-lifetimes-owners-and-borrowers-oh-my-3fem</link>
      <guid>https://dev.to/cthutu/rust-2-lifetimes-owners-and-borrowers-oh-my-3fem</guid>
      <description>&lt;p&gt;One of the abilities that Rust promises is memory safety, and I'm here to say that it delivers.  So much so, that I will not use a programming language for low-level systems programming that doesn't have the same promises now.&lt;/p&gt;

&lt;p&gt;Microsoft discovered that 70% of their bugs were memory safety issues (&lt;a href="https://www.zdnet.com/article/microsoft-70-percent-of-all-security-bugs-are-memory-safety-issues/"&gt;reference&lt;/a&gt;), which means such bugs would be impossible to create if their software were written using Rust.&lt;/p&gt;

&lt;p&gt;So what are the memory safety issues, and how does Rust make them impossible to produce?&lt;/p&gt;

&lt;h2&gt;
  
  
  Memory safety
&lt;/h2&gt;

&lt;p&gt;Memory safety, in a nutshell, is making it impossible to access (reading or writing) memory that you shouldn't.  For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Writing to memory that is read-only.  Examples of read-only memory are your code and string literals.&lt;/li&gt;
&lt;li&gt;Reading from or writing to memory you haven't allocated specifically from the your operating system.&lt;/li&gt;
&lt;li&gt;Accessing the area of your stack that isn't used by local variables or parameters.&lt;/li&gt;
&lt;li&gt;Dereferencing a null pointer.&lt;/li&gt;
&lt;li&gt;Writing to memory at the same time as another thread of your program is accessing it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So how do such bad memory operations occur?  Other languages (such as C++) make it easy for you to execute such bad operations as they do not protect you from them.  This is why much software crashes all the time.  Let's go through them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use after free
&lt;/h3&gt;

&lt;p&gt;This means that after you free your memory and give it back to the operating system for reuse, you go ahead and continue using it.  This can be caused in most languages by &lt;code&gt;pointer aliasing&lt;/code&gt;.  This means that your code has more than one pointer pointing to some memory, perhaps in different parts of your code.  When one pointer is used to free some memory, the other pointers now "dangle".  This means, they point to nothing.  And so the main reason use-after-free occurs is when these other pointers continue to be used.  That can lead to a crash (or segmentation fault).  Look at this C++ code to demonstrate how this can happen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;ref_v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;  &lt;span class="c1"&gt;// a&lt;/span&gt;
&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// b&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"v[0] = "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;ref_v&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the line &lt;code&gt;a&lt;/code&gt;, we take a reference, which is a pointer, to the first element of the C++ vector.  After we push the next value in line &lt;code&gt;b&lt;/code&gt;, it is possible that the memory used by the vector is moved because it needs to be enlarged.  This will invalidate the reference stored in &lt;code&gt;ref_v&lt;/code&gt;.  It is, in effect, pointing to garbage.  Then we use it in line &lt;code&gt;c&lt;/code&gt; - a use-after-free bug.&lt;/p&gt;

&lt;h3&gt;
  
  
  Double free
&lt;/h3&gt;

&lt;p&gt;Another problem, which is really a use-after-free bug, is when memory is given back to the operating system more than once, either through the same pointer, or via aliasing pointers.  This can lead to a crash.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using uninitialised memory
&lt;/h3&gt;

&lt;p&gt;This is when memory that is either allocated on the heap or the stack is not initialised to good data, and then later the program uses it assuming it is.  This means your data will have random values in it, and if your data is a pointer, it means that your code will be more likely to access invalid memory addresses.  That can lead to a crash.&lt;/p&gt;

&lt;h3&gt;
  
  
  Buffer underflow and overflow
&lt;/h3&gt;

&lt;p&gt;This happens if while reading and writing to a block of memory, you access past the end or beginning of that block.  For example, you allocate an array of 10 items, but ask for the 11th.  This can, if you're lucky, cause a segmentation fault and crash your program.  If you're unlucky, it can corrupt the data around that memory area and you won't see the effects of that issue until much later, making it hard to pinpoint where the problem occurred.  Buffer overflows and underflows are the source of many security issues too.&lt;/p&gt;

&lt;h3&gt;
  
  
  Null dereferencing
&lt;/h3&gt;

&lt;p&gt;Tony Hoare once called this his "billion dollar mistake".  Setting a pointer to address 0, otherwise called null, usually means, in most programming languages, "this pointer points to nothing".  Unfortunately, in those languages, there is nothing stopping you from using the pointer still.  Trying to access address 0 on most operating systems will result in an immediate crash.  This is usually the easiest bug to track down and fix, but still, you don't want them occurring after you release your software.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data races
&lt;/h3&gt;

&lt;p&gt;A data race occurs when one thread is writing to memory and another thread is reading or writing to it at the same time.  This can lead to any thread reading or writing the wrong information.&lt;/p&gt;

&lt;p&gt;Imagine this function being run at exactly the same time on two threads:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;gCounter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;increment_counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gCounter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// a&lt;/span&gt;
    &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// b&lt;/span&gt;
    &lt;span class="n"&gt;gCounter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// c&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'd expect the answer to be 2 after both functions have completed.  However, if thread 1 executes lines &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; after thread 2 has executed line &lt;code&gt;a&lt;/code&gt; and before it has executed line &lt;code&gt;c&lt;/code&gt;, both threads will write 1 to &lt;code&gt;gCounter&lt;/code&gt;.  This is called a data race.  Although this is often not a crash bug, it can be amazingly hard to track down.  This is because unless those or similar conditions happen, you will not have a data race.  So, in more complex situations, your application may run correctly 99% of the time, resulting in you desperately trying to recreate that 1% occurrence to track down the bug.&lt;/p&gt;

&lt;p&gt;In software development, there is nothing worse than trying to fix a bug you cannot recreate.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rust to the Rescue
&lt;/h2&gt;

&lt;p&gt;All those bugs I've described above are impossible to occur if you write your program in Rust and avoid its &lt;code&gt;unsafe&lt;/code&gt; keyword (used to lower Rust's shields when you need to).  They are impossible to occur because any such attempt to produce those bugs results in a compilation error.  This means that you can't even start a program with those bugs.  This is a huge win for programmers.&lt;/p&gt;

&lt;p&gt;So how does it do this?  It is achieved using its memory model of ownership and borrowing.  The 'borrow checker' is the analytical part of the compiler that will track and detect these memory issues.  And when you first use Rust, you will butt heads with the borrow checker and hate it.  But eventually, as you realise the borrow checker is always right, you will learn to love it and it will be your best friend in writing software.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ownership
&lt;/h2&gt;

&lt;p&gt;Every data used in Rust is owned by a single variable and ONLY one variable.  When this variable goes out of scope, the data is deleted.  Rust, therefore, always knows when to deallocate memory: as soon as the owner goes out of scope.  This is why you can allocate memory and not care about deallocating it.  You cannot do this in other languages because there might be two references to the data.  Deallocating when one reference goes out of scope will cause a dangling pointer so those languages do not automatically deallocate.&lt;/p&gt;

&lt;p&gt;When you assign a variable to another, or pass a variable to a function, you transfer ownership because you cannot break the one-owner rule.  Examine this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Ownership of vector is transferred to b.&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Compiler error!  a does not own any data.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In line 1, the &lt;code&gt;a&lt;/code&gt; variable owns the vector &lt;code&gt;[1, 2, 3]&lt;/code&gt;.  In line 2, by assigning &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;b&lt;/code&gt;, you transfer ownership.  The variable &lt;code&gt;b&lt;/code&gt; now owns the vector, and as a result &lt;code&gt;a&lt;/code&gt; owns nothing.  It is a compilation error to use &lt;code&gt;a&lt;/code&gt; afterwards.  So as a result, in line 3 you would get a compilation error.&lt;/p&gt;

&lt;p&gt;The same thing occurs when passing variables to functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;print_vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print_vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// Compilation error!!!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using &lt;code&gt;a&lt;/code&gt; as a parameter to &lt;code&gt;print_vector&lt;/code&gt;, you transfer ownership to &lt;code&gt;print_vector&lt;/code&gt;'s local variable &lt;code&gt;v&lt;/code&gt;.  When &lt;code&gt;v&lt;/code&gt; goes out of scope as the function ends, the compiler knows to destroy the vector.  Using &lt;code&gt;a&lt;/code&gt; afterwards is a compilation error.&lt;/p&gt;

&lt;p&gt;If we just had these ownership rules, programming in Rust would be very hard to do, because as soon as you call a function, you've lost the use of your variables.  This is where borrowing comes in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Borrowing
&lt;/h2&gt;

&lt;p&gt;When you own a book and you want to let someone else read it, but still want to own the book afterwards, you lend it.  That is, the other person receiving your book 'borrows' it with the understanding that they will give it back.  This is essentially Rust's borrowing model.&lt;/p&gt;

&lt;p&gt;But there's slightly more to it.  When you lend out some data, you can choose to do it mutably or immutably.  That is, the borrower may have permission to change the data, or not.  This is important.  Rust allows infinite immutable borrows as long as there is not a single mutable borrow.  Likewise, Rust allows only a single mutable borrow, if and only if, there are no immutable borrows.  This simple rule makes data races impossible.  Put a different way, if your function has a mutable borrow, you know for a fact that it is the ONLY reference to the data and you're free to change it.&lt;/p&gt;

&lt;p&gt;A borrow in Rust is symbolised by a &lt;code&gt;&amp;amp;&lt;/code&gt; symbol before the type.  You are essentially creating a pointer.  Looking at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time line 3 is allowed and this code will compile.  Rust allows as many immutable references (they are immutable by default) as you would like.  Also bear in mind that the owner can still mutate the vector as in this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But didn't we break the rule that we can't mutate data if we have immutable borrows?  No, because Rust is smart enough to see, in this example, that &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; are not used any more and &lt;code&gt;a&lt;/code&gt; is free to mutate its data.&lt;/p&gt;

&lt;p&gt;If we had this, however:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then we would have a compilation error.  By using &lt;code&gt;b&lt;/code&gt; in the &lt;code&gt;println!&lt;/code&gt; statement, we are using an immutable borrow at the same time as we are mutating the data.  Remember, as in the C++ example above, pushing more data can move the memory used by the vector and invalidate &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; causing a use-after-free bug.&lt;/p&gt;

&lt;p&gt;To mutably borrow we use the &lt;code&gt;mut&lt;/code&gt; keyword after the &lt;code&gt;&amp;amp;&lt;/code&gt;.  Let us look at the &lt;code&gt;increment_counter&lt;/code&gt; function above rewritten in Rust:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;increment_counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note, you can't have global mutable variables in Rust unless you use the &lt;code&gt;unsafe&lt;/code&gt; keyword to change them, so we pass it as a parameter.  We're only talking about safe Rust in this article for now.&lt;/p&gt;

&lt;p&gt;Also note, for mutable borrows you must use the &lt;code&gt;*&lt;/code&gt; operator to dereference them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Slices
&lt;/h2&gt;

&lt;p&gt;Slices are views on an area of memory.  They are sometimes called fat pointers.  They consist of a pointer and a length.  Unlike C++, where a pointer can pointer to a single or multiple values, in Rust a better distinction is made.  A borrow, mutable or otherwise, always references a single data value.  To reference multiple data values, slices are used.&lt;/p&gt;

&lt;p&gt;Slices cannot own data because, by definition, they are views on already existing data.  So they are borrows.  You can always identify a slice by the type &lt;code&gt;&amp;amp;[T]&lt;/code&gt; where T is any data type.  Without the ampersand, the type &lt;code&gt;[T]&lt;/code&gt; is just a fixed array and needs to be owned.&lt;/p&gt;

&lt;p&gt;Indexing a slice at runtime incurs a bounds check.  Trying to access a slice using an index that is equal or larger than its size will cause a panic and your program will exit if the panic is not trapped.  This stops the buffer overflow bug.  Also, buffer underflows cannot occur because slice indices are always unsigned.  That is, you cannot have an index less than 0.&lt;/p&gt;

&lt;p&gt;As in other borrows, mutable slices are written as &lt;code&gt;&amp;amp;mut [T]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also, in Rust there is a type &lt;code&gt;str&lt;/code&gt;, which is an alias for &lt;code&gt;[char]&lt;/code&gt;, so whenever you see &lt;code&gt;&amp;amp;str&lt;/code&gt;, think of it as a slice of a character array.  String literals will have the type &lt;code&gt;&amp;amp;str&lt;/code&gt; as they are sliced views on static data in your executable.  So in this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;a&lt;/code&gt; is of type &lt;code&gt;&amp;amp;str&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lifetimes
&lt;/h2&gt;

&lt;p&gt;If you store a borrowed reference to some data and its owner goes out of scope, will you not have a dangling pointer?  In short, no, and that is where lifetimes come in.&lt;/p&gt;

&lt;p&gt;Every variable in Rust has a lifetime and Rust ensures that ALL borrowed references have the same lifetime or shorter than the owner that lent the reference.  So in the example above, Rust would not allow you to assign a borrowed reference to another variable unless Rust could prove that the variable had a equal or shorter lifetime than the owner.&lt;/p&gt;

&lt;p&gt;This means, that when the owner goes out of scope, Rust knows for a fact there are no other references and it is safe to destroy the data.&lt;/p&gt;

&lt;p&gt;Lifetime syntax is written as &lt;code&gt;'name&lt;/code&gt; where &lt;code&gt;name&lt;/code&gt; can be anything.  Usually, these lifetime annotations can be implied.  For example, when you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;substr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the compiler is really seeing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;substr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have chosen &lt;code&gt;a&lt;/code&gt; as the name of the lifetime.  And as you can see, the lifetime of the result must match the lifetime of the input parameter &lt;code&gt;s&lt;/code&gt;.  This makes sense as the function will return a reference to the data passed in as &lt;code&gt;s&lt;/code&gt;.  Notice, that the lifetime annotation is declared within &lt;code&gt;&amp;lt;...&amp;gt;&lt;/code&gt; next to the function's name.&lt;/p&gt;

&lt;p&gt;The implied lifetime annotations are called &lt;code&gt;lifetime elision&lt;/code&gt;.  Rust has various rules for eliding lifetimes.  If your function is more complicated, the compiler might complain and you will have to add the annotations yourself.  If you're interested you can find more information &lt;a href="https://doc.rust-lang.org/nomicon/lifetime-elision.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There is another lifetime that data can have and that is &lt;code&gt;'static&lt;/code&gt;.  This means that the data lasts as long as the program itself.  Functions have lifetimes of &lt;code&gt;'static&lt;/code&gt; for example.  Also literal strings are immutable character slices (i.e. &lt;code&gt;&amp;amp;str&lt;/code&gt;) with a lifetime of &lt;code&gt;'static&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Another place where lifetime annotation occurs is if you have references (immutably borrowed or otherwise) inside structures.  For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will give a compilation error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error[E0106]: missing lifetime specifier
 --&amp;gt; test.rs:2:11
  |
2 |     name: &amp;amp;str,
  |           ^ expected named lifetime parameter
  |
help: consider introducing a named lifetime parameter
  |
1 | struct Person&amp;lt;'a&amp;gt; {
2 |     name: &amp;amp;'a str,
  |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the Rust compiler will give you a nice hint on how to fix it.  This ensures that an instance of the &lt;code&gt;Person&lt;/code&gt; structure cannot outlive the data that &lt;code&gt;name&lt;/code&gt; references.  If it did, we'd have a dangling pointer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary of Syntax
&lt;/h2&gt;

&lt;p&gt;Below, for convenience, I summarise the syntax used for the different types of data:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Syntax&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Owner&lt;/td&gt;
&lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Immutably borrowed&lt;/td&gt;
&lt;td&gt;&amp;amp;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mutably borrowed&lt;/td&gt;
&lt;td&gt;&amp;amp;mut T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Immutable borrowed slice&lt;/td&gt;
&lt;td&gt;&amp;amp;[T]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mutably borrowed slice&lt;/td&gt;
&lt;td&gt;&amp;amp;mut [T]&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Bugs begone!
&lt;/h2&gt;

&lt;p&gt;I'd like to go over the different bug classes again and discuss how Rust makes them impossible.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use After Free
&lt;/h3&gt;

&lt;p&gt;Ownership and lifetime rules ensure that when the owner finally needs to be destroyed, there are no other references to it.  This is because references caused by borrowing cannot outlive the owners.&lt;/p&gt;

&lt;h3&gt;
  
  
  Double Delete
&lt;/h3&gt;

&lt;p&gt;Deletion can only occur when the owner goes out of scope.  Because Rust only allows a single owner of data, there cannot be another owner going out of scope later.  You cannot delete data via a borrowed reference either.&lt;/p&gt;

&lt;h3&gt;
  
  
  Uninitialised Data
&lt;/h3&gt;

&lt;p&gt;I didn't talk about this before, but Rust insists on all new variables to be initialised.  You cannot do this, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;a&lt;/code&gt; is undefined when &lt;code&gt;println!&lt;/code&gt; is executed and so the compiler stops this from happening.  However, it is OK to initialise &lt;code&gt;a&lt;/code&gt; on a different statement as long as you don't use &lt;code&gt;a&lt;/code&gt; before you do so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This is OK&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that &lt;code&gt;a&lt;/code&gt; doesn't even have to be defined &lt;code&gt;mut&lt;/code&gt; because Rust sees that the &lt;code&gt;a = 42&lt;/code&gt; statement is in fact an initialisation and not a mutation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Buffer Overflow and Underflow
&lt;/h3&gt;

&lt;p&gt;Buffer overflows are impossible because Rust, at compile time and runtime, bounds-checks array accesses.  Any violation will result in a panic.&lt;/p&gt;

&lt;p&gt;Buffer underflows are impossible because you cannot use signed indices.&lt;/p&gt;

&lt;h3&gt;
  
  
  Null Dereferencing
&lt;/h3&gt;

&lt;p&gt;Safe Rust doesn't allow you to dereference pointers.  Also, borrowed references cannot be null either.  Therefore, it is impossible to dereference a null pointer.  If you do wish to have a type that could point to some data or not, Rust provides the &lt;code&gt;Option&lt;/code&gt; enum, which I will be visiting in my next blog.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data Races
&lt;/h3&gt;

&lt;p&gt;Rust does not allow more than one mutable reference (via borrowing or ownership), and it doesn't allow any mutable references if a single immutable reference exists.  This means that given a mutable reference, Rust guarantees that this is the only way you can mutate it at that time.  Therefore, data races cannot occur.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Word about the Copy Trait
&lt;/h2&gt;

&lt;p&gt;When I said that passing a variable to a function or assigning it to another transfers ownership, I was telling you half the story.  For some built-in types, by default, are marked by the &lt;code&gt;Copy&lt;/code&gt; trait.  I will talk about traits another time, but essentially it means that the type is marked with a property.  That property allows the compiler to copy the data byte-for-byte into the new variable or parameter.  This means that ownership is not transferred but a new owner is created with a copy of the data.&lt;/p&gt;

&lt;p&gt;This is why in the examples above I used vectors to demonstrate transfer of ownership as &lt;code&gt;Vec&lt;/code&gt; does not implement the &lt;code&gt;Copy&lt;/code&gt; trait.  Types that do implement it are so-called POD types (Pieces Of Data) such as &lt;code&gt;i32&lt;/code&gt;, &lt;code&gt;u32&lt;/code&gt;, &lt;code&gt;f32&lt;/code&gt;, &lt;code&gt;char&lt;/code&gt; etc.  Because of this, the following code is correct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// a is an i32, which implements the Copy trait&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; become owners of the copy of &lt;code&gt;a&lt;/code&gt;'s data.  So, both &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; will hold the value 42.&lt;/p&gt;

&lt;p&gt;You can also implement the Copy trait easily for a structure as long as all its fields also implement the Copy trait:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[derive(Copy,&lt;/span&gt; &lt;span class="nd"&gt;Clone)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also note that the &lt;code&gt;Clone&lt;/code&gt; trait must also be implemented if &lt;code&gt;Copy&lt;/code&gt; is.  The &lt;code&gt;Clone&lt;/code&gt; trait provides the &lt;code&gt;clone()&lt;/code&gt; method on the type and is required to do the actual copy.  After adding the &lt;code&gt;#[derive(Copy, Clone)]&lt;/code&gt; statement, this code is possible:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Matt"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;48&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; will hold different instances of &lt;code&gt;Person&lt;/code&gt; but contain the same values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope this helps with groking what borrowing an ownership means and how it a) avoids memory bugs; and b) allows the compiler to deallocate data at the right time so Rust does not need a garbage collector.&lt;/p&gt;

&lt;p&gt;Please let me know in the discussion if there are details I have wrong or left out.  I am always interested in improving this text.&lt;/p&gt;

&lt;p&gt;Next blog will talk about &lt;code&gt;Option&lt;/code&gt; and &lt;code&gt;Result&lt;/code&gt; enum types.&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
    <item>
      <title>Rust #1: Creating your development environment</title>
      <dc:creator>Matt Davies</dc:creator>
      <pubDate>Sat, 19 Jun 2021 13:32:32 +0000</pubDate>
      <link>https://dev.to/cthutu/rust-1-creating-your-development-environment-55bi</link>
      <guid>https://dev.to/cthutu/rust-1-creating-your-development-environment-55bi</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;This blog is my attempt at documenting what I've learnt on my journey using Rust.  I have worked as a video game programmer for over 25 years and as a result have used C++ as my main language of choice, even for personal projects.  Today, I will never touch C++ willingly again since discovering and learning about Rust.  I will restrict C++'s use to my day job, and that's all.  In my opinion, Rust has made C++ redundant for writing new software.&lt;/p&gt;

&lt;p&gt;I have managed to hone my development environment for Rust programming over the last few months and I want to share my approach.  This is what my first article is about - setting up a development environment for effective Rust programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;I will assume the reader knows their way around a terminal (or shell).  That is, they know how to make folders, set the current folder, and launch commands all via their terminal.&lt;/p&gt;

&lt;p&gt;I will also assume that the reader has prior programming experience, perhaps in C, C++ or similar language.  These guides will not be teaching you how to program, but rather how to apply your current programming skills to a new language.&lt;/p&gt;

&lt;p&gt;You will also need an internet connection.  The Rust build system will constantly download packages (called crates) from the world wide web.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Rust
&lt;/h2&gt;

&lt;p&gt;The best way to install Rust is using the tool &lt;code&gt;Rustup&lt;/code&gt;.  Windows' users will require downloading an executable, but for operating systems with more sane command shells, a single cut &amp;amp; paste will suffice.&lt;/p&gt;

&lt;p&gt;Hop over to &lt;a href="http://rustup.rs" rel="noopener noreferrer"&gt;http://rustup.rs&lt;/a&gt; and follow the instructions.  After running the executable &lt;code&gt;rustup-init.exe&lt;/code&gt; on Windows, or executing the shell command on other operating systems, you will see a screen-full of text and 3 options:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuiwebemv23i027szbsto.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuiwebemv23i027szbsto.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go ahead and choose option 1 and you will have Rust and its associated tools installed.  To make sure, open up your favourite terminal and type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rustc --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if everything went well (and I hope it does because this guide will not help you otherwise), you should see something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rustc 1.53.0 (53cb7b09b 2021-06-17)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The installation procedure involves &lt;code&gt;rustup&lt;/code&gt; downloading and installing "components".  The components you might have seen being installed would be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;cargo&lt;/strong&gt; - the ultimate, bad-ass package/build/do-everything tool that will be the centre of your Rust-programming experience.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;clippy&lt;/strong&gt; - this is a linting tool that can scan your code and make suggestions on how to improve it, or catch problems.  This is an essential part of your workflow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;rust-docs&lt;/strong&gt; - a cargo plug-in that provides auto-generation of documentation from your source code.  If you're familiar with Doxygen, it's similar to that.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;rustc&lt;/strong&gt; - the compiler, nuff said!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;rustfmt&lt;/strong&gt; - Rust's very opinionated code formatter.  It will auto-format your code according to a set of standards so that everyone's Rust code looks the same.  This can be annoying at first but you reap the benefits if you stick with it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Your first Rust program
&lt;/h2&gt;

&lt;p&gt;Let's try the obligatory Hello World program.  And because of Cargo, you can do this without typing a line of code!  Most guides, will demonstrate how to use &lt;code&gt;rustc&lt;/code&gt; but you do not need to touch this directly.  You will use Cargo to check, build, test, benchmark and run your code as well as many other things.  So jump into your favourite terminal, &lt;code&gt;cd&lt;/code&gt; to your folder you've set aside for Rust development (I use Windows and so my folder of choice will be &lt;code&gt;r:\&lt;/code&gt;) and type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo new hello
$ cd hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will get a message back telling you that you've created a binary package called 'hello'.&lt;/p&gt;

&lt;p&gt;Let's look inside your folder that you've just created.  First, you will see a &lt;code&gt;Cargo.toml&lt;/code&gt; file.  This gives information to Cargo on how to build your project and other meta-data (like authorship, version etc).  We'll cover this later.&lt;/p&gt;

&lt;p&gt;Secondly, there is a hidden file and directory related to &lt;code&gt;git&lt;/code&gt;, which is the default source control system used by most Rust programmers.  You can switch it to Mercurial or something else.  To read about that, rush over to &lt;a href="https://doc.rust-lang.org/cargo/commands/cargo-new.html" rel="noopener noreferrer"&gt;https://doc.rust-lang.org/cargo/commands/cargo-new.html&lt;/a&gt; and checkout the &lt;code&gt;--vcs&lt;/code&gt; command line option.  I will be ignoring this file and folder in this guide.&lt;/p&gt;

&lt;p&gt;Finally, there is a &lt;code&gt;src&lt;/code&gt; folder, and that is where all your rust source code lies.  If you look inside, you will see a lonely file called &lt;code&gt;main.rs&lt;/code&gt;.  Let's view it.  You can use &lt;code&gt;type&lt;/code&gt; (Windows) or &lt;code&gt;cat&lt;/code&gt; (other operating systems) to show the contents of &lt;code&gt;src/main.rs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn main() {
    println!("Hello, world!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The hello world program has already been written, courtesy of Cargo.  Let's examine the code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;fn&lt;/strong&gt; marks the start of a function and in this case defines the function called &lt;code&gt;main&lt;/code&gt;.  All Rust applications start at this function, like C/C++ does.  If you build with Cargo, which we will, there also has to be a &lt;code&gt;main.rs&lt;/code&gt; file in the root of the &lt;code&gt;src&lt;/code&gt; folder.  The parentheses following it usually contain parameters (we have none right now), and the code belonging to that function is in between the braces.  If you are used to C, C++, C#, JavaScript etc, all this will be familiar to you.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;println!&lt;/strong&gt; will print text to standard output.  The exclamation mark in the name indicates that &lt;code&gt;println!&lt;/code&gt; is not a function but a macro.  As in Lisp macros, not the useless C style macros.  Rust can generate code during compilation.  In this case, &lt;code&gt;println!&lt;/code&gt; will expand to many statements, depending on its arguments, that will be inserted at the point of the call.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;OK, let's run it.  Type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo run
   Compiling hello v0.1.0 (R:\hello)
    Finished dev [unoptimized + debuginfo] target(s) in 1.11s
     Running `target\debug\hello.exe`
Hello, world!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will compile the code and run it.  The astute of you will have noticed a new folder appearing called &lt;code&gt;target&lt;/code&gt;.  Dig deeper and inside that you will see a &lt;code&gt;debug&lt;/code&gt; folder.  Finally, inside that you will see the &lt;code&gt;hello.exe&lt;/code&gt; you just built and that Cargo just ran.&lt;/p&gt;

&lt;p&gt;In fact, the compiler output before the 'Hello, world!' message describes the type of application you just built (unoptimised and including debug information) and where to find it.&lt;/p&gt;

&lt;p&gt;We can build and run an optimised one too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo run --release
   Compiling hello v0.1.0 (R:\hello)
    Finished release [optimized] target(s) in 0.66s
     Running `target\release\hello.exe`
Hello, world!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are some other Cargo commands worth knowing to begin with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;cargo build&lt;/code&gt; - this will just build the final executable.  &lt;code&gt;cargo run&lt;/code&gt; will call this implicitly if it needs to.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cargo clean&lt;/code&gt; - this will delete the &lt;code&gt;target&lt;/code&gt; folder and all the generated files within.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cargo check&lt;/code&gt; - useful while writing code.  This will compile your code but without generating the code.  This is a lot faster than &lt;code&gt;cargo build&lt;/code&gt; and is useful if you just want to check for syntax errors.  Because of the excellent IDE integration that I will demonstrate later, this is rarely needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One more point, after building your executables, a &lt;code&gt;Cargo.lock&lt;/code&gt; file appeared too.  This file is generated to lock down versions of other packages you might have used to build your program.  This allows the Rust compiler to recreate the exact executable after a clean.  It ensures that the same versions of packages are downloaded, built and linked with your application as before.  For now, we don't use packages so this file contains very little information.  If you use source control (like Git), you should include this file during submission, along with &lt;code&gt;Cargo.toml&lt;/code&gt; and your source code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Editor
&lt;/h2&gt;

&lt;p&gt;The second step in starting to learn Rust is setting up your programming environment just right so as to minimise friction between you and your work.  The main part of that decision is which editor to use.  I've tried writing Rust code in Emacs, Vim, Sublime but the text editor I eventually settled on is &lt;code&gt;Visual Studio Code&lt;/code&gt;.  For me, it's the best product Microsoft have ever created.&lt;/p&gt;

&lt;p&gt;Why Visual Studio Code?  In my opinion, it has the most mature tooling for writing Rust code, and for interacting with Rust's ecosystem and build tools (i.e. Cargo).&lt;/p&gt;

&lt;p&gt;So go over to &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;https://code.visualstudio.com/&lt;/a&gt; and download and install it.&lt;/p&gt;

&lt;p&gt;Restart your terminal, and enter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ code .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And lo and behold:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcihq0k4ls4nf28z0b8v7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcihq0k4ls4nf28z0b8v7.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The terminal is nice and all but there's all that typing.  Don't we do enough while coding?  So let's see if we can build from Visual Studio Code (hereafter referred to as VS Code).  Currently, it can't because it needs extensions.&lt;/p&gt;

&lt;h3&gt;
  
  
  VS Code Extensions
&lt;/h3&gt;

&lt;p&gt;To turn VS Code into a powerhouse editor of Rust, we need to install some extensions.  By clicking on the 5th icon on the left (it looks like 4 squares, with one of them offset) you open up the extensions interface.  Use the search box to find the necessary extensions listed below, then click on the blue 'install' buttons beside the extension's entry:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;rust-analyzer&lt;/strong&gt; - provides the 'intellisense' for Rust and many other Rust-related utilities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C/C++&lt;/strong&gt; - what???  You need a C/C++ extension?  Yes, it contains the debugger and it's useful for inserting breakpoints and stepping through your code.  You can also use &lt;code&gt;CodeLLDB&lt;/code&gt; but for me, the &lt;code&gt;C/C++&lt;/code&gt; one feels quicker.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also, there are some useful extensions to install, that while unnecessary, are very useful all the same:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;crates&lt;/strong&gt; - checks the versions of any crates you reference (more on that later).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Markdown All in One&lt;/strong&gt; - you will be writing several markdown files (files with &lt;code&gt;.md&lt;/code&gt; extensions).  This extension makes it easier to do so and provides a useful preview mode.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Even Better TOML&lt;/strong&gt; - some editor support for editing TOML files such as &lt;code&gt;Cargo.TOML&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rewrap&lt;/strong&gt; - not related to Rust but provides the ability to reformat text (in markdown files and Rust comments) so that they fit on the lines better.  By pressing &lt;code&gt;Alt+Q&lt;/code&gt;, you can change a couple of long comments into several shorter ones that fit on the screen.  As you will eventually see, Rust comments will take a big part in document generation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you clear the search box, you will see the extensions you've installed.  Some buttons beside them may be labelled 'Reload Required', and if you do, press it to restart VS Code in a matter of seconds.&lt;/p&gt;

&lt;p&gt;After installing &lt;code&gt;rust-analyzer&lt;/code&gt;, you will see a dialog box pop up in the bottom right corner of the window.  If you miss it, click on the &lt;code&gt;notifications&lt;/code&gt; icon in the bottom-right of the window.  It should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzf02qyu18p5xrwscxibx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzf02qyu18p5xrwscxibx.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click the 'Download now' button.  If you don't see it, restart VS Code and it should show again.  This will download the Rust Language Server (RLS), which is the external program that can analyse your source code and communicate back to VS Code, via the extension, to display useful information about your program, including auto-complete.&lt;/p&gt;

&lt;p&gt;When downloaded, 'rust-analyzer' will get to work looking at your code.  Open up &lt;code&gt;src/main.rs&lt;/code&gt; using VS Code and you will see two new links above the &lt;code&gt;main&lt;/code&gt; function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6dnrztlk2l9gwic0y9xa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6dnrztlk2l9gwic0y9xa.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on &lt;code&gt;Run&lt;/code&gt; and VS Code will build and run your program.  You can see the results in the terminal window that automatically opens up.&lt;/p&gt;

&lt;p&gt;Alternatively, you can click on &lt;code&gt;Debug&lt;/code&gt; to do the same thing.  But right now VS Code isn't properly set up for debugging. &lt;/p&gt;

&lt;h3&gt;
  
  
  Debugging in VS Code
&lt;/h3&gt;

&lt;p&gt;If you haven't used VS Code before, it probably isn't set up for debugging facilities like breakpoints.  So press &lt;code&gt;Ctrl+Comma&lt;/code&gt; to open up VS Code's settings window.  Type &lt;code&gt;breakpoints&lt;/code&gt; in the search bar at the top of this window, and make sure &lt;code&gt;Debug: Allow Breakpoints Everywhere&lt;/code&gt; is selected:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnt39ll3za681kl7nqbtt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnt39ll3za681kl7nqbtt.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While you're in the settings window, retype the search parameter to be &lt;code&gt;formatter&lt;/code&gt;.  Ensure &lt;code&gt;Editor: Default Formatter&lt;/code&gt; is set to &lt;code&gt;rust-analyzer&lt;/code&gt; and &lt;code&gt;Editor: Format On Save&lt;/code&gt; is checked.  This will allow the &lt;code&gt;rustfmt&lt;/code&gt; component mentioned earlier to do its magic every time you save a file.  It's a really nice time-saver.  You can type code as messily as you want in the sweet knowledge that when you save, everything will move to its correct place, and missing trailing commas and spacing will be inserted.&lt;/p&gt;

&lt;p&gt;Now you've done this, you can hit &lt;code&gt;F9&lt;/code&gt; while editing to add a breakpoint to the very line you're on.  Try doing this now on the &lt;code&gt;println!&lt;/code&gt; statement in &lt;code&gt;src/main.rs&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzd6a2lzn33q92w9128cl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzd6a2lzn33q92w9128cl.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on 'Debug' and see the code stop on the line before it executes it.  Tap &lt;code&gt;F10&lt;/code&gt; to step over the next line and you will see 'Hello, world!' appear as it executes it.  Press &lt;code&gt;F5&lt;/code&gt; to continue running, or &lt;code&gt;Shift+F5&lt;/code&gt; to stop debugging altogether.  Don't forget to tap &lt;code&gt;F9&lt;/code&gt; again on the line with the breakpoint to remove it!&lt;/p&gt;

&lt;h3&gt;
  
  
  Launchers and Tasks in VS Code
&lt;/h3&gt;

&lt;p&gt;These links in your code are very convenient, but it requires that you have &lt;code&gt;src/main.rs&lt;/code&gt; open at the &lt;code&gt;main&lt;/code&gt; function to see them.  Wouldn't it be better if we could press a key and have it run or build?&lt;/p&gt;

&lt;p&gt;First step is coercing VS Code to build your source code at a press of a key.  Press &lt;code&gt;F1&lt;/code&gt; to bring up the command palette, which is an interface in VS Code that contains all the possible commands you can give to it.  Now type &lt;code&gt;build&lt;/code&gt; and the command menu will whittle down to a few entries, one of which is &lt;code&gt;Tasks: Configure Default Build Task&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0phjdiaddkam5prnth5z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0phjdiaddkam5prnth5z.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select it.  A new menu will appear with a list of possible Cargo commands.  We want to choose &lt;code&gt;rust: cargo build&lt;/code&gt;.  This will create a new file in your project at &lt;code&gt;.vscode/tasks.json&lt;/code&gt;.  This file defines a task that VS Code can run.  It calls &lt;code&gt;cargo build&lt;/code&gt; and that's all.  But let's improve on it.&lt;/p&gt;

&lt;p&gt;Firstly, change the &lt;code&gt;label&lt;/code&gt; entry to something shorter.  It can be anything you want, but I usually change it to &lt;code&gt;build&lt;/code&gt;.  It will be referenced by another file later.&lt;/p&gt;

&lt;p&gt;Secondly, we would like the terminal window in VS Code to clear every time we start a build so we can clear distinguish current messages from a previous build's messages.  Add a comma at the end of the &lt;code&gt;label&lt;/code&gt; line and then start typing &lt;code&gt;presentation&lt;/code&gt;.  After a few characters you should see an intellisense window open up suggesting &lt;code&gt;presentation&lt;/code&gt;.  Hit ENTER and VS Code will auto-complete the entry.  Very handy!  Now change the &lt;code&gt;clear&lt;/code&gt; entry so it says &lt;code&gt;true&lt;/code&gt; instead of &lt;code&gt;false&lt;/code&gt;.  The final version should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"tasks"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"cargo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"problemMatcher"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"$rustc"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"group"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"kind"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"isDefault"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"label"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"presentation"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"echo"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"reveal"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"always"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"focus"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"panel"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"shared"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"showReuseMessage"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"clear"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, select &lt;code&gt;File &amp;gt; Preferences &amp;gt; Keyboard Shortcuts&lt;/code&gt; from the menu and type &lt;code&gt;build&lt;/code&gt; in the search bar of the new keyboard shortcuts window.&lt;/p&gt;

&lt;p&gt;Double-click under the &lt;code&gt;Keybinding&lt;/code&gt; column on the row that says &lt;code&gt;Tasks: Run Build Task&lt;/code&gt;.  This will allow you to bind a key to that command.  Type &lt;code&gt;F7&lt;/code&gt; followed by &lt;code&gt;ENTER&lt;/code&gt; and you're done.  VS Code will warn you that other bindings use the same key, but ignore those as they will not conflict.  I chose F7 because that's the usual key on the Visual Studio IDE for building and I am used to it.  Of course, you can choose any other key combination that suits you.&lt;/p&gt;

&lt;p&gt;Let's test it!  Hit F7 now (or whichever key combination you chose) and the terminal window in VS Code will clear and start your build!&lt;/p&gt;

&lt;p&gt;Now have it building at a press of a key, we want it to run (after building) at a press of another key.  VS Code already has &lt;code&gt;F5&lt;/code&gt; set up for that task, but currently doesn't know how.&lt;/p&gt;

&lt;p&gt;Click on the 4th icon on the left side of VS Code, the one that looks like a triangle with a bug on the corner.  When you do this, you will see a new interface with a big &lt;code&gt;Run and Debug&lt;/code&gt; button (but don't press it!).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4h2vv2g349ak9dp30sbp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4h2vv2g349ak9dp30sbp.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Under that button, you should see a link that says &lt;code&gt;create a launch.json file&lt;/code&gt;.  To run your program at a press of a button, we need to create a launcher.  Launchers are configured via this &lt;code&gt;launch.json&lt;/code&gt; file, so go ahead and click on that link. You will see a dialog asking you to select and environment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsirx0c7aew8piwszlbrm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsirx0c7aew8piwszlbrm.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Choose &lt;code&gt;C++ (Windows)&lt;/code&gt; if you're on Windows and you used the &lt;code&gt;C/C++&lt;/code&gt; extension, or choose &lt;code&gt;C++ (GDB/LLDB)&lt;/code&gt; otherwise.  This will create a new file called &lt;code&gt;.vscode/launch.json&lt;/code&gt;.  As before, let's make a few changes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Change the &lt;code&gt;program&lt;/code&gt; value so it reads &lt;code&gt;${workspaceFolder}/target/debug/hello.exe&lt;/code&gt;.  This needs to match the final executable you're running.&lt;/li&gt;
&lt;li&gt;Change the &lt;code&gt;console&lt;/code&gt; value so it reads &lt;code&gt;integratedTerminal&lt;/code&gt; and add a comma at the end.&lt;/li&gt;
&lt;li&gt;After the &lt;code&gt;console&lt;/code&gt; entry, and the reason you added a comma, add the entry &lt;code&gt;"preLaunchTask": "build"&lt;/code&gt;.  This is the line that references the label text you entered in &lt;code&gt;tasks.json&lt;/code&gt;.  They must match.  This line tells the launcher to ensure that the build task is run first.  We want to run the latest version of our program, don't we?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The final result will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Use&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;IntelliSense&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;learn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;about&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;possible&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;attributes.&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Hover&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;view&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;descriptions&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;of&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;existing&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;attributes.&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;For&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;more&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;information&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;visit:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;https://go.microsoft.com/fwlink/?linkid=&lt;/span&gt;&lt;span class="mi"&gt;830387&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"0.2.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"configurations"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"(Windows) Launch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"cppvsdbg"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"request"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"launch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"program"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"${workspaceFolder}/target/debug/hello.exe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"args"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"stopAtEntry"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"cwd"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"${fileDirname}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"environment"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"preLaunchTask"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"build"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Press &lt;code&gt;F5&lt;/code&gt; and the launcher should run.  You should see in your terminal, build messages and the output of the program.&lt;/p&gt;

&lt;p&gt;Occasionally, I have seen VS Code struggle with this.  If it doesn't run, check your &lt;code&gt;tasks.json&lt;/code&gt; and &lt;code&gt;launch.json&lt;/code&gt; files, and run &lt;code&gt;cargo clean&lt;/code&gt; from the terminal within your project folder.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compiler Errors
&lt;/h2&gt;

&lt;p&gt;This blog is getting a bit long now, so one last thing to talk about.  What happens if you have errors in your code.  The good news is that the &lt;code&gt;rust-analyzer&lt;/code&gt; extension and VS Code will conspire to help you out.  Let's add an error in our code.  After the &lt;code&gt;println!&lt;/code&gt; statement, add a new one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then hit save (&lt;code&gt;Ctrl+S&lt;/code&gt;).  &lt;code&gt;rust-analyzer&lt;/code&gt; will generally only see your changes after saving.  Now the error is in the program, without building multiple things will happen:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A red line will be visible under &lt;code&gt;foo&lt;/code&gt;.  Move your mouse pointer over it and you will see a popup describing the error.&lt;/li&gt;
&lt;li&gt;Looking at the tab above your editing window, you will see that the filename has turned red with a number after it.  (There is also a letter U but that's related to source control so ignore it).  That number is how many errors there are.  The tab text can turn yellow too, which means there are warnings but no errors.  This time the number counts how many warnings there are.  At a glance you can see how many errors in your program.&lt;/li&gt;
&lt;li&gt;On the scrollbar in your window, you will see a little red marker.  This shows roughly where your error is.&lt;/li&gt;
&lt;li&gt;If your terminal window is still open in VS Code, you will see a number next to the &lt;code&gt;Problems&lt;/code&gt; tab.  Inside this tab, you will see a list of errors.  Click on them to jump to the line of code with the error.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Wow, that's a lot of clues something has gone wrong with your program.  Now hit your build key to run the build task (remember mine was &lt;code&gt;F7&lt;/code&gt;).  In the &lt;code&gt;Terminal&lt;/code&gt; tab below you will see the output of the compiler.  It usually has a better error message than seen elsewhere:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zerdnevnrf9xdjyn43n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zerdnevnrf9xdjyn43n.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Rust provides the best error messages EVER!  You can even go to your terminal and type &lt;code&gt;rustc --explain E0765&lt;/code&gt;, as described, to explain the error above in more detail.  Truly amazing!&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;That was a lot to go over.  But we installed Rust and it's tools, installed VS Code and extended it to support Rust, and then showed how to use VS Code to build and run your program.&lt;/p&gt;

&lt;p&gt;Part #2 will talk about the borrow checker, ownership, lifetimes and all those nice things.  See you then! &lt;/p&gt;

</description>
      <category>rust</category>
      <category>vscode</category>
    </item>
  </channel>
</rss>
