<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: SheerLuck</title>
    <description>The latest articles on DEV Community by SheerLuck (@sheerluck).</description>
    <link>https://dev.to/sheerluck</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%2F3682440%2Fdd5a7212-c6a5-4e43-829f-22184f5807ce.jpg</url>
      <title>DEV Community: SheerLuck</title>
      <link>https://dev.to/sheerluck</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sheerluck"/>
    <language>en</language>
    <item>
      <title>Scalars Explained</title>
      <dc:creator>SheerLuck</dc:creator>
      <pubDate>Sun, 05 Apr 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/sheerluck/scalars-explained-381b</link>
      <guid>https://dev.to/sheerluck/scalars-explained-381b</guid>
      <description>&lt;p&gt;In this series we are going to learn all the maths that is required to learn machine learning or deep learning. All the post, will explain the concept, will have geometric intuition or understanding and then python code implementation. We will start from linear algebra.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Scalar?
&lt;/h2&gt;

&lt;p&gt;Before going to vectors, lets first understand what is a scalar? A scalar is a single real number. That is the complete definition. When we write: $$s \in \mathbb{R} $$&lt;/p&gt;

&lt;p&gt;We are saying: $s$ is an element of the set of all real numbers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Scalar has only one piece of information: its &lt;strong&gt;value&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Scalars live in $\mathbb{R}$ (the real number line). It includes every decimal, every fraction, every irrational number. Basically everything on a continuous number line.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Can You Do With Scalars
&lt;/h2&gt;

&lt;p&gt;The standard arithmetic operations all apply:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Notation&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Addition&lt;/td&gt;
&lt;td&gt;$a + b$&lt;/td&gt;
&lt;td&gt;$3 + 5 = 8$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Subtraction&lt;/td&gt;
&lt;td&gt;$a-b$&lt;/td&gt;
&lt;td&gt;$3-5=-2$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiplication&lt;/td&gt;
&lt;td&gt;$a.b$&lt;/td&gt;
&lt;td&gt;$3.5=15$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Division&lt;/td&gt;
&lt;td&gt;$a/b$&lt;/td&gt;
&lt;td&gt;$3/5=0.6$&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These operations always produce another scalar. The output stays in $\mathbb{R}$.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Scalars Are Not Enough
&lt;/h2&gt;

&lt;p&gt;Let's say you want to describe how an object is moving through 3D space. The object moves at 60 km/h. But 60 km/h where? Diagonally upward? A scalar cannot encode this. You need to know both how much (magnitude) and which way (direction)&lt;/p&gt;

&lt;p&gt;In ML, you have a house with features: area = 1200 sq ft, bedrooms = 3, age = 10 years, price = $250,000. These are four separate scalars. But a model needs to process them together as one object, not as four disconnected numbers. A scalar cannot represent structured, multi-dimensional data.&lt;/p&gt;

&lt;p&gt;This is the fundamental limitation of scalars:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A scalar carries magnitude (size/value) only. It carries no directional or structural information.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The Two Things We Need That Scalars Cannot Provide
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Direction - When a quantity has both a size and direction like displacement, velocity, force. Here, a single number is insufficient. You need a mathematical object that encodes both simultaneously.&lt;/li&gt;
&lt;li&gt;Structure across multiple dimensions - When data has multiple features that belong together as one unit like a data point in ML, you need an object that holds all dimensions at once and supports operations across them.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Both the things can be done with &lt;strong&gt;vectors&lt;/strong&gt; , which is what we study from next article.&lt;/p&gt;

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

&lt;p&gt;This was a quick one. In the next one you'll learn about vectors and vector spaces. See you soon.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>machinelearning</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Learn Rust Basics By Building a Brainfuck Interpreter</title>
      <dc:creator>SheerLuck</dc:creator>
      <pubDate>Sat, 04 Apr 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/sheerluck/learn-rust-basics-by-building-a-brainfuck-interpreter-320d</link>
      <guid>https://dev.to/sheerluck/learn-rust-basics-by-building-a-brainfuck-interpreter-320d</guid>
      <description>&lt;p&gt;Hi, In this series we are going to learn Rust programming language by building exciting projects per article. For every article, we will first learn some concepts and then build a mini project. I promise you these mini projects will be exciting. The only prerequisite is, you should know any one programming language like just basics of it as this series will focus on teaching rust and not programming from zero.&lt;/p&gt;

&lt;p&gt;My motive is to explain how to do a certain thing in Rust, so I won't explain how a loop works or how a function works. I'll just explain how to work with variables, functions, loops etc in Rust but I'll explain Rust specific concepts in detail.&lt;/p&gt;

&lt;p&gt;In this post, we are going to &lt;strong&gt;build a brainfuck language interpreter in Rust&lt;/strong&gt;. But before that, we will learn about variables and mutability, scalar types, compound types, functions, basic string handling (we will dive deep in the next article), &lt;code&gt;println!&lt;/code&gt; &amp;amp; basic macros. We will also learn about cargo and control flow in rust like if and else blocks, loops, and match (this is interesting).&lt;/p&gt;

&lt;p&gt;You can get the source code from &lt;strong&gt;&lt;a href="https://github.com/MrSheerluck/brainfuck-interpreter-in-rust" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's start, I can't wait.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Rust Works and Cargo
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What happens when you write a Rust program?
&lt;/h3&gt;

&lt;p&gt;Rust is a &lt;strong&gt;compiled language&lt;/strong&gt;. This means before the program can run, it has to be translated from human readable Rust code into machine code that your CPU can directly execute. This translation is done by the &lt;strong&gt;Rust compiler&lt;/strong&gt; called &lt;code&gt;rustc&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cargo
&lt;/h3&gt;

&lt;p&gt;Cargo is Rust's official build system and package manager. It does multiple things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;creates new projects with a standard folder structure&lt;/li&gt;
&lt;li&gt;compiles the rust code by invoking &lt;code&gt;rustc&lt;/code&gt; under the hood&lt;/li&gt;
&lt;li&gt;downloads and manages external libraries(in rust world we call them &lt;strong&gt;crates&lt;/strong&gt; )&lt;/li&gt;
&lt;li&gt;run tests, benchmarks and documentation generation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you'll install Rust (via &lt;code&gt;rustup&lt;/code&gt;), Cargo comes with it automatically.&lt;/p&gt;

&lt;p&gt;Let me show you some of the common commands that you'll be using constantly:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cargo new project_name&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;creates a new project folder with boilerplate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cargo run&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;compiles and immediately runs the binary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cargo check&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;checks for errors without producing a binary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cargo build&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;compiles your project, produces a binary&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Project Structure
&lt;/h3&gt;

&lt;p&gt;Let's try to create a project with Cargo: Open up your terminal and run the following command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;It'll create a new Rust project called &lt;code&gt;hello&lt;/code&gt;. Now, open the project folder in your preferred editor and you'll see a folder structure like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hello/
├── Cargo.toml
└── src/
    └── main.rs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let me explain what each of these files are:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;Cargo.toml&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;This is the &lt;strong&gt;manifest file&lt;/strong&gt; for your project. It's written in TOML format (a simple config format). It contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[package]&lt;/span&gt;
&lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;
&lt;span class="py"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.1.0"&lt;/span&gt;
&lt;span class="py"&gt;edition&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"2024"&lt;/span&gt;

&lt;span class="nn"&gt;[dependencies]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt; - the name of your project&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;version&lt;/code&gt; - your project's version&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;edition&lt;/code&gt; - which edition of Rust you are using&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[dependencies]&lt;/code&gt; - this is where you list down the external crates your project needs. Initially its empty.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;src/main.rs&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;This is the entry point of every Rust program. Cargo generates this for you:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;fn&lt;/code&gt; is the keyword to define a function in Rust. &lt;code&gt;main&lt;/code&gt; is the name of the function. The curly braces &lt;code&gt;{}&lt;/code&gt; defines the body of the function.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;println!("Hello, world!");&lt;/code&gt; - &lt;code&gt;println!&lt;/code&gt; is a &lt;code&gt;macro&lt;/code&gt;, not a regular function. Notice there is a &lt;code&gt;!&lt;/code&gt; right before the parentheses &lt;code&gt;!()&lt;/code&gt;. Macros are just code that generates other code at compile time. You don't need to understand how they work internally right now. Just keep in mind that &lt;code&gt;println!&lt;/code&gt; prints a line of text to the terminal followed by a new line.&lt;/p&gt;

&lt;p&gt;The text inside the quotes is a &lt;strong&gt;string literal&lt;/strong&gt;. if you want to print values inside the string, you use &lt;code&gt;{}&lt;/code&gt; as a placeholder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The value is {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Variables and Mutability
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Declaring Variables
&lt;/h3&gt;

&lt;p&gt;In Rust, you declare a variable with &lt;code&gt;let&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a variable named &lt;code&gt;x&lt;/code&gt; and binds the value 5 to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variables are Immutable by Default
&lt;/h3&gt;

&lt;p&gt;This is one of Rust's core design decisions. Once you bind a value to a variable, you cannot change it unless you explicitly say you want to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ERROR: cannot assign twice to immutable variable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make a variable mutable, you need to add &lt;code&gt;mut&lt;/code&gt; keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// OK this is fine&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Shadowing
&lt;/h3&gt;

&lt;p&gt;Rust allows you to &lt;strong&gt;shadow&lt;/strong&gt; a variable. This means you can declare a new variable with the same name which replaces the previous one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// this is a new `x`, not mutating the old one&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&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;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints 12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need to understand that shadowing is different than &lt;code&gt;mut&lt;/code&gt;. With shadowing, you are creating a brand new variable (and can even change its type). But with &lt;code&gt;mut&lt;/code&gt;, you're modifying the same variable in place.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Type
&lt;/h2&gt;

&lt;p&gt;Rust is &lt;strong&gt;statically typed&lt;/strong&gt;. This means every variable has a type known at compile time. Most of the time the compiler can infer the type from context, so you don't have to write it explicitly. But you can always annotate it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;: i32&lt;/code&gt; after the variable name is the &lt;strong&gt;type annotation&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Integer Types
&lt;/h3&gt;

&lt;p&gt;Rust has multiple integer types. They differ in size (how many bits they use) and whether they can hold negative numbers:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;th&gt;Range&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;i8&lt;/td&gt;
&lt;td&gt;8-bit signed&lt;/td&gt;
&lt;td&gt;-128 to 127&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;i16&lt;/td&gt;
&lt;td&gt;16-bit signed&lt;/td&gt;
&lt;td&gt;-32,768 to 32,767&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;i32&lt;/td&gt;
&lt;td&gt;32-bit signed&lt;/td&gt;
&lt;td&gt;~-2 billion to ~2 billion&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;i64&lt;/td&gt;
&lt;td&gt;64-bit signed&lt;/td&gt;
&lt;td&gt;very large range&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;u8&lt;/td&gt;
&lt;td&gt;8-bit unsigned&lt;/td&gt;
&lt;td&gt;0 to 255&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;u16&lt;/td&gt;
&lt;td&gt;16-bit unsigned&lt;/td&gt;
&lt;td&gt;0 to 65,535&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;u32&lt;/td&gt;
&lt;td&gt;32-bit unsigned&lt;/td&gt;
&lt;td&gt;0 to ~4 billion&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;u64&lt;/td&gt;
&lt;td&gt;64-bit unsigned&lt;/td&gt;
&lt;td&gt;ver large range&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;usize&lt;/td&gt;
&lt;td&gt;pointer-sized&lt;/td&gt;
&lt;td&gt;depends on your OS (64-bit on 64-bit systems)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;i32&lt;/code&gt; is the default integer type when Rust infers. &lt;code&gt;usize&lt;/code&gt; is special because its used for indexing into collections (arrays, vectors etc) because its size matches memory address size of your machine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Boolean
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;is_active&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;is_done&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only two values are there for boolean type in Rust: &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Character
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rust's char type represents a single Unicode scalar value. It uses single quotes (double quotes are for strings). A char is 4 bytes in Rust, not 1, because it can hold any unicode character.&lt;/p&gt;

&lt;h3&gt;
  
  
  Floating Point
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.14&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;f32&lt;/code&gt; and &lt;code&gt;f64&lt;/code&gt; are 32-bit and 64-bit floating point numbers. The default is &lt;code&gt;f64&lt;/code&gt; because on modern hardware it's roughly as fast as &lt;code&gt;f32&lt;/code&gt; but more precise.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compound Types
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Tuples
&lt;/h3&gt;

&lt;p&gt;A tuple groups multiple values of potentially different types into one compound value. Let me show you an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;tup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;f64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;6.4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One thing to note about tuples is that they have a fixed length. Once they are declared, then you cannot add or remove elements to the tuple.&lt;/p&gt;

&lt;p&gt;But you can access elements from tuple, to do that you can use the &lt;strong&gt;dot notation&lt;/strong&gt; with the index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tup&lt;/span&gt;&lt;span class="na"&gt;.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 500&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tup&lt;/span&gt;&lt;span class="na"&gt;.1&lt;/span&gt; &lt;span class="c1"&gt;// 6.4&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tup&lt;/span&gt;&lt;span class="na"&gt;.2&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also destructure a tuple. Destructuring means unpacking the tuple elements in individual variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tup&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 500&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Arrays
&lt;/h3&gt;

&lt;p&gt;An Array holds multiple values of the same type, with a fixed length.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The type annotation &lt;code&gt;[i32; 5]&lt;/code&gt; means its an array of &lt;code&gt;i32&lt;/code&gt; with exactly 5 elements.&lt;/p&gt;

&lt;p&gt;To access elements in array, you can use these &lt;code&gt;[]&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&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="c1"&gt;// 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If you try to access an index that's out of bounds, then Rust will &lt;strong&gt;panic&lt;/strong&gt; ( crash at runtime with an error message )&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can also create an array where every element is the same value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;zeros&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0u8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// 25 elements all set to 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means, create an array of type &lt;code&gt;u8&lt;/code&gt; with 25 elements and intialize all the elements to 0.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Defining Functions
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To define a function in rust you need to use the &lt;code&gt;fn&lt;/code&gt; keyword. &lt;code&gt;add&lt;/code&gt; is the function's name. &lt;code&gt;(x: i32, y: i32)&lt;/code&gt; are the parameters. Parameter types are always required. &lt;code&gt;-&amp;gt; i32&lt;/code&gt; this means the function returns an &lt;code&gt;i32&lt;/code&gt; type and the body is inside the &lt;code&gt;{}&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Return Values
&lt;/h3&gt;

&lt;p&gt;In Rust, the last expression is a function body is automatically returned, that's why we didn't have a return keyword in the above function.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Statements&lt;/strong&gt; end with a semicolon &lt;code&gt;;&lt;/code&gt; and do not produce a value but &lt;strong&gt;Expressions&lt;/strong&gt; do not end with a semicolon and produce a value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you add a semicolon to the last line, it becomes a statement, produces no value and the function now returns &lt;code&gt;()&lt;/code&gt; (this is called a unit type, basically nothing) instead of &lt;code&gt;i32&lt;/code&gt; and this would be a compilation error.&lt;/p&gt;

&lt;p&gt;You can also keep it simple and use the return keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// explicit return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="c1"&gt;// implicit return&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Control Flow
&lt;/h2&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&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;"less than 5"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;5&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;"exactly 5"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"greater than 5"&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;In Rust, the condition doesn't need parentheses but the body must be inside the curly braces.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;If&lt;/code&gt; is an Expression
&lt;/h3&gt;

&lt;p&gt;This is important to remember that in Rust, &lt;code&gt;if&lt;/code&gt; is not just a statement, its an expression that produces a value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"even"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"odd"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that both the branches should produce the same type otherwise the compilation will fail.&lt;/p&gt;

&lt;h3&gt;
  
  
  Loop
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;loop&lt;/code&gt; runs a block of code forever until you explicitly break out of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;loop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;loop&lt;/code&gt; can also return a value through &lt;code&gt;break&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&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;// returns this value from the loop&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  While Loop
&lt;/h3&gt;

&lt;p&gt;While loop runs as longs as a condition is true:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="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;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  For Loop
&lt;/h3&gt;

&lt;p&gt;The most common loop in Rust. The &lt;code&gt;for&lt;/code&gt; loop iterates over a collection or a range:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// iterating over an array&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&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;element&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;arr&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;element&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// iterating over a range&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;5&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;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints 0, 1, 2, 3, 4&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// 0..=5 includes 5 (inclusive on both ends)&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..=&lt;/span&gt;&lt;span class="mi"&gt;5&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;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints 0, 1, 2, 3, 4, 5&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;0..5&lt;/code&gt; is a range, it starts from 0 and goes up to but not including 5. But &lt;code&gt;0..=5&lt;/code&gt; is an inclusive range that includes 5.&lt;/p&gt;

&lt;h2&gt;
  
  
  Match
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;match&lt;/code&gt; is Rust's pattern matching construct. It compares a value against a series of &lt;strong&gt;patterns&lt;/strong&gt; and executes the code for the first pattern that matches:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"one"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"two"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"three"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"four"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"other"&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;Each line inside the &lt;code&gt;match&lt;/code&gt; is called an &lt;strong&gt;arm&lt;/strong&gt; : &lt;code&gt;pattern =&amp;gt; code&lt;/code&gt;&lt;code&gt;_&lt;/code&gt; is the wildcard pattern, it matches anything. It's used as a catch all default case. One thing you need to remember is that &lt;code&gt;match&lt;/code&gt; is exhaustive. This means you must cover all the possible values. If you don't the compiler will reject your code.&lt;/p&gt;

&lt;p&gt;Like &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;match&lt;/code&gt; is also an expression and can return a value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"one"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"two"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"other"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can also match multiple patterns with &lt;code&gt;|&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"one or two"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;..=&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"three through nine"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"something else"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Vectors (Brief Introduction)
&lt;/h2&gt;

&lt;p&gt;Arrays have a fixed size known at compile time but sometimes you need a collection that can grow. This is where you can use a &lt;code&gt;Vec&amp;lt;T&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;char&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Vec&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Vec::new()&lt;/code&gt; creates an empty vector&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.push(value)&lt;/code&gt; adds an element to the end&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.pop()&lt;/code&gt; removes and returns the last element&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.len()&lt;/code&gt; returns the number of elements&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Basic String Types
&lt;/h2&gt;

&lt;p&gt;Rust has two main string types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;amp;str&lt;/code&gt; this is a string slice. It is an immutable reference to a sequence of UTF-8 bytes. String literals like &lt;code&gt;"hello"&lt;/code&gt; have type &lt;code&gt;&amp;amp;str&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;String&lt;/code&gt; this is a heap allocated growable string. We will dive deep into these in the next article but for now just understand that if you need to have string literals, just use &lt;code&gt;&amp;amp;str&lt;/code&gt;. If you need to build a string dynamically, then use the &lt;code&gt;String&lt;/code&gt; type.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To iterate over each character of a string, you can use &lt;code&gt;chars()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="nf"&gt;.chars&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;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is great, now you know everything to build your project. We are going to build a &lt;strong&gt;Brainfuck Interpreter&lt;/strong&gt; in Rust.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Brainfuck Interpreter
&lt;/h2&gt;

&lt;p&gt;Before writing any code, lets fully understand what we've building.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Brainfuck?
&lt;/h3&gt;

&lt;p&gt;Brainfuck is a programming language with only 8 instructions. The entire language fits in 8 characters. Despite that, it can compute anything a normal programming language can.&lt;/p&gt;

&lt;p&gt;A brainfuck program operates on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A memory tape - an array of cells, each holding a number (a &lt;code&gt;u8&lt;/code&gt;, so values 0-255).&lt;/li&gt;
&lt;li&gt;A data pointer - an index that points to the current cell on the tape. It starts at 0 (the leftmost cell)&lt;/li&gt;
&lt;li&gt;A program - a string of characters, most of which are instructions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The 8 Instructions
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Character&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&amp;gt;&lt;/td&gt;
&lt;td&gt;Move the data pointer one cell to the right&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;lt;&lt;/td&gt;
&lt;td&gt;Move the data pointer one cell to the left&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;+&lt;/td&gt;
&lt;td&gt;Increment the value at the current cell by 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;Decrement the values at the current cell by 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;.&lt;/td&gt;
&lt;td&gt;Output the current cell's value as an ASCII character&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;,&lt;/td&gt;
&lt;td&gt;Read one byte of input and store it in the current cell&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;[&lt;/td&gt;
&lt;td&gt;If the current cell is 0, jump forward to the matching]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;]&lt;/td&gt;
&lt;td&gt;If the current cell is non-zero, jump back to the matching [&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Every other character in a Brainfuck program is a comment, these gets simply ignored.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Loop Works
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;[&lt;/code&gt; and &lt;code&gt;]&lt;/code&gt; together form a loop. Let me explain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When you hit &lt;code&gt;[:&lt;/code&gt; check the current cell. If its &lt;code&gt;0&lt;/code&gt;, skip everything until the matching &lt;code&gt;]&lt;/code&gt;. If its non zero, enter the loop body.&lt;/li&gt;
&lt;li&gt;When you hit &lt;code&gt;]:&lt;/code&gt; check the current cell. If its non-zero, jump back to the matching &lt;code&gt;[&lt;/code&gt;. If its 0, exit the loop. This is basically a &lt;code&gt;while (cell != 0) {...}&lt;/code&gt; loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What We Need to Build
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;A memory tape&lt;/li&gt;
&lt;li&gt;A data pointer&lt;/li&gt;
&lt;li&gt;A way to iterate through the program instruction by instruction basically a program counter (pc) as a &lt;code&gt;usize&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Bracket matching when we hit &lt;code&gt;[&lt;/code&gt; or &lt;code&gt;]&lt;/code&gt;, we need to find the matching counterpart. We will precompute this into a map before running.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Create the Project
&lt;/h3&gt;

&lt;p&gt;Let's start build our brainfuck interpreter. Open up your terminal and create a new project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo new brainfuck-interpreter
&lt;span class="nb"&gt;cd &lt;/span&gt;brainfuck-interpreter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the project in your preferred editor and open the &lt;code&gt;src/main.rs&lt;/code&gt; file and delete all the code&lt;/p&gt;

&lt;h3&gt;
  
  
  The Memory Tape and Pointers
&lt;/h3&gt;

&lt;p&gt;Type this into &lt;code&gt;src/main.rs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"++++++++[&amp;gt;++++[&amp;gt;++&amp;gt;+++&amp;gt;+++&amp;gt;+&amp;lt;&amp;lt;&amp;lt;&amp;lt;-]&amp;gt;+&amp;gt;+&amp;gt;-&amp;gt;&amp;gt;+[&amp;lt;]&amp;lt;-]&amp;gt;&amp;gt;.&amp;gt;---.+++++++..+++.&amp;gt;&amp;gt;.&amp;lt;-.&amp;lt;.+++.------.--------.&amp;gt;&amp;gt;+.&amp;gt;++."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;tape&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0u8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;30000&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;pc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let me explain what we just did:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;let program = "..."&lt;/code&gt; this is a brainfuck program that prints "Hello World!". It's just a &lt;code&gt;&amp;amp;str&lt;/code&gt;(string literal). Our interpreter will read through it character by character.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let mut tape = [0u8; 30000]&lt;/code&gt; this is our memory tape. This creates an array of 30,000 elements, each of them initialized to &lt;code&gt;0&lt;/code&gt;, and each of them is of type &lt;code&gt;u8&lt;/code&gt;(values 0-255). We need &lt;code&gt;mut&lt;/code&gt; because instructions &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;-&lt;/code&gt; will modify the cells.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let mut dp: usize = 0&lt;/code&gt; dp stands for &lt;strong&gt;data pointer&lt;/strong&gt;. It's the index into tape that points to the current cell. It starts at 0. Its &lt;code&gt;usize&lt;/code&gt; because array indices in Rust must be &lt;code&gt;usize&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let mut pc: usize = 0&lt;/code&gt; pc stands for &lt;strong&gt;program counter&lt;/strong&gt;. Its the index into program pointing at the current instruction. It starts at 0.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Precomputing Bracket Matches
&lt;/h3&gt;

&lt;p&gt;Before we run the program, we'll precompute where every &lt;code&gt;[&lt;/code&gt; matches with its &lt;code&gt;]&lt;/code&gt; and vice versa. This way, when we need to jump, we just do a lookup instead of scanning through the program every time. We'll store this in a &lt;code&gt;Vec&lt;/code&gt; where the index is the position of &lt;code&gt;[&lt;/code&gt; or &lt;code&gt;]&lt;/code&gt; and the value is the position of its matching counterpart.&lt;/p&gt;

&lt;p&gt;Add this before main, as a separate function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;build_bracket_map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="nf"&gt;.as_bytes&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;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0usize&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Vec&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="sc"&gt;b'['&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="sc"&gt;b']'&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;open&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="nf"&gt;.pop&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;"Unmatched ]"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="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;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="nf"&gt;.is_empty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;panic!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Unmatched ["&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;map&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let me explain everything:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fn build_bracket_map(program: &amp;amp;str) -&amp;gt; Vec&amp;lt;usize&amp;gt;&lt;/code&gt; this function takes a &lt;code&gt;&amp;amp;str&lt;/code&gt;(the program text) and returns a &lt;code&gt;Vec&amp;lt;usize&amp;gt;&lt;/code&gt;(the bracket map)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let bytes = program.as_bytes()&lt;/code&gt; here &lt;code&gt;as_bytes()&lt;/code&gt; converts the &lt;code&gt;&amp;amp;str&lt;/code&gt; into a slice of raw bytes &lt;code&gt;&amp;amp;[u8]&lt;/code&gt;. This lets us compare characters as byte values using &lt;code&gt;b'['&lt;/code&gt; syntax(a byte literal). Its slightly more efficient than working with &lt;code&gt;char&lt;/code&gt; here and brainfuck only uses ASCII characters so its completely valid.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let mut map = vec![0usize; len]&lt;/code&gt; here &lt;code&gt;vec![value; len]&lt;/code&gt; is a macro that creates a &lt;code&gt;Vec&lt;/code&gt; with &lt;code&gt;count&lt;/code&gt; elements all set to &lt;code&gt;value&lt;/code&gt;. So this creates a vector of &lt;code&gt;len&lt;/code&gt; zeroes. Here, every position starts as &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let mut stack: Vec&amp;lt;usize&amp;gt; = Vec::new()&lt;/code&gt; this is our stack for tracking open brackets. When we see a &lt;code&gt;[&lt;/code&gt;, we push its position. When we see a &lt;code&gt;]&lt;/code&gt;, we pop the most recent &lt;code&gt;[&lt;/code&gt; position because that is its matching opening bracket.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;for i in 0..len&lt;/code&gt; loop here we iterate through every index of the program

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;match bytes[i]&lt;/code&gt; we match on the byte at position &lt;code&gt;i&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b'[' =&amp;gt; stack.push(i)&lt;/code&gt; when we see an open bracket, we push its index onto the stack.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b']'&lt;/code&gt; when we see a close bracket

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;stack.pop()&lt;/code&gt; removes and returns the last pushed index(the matching &lt;code&gt;[&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.expect("Unmatched ]")&lt;/code&gt; if the stack is empty, there's no matching &lt;code&gt;[&lt;/code&gt;, so we crash with this error message. &lt;code&gt;.expect()&lt;/code&gt; is a method on &lt;code&gt;Option&lt;/code&gt; that either unwraps the value or panics with your message. We'll cover &lt;code&gt;Option&lt;/code&gt; deeply later, but for now &lt;code&gt;pop()&lt;/code&gt; returns &lt;code&gt;None&lt;/code&gt; if the stack is empty and &lt;code&gt;expect()&lt;/code&gt; handles that.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;map[open] = i&lt;/code&gt; this means at the &lt;code&gt;[&lt;/code&gt; position, the jump target is &lt;code&gt;]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;map[i] = open&lt;/code&gt; this means at the &lt;code&gt;]&lt;/code&gt; position, the jump target is &lt;code&gt;[&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;_ =&amp;gt; {}&lt;/code&gt; for any other character, we will just ignore and do nothing&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;if !stack.is_empty() {panic!(...)}&lt;/code&gt; after processing the whole program, if the stack still has entries, that means there are unmatched &lt;code&gt;[&lt;/code&gt;. &lt;code&gt;panic!&lt;/code&gt; crashes the program with a message&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Main Execute Loop
&lt;/h3&gt;

&lt;p&gt;Now back inside &lt;code&gt;main&lt;/code&gt; function, after the variable declarations, call &lt;code&gt;build_bracket_map&lt;/code&gt; and then write the execution loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"++++++++[&amp;gt;++++[&amp;gt;++&amp;gt;+++&amp;gt;+++&amp;gt;+&amp;lt;&amp;lt;&amp;lt;&amp;lt;-]&amp;gt;+&amp;gt;+&amp;gt;-&amp;gt;&amp;gt;+[&amp;lt;]&amp;lt;-]&amp;gt;&amp;gt;.&amp;gt;---.+++++++..+++.&amp;gt;&amp;gt;.&amp;lt;-.&amp;lt;.+++.------.--------.&amp;gt;&amp;gt;+.&amp;gt;++."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;tape&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0u8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;30000&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;pc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;bracket_map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;build_bracket_map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;program&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;bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="nf"&gt;.as_bytes&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;pc&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="sc"&gt;b'&amp;gt;'&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sc"&gt;b'&amp;lt;'&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sc"&gt;b'+'&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tape&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tape&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nf"&gt;.wrapping_add&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="sc"&gt;b'-'&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tape&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tape&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nf"&gt;.wrapping_sub&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="sc"&gt;b'.'&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;print!&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;tape&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;char&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="sc"&gt;b','&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* input not needed for Hello World */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="sc"&gt;b'['&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;tape&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;pc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bracket_map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pc&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="sc"&gt;b']'&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;tape&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;pc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bracket_map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pc&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;pc&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let me explain this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;while pc &amp;lt; bytes.len()&lt;/code&gt; this keep executing as long as the program counter hasn't gone past the end of the program.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b'&amp;gt;' =&amp;gt; dp += 1&lt;/code&gt; moves the data pointer right, just incrementing the index&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b'&amp;lt;' =&amp;gt; dp -= 1&lt;/code&gt; moves the data pointer left, just decrementing the index&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b'+' =&amp;gt; tape[dp].wrapping_add(1)&lt;/code&gt; this increments the current cell. We use &lt;code&gt;.wrapping_add(1)&lt;/code&gt; instead of &lt;code&gt;tape[dp] += 1&lt;/code&gt; because our cells are &lt;code&gt;u8&lt;/code&gt;(0-255). If the value is 255 and you add 1, a normal += would panic in debug mode due to integer overflow. The &lt;code&gt;.wrapping_add&lt;/code&gt; instead wraps around to 0. This is standard brainfuck behaviour.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b'-' =? tape[dp].wrapping_sub(1)&lt;/code&gt; this decrements the current cell. Same idea as add operation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b'.' =&amp;gt; print!("{}", tape[dp] as char)&lt;/code&gt; this prints the current cell as an ASCII character. &lt;code&gt;tape[dp] as char&lt;/code&gt; is a type cast from &lt;code&gt;u8&lt;/code&gt; to &lt;code&gt;char&lt;/code&gt; type. For example, the value &lt;code&gt;72&lt;/code&gt; becomes &lt;code&gt;H&lt;/code&gt;. &lt;code&gt;print!&lt;/code&gt;(without &lt;code&gt;ln&lt;/code&gt;) prints without creating a newline.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b','&lt;/code&gt; we leave it as an empty block as we are not taking input dynamically.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b'['&lt;/code&gt; if the current cell is &lt;code&gt;0&lt;/code&gt;, then jump to the matching &lt;code&gt;]&lt;/code&gt; by setting &lt;code&gt;pc = bracket_map[pc]&lt;/code&gt;. The &lt;code&gt;pc += 1&lt;/code&gt; at the bottom of the loop then moves us past the &lt;code&gt;]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b']'&lt;/code&gt; if the current cell is non-zero, then jump back to the matching &lt;code&gt;[&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;_ =&amp;gt; {}&lt;/code&gt; we are using this again to ignore any non instruction character&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pc += 1&lt;/code&gt; after processing each instruction, move the program counter to the next character&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;println!()&lt;/code&gt; after the program finishes, it prints a newline so your terminal prompt appears on a fresh line.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, we covered a bunch of stuff and also built our project. Now lets try running it and see it in action. In your terminal, type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see an output like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Compiling brainfuck-interpreter v0.1.0 (/Users/.../.../.../brainfuck-interpreter)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.10s
     Running `target/debug/brainfuck-interpreter`
Hello World!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In this post, you understood most of basic common things in Rust and build a simple brainfuck interpreter in Rust. In the next one, you are going to learn about ownership, borrowing and slices and build a &lt;strong&gt;mini grep clone&lt;/strong&gt; that's gonna be fun. I hope to see you soon. Till then, goodbye!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>rust</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
