<?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>Forem: Austin Tindle</title>
    <description>The latest articles on Forem by Austin Tindle (@tindleaj).</description>
    <link>https://forem.com/tindleaj</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%2F172863%2F39499132-1401-4ee4-b9f2-9ae0cc587652.jpeg</url>
      <title>Forem: Austin Tindle</title>
      <link>https://forem.com/tindleaj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/tindleaj"/>
    <language>en</language>
    <item>
      <title>Rust in 2021 - Rust for JavaScript developers</title>
      <dc:creator>Austin Tindle</dc:creator>
      <pubDate>Wed, 14 Oct 2020 00:00:00 +0000</pubDate>
      <link>https://forem.com/tindleaj/rust-in-2021-rust-for-javascript-developers-1egl</link>
      <guid>https://forem.com/tindleaj/rust-in-2021-rust-for-javascript-developers-1egl</guid>
      <description>&lt;p&gt;&lt;em&gt;This post is a response to the Rust core team’s &lt;a href="https://blog.rust-lang.org/2020/09/03/Planning-2021-Roadmap.html"&gt;call for blogs&lt;/a&gt;. I'm a little late for the official deadline, but this post isn't so much about what I think should be in the 2021 edition of Rust as much as it's about reiterating my thoughts from 2019. Specifically, I think the focus for 2021 should be to continue making learning Rust easier and more accessible for non-systems programmers.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Back in November of 2019 (a lifetime ago, it seems), I wrote up &lt;a href="https://medium.com/@tindleaj/rust-in-2020-learning-52333b7aa857"&gt;some thoughts&lt;/a&gt; on some possible goals for Rust in 2020. The TL;DR of that post: we need more learning resources for newcomers to Rust, specifically resources that aren't the standard long-form writing like books and articles. I think Rust has made some fantastic strides towards this goal this year (and really, it will always be a moving goalpost -- we can always continue to improve), with a plethora of new and exciting projects that serve different groups of newcomers trying to learn the language.&lt;/p&gt;

&lt;p&gt;I care about this goal because, back in 2017 when I first started trying to learn Rust, I didn't feel like there were a ton of resources that were made for &lt;em&gt;me&lt;/em&gt;. And by me, I mean a web developer with no formal CS background and no real experience doing systems work. Back then the official Rust website supported that sentiment, with the stated goal that: &lt;a href="https://web.archive.org/web/20170602054417/https://www.rust-lang.org/en-US/"&gt;Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety&lt;/a&gt;. I didn't even really know what a segfault was, and I certainly couldn't tell you what a safe thread looked like. But a few things kept me interested, beyond sheer stubbornness and the words &lt;em&gt;blazing fast&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;First off, I'd already all but given up on learning C or C++. After struggling through several different learning resources, I was basically told straight up that with my background, it would be 3-5 years before I was done continuously shooting myself in the foot with C, and could then maybe call myself a novice. C++ looked to be a similar timeline, just to figure out how to write good, clean, modern code without again shooting myself innumerable times in the foot. Coming from JavaScript, I felt like there was too much that I didn't know I didn't know, and I couldn't figure out a good path out of the dark on my own. At that point, Rust seemed like it could be an alternative, even if it was just people on HackerNews who said so.&lt;/p&gt;

&lt;p&gt;Another thing, somewhat related to the first, was that I truly didn't feel like a &lt;em&gt;Real Programmer&lt;/em&gt;, having spent most of my professional time to that point working with JavaScript and PHP. This sentiment also probably had a lot to do with how often I was on the orange site, but at the time it was something I felt pretty strongly. Languages like Rust and C and C++ felt like doorways into a new world, with crazy things like compilers and pointers and memory and other dangers waiting on the other side, if I could just figure out how to get through.&lt;/p&gt;

&lt;p&gt;And for these reasons, at least initially, I kept trying to learn Rust. I bought all 4 books available for sale at the time. I worked through the blog posts and series from others who were kind and industrious enough to publish them. I wrote some (very, very bad) compilers. I learned what a pointer is. But it hasn't been an easy process, and it hasn't been continuous either. I've gotten frustrated and quit more than once in the past 3 years, and those niggling thoughts about not being a good enough programmer were hard to shake at times. Even now, 3 years down the road (ironic, huh?), I still feel pretty novice in some areas, although looking back I can see really just how much I've learned from the process.&lt;/p&gt;

&lt;p&gt;All of this to say, I don't think I'm the only JavaScript developer who has tried to learn Rust and struggled with it in the past three years. According to last year's survey, the second most common response to the question "Why not Rust" &lt;a href="https://blog.rust-lang.org/2020/04/17/Rust-survey-2019.html#why-not-use-rust"&gt;had to do with the learning curve&lt;/a&gt;. Of those respondents, the vast majority were web application developers (read: JavaScript, PHP, Ruby, etc. developrs). Of those who've tried to get over that learning curve, I think many of them were eventually turned away by the lack of diverse learning resources. And like I said before, as bad as 2020 has been for everything else, I think it's been a pretty great year for new Rust educational content. But, we can do better. And I hope we do better in 2021.&lt;/p&gt;

&lt;p&gt;And by &lt;em&gt;we&lt;/em&gt;, I really do mean &lt;em&gt;we&lt;/em&gt;; they say you should build things that solve your own problems. So, I'm putting together a series of screencasts, exercises, and other resources aimed at teaching Rust to JavaScript developers. It's still pretty early on, but I hope to have some parts of it ready soon, with the whole project done sometime in January of 2021. If this is something you think would be awesome to have, just shoot me an email at &lt;em&gt;&lt;a href="mailto:tindleaj@gmail.com"&gt;tindleaj@gmail.com&lt;/a&gt;&lt;/em&gt; and I'll keep you updated.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>2021</category>
      <category>javascript</category>
    </item>
    <item>
      <title>A JavaScript Developer's Cheatsheet for Rust</title>
      <dc:creator>Austin Tindle</dc:creator>
      <pubDate>Sun, 23 Aug 2020 00:00:00 +0000</pubDate>
      <link>https://forem.com/tindleaj/a-javascript-developer-s-cheatsheet-for-rust-2fm1</link>
      <guid>https://forem.com/tindleaj/a-javascript-developer-s-cheatsheet-for-rust-2fm1</guid>
      <description>&lt;p&gt;Learning a new programming language is a great opportunity to learn new universal concepts and patterns that apply to all languages, not just the one you're learning. However, before you can get a handle on all the new stuff a language provides, first you have to figure out how to write the new language like you would write whatever old language(s) you know.&lt;/p&gt;

&lt;p&gt;For the JavaScript developer, Rust offers a plethora of new and sometimes brain-bending concepts that exist in Rust but not in JavaScript. But in order to appreciate those concepts, first you have to get a handle on the basic syntax of the language. To speed up that process, you can use the JavaScript you already know to draw parallels to the Rust equivalents.&lt;/p&gt;

&lt;p&gt;This cheatsheet provides some basic comparisons between JavaScript syntax and the parallels in Rust. It purposefully sticks to the basics that have decent parallels, to get you comfortable writing some simple programs in Rust.&lt;/p&gt;

&lt;p&gt;Don't let the label of "Systems Programming Language" discourage you. Rust is an incredibly accessible language, in more ways than one. Use what you already know and &lt;a href="https://tndl.me/blog/2020/introduction-to-rust/"&gt;learn some Rust&lt;/a&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Variables (bindings)
&lt;/h2&gt;

&lt;p&gt;Rust variables are immutable by default. This is sort of like having all varaibles be &lt;code&gt;const&lt;/code&gt; in JavaScript. JavaScript &lt;code&gt;const&lt;/code&gt; is shallow, but Rust variables cannot be mutated at all unless you declare that variable &lt;code&gt;mut&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScriptlet value = 10;let greeting = "Hello!";let counter = 0;counter += 1;

// Rustlet value = 10; // Cannot be changedlet greeting = "Hello!"; // Also immutablelet mut counter = 0; // This can be changedcounter += 1;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Printing Output
&lt;/h2&gt;

&lt;p&gt;Rust's &lt;code&gt;println!&lt;/code&gt; takes a string argument, which sort of acts like a JavaScript template string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScriptlet name = "Austin Tindle";console.log(`Hello ${name}!`);

// Rustlet name = "Austin Tindle";println!("Hello {}!", name);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Functions
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;main&lt;/code&gt; function in Rust is the entrypoint to the program, and other functions need to be called from &lt;code&gt;main&lt;/code&gt;. In JavaScript there is no special entrypoint function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScriptfunction weather() { console.log("Sunny!");}weather();

// Rustfn weather() { println!("Sunny!");}fn main() { weather();}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conditionals
&lt;/h2&gt;

&lt;h3&gt;
  
  
  If/Else
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScriptif (true === false) { console.log("Never happens.");} else if (false === true) { console.log("Also never happens.");} else { console.log("Perfection.");}

// Rustif true == false { println!("Impossible!");} else if false == true { println!("Still impossible!");} else { println!("This works.");}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Unlike in JavaScript, Rust does not have "truthy" values. It's strict static typing means that conditional expressions need to evaluate to a &lt;code&gt;bool&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Rustlet not\_a\_boolean = "I'm a String";if not\_a\_boolean { // Error: mismatched types expected `bool`, found `&amp;amp;str`}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Switch &amp;amp; Match
&lt;/h3&gt;

&lt;p&gt;Switch statements aren't as widely used in JavaScript as if/else, but match statements in Rust are very popular. They aren't exactly the same, and match statements have a lot of powerful uses not available to JavaScript switch statements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScriptlet stone = "Thunder Stone";switch (stone) { case "Thunder Stone": console.log("Jolteon!"); break; case "Water Stone": console.log("Vaporeon!"); break; case "Fire Stone": console.log("Flareon!"); break; default: console.log("Eevee!");}

// Rustlet stone = "Thunder Stone";match stone { "Thunder Stone" =&amp;gt; println!("Jolteon!"), "Water Stone" =&amp;gt; println!("Vaporeon!"), "Fire Stone" =&amp;gt; println!("Flareon!"), \_ =&amp;gt; println!("Eevee!")}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Importing Other Code Using Modules
&lt;/h2&gt;

&lt;p&gt;In Rust, any function marked &lt;code&gt;pub&lt;/code&gt; can be imported into another file. Any file that is not &lt;code&gt;main.rs&lt;/code&gt; or &lt;code&gt;lib.rs&lt;/code&gt; automatically gets a namespace based on its source file name. The &lt;code&gt;mod&lt;/code&gt; keyword pulls in source files with equivalent filenames.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;use&lt;/code&gt; keyword brings nested items into the current scope, sort of like the &lt;code&gt;import {x} from 'y'&lt;/code&gt; syntax in JavaScript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScript// houston.jsexport default function launch() { console.log("Liftoff!");}export function abort() { console.log("Aborting!");}

// JavaScript// main.jsimport launch, { abort } from "./houston";launch();abort();

// Rust// houston.rspub fn launch() { println!("Liftoff!");}pub fn abort() { println!("Aborting!");}

// Rust// main.rsmod houston;use houston::{ launch };fn main() { launch(); houston::abort();}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Arrays &amp;amp; Vectors
&lt;/h2&gt;

&lt;p&gt;Rust has a data type called 'array', but it's not the type of array we're used to in JavaScript. A growable list is called a Vector in Rust, and is available via&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScriptlet languages = ["JavaScript", "TypeScript", "Rust", "HTML"];languages.pop();console.log(languages[0]);

// Rust// A shorthand macro syntaxlet mut languages = vec!["JavaScript", "TypeScript"];languages.push("Rust");// Full syntaxlet mut alphabets = Vec::new();alphabets.push("Greek");alphabets.push("Roman");println!("{} {}", languages[2], alphabets[0]);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Iterating
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScriptlet utensils = ["Fork", "Spoon", "Spork", "Knife"];for (let utensil of utensils) { console.log(`Eating with a ${utensil}.`);}

// Rustlet utensils = vec!["Fork", "Spoon", "Spork", "Knife"];for utensil in utensils.iter() { println!("Eating with a {}.", utensil);}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Other Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://tndl.me/blog/introduction_to_rust"&gt;Introduction to Rust for Node Developers&lt;/a&gt; A project-based intro to Rust.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=BBvcK_nXUEg&amp;amp;list=PL85XCvVPmGQijqvMcMBfYAwExx1eBu1Ei&amp;amp;index=10&amp;amp;t=0s"&gt;RustConf 2020 - Rust for Non-Systems Programmers by Rebecca Turner&lt;/a&gt; Fantastic talk and an inspiration for this resource.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.sheshbabu.com/posts/rust-for-javascript-developers-tooling-ecosystem-overview/"&gt;Rust for JavaScript Developers Blog Series by Sheshbabu Chinnakonda&lt;/a&gt; A great intro series on Rust for JavaScripters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Are you a JavaScript developer trying to learn Rust? Send me an email at &lt;a href="//mailto:tindleaj@gmail.com"&gt;tindleaj@gmail.com&lt;/a&gt;. I'm working on stuff you'll be interested in.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Introduction to Rust for Node Developers</title>
      <dc:creator>Austin Tindle</dc:creator>
      <pubDate>Mon, 22 Jun 2020 15:58:38 +0000</pubDate>
      <link>https://forem.com/tindleaj/introduction-to-rust-for-node-developers-3j05</link>
      <guid>https://forem.com/tindleaj/introduction-to-rust-for-node-developers-3j05</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://tndl.me/blog/2020/introduction-to-rust/"&gt;tndl.me&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In this article we will build a simple command line program that returns the word count of a file. This will essentially be a simpler version of the Unix utility &lt;code&gt;wc&lt;/code&gt;, written in Rust. The goal of this article is to give an introduction to some core Rust concepts for readers who might be more familiar with web-focused languages such as JavaScript and Typescript. Therefore, the Rust code examples will be compared to similar code and concepts in JavaScrip or TypeScript. This guide also assumes no prior knowledge of Rust or related tools, but it does assume you have &lt;code&gt;node&lt;/code&gt; installed on your machine already.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Notes&lt;/li&gt;
&lt;li&gt;
Setting up

&lt;ul&gt;
&lt;li&gt;Project structure&lt;/li&gt;
&lt;li&gt;Running the project&lt;/li&gt;
&lt;li&gt;Tour of a "Hello World" program in Rust&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
The &lt;code&gt;miniwc&lt;/code&gt; program

&lt;ul&gt;
&lt;li&gt;Building a foundation&lt;/li&gt;
&lt;li&gt;Types&lt;/li&gt;
&lt;li&gt;Structures (&lt;code&gt;struct&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Implementations (&lt;code&gt;impl&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Enumerations (&lt;code&gt;enum&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Handling arguments&lt;/li&gt;
&lt;li&gt;Using Iterators&lt;/li&gt;
&lt;li&gt;Handling all &lt;code&gt;Option&lt;/code&gt;s&lt;/li&gt;
&lt;li&gt;Reading file contents&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Result&lt;/code&gt; and &lt;code&gt;expect()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Counting words&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
Conclusion

&lt;ul&gt;
&lt;li&gt;Additional resources&lt;/li&gt;
&lt;li&gt;For learning&lt;/li&gt;
&lt;li&gt;Other&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Notes
&lt;/h2&gt;

&lt;p&gt;A couple of notes and assumptions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No previous knowledge of Rust is assumed. We'll go over all of the necessary concepts as they come up, and I'll link to relevant content where I think more detail or rigor is needed. I think that knowing how the fundamentals of things work is important, and I think you should as well.&lt;/li&gt;
&lt;li&gt;Roughly intermediate-level experience with JavaScript is assumed. If you're just getting started with JavaScript or haven't built anything non-trivial with it, you might want to save this resource for later.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting up
&lt;/h2&gt;

&lt;p&gt;In order to get started, first we need to set up a new Rust project. If you haven't yet installed Rust on your computer, you can take a look at &lt;a href="https://www.rust-lang.org/learn/get-started"&gt;the official 'getting started' guide&lt;/a&gt;, or the &lt;a href="https://doc.rust-lang.org/book/ch01-01-installation.html"&gt;first chapter of The Rust Book&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once you have &lt;code&gt;cargo&lt;/code&gt; available, go ahead and run &lt;code&gt;cargo new miniwc --bin&lt;/code&gt; in a suitable directory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Project structure
&lt;/h3&gt;

&lt;p&gt;The logical next question is "What is &lt;code&gt;cargo&lt;/code&gt;?". &lt;code&gt;cargo&lt;/code&gt; is a direct parallel to &lt;code&gt;npm&lt;/code&gt; in the Node ecosystem, in other words Rust's built-in package manager. You can view popular &lt;code&gt;crates&lt;/code&gt; (packages) available at &lt;a href="https://crates.io"&gt;crates.io&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;cargo new miniwc --bin&lt;/code&gt; command tells &lt;code&gt;cargo&lt;/code&gt; to create a new &lt;em&gt;binary&lt;/em&gt; (able to run on our machine) Rust project named &lt;code&gt;miniwc&lt;/code&gt; in the directory &lt;code&gt;./miniwc&lt;/code&gt; and set up the basic boilerplate project structure: &lt;code&gt;Cargo.toml&lt;/code&gt;, &lt;code&gt;src/main.rs&lt;/code&gt;, and a &lt;code&gt;.gitignore&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Cargo.toml&lt;/code&gt;: Analogous to Node's &lt;code&gt;package.json&lt;/code&gt;. This is where you put project information and declare project dependencies&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Cargo.lock&lt;/code&gt;: This is a manifest managed by &lt;code&gt;cargo&lt;/code&gt;, that tracks exact dependency versions. It is analogous to Node's &lt;code&gt;package-lock.json&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;src/main.rs&lt;/code&gt;: Our project is a &lt;em&gt;binary&lt;/em&gt; project, meaning we can compile and run it on our machine. &lt;code&gt;cargo&lt;/code&gt; creates a &lt;code&gt;main.rs&lt;/code&gt; file as the default entry point for compiling our source code.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.gitignore&lt;/code&gt;: A standard &lt;code&gt;git&lt;/code&gt; artifact, tells &lt;code&gt;git&lt;/code&gt; what files to ignore from source control.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Running the project
&lt;/h3&gt;

&lt;p&gt;That's it for the project structure, but what about actually running the code? In &lt;code&gt;node&lt;/code&gt;, we have &lt;code&gt;npm&lt;/code&gt; which allows us to define scripts such as &lt;code&gt;start&lt;/code&gt; and &lt;code&gt;test&lt;/code&gt;, and then run those commands via &lt;code&gt;npm run start&lt;/code&gt; or &lt;code&gt;npm run test&lt;/code&gt;. &lt;code&gt;cargo&lt;/code&gt; gives us similar functionality. Running &lt;code&gt;cargo run&lt;/code&gt; in our project directory will run our boilerplate project. Try it out, and you should see &lt;code&gt;Hello, world!&lt;/code&gt; printed to your console.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You may have noticed a new &lt;code&gt;target/&lt;/code&gt; directory appear after you ran &lt;code&gt;cargo run&lt;/code&gt;. This is a folder managed by &lt;code&gt;cargo&lt;/code&gt; to store build artifacts and other dependencies of the compilation process. For a more detailed guide to &lt;code&gt;cargo&lt;/code&gt; and an overview of concepts like the &lt;code&gt;target/&lt;/code&gt; directory, check out &lt;a href="https://doc.rust-lang.org/cargo/index.html"&gt;The Cargo Book&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Tour of a "Hello World" program in Rust
&lt;/h3&gt;

&lt;p&gt;Let's take a moment to take a look at the auto-generated code within &lt;code&gt;main.rs&lt;/code&gt; and draw some basic parallels from the JavaScript world to that of Rust:&lt;/p&gt;

&lt;p&gt;File: src/main.rs&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we ported the above Rust program to JavaScript it would look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello, world!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Since `main()` isn't a special function in JavaScript,&lt;/span&gt;
&lt;span class="c1"&gt;// we have to invoke it if we want our code to run:&lt;/span&gt;
&lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;If the distinction between compiled and interpreted languages is a bit hazy for you, take a look at &lt;a href="https://guide.freecodecamp.org/computer-science/compiled-versus-interpreted-languages/"&gt;this article&lt;/a&gt; for a more in-depth treatment.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fn&lt;/code&gt; is the &lt;em&gt;function&lt;/em&gt; keyword in Rust, and &lt;code&gt;main&lt;/code&gt; denotes to the name of the function. &lt;code&gt;main&lt;/code&gt; is a special function name in Rust (as it is in other compiled languages like C) and it lets the Rust &lt;em&gt;compiler&lt;/em&gt; know that this is the entry point of an executable program. &lt;code&gt;()&lt;/code&gt; is the list of &lt;em&gt;arguments&lt;/em&gt;. In this case there are no arguments, so the parentheses are empty.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;body&lt;/em&gt; of the &lt;code&gt;main&lt;/code&gt; function is declared with &lt;code&gt;{ }&lt;/code&gt;, and represents its &lt;em&gt;scope&lt;/em&gt;. Inside the body of &lt;code&gt;main&lt;/code&gt;, we have &lt;code&gt;println!("Hello, world!");&lt;/code&gt;. This looks like a function, but in fact is a &lt;em&gt;macro&lt;/em&gt;. In Rust &lt;em&gt;macros&lt;/em&gt; are denoted by the &lt;code&gt;!&lt;/code&gt; at the end of a keyword.&lt;/p&gt;

&lt;p&gt;There is no great parallel for &lt;em&gt;macros&lt;/em&gt; in JavaScript, but a simple definition is that &lt;em&gt;macros&lt;/em&gt; are code that generate other code when the program is compiled. Rust will replace &lt;code&gt;println!&lt;/code&gt; with code for printing to &lt;em&gt;standard out&lt;/em&gt; that works for whatever computer architecture you're compiling the Rust code for. In my case this would be code for printing in macOS, but it might be different for you.&lt;/p&gt;

&lt;p&gt;With the basic setup and syntax tour out of the way, we can move on to an overview of our &lt;code&gt;miniwc&lt;/code&gt; program.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;cargo&lt;/code&gt; isn't strictly necessary to create Rust binaries, it just provides some convenient tools and a bit of boilerplate to get you started. All you need to compile Rust projects is the Rust Compiler (&lt;code&gt;rustc&lt;/code&gt;). Running &lt;code&gt;rustc foobar.rs&lt;/code&gt; on any valid and correct Rust program will output an executable binary. Don't believe me? Try it with the code above!&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The &lt;code&gt;miniwc&lt;/code&gt; program
&lt;/h2&gt;

&lt;p&gt;At the end of this article, we will have an executable program that takes a filename as an argument and returns the word count of that document.&lt;/p&gt;

&lt;p&gt;Let's get into it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building a foundation
&lt;/h3&gt;

&lt;p&gt;Before we can begin tackling the program requirements we've outlined above, there are several Rust concepts that we need to anchor to their counterparts in JavaScript. &lt;a href="https://blog.usejournal.com/you-probably-shouldt-be-using-react-2f45ca487c8e?source=friends_link&amp;amp;sk=b88d7e3f0c715965bbc859b5ce8053e1"&gt;I'm a big advocate for understanding bedrock concepts&lt;/a&gt;, especially as you move past the beginner stage where you know how to get things done, but maybe not why you're doing them that way. I feel that Rust is a great tool to put the effort in and &lt;em&gt;really&lt;/em&gt; learn, so before we go ahead and actually write the code for our program, we're going to explore a prelude of necessary concepts, step by step. These include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The type system in Rust, and how it relates to types in JavaScript&lt;/li&gt;
&lt;li&gt;Rust &lt;code&gt;struct&lt;/code&gt;s, their similarity to JavaScript &lt;code&gt;Objects&lt;/code&gt;, and an overview on how to use them to provide &lt;em&gt;structure&lt;/em&gt; to our code&lt;/li&gt;
&lt;li&gt;Rust &lt;code&gt;impl&lt;/code&gt;s, the JavaScript &lt;em&gt;Prototypal Inheritance&lt;/em&gt; model, and how we can create reusable functionality in our Rust code&lt;/li&gt;
&lt;li&gt;A quick note on &lt;em&gt;enumerations&lt;/em&gt; (&lt;code&gt;enum&lt;/code&gt;s)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are some concepts here that may seem very foreign, but they all map to JavaScript concepts you probably already know and use regularly. If you have a good grasp on the above topics already, feel free to skip the next few sections. Otherwise, let's unpack them one at a time.&lt;/p&gt;

&lt;h4&gt;
  
  
  Types
&lt;/h4&gt;

&lt;p&gt;Rust is a &lt;em&gt;statically typed language&lt;/em&gt;, and therefore it expects explicit &lt;em&gt;type&lt;/em&gt; annotations in the places in your code where it isn't obvious what the type of a value is. If you have experience with TypeScript, this concept should be familiar.&lt;/p&gt;

&lt;p&gt;Two common ways you'll interact with &lt;em&gt;types&lt;/em&gt; in Rust is through argument types and return types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;example_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;integer_arg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;string_arg&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;other_arg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;OurCustomType&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="c"&gt;// ---snip---&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the above example, we pass three arguments to our &lt;code&gt;example_function&lt;/code&gt;, &lt;code&gt;integer_arg&lt;/code&gt; with the type &lt;code&gt;i64&lt;/code&gt; (a 64-bit signed integer), &lt;code&gt;string_arg&lt;/code&gt; with the type &lt;code&gt;String&lt;/code&gt;, and &lt;code&gt;other_arg&lt;/code&gt; with the made-up example type &lt;code&gt;OurCustomType&lt;/code&gt;. These type annotations are denoted by the colon (&lt;code&gt;:&lt;/code&gt;) following the argument name. After the list of arguments, there's an arrow (&lt;code&gt;-&amp;gt;&lt;/code&gt;) followed by &lt;code&gt;String&lt;/code&gt; which signifies that this function will return a &lt;code&gt;String&lt;/code&gt; value.&lt;/p&gt;

&lt;p&gt;JavaScript is a dynamically typed language, which means all of the &lt;em&gt;type&lt;/em&gt; behavior we have to specifically define in our Rust code is handled under the hood by the JavaScript runtime. JavaScript has primitive types like &lt;code&gt;Number&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt;, but it doesn't require the programmer to be explicit about what &lt;em&gt;types&lt;/em&gt; correspond to each value. JavaScript also doesn't allow the programmer to come up with their own types, like the &lt;code&gt;Args&lt;/code&gt; type we saw previously in the &lt;code&gt;args&lt;/code&gt; function signature. &lt;a href="https://hackernoon.com/statically-typed-vs-dynamically-typed-languages-e4778e1ca55"&gt;This is both powerful and limiting&lt;/a&gt;, depending on the context and use-case.&lt;/p&gt;

&lt;h4&gt;
  
  
  Structures (&lt;code&gt;struct&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;With the basics of &lt;em&gt;types&lt;/em&gt; in Rust under our belts, let's take a moment to unwrap another fundamental Rust concept that we'll need going forward: &lt;code&gt;struct&lt;/code&gt;. Rust, unlike modern JavaScript, has no concept of &lt;code&gt;class&lt;/code&gt; and it doesn't have a catch-all, ubiquitous name/value collection like JavaScript's &lt;code&gt;Object&lt;/code&gt; type. Instead, Rust allows you to associate fields and related functions using &lt;em&gt;structures&lt;/em&gt;, via the keyword &lt;code&gt;struct&lt;/code&gt;. This is somewhat similar to how &lt;code&gt;objects&lt;/code&gt; are used in JavaScript. Compare the following two examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Message title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is a message.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="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;Message&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;title&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;body&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;let&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Message&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;title&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;"Message title"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="n"&gt;body&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;"This is a message."&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;Since Rust doesn't give you an arbitrary bucket of key/value pairs to work with (like JavaScript does with &lt;code&gt;Objects&lt;/code&gt;), we first need to define the &lt;em&gt;structure&lt;/em&gt; of our &lt;code&gt;Message&lt;/code&gt; type, via the &lt;code&gt;struct&lt;/code&gt; keyword. Note how in the JavaScript example, we just assign &lt;code&gt;String&lt;/code&gt; values to the &lt;code&gt;message&lt;/code&gt; and &lt;code&gt;body&lt;/code&gt; keys. This is a very common pattern, and in some cases is extremely powerful and simple. In the Rust example, we have to be explicit about the types of values each &lt;em&gt;field&lt;/em&gt; (note that in Rust, &lt;a href="https://doc.rust-lang.org/book/ch05-01-defining-structs.html#defining-and-instantiating-structs"&gt;we call these key/value pairs &lt;em&gt;fields&lt;/em&gt;&lt;/a&gt;, while in JavaScript they're called &lt;em&gt;properties&lt;/em&gt;). Once we've told the Rust compiler what our &lt;code&gt;Message&lt;/code&gt; &lt;em&gt;fields&lt;/em&gt; will contain, we then can create a new &lt;code&gt;Message&lt;/code&gt; with our specific field values.&lt;/p&gt;

&lt;h4&gt;
  
  
  Implementations (&lt;code&gt;impl&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;JavaScript uses an inheritance model called &lt;em&gt;Prototypal Inheritance&lt;/em&gt; in order to allow for extending and reusing behavior in your code. Another familiar model that accomplishes something similar is the more traditional class-based model you may have come across in other languages like Java and TypeScript (JavaScript has &lt;code&gt;class&lt;/code&gt; syntax, but it's just sugar over its prototypal inheritance model).&lt;/p&gt;

&lt;p&gt;For the purposes of this project, you don't need to be super familiar with the ins and outs of &lt;em&gt;Prototypal Inheritance&lt;/em&gt; or &lt;em&gt;Object Oriented Programming&lt;/em&gt;, but if you're interested in diving in, Mozilla offers an in-depth treatment &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain"&gt;here&lt;/a&gt;. What we're specifically interested in is how JavaScript allows you to implement and reuse behavior, versus how Rust does it. Consider the following JavaScript example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Using JavaScript's `class` syntax because&lt;/span&gt;
&lt;span class="c1"&gt;// it's simpler for this example&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Message&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&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="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;PrivateMessage&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Message&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;private: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;content&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// hello&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;privateMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;PrivateMessage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;privateMessage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// private: hello&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, we've modeled &lt;code&gt;PrivateMessage&lt;/code&gt; &lt;em&gt;as a&lt;/em&gt; &lt;code&gt;Message&lt;/code&gt;. It inherits the &lt;code&gt;send&lt;/code&gt; function we defined on &lt;code&gt;Message&lt;/code&gt;, but we can change it to be specific for our &lt;code&gt;PrivateMessage&lt;/code&gt; class. Rust has a different way of doing things. Let's take a look at the same idea, expressed in Rust:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;PrivateMessage&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;NormalMessage&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;Message&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;send&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;content&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="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;content&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="n"&gt;Message&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;NormalMessage&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="c"&gt;// Use the default `send`&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Message&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;PrivateMessage&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;send&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;content&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"private: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&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;pub&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;NormalMessage&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
  &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="nf"&gt;.send&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="c"&gt;// hello&lt;/span&gt;

  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;private_message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PrivateMessage&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
  &lt;span class="n"&gt;private_message&lt;/span&gt;&lt;span class="nf"&gt;.send&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="c"&gt;// private: hello&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this version of the program, we've defined &lt;code&gt;Message&lt;/code&gt; as a &lt;em&gt;trait&lt;/em&gt;, which can be &lt;em&gt;implemented&lt;/em&gt; by our other code. In other words, our &lt;code&gt;PrivateMessage&lt;/code&gt; and &lt;code&gt;NormalMessage&lt;/code&gt; structs&lt;code&gt;NormalMessage&lt;/code&gt; uses the default &lt;code&gt;send&lt;/code&gt; implementation that we define in the &lt;code&gt;Message&lt;/code&gt; trait, while &lt;code&gt;PrivateMessage&lt;/code&gt; implements its own version of &lt;code&gt;send&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Hopefully this sheds a bit of light onto the basics of Rust inheritance (via &lt;code&gt;traits&lt;/code&gt; and &lt;code&gt;impl&lt;/code&gt;) versus JavaScript (via prototypes). If any of this still feels opaque, take some time to dive-in to the relevant sections in the Rust Book:&lt;/p&gt;

&lt;h4&gt;
  
  
  Enumerations (&lt;code&gt;enum&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;If you're familiar with TypeScript, then Rust's &lt;code&gt;enum&lt;/code&gt; &lt;em&gt;type&lt;/em&gt; is a close parallel. If not, &lt;em&gt;enumerations&lt;/em&gt; are relatively straightforward: they define a &lt;em&gt;type&lt;/em&gt; that can be one of several &lt;em&gt;variants&lt;/em&gt;. For example, we can create an &lt;em&gt;enum&lt;/em&gt; that represents the different types of common U.S. coinage like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;Coin&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;Penny&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;Nickel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;Dime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;Quarter&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And we can reference any single variant via:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;penny&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Coin&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Coin&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Penny&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;dime&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Coin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Coin&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Dime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, both &lt;code&gt;penny&lt;/code&gt; and &lt;code&gt;dime&lt;/code&gt; are &lt;code&gt;Coin&lt;/code&gt;s (they have the &lt;code&gt;Coin&lt;/code&gt; type), but we can get more specific and state the &lt;em&gt;variant&lt;/em&gt; of &lt;code&gt;Coin&lt;/code&gt; that each variable holds. In JavaScript&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling arguments
&lt;/h3&gt;

&lt;p&gt;Now that we've explored the necessary foundational concepts to understand and implement our &lt;code&gt;miniwc&lt;/code&gt; program, let's get back to our &lt;code&gt;miniwc&lt;/code&gt; program. As mentioned before, our program should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Be executable&lt;/li&gt;
&lt;li&gt;Take a filename as an argument&lt;/li&gt;
&lt;li&gt;Return the word count of that document&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Currently, our program does none of the things outlined above. When you execute &lt;code&gt;cargo run&lt;/code&gt; from the command line, we still just see &lt;code&gt;Hello, world!&lt;/code&gt; printed out. Let's take it step by step, and first handle taking a filename as an argument.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;node&lt;/code&gt;, one of the global variables made available to our programs during runtime is the &lt;code&gt;process.argv&lt;/code&gt; variable. This variable contains all of the arguments passed to your &lt;code&gt;node&lt;/code&gt; program. To take command line arguments and print them out using &lt;code&gt;node&lt;/code&gt;, we could do the following:&lt;/p&gt;

&lt;p&gt;File: main.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arg&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&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 you save and run that program in the root of the project using &lt;code&gt;node main.js hello&lt;/code&gt;, you should get three outputs. The first output is the program running our JavaScript code (in this case &lt;code&gt;node&lt;/code&gt;). The second is the filename of the program being run, and the third is the argument we passed in.&lt;/p&gt;

&lt;p&gt;Rust does not have a runtime environment like &lt;code&gt;node&lt;/code&gt;, so how can we get arguments passed to our program?&lt;/p&gt;

&lt;p&gt;Although Rust doesn't have a language-specific runtime environment, the operating system your Rust program runs on &lt;em&gt;is&lt;/em&gt; technically a runtime. And luckily for us, the operating system provides a way to inject variables into programs. We won't need to get into the specifics of how that happens (and the potential pitfalls), because the &lt;em&gt;Rust standard library&lt;/em&gt; provides an easy way for us to access the arguments passed to our program, via the &lt;code&gt;std::env&lt;/code&gt; module. Similar to how &lt;code&gt;process.argv&lt;/code&gt; works in &lt;code&gt;node&lt;/code&gt;, the &lt;code&gt;std::env&lt;/code&gt; module will allow us to get a list of arguments we can then use how we'd like.&lt;/p&gt;

&lt;p&gt;In order to make the &lt;code&gt;std::env&lt;/code&gt; module more ergonomic to use, we can &lt;code&gt;use&lt;/code&gt; it at the top of our program like so: &lt;code&gt;use std::env&lt;/code&gt;. The &lt;code&gt;use&lt;/code&gt; keyword allows us to bring a module into scope. The &lt;code&gt;std&lt;/code&gt; library is already available to our program, so we could just type &lt;code&gt;std::env::foo_function&lt;/code&gt; every time we wanted to use something from the &lt;code&gt;env&lt;/code&gt; module, but with &lt;code&gt;use&lt;/code&gt; we can bring the &lt;code&gt;env&lt;/code&gt; module directly into scope. A loose parallel between &lt;code&gt;use&lt;/code&gt; to an equivalent in JavaScript would be taking a globally available function like &lt;code&gt;global.console.log&lt;/code&gt; and setting it to its own variable for easier use, for example &lt;code&gt;let log = global.console.log&lt;/code&gt;. With the &lt;code&gt;env&lt;/code&gt; module in scope, we can now use the public function &lt;a href="https://doc.rust-lang.org/std/env/fn.args.html"&gt;&lt;code&gt;args&lt;/code&gt;&lt;/a&gt;, which exists in the &lt;code&gt;env&lt;/code&gt; module.&lt;/p&gt;

&lt;p&gt;This function will return a value with the &lt;em&gt;type&lt;/em&gt; of &lt;code&gt;Args&lt;/code&gt;. &lt;code&gt;Args&lt;/code&gt; &lt;em&gt;implements&lt;/em&gt; the &lt;em&gt;trait&lt;/em&gt; &lt;code&gt;Iterator&lt;/code&gt;, which allows us to &lt;em&gt;iterate&lt;/em&gt; over the returned arguments. The function signature for &lt;code&gt;args&lt;/code&gt; looks like so: &lt;code&gt;fn args() -&amp;gt; Args&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Except for &lt;code&gt;Iterator&lt;/code&gt; and the idea of &lt;em&gt;iterating&lt;/em&gt;, these are all concepts we've explored in the last few sections, so now let's put them to work. Once you've added the &lt;code&gt;use&lt;/code&gt; statement for &lt;code&gt;std::env&lt;/code&gt;, your program should look like this:&lt;/p&gt;

&lt;p&gt;File: src/main.rs&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="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="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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's enhance our program and print out all of the arguments that we pass in from the command line:&lt;/p&gt;

&lt;p&gt;File: src/main.rs&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="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;for&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt; &lt;span class="n"&gt;in&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="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;arg&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;em&gt;If the &lt;code&gt;println!&lt;/code&gt; macro call seems a bit strange, you can &lt;a href="https://doc.rust-lang.org/book/ch19-06-macros.html"&gt;dive deeper here&lt;/a&gt;, but you can also simply think of &lt;code&gt;println!&lt;/code&gt; as similar to JavaScript template literals: anything between &lt;code&gt;{}&lt;/code&gt; will be replaced with the variable you pass as subsequent arguments. Play around with it a bit to get a more intuitive feel for how it works.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now let's run the program and pass it some arguments via &lt;code&gt;cargo run -- hello world&lt;/code&gt; (we separate the commands passed to &lt;code&gt;cargo&lt;/code&gt; and the commands passed to our program with &lt;code&gt;--&lt;/code&gt;). You should get the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;target/debug/miniwc
hello
world
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first line of our output is actually the name of the program running, by convention. It's &lt;code&gt;target/debug/miniwc&lt;/code&gt; because that's the binary created for us by &lt;code&gt;cargo&lt;/code&gt;. If you compiled this project for release, or used &lt;code&gt;rustc&lt;/code&gt; to compile, then the first item in the &lt;code&gt;args()&lt;/code&gt; value would just be &lt;code&gt;miniwc&lt;/code&gt;. On the next two lines we see the two arguments we passed in.&lt;/p&gt;

&lt;p&gt;Our program now nominally supports passing in arguments via the command line. Now we're ready to do something with them.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using Iterators
&lt;/h4&gt;

&lt;p&gt;Let's start by binding the value of the first argument passed in by the user (ignoring the program path argument, which comes first) using the &lt;code&gt;nth&lt;/code&gt; method on the &lt;code&gt;Args&lt;/code&gt; &lt;em&gt;type&lt;/em&gt;. &lt;code&gt;Args&lt;/code&gt; is the type of the value returned from &lt;code&gt;std::env::args()&lt;/code&gt;, and it &lt;em&gt;implements&lt;/em&gt; the &lt;code&gt;Iterator&lt;/code&gt; type, thereby inheriting all of the methods on &lt;code&gt;Iterator&lt;/code&gt;. As per &lt;a href="https://doc.rust-lang.org/std/env/struct.Args.html"&gt;the &lt;code&gt;Args&lt;/code&gt; documentation&lt;/a&gt;, &lt;code&gt;Args&lt;/code&gt; specifically gives us an &lt;code&gt;Iterator&lt;/code&gt; whose values are &lt;code&gt;String&lt;/code&gt;s.&lt;/p&gt;

&lt;p&gt;One of the &lt;a href="https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.nth"&gt;methods we get by inheriting from &lt;code&gt;Iterator&lt;/code&gt; is &lt;code&gt;nth&lt;/code&gt;&lt;/a&gt;, which returns the value of the &lt;code&gt;Iterator&lt;/code&gt; item at the index given to &lt;code&gt;nth&lt;/code&gt;. For example, &lt;code&gt;env::args().nth(1)&lt;/code&gt; should give us the value at index &lt;code&gt;1&lt;/code&gt; of the &lt;code&gt;args_list&lt;/code&gt;. You can think of &lt;code&gt;Iterator&lt;/code&gt; as sort of giving the properties of a JavaScript &lt;code&gt;Array&lt;/code&gt; to any type that &lt;em&gt;implements&lt;/em&gt; &lt;code&gt;Iterator&lt;/code&gt;. Like &lt;code&gt;Array&lt;/code&gt;s, &lt;code&gt;Iterators&lt;/code&gt; come with all sorts of useful &lt;a href="https://doc.rust-lang.org/std/iter/trait.Iterator.html#provided-methods"&gt;methods&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;nth&lt;/code&gt;, we should now be able to grab the first argument passed to our program. Let's set that value to a variable, and try to print it out with the following code:&lt;/p&gt;

&lt;p&gt;File: src/main.rs&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;pub&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;filename&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;.nth&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="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;filename&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;After a &lt;code&gt;cargo run -- hello&lt;/code&gt;, we see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error[E0277]: `std::option::Option&amp;lt;std::string::String&amp;gt;` doesn't implement `std::fmt::Display`
 --&amp;gt; src/main.rs:5:20
  |
5 |     println!("{}", filename)
  |                    ^^^^^^^^ `std::option::Option&amp;lt;std::string::String&amp;gt;` cannot be formatted with the default formatter
  |
  = help: the trait `std::fmt::Display` is not implemented for `std::option::Option&amp;lt;std::string::String&amp;gt;`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
  = note: required by `std::fmt::Display::fmt`

error: aborting due to previous error
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;An error! What happened?&lt;/p&gt;

&lt;h4&gt;
  
  
  Handling all &lt;code&gt;Option&lt;/code&gt;s
&lt;/h4&gt;

&lt;p&gt;The issue with our code is that &lt;code&gt;nth&lt;/code&gt; doesn't return a &lt;code&gt;String&lt;/code&gt; directly, but instead returns a type called &lt;code&gt;Option&lt;/code&gt;. &lt;code&gt;Option&lt;/code&gt; is part of an interesting feature of Rust: it has no &lt;code&gt;null&lt;/code&gt; primitive type. Unlike most languages which have a &lt;code&gt;null&lt;/code&gt; type (and very much unlike JavaScript which has &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt;), Rust forces you to account for all possible values when working with operations that are influenced by things outside of the program's control, like accepting command line arguments or doing file I/O. To do this, Rust makes use of the &lt;code&gt;Option&lt;/code&gt; &lt;em&gt;enum&lt;/em&gt;, which can either be &lt;code&gt;Some(value)&lt;/code&gt; or &lt;code&gt;None&lt;/code&gt;. If the value is &lt;code&gt;None&lt;/code&gt;, Rust makes you explicitly handle it, otherwise it will be a compile time error like we saw above. While this may seem overly rigid, this is one of the features of Rust that leads to less error-prone programs.&lt;/p&gt;

&lt;p&gt;Let's look at a JavaScript example that illustrates this point:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Get the first argument passed in by the user&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;argv&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;// Do really important stuff&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&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 a subtle error that will only happen sometimes in this code. Can you spot it? If we pass an argument to our program -- &lt;code&gt;node main.js hello&lt;/code&gt; -- then it behaves as expected. However, if we don't pass an argument, we'll get an error that's probably very familiar if you use JavaScript a lot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(arg.split(''))
                  ^

TypeError: Cannot read property 'split' of undefined
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this case, it's easy to see what went wrong: if we don't pass an argument to our program, we end up setting our &lt;code&gt;arg&lt;/code&gt; variable to the value at an array index that doesn't exist. JavaScript defaults that value to &lt;code&gt;undefined&lt;/code&gt;, which then causes an error later on in our &lt;code&gt;handleArg&lt;/code&gt; function when we try to &lt;code&gt;split()&lt;/code&gt; the undefined value.&lt;/p&gt;

&lt;p&gt;While this example is trivial to fix, it's very easy to introduce this kind of bug into a larger JavaScript program, where it's potentially much harder to find the original cause of the &lt;code&gt;undefined&lt;/code&gt; value. A typical fix would have us check that the value exists before trying to use it, but that requires more code and more diligent programmers.&lt;/p&gt;

&lt;p&gt;In cases where we're dealing with input to our program that can be undefined, Rust forces us to handle the potential undefined value with the &lt;code&gt;Option&lt;/code&gt; type before the program will even compile. We can see the &lt;code&gt;Option&lt;/code&gt; type in action if we tweak our &lt;code&gt;println!&lt;/code&gt; call a bit:&lt;/p&gt;

&lt;p&gt;File: src/main.rs&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;pub&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;filename&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;.nth&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="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;filename&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;em&gt;This solution was hinted at in our error message from before. By adding the &lt;code&gt;:?&lt;/code&gt; to the curly brackets, we're essentially telling the &lt;code&gt;println!&lt;/code&gt; macro that we want to be more lenient about the types of values we can print to the console (specifically, we've added the &lt;a href="https://doc.rust-lang.org/rust-by-example/hello/print/print_debug.html"&gt;debug format trait&lt;/a&gt;).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If this doesn't make much sense, don't worry about it for now. In general, the Rust compiler is very helpful, and you can usually rely on its suggestions to fix your code if you've gotten stuck. In this case, let's follow its advice and see what we get.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After a &lt;code&gt;cargo run -- hello&lt;/code&gt;, you should see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Some("hello")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There it is! Since we passed in an argument to our program, &lt;code&gt;env::args.nth(1)&lt;/code&gt; contains &lt;code&gt;Some&lt;/code&gt; value. Now, try running the program without an argument. This time you should've gotten the &lt;code&gt;None&lt;/code&gt; variant, just as we expected.&lt;/p&gt;

&lt;p&gt;Now that we understand a bit about what's going on with Rust's &lt;code&gt;Option&lt;/code&gt; type, how do we actually get to the value inside &lt;code&gt;Some&lt;/code&gt;? Conveniently, Rust offers us a shortcut for grabbing values we are pretty sure are going to exist in our program:&lt;/p&gt;

&lt;p&gt;File: src/main.rs&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;pub&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;filename&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;.nth&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="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;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// we no longer need the ':?'&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;unwrap()&lt;/code&gt; is a method available on &lt;code&gt;Option&lt;/code&gt;, and it's pretty straightforward. If there is &lt;code&gt;Some(value)&lt;/code&gt;, then return the value. If not, then &lt;em&gt;panic&lt;/em&gt; (error out). &lt;code&gt;unwrap()&lt;/code&gt; also serves as a sort of "TODO" flag, because it signals that you should replace it before releasing your program into the world.&lt;/p&gt;

&lt;p&gt;When we run our program with at least one argument now, we should get that argument printed to the console. If we run it without any arguments, we should get a &lt;em&gt;panic&lt;/em&gt; along the lines of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;thread 'main' panicked at 'called `Option::unwrap()` on a `None` value'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With that brief foray into Rust &lt;code&gt;Option&lt;/code&gt;s out of the way, let's next move on to actually reading text files from the system.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reading file contents
&lt;/h3&gt;

&lt;p&gt;The Rust standard library &lt;a href="https://doc.rust-lang.org/std/fs/index.html"&gt;contains a module for filesystem operations&lt;/a&gt;. This module is very similar in functionality to the &lt;code&gt;fs&lt;/code&gt; module in the Node standard library. In Node, we could use the contents of a file like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;words.txt&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;a href="https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback"&gt;The &lt;code&gt;readFile()&lt;/code&gt; function&lt;/a&gt; takes a file, an optional encoding and a callback to handle either an error or the returned contents. The Rust &lt;code&gt;std::fs::read_to_string&lt;/code&gt; function does something very similar, taking a file path and returning a &lt;code&gt;Result&amp;lt;String&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;Result&lt;/code&gt; and &lt;code&gt;expect()&lt;/code&gt;
&lt;/h4&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 produce a value or something else (&lt;code&gt;None&lt;/code&gt; being the 'something else' for &lt;code&gt;Option&lt;/code&gt;). In the case of &lt;code&gt;Result&lt;/code&gt;, the results are either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Ok(T)&lt;/code&gt;, where &lt;code&gt;T&lt;/code&gt; is an arbitrary type, or,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Error&lt;/code&gt; if the operation fails.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the case of &lt;code&gt;fs::read_to_string&lt;/code&gt;, the &lt;code&gt;Ok&lt;/code&gt; result is &lt;code&gt;Ok(String)&lt;/code&gt;, since on a successful "read this file to a string" operation, the value we want back is a &lt;code&gt;String&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's add a simple text file to our project and test it out. Add the following text to a file called &lt;code&gt;words.txt&lt;/code&gt; in the root of the project:&lt;/p&gt;

&lt;p&gt;File: words.txt&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This is a file containing words
There are several words on this line
This one is short
The end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let's use &lt;code&gt;read_to_string&lt;/code&gt; to read &lt;code&gt;words.txt&lt;/code&gt; to a variable:&lt;/p&gt;

&lt;p&gt;File: src/main.rs&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="n"&gt;env&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="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;pub&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;filename&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;.nth&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="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;file_contents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;read_to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Error reading file to string"&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;file_contents&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;Here we use &lt;code&gt;expect()&lt;/code&gt;, which is very similar to &lt;code&gt;unwrap&lt;/code&gt; except it allows us to pass a custom panic message. If we run our program and pass it the argument the path of our text file (&lt;code&gt;cargo run -- words.txt&lt;/code&gt;), we should see our text printed to the console.&lt;/p&gt;

&lt;p&gt;Now that we've successfully read our text file and put its contents in a variable, we can complete the final step of counting the words in that file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Counting words
&lt;/h3&gt;

&lt;p&gt;Simple text manipulation like counting the number of individual words (separated by whitespace) is a great way to explore the power behind one of Rust's core philosophies, that of &lt;em&gt;zero cost abstractions&lt;/em&gt;. The gist of this idea is two-fold: first, you shouldn't pay (in performance or size) for any part of the programming language that you don't use, and second, if you do choose to use a language feature then it will be just as fast (or faster) than if you wrote the feature yourself. By following this simple philosophy, Rust places itself as a prime choice for writing programs that need to be mindful of space and speed considerations.&lt;/p&gt;

&lt;p&gt;To illustrate this point, let's take another example from JavaScript. A JavaScript implementation (&lt;code&gt;node&lt;/code&gt;, the browser, etc), has to include a &lt;em&gt;garbage collector&lt;/em&gt; in order to manage memory the program uses. Even if all you do is &lt;code&gt;console.log('Hello World')&lt;/code&gt;, the entirety of the JavaScript runtime, including the &lt;em&gt;garbage collector&lt;/em&gt; have to be there. In Rust, when you &lt;code&gt;println!&lt;/code&gt;, the only code that gets compiled and run is the code specifically needed to print things.&lt;/p&gt;

&lt;p&gt;It is worth noting that sometimes we don't really care that much about speed or size of our programs, and in those cases Rust doesn't have much of an advantage over JavaScript or any other language. But, when we do care about those things Rust really comes into it's own. In many cases with Rust you get the flexibility and expressive power of a super high level programming language while also getting near-unmatched performance. Let's look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="n"&gt;env&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="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;pub&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;filename&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;.nth&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="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;file_contents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;read_to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Error retrieving file"&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;number_of_words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;file_contents&lt;/span&gt;&lt;span class="nf"&gt;.split_whitespace&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.count&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;number_of_words&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;Here we've added a single line to our program, changed another, and essentially achieved our desired functionality. Let's take it step-by-step.&lt;/p&gt;

&lt;p&gt;Once we have the file contents from our &lt;code&gt;words.txt&lt;/code&gt; file bound to a variable, we take that&lt;code&gt;file_contents&lt;/code&gt; &lt;code&gt;String&lt;/code&gt; and split it up on &lt;a href="https://doc.rust-lang.org/std/primitive.str.html#method.split_whitespace"&gt;any Unicode whitespace via &lt;code&gt;split_whitespace&lt;/code&gt;&lt;/a&gt;. This returns an &lt;em&gt;Iterator&lt;/em&gt; value. This would be roughly the equivalent of using the &lt;code&gt;split()&lt;/code&gt; method on a &lt;code&gt;String&lt;/code&gt; in JavaScript, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;exampleString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is an example&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;exampleString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// Array(4) [ "This", "is", "an", "example" ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once we've done that, we can consume the &lt;code&gt;Iterator&lt;/code&gt; with &lt;code&gt;count()&lt;/code&gt; to get the number of items in it. A similar approach in JavaScript would be to use the &lt;code&gt;length&lt;/code&gt; property of the returned &lt;code&gt;Array&lt;/code&gt; from before.&lt;/p&gt;

&lt;p&gt;Finally, we print the resulting count to the console. And that's it! Run &lt;code&gt;cargo run -- words.txt&lt;/code&gt; to see the number of words in our text file.&lt;/p&gt;

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

&lt;p&gt;This program is very simple, but it illustrates a plethora of core Rust concepts. It also leaves out some other very important tools and ideas. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We could handle the &lt;code&gt;Error&lt;/code&gt; and &lt;code&gt;None&lt;/code&gt; cases in our argument-handling and I/O functionality &lt;a href="https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html"&gt;using &lt;code&gt;match&lt;/code&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;We could have counted the individual words using &lt;code&gt;Vectors&lt;/code&gt; and &lt;code&gt;loops&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;We could have opted for a more object-oriented approach and contained our functionality to &lt;code&gt;struct&lt;/code&gt;s and &lt;code&gt;impls&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;And lots more&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you've made it this far, thanks so much for reading! Writing this article has been a learning process for me, and I still very much consider myself a Rust beginner. If you spot any mistakes, or see any grievous infractions of best-practices, please reach out at &lt;code&gt;tindleaj[at]gmail[dot]com&lt;/code&gt; or &lt;a href="https://twitter.com/tindleaj"&gt;@tindleaj&lt;/a&gt; If you're interested in learning more Rust, there are a ton of other great, free, and current resources to do so.&lt;/p&gt;

&lt;h3&gt;
  
  
  Additional resources
&lt;/h3&gt;

&lt;h4&gt;
  
  
  For learning
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://doc.rust-lang.org/stable/book/"&gt;The Rust Programming Language&lt;/a&gt; - official, incredibly well written, definitely should be your first stop&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/rust-lang/rustlings"&gt;Rustlings&lt;/a&gt; - awesome interactive learning tool&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/Mercateo/rust-for-node-developers"&gt;Rust for Node developers&lt;/a&gt; - a big inspiration for this article&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://doc.rust-lang.org/rust-by-example/"&gt;Rust by Example&lt;/a&gt; - says it right on the tin&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://stevedonovan.github.io/rust-gentle-intro/readme.html"&gt;A Gentle Introduction to Rust&lt;/a&gt; - a tour through some of the great Rust features&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://exercism.io/tracks/rust"&gt;Exercism.io&lt;/a&gt; - more small, interactive projects&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Other
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://os.phil-opp.com/"&gt;Writing an OS in Rust&lt;/a&gt; - incredible project, I aspire to one day be this good&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://intermezzos.github.io/"&gt;IntermezzOS&lt;/a&gt; - more operating systems&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://bfnightly.bracketproductions.com/rustbook/chapter_0.html"&gt;Roguelike Tutorial - In Rust&lt;/a&gt; - I haven't gone through this one yet myself, but I've heard really good things&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://readrust.net/"&gt;Read Rust&lt;/a&gt; - great source for Rust related news and happenings&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>rust</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>Finding Freelance Work as a New Developer</title>
      <dc:creator>Austin Tindle</dc:creator>
      <pubDate>Wed, 03 Jun 2020 00:00:00 +0000</pubDate>
      <link>https://forem.com/tindleaj/finding-freelance-work-as-a-new-developer-2dmd</link>
      <guid>https://forem.com/tindleaj/finding-freelance-work-as-a-new-developer-2dmd</guid>
      <description>&lt;p&gt;I had someone reach out recently about advice for new programmers who want to freelance, given that finding clients is even harder right now than it would otherwise be. Specifically how does a new freelancer find opportunities for contract work? My response got long enough that I figured I'd post it, so here it is.&lt;/p&gt;

&lt;p&gt;Finding contract work as a brand-new freelancer is tough. My first year or so of doing freelance web development, I reached out to small local businesses, friends and family, and organizations, looking for the lack of a website to guide me to paying customers. I found a decent number of people who wanted websites, but not very many (read: none) who wanted to pay me to build them. Given that I didn't need to support myself at the time, that was ok. But, if you're a new freelancer &lt;em&gt;and you want to make money&lt;/em&gt;, you'll need a better approach.&lt;/p&gt;

&lt;p&gt;After a few years of stumbling down several fruitless avenues in search of freelance clients, I ended up finding the most success by doing two things: I charged more than I thought I was worth, and I reached out to people in smaller, professional communities.&lt;/p&gt;

&lt;p&gt;The first point is pretty simple. Charge more. The point of this isn't a greedy cash grab; if I were to sort my past clients by how great they were to work with, the ones at the top would also be the ones that paid me the most.&lt;/p&gt;

&lt;p&gt;If you're worried about being inexperienced and not being able to justify charging more, then do pro-bono work, or personal projects. Or just don't worry about it, and charge more anyway. The Imposter Syndrome never really goes away, so you might as well start learning to ignore it now. I worked the hardest and burned myself out the fastest working for people who paid me the least. Don't do that.&lt;/p&gt;

&lt;p&gt;An arbitrary rule of thumb here is to do some research, decide on an hourly rate for yourself, then double it. Or, if you're charging a rate for the whole project, double that too. One caveat for project-based rates that I ran into early on is that it's hard to know how long a project will take you when you don't have a ton of experience, and so you could end up charging too much or too little. Finding what works for you will require getting some projects under your belt, but I've found that being very up-front and transparent about my rate and my estimates has worked well in the past.&lt;/p&gt;

&lt;p&gt;If you opt for a flat rate for your project, being abundantly clear that estimates are hard, and adjustments might need to be made to the rate as the project progresses are good safety measures. Even better, breaking the project into several distinct milestones where payments are made after each deliverable lets you more easily adjust for any scope creep or previously unknown obstacles incrementally instead of having to get it right all at once, at the outset of the project.&lt;/p&gt;

&lt;p&gt;Finding niche professional communities is a little more nebulous. The key here is you have to find communities that are both:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Small enough that you can connect directly with people who might need your services, and,&lt;/li&gt;
&lt;li&gt;Are made up of individuals willing/able to pay you for your services.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I started out by spending time on reddit communities like r/forhire, and the monthly HackerNews hiring threads, and then gradually branched out into other communities where the focus wasn't necessarily just about 'finding work', but still had members who I could connect with and who might have projects they'd be willing to pay my rate for. The IndieHacker community comes to mind as a niche community that potentially fits this bill (although I've never personally looked for freelance work there).&lt;/p&gt;

&lt;p&gt;Once you've found a suitable community, all that's left is reaching out to people and starting a dialogue.&lt;/p&gt;

&lt;p&gt;In my mind, the biggest differentiator for any professional programmer is their ability to communicate effectively through writing. Freelancers especially need to spend time working on being clear, consistent, and persuasive. Your first interactions with a client will usually be written. Many clients won't be programmers themselves, so written updates will be a necessary deliverable once you do have the contract.&lt;/p&gt;

&lt;p&gt;As a freelancer, your written communication needs to be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Clear&lt;/strong&gt;. You have to be able to explain the details of your work, technical and otherwise, to your client.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consistent&lt;/strong&gt;. This has been one of my most common freelancing failures. There's almost no such thing as too much communication with your client, but too little communication is a project killer. Even if it's to report roadblocks or unforeseen issues, regularly communicating is critical&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Persuasive&lt;/strong&gt;. Part of being a freelancer is to be constantly articulating your value to your client. It doesn't matter if your code will be 100x faster than what's already there, your client won't care unless you sell them on the value of that speedup. In my experience, persuasive writing is brief, backed by facts/data, and draws upon your industry experience to add context to your decisions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's it. My freelancing advice basically boils down to charge more, find communities of people who will pay you, and focus on well written communication with those people.&lt;/p&gt;

&lt;p&gt;If you're interested in more advice on freelancing, &lt;a href="https://daedtech.com/"&gt;DaedTech&lt;/a&gt; is a blog that dives into freelancing topics with lot more depth, and I've found a lot of Eric's content to be super useful.&lt;/p&gt;

</description>
      <category>freelance</category>
      <category>programming</category>
      <category>work</category>
    </item>
    <item>
      <title>Do Significant Work</title>
      <dc:creator>Austin Tindle</dc:creator>
      <pubDate>Thu, 28 May 2020 00:00:00 +0000</pubDate>
      <link>https://forem.com/tindleaj/do-significant-work-5630</link>
      <guid>https://forem.com/tindleaj/do-significant-work-5630</guid>
      <description>&lt;p&gt;The Stripe Press just published a beautiful reprint of Richard Hamming's &lt;a href="https://press.stripe.com/#why-are-we-doing-this"&gt;&lt;em&gt;The Art of Doing Science and Engineering&lt;/em&gt;&lt;/a&gt;, which led me (and quite a few others, I imagine) to read some of his work for the first time. The written form of his lecture, "You and Your Research", is &lt;a href="https://press.stripe.com/#why-are-we-doing-this"&gt;chapter thirty of the book&lt;/a&gt; (go read it if you haven't).&lt;/p&gt;

&lt;p&gt;It opens with Hamming admitting that the lecture could just as easily be called “You and Your Engineering Career,” or even “You and Your Career". He suggests that we should try to do significant things in our careers, rather than insignificant things. Hamming means 'significant' in the 'significant to mankind' sense, what he says applies to the 'personally significant' just as well. To do significant things, we need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Focus on the most important problems you can try to solve&lt;/li&gt;
&lt;li&gt;Work at those problems consistently&lt;/li&gt;
&lt;li&gt;Be excellent in our work&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not everyone can chose to work on they believe to be the Important Problems. Being able to do so is definitely a sort of privilege. However, there will always be problems that are the most important within your ability to solve. Hamming says to work on those.&lt;/p&gt;

&lt;p&gt;You can apply this recursively. What's the most important piece of the important problem you're solving? Focus on that. It sounds simple, but is everyone you know working on the problem most important to them? Are you?&lt;/p&gt;

&lt;p&gt;Being consistent, determined, driven -- that's harder to change. Everyone has some minimum they can do consistently. Hamming talks about finding that minimum, and then growing from there. Everyone can be consistent to a different degree, but we can all improve.&lt;/p&gt;

&lt;p&gt;The final point is the one that resonated with me the most. Be excellent. So much of the field I work in is filled with bloat, workarounds, hacks, "good enough", and crumbling towers of complexity. The reality is that excellence isn't a prerequisite for generating profit. Excellence is, however, a prerequisite for doing significant things. This is the clearest goal, and yet the hardest to achieve. We all know what excellence looks like -- some even make a habit of excellence already. But it's easy to do mediocre work, and in some ways it's even easier to do good work. Excellent work is usually hard work.&lt;/p&gt;

&lt;p&gt;Hamming concludes on the point of excellence. For him, the most important thing about the work we do is how we do it:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"... it is nice to end up where you wanted to be, but the person you are when you get there is far more important. I believe a life in which you do not try to extend yourself regularly is not worth living—but it is up to you to pick the goals you believe are worth striving for."&lt;/em&gt;&lt;/p&gt;

</description>
      <category>career</category>
      <category>beginners</category>
      <category>productivity</category>
    </item>
    <item>
      <title>You're not ready. Do it anyway</title>
      <dc:creator>Austin Tindle</dc:creator>
      <pubDate>Wed, 27 May 2020 00:00:00 +0000</pubDate>
      <link>https://forem.com/tindleaj/you-re-not-ready-do-it-anyway-2o8g</link>
      <guid>https://forem.com/tindleaj/you-re-not-ready-do-it-anyway-2o8g</guid>
      <description>&lt;p&gt;In &lt;em&gt;Surely You're Joking Mr. Feynman&lt;/em&gt;, Richard Feynman talks about spending a summer at Caltech to "Just try out biology", a field he was completely untrained in and unfamiliar with:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"After the war, every summer I would go traveling by car somewhere in the United States. One year, after I was at Caltech, I thought, ";'This summer, instead of going to a different place, I'll go to a different &lt;strong&gt;field&lt;/strong&gt;.'&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;...I went over to the biology lab to tell them my desire, and Bob Edgar, a young post­doc who was sort of in charge there, said ... "You'll have to really do some research, just like a graduate student, and we'll give you a problem to work on." That suited me fine.&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;Feynman goes on to do work in the field and even contributes some non-trivial new findings to biology. Later in the book he sums up his experience with:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"I always do that, get into something and see how far I can go."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I read this memoir in the last year or so, and this section made me think some uncomfortable things about my own mindset when it comes to growing in one way or another. I realized that I've approached new and interesting things as a &lt;a href="https://zettelkasten.de/posts/collectors-fallacy/"&gt;collector&lt;/a&gt;, excited about a topic or field or tool, but &lt;a href="https://nesslabs.com/start-now"&gt;never quite &lt;em&gt;ready&lt;/em&gt;&lt;/a&gt; to go beyond a superficial exploration of that topic. I've piled up bookshelves with resources on all kinds of things I'd love to learn about, but never seem to find the time for. Months, or even years later, I've looked at some of those books and said to myself, "Wow, I thought I'd know all about that by now".&lt;/p&gt;

&lt;p&gt;My rationales for not being ready have been pretty straightforward: "I have to learn some more of the basics before getting to that", or "I need to learn this other thing before that'll make sense".&lt;/p&gt;

&lt;p&gt;Reading about Feynman's detour into biology made me think about that rationale a little more critically. And after some thought, it started to crumble. You can't &lt;em&gt;really&lt;/em&gt; be 'ready' for much of anything. Now I've been thinking, if you ever find yourself 'ready', that's probably a good indication that you could go a bit farther, dig a bit deeper, try something even more novel.&lt;/p&gt;

&lt;p&gt;So now, I'm getting into some things. We'll see how far I can go.&lt;/p&gt;

</description>
      <category>motivation</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Learn, Apply, Teach, Repeat: Guidelines for Technical Learning</title>
      <dc:creator>Austin Tindle</dc:creator>
      <pubDate>Wed, 18 Dec 2019 03:12:34 +0000</pubDate>
      <link>https://forem.com/tindleaj/learn-apply-teach-repeat-guidelines-for-technical-learning-4oom</link>
      <guid>https://forem.com/tindleaj/learn-apply-teach-repeat-guidelines-for-technical-learning-4oom</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article was originally posted on my Medium account &lt;a href="https://medium.com/@tindleaj/learn-apply-teach-repeat-guidelines-for-technical-learning-ad164a5f8abb"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;I'm very interested in improving my ability to learn technical concepts efficiently. In this article, I'll go over the heuristics I've found that work well for me: Read actively, use multiple sources/formats, work at memorizing, apply the concepts, and finally &lt;strong&gt;teach&lt;/strong&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It all started with the observation (one I'm sure many of you are familiar with) that I have an interest in learning many things, but only some of those things stick after trying to learning them. I'd read a book, or watch a video lecture, only to then run into roadblocks. Some of the content I would be able to pick up easily, only to forget it later. Other content would be too opaque for me to make much progress with. The worst was content I thought I had learned, but would then struggle with when it came time to write down my thoughts on the subject or to talk about it with others. This is all extremely discouraging: what's the point of learning in the face of all these issues?&lt;/p&gt;

&lt;p&gt;Before getting into it, a couple of notes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The following heuristics represent my current personal strategy, and I have no idea if they'll work for you. I think they're all pretty useful, but I'm sure they'll continue to change as I learn more.&lt;/li&gt;
&lt;li&gt;Where applicable I'll link to the resources that have inspired these points or that I've found useful.&lt;/li&gt;
&lt;li&gt;Most of the content I've been trying to learn recently has been technical/programming concepts, including the &lt;a href="https://doc.rust-lang.org/book/"&gt;Rust programming language&lt;/a&gt; and &lt;a href="https://www.coursera.org/learn/build-a-computer/home/welcome"&gt;fundamental computer principles&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And with that, my heuristics for learning effectively:&lt;/p&gt;

&lt;h2&gt;
  
  
  Read with intent
&lt;/h2&gt;

&lt;p&gt;Most of the learning you'll end up doing will probably be via reading. This is because most content is written, especially the niche or advanced stuff. Even so, this guideline also applies to things like video lectures. The problem that lot's of people fall into, or at least that I've fallen into, is that there are two distinct modes for reading: active and passive. Passive reading is the easy kind of reading, and it's the default mode. This is the way I've found myself reading most novels and shallow content like blog posts and social media. You can't read to learn like this. You have to instead read actively. You have to focus on the main ideas, pause on topics you don't understand until you can find a way to understand them, and also give your brain time to digest new material (don't cram). These are all parts of what it means to read actively.&lt;/p&gt;

&lt;p&gt;Active reading is something that has been in sort of the periphery of understanding for me. In other words, it's something that I knew was there but didn't know how to do the &lt;strong&gt;right&lt;/strong&gt; way. I probably still don't know how to do it the &lt;strong&gt;right&lt;/strong&gt; way, but reading the book &lt;em&gt;&lt;a href="https://amzn.to/2PoabPJ"&gt;How to Read a Book&lt;/a&gt;&lt;/em&gt; really changed the way I practice actively reading. There's a good &lt;a href="http://pne.people.si.umich.edu/PDF/howtoread.pdf"&gt;&lt;code&gt;tldr;&lt;/code&gt; of the book&lt;/a&gt; made by the University of Michigan that expands on the principles above.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;tldr;&lt;/code&gt; of the &lt;code&gt;tldr;&lt;/code&gt; (just read it, it's 11 pages with lots of pictures) that you should read the same material multiple times. The first read to skim and understand the main concepts, another to deeply understand the specifics of the material, and a third to synthesize that material with other related material. It also goes into some specific techniques for how to outline and extract important content from a written resource. I've found these techniques super useful, especially when combined with tools for &lt;em&gt;memorization&lt;/em&gt;, the next learning heuristic I'll outline in this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Commit your learning to memory
&lt;/h2&gt;

&lt;p&gt;The basic prerequisite for any learning is that you have to have a baseline understanding of whatever the topic or content is. Once you have that, you then have to remember it. Accomplishing this part might seem a bit opaque, and it is -- without the right tools. The right tool definitely seems to be &lt;em&gt;Spaced Repetition Learning&lt;/em&gt;. &lt;a href="https://en.wikipedia.org/wiki/Spaced_repetition"&gt;The details of that concept are very interesting&lt;/a&gt;, and you should definitely look it up if you're inclined, but it boils down to the idea that if you review material right before you're about to forget it, you can go longer and longer times between reviews without forgetting. Eventually, you'll never have to review it again, you'll just remember it forever.&lt;/p&gt;

&lt;p&gt;Tools like &lt;a href="https://apps.ankiweb.net/"&gt;Anki&lt;/a&gt; and &lt;a href="https://mochi.cards/"&gt;Mochi&lt;/a&gt; help you do this pretty easily. My current workflow is to identify main points or vocabulary (via highlighting or written notes), then to distill those into Anki cards which I then review. I also constantly delete and update existing cards to keep the my decks free of stale information. This happens as my understanding on topics becomes more nuanced or I decide a particular concept isn't worth remembering any more. An important part of using spaced repetition is that you have to be consistent: committing to reviewing cards regularly is a necessary part of the process.&lt;/p&gt;

&lt;p&gt;Spaced repetition and Anki in particular have been written about by people more experienced than I. In particular I've learned a lot from &lt;a href="https://www.jackkinsella.ie/articles/janki-method"&gt;The Janki Method&lt;/a&gt; by Jack Kinsella and &lt;a href="http://augmentingcognition.com/ltm.html"&gt;Augmenting Long-term Memory&lt;/a&gt; by Michael Nielsen.&lt;/p&gt;

&lt;h2&gt;
  
  
  Vary your learning resources
&lt;/h2&gt;

&lt;p&gt;This heuristic is pretty straight forward, but I think it adds a lot to the learning process. For very niche or advanced topics this might be harder, but ideally you should find multiple resources on the same topic, and consume them in parallel. I've found that having a written resource like a book or series of articles, along with a video-based resource like lectures or a MOOC class works very well. This guideline becomes especially helpful when I get stuck on a particular concept as it's presented by one resource. For example, I had a hard time understanding referencing and dereferencing values in Rust as it's presented by the Rust Book until I watched a YouTube lecture on that specific topic.&lt;/p&gt;

&lt;p&gt;How you vary your learning resources is up to you. I've found that I learn really well by mixing textbooks and MOOC courses, but you could also explore audio books, in-person classes, or finding a mentor. For programming, I've also used interactive learning resources like &lt;a href="https://www.codecademy.com/"&gt;Codeacademy&lt;/a&gt; and &lt;a href="https://github.com/rust-lang/rustlings"&gt;rustlings&lt;/a&gt;, to name just two.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make something
&lt;/h2&gt;

&lt;p&gt;The previous three points are roughly half of the battle (in my mind) when it comes to gaining a real understanding of a new topic. The other half is all about having context and purpose around your topic of choice. If you aren't doing anything with what you're learning, then what's the point?&lt;/p&gt;

&lt;p&gt;It can be helpful to have a project/goal in mind before you start learning at all, but that's not always the case. I've definitely run into situations where I had an abstract interest in a topic, but didn't really know enough about it yet to come up with a project. However, I think at some point you have to anchor the academic thought-stuff of whatever you're learning in the messy reality of a project. For me, most of my recent learning has centered around programming, and so most of my projects have been programs.&lt;/p&gt;

&lt;p&gt;For non-programming topics this applied portion of the learning process will obviously be different. Som possibilities are a physical creation, artwork, blog post, etcetera, etcetera. The important part is that you apply what you're learning.&lt;/p&gt;

&lt;h2&gt;
  
  
  Teach
&lt;/h2&gt;

&lt;p&gt;The real test for whether you really grok a subject: can you teach it to someone else? Even if you can't that's usually OK too. I've found that the process of trying to instruct someone else on a topic serves two purposes. It both reinforces my previous learning, and clearly highlights the gaps in my own understanding. The reinforcement is useful, but getting a better idea for what you don't know is even better. It's fine to take a shot at teaching something you're still learning yourself. It's part of the learning process, and you'll be surprised by the number of insights you can provide for others even if you don't have a perfect grasp yourself.&lt;/p&gt;

&lt;p&gt;Teaching can take numerous forms, but it doesn't all have to be the stand-in-front-of-the-class, draw-it-on-the-whiteboard variety. Most of the time, "teaching" can just consist of a conversation with a colleague or friend, or an article about the subject you're learning.&lt;/p&gt;

&lt;h2&gt;
  
  
  Notes
&lt;/h2&gt;

&lt;p&gt;Notably, these guidelines shouldn't necessarily be done in the order I've presented here. There are constant feedback loops between comprehension, memorization, application, and instruction, and you should find yourself switching between them fairly regularly. Also note, it's best to go without too much rigid structure (at least that's how I've been doing it).&lt;/p&gt;

&lt;p&gt;Everything I've written here has been written or said by someone else before me. There's nothing groundbreaking about any one of these points, but it's taken me a while to distill these ideas and make them applicable to how I learn. Hopefully some of you will find the distillation useful.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>learning</category>
    </item>
    <item>
      <title>Rust's `slice::windows` is really cool</title>
      <dc:creator>Austin Tindle</dc:creator>
      <pubDate>Mon, 17 Jun 2019 02:58:02 +0000</pubDate>
      <link>https://forem.com/tindleaj/rust-s-slice-windows-is-really-cool-3imn</link>
      <guid>https://forem.com/tindleaj/rust-s-slice-windows-is-really-cool-3imn</guid>
      <description>&lt;p&gt;&lt;em&gt;Cover photo by Gordon Williams on Unsplash&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This post is one of several that outline my perspective on learning Rust as someone who primarily uses JavaScript. You can find the rest &lt;a href="https://medium.com/@tindleaj"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Rust has a lot of powerful features associated with its primitives and standard library. A cool one I just came across is the method &lt;a href="https://doc.rust-lang.org/std/primitive.slice.html#method.windows"&gt;&lt;code&gt;slice::windows&lt;/code&gt;&lt;/a&gt;. This method returns an iterator over a &lt;code&gt;slice&lt;/code&gt;. The iterator length can be specified. It lets you iterate over a slice and have a &lt;em&gt;window&lt;/em&gt; of a specific size on each pass. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// windows.rs

let slice = ['w', 'i', 'n', 'd', 'o', 'w', 's'];

for window in slice.windows(2) {
  &amp;amp;println!{"[{}, {}]", window[0], window[1]};
}

// prints: [w, i] -&amp;gt; [i, n] -&amp;gt; [n, d] -&amp;gt; [d, o] -&amp;gt; [o, w] -&amp;gt; [w, s]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is pretty cool, and doesn't really have a parallel in JavaScript. The closest that I can come up with to achieve the same is this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// windows.js

let slice = ["w", "i", "n", "d", "o", "w", "s"];

slice.forEach((char, index) =&amp;gt; {
  if (index === slice.length - 1) return;

  console.log(`[${slice[index]}, ${slice[index + 1]}]`);
});

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The JavaScript approach is definitely less ergonomic, and I really like the concept of windows as an abstraction for use with iterators. &lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
