<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Raahim Fareed]]></title><description><![CDATA[I write about code.]]></description><link>https://blog.raahimfareed.com</link><generator>RSS for Node</generator><lastBuildDate>Fri, 24 Apr 2026 15:31:30 GMT</lastBuildDate><atom:link href="https://blog.raahimfareed.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Shebangs: Scripting Made Easier]]></title><description><![CDATA[Shebang?
Shebang, also known as Hashbang is a combination of characters #!. It specifies which interpreter is supposed to run the script.
Shebangs are always supposed to be at the top of the script.
They are helpful when scripting.
How to write a she...]]></description><link>https://blog.raahimfareed.com/shebangs</link><guid isPermaLink="true">https://blog.raahimfareed.com/shebangs</guid><category><![CDATA[shell]]></category><category><![CDATA[terminal]]></category><category><![CDATA[shebang]]></category><category><![CDATA[Script]]></category><category><![CDATA[Linux]]></category><dc:creator><![CDATA[Raahim Fareed]]></dc:creator><pubDate>Sat, 30 Jul 2022 13:16:28 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1659186495325/KaXs6IJXS.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-shebang">Shebang?</h3>
<p>Shebang, also known as Hashbang is a combination of characters <code>#!</code>. It specifies which interpreter is supposed to run the script.</p>
<p>Shebangs are always supposed to be at the top of the script.</p>
<p>They are helpful when scripting.</p>
<h3 id="heading-how-to-write-a-shebang">How to write a shebang?</h3>
<p>To write a shebang, firstly we type <code>#!</code> followed by the path of the interpreter. For example, for <code>sh</code> we would write it as</p>
<pre><code class="lang-bash"><span class="hljs-meta">#! /bin/sh</span>
</code></pre>
<p>and for Python we'd write it as</p>
<pre><code class="lang-bash"><span class="hljs-meta">#! /usr/bin/python</span>
</code></pre>
<h3 id="heading-where-is-my-x-interpreter-located">Where is my x interpreter located?</h3>
<p>We can find the path of our binary by using <code>which</code> command.</p>
<h4 id="heading-which-usage"><code>which</code> usage</h4>
<pre><code class="lang-console">$ which programname
</code></pre>
<p>For example, for python we'd do:</p>
<pre><code class="lang-console">$ which python
/usr/bin/python
</code></pre>
<p>This gives the path of python in my computer, it may be different in your computer.</p>
<h3 id="heading-how-does-it-actually-work">How does it actually work?</h3>
<p>To know how it works, let's first create a simple script <code>name.sh</code></p>
<pre><code class="lang-bash"><span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Enter your name: "</span>
<span class="hljs-built_in">read</span> name
<span class="hljs-built_in">echo</span> <span class="hljs-string">"My name is <span class="hljs-variable">$name</span>"</span>
</code></pre>
<p>All this script does is, reads input, and echoes it.</p>
<p>To run it, we simply do</p>
<pre><code class="lang-console">$ bash ./name.sh
Enter your name: Raahim
My name is Raahim
</code></pre>
<p>Notice how we run the file with <code>bash</code>? What if we didn't want to type <code>bash</code> everytime we wanted to run the file. That's where shebangs come in handy. Let's add one to our file</p>
<pre><code class="lang-bash"><span class="hljs-meta">#! /bin/bash</span>
<span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Enter your name: "</span>
<span class="hljs-built_in">read</span> name
<span class="hljs-built_in">echo</span> <span class="hljs-string">"My name is <span class="hljs-variable">$name</span>"</span>
</code></pre>
<p>Now to run the file, we'd do</p>
<pre><code class="lang-console">$ ./name.sh
bash: ./name.sh: Permission denied
</code></pre>
<p>Unfortunately, we get a permissions error. Nothing to panic about though, it's as expected. Let's take a look at our file's permissions.</p>
<pre><code class="lang-console">$ ls -l name.sh
-rw-r--r-- 1 raahim raahim 75 Jul 30 17:08 name.sh
</code></pre>
<p><code>-rw-r--r--</code> these are the permissions of our file. <code>r</code> means read, <code>w</code> means write and <code>x</code> means execute. We need <code>x</code> to be able to run our file. Let's change that</p>
<pre><code class="lang-console">$ chmod +x name.sh
$ ls -l name.sh
-rwxr-xr-x 1 raahim raahim 75 Jul 30 17:08 name.sh
</code></pre>
<p>Let's try to run our file again</p>
<pre><code class="lang-console">$ ./name.sh
Enter your name: Raahim
My name is Raahim
</code></pre>
<p>Voila!</p>
<h3 id="heading-how-to-avoid-shebang">How to avoid shebang?</h3>
<p>Sometimes, maybe we want to run a file with a different interpreter. For example, maybe we want to run a script with <code>zsh</code> instead of <code>bash</code>.</p>
<p>Specifying an interpreter explicitly ignores the shebang. For example to run our <code>name.sh</code> file with <code>zsh</code> we'd just do</p>
<pre><code class="lang-console">$ zsh ./name.sh
</code></pre>
<h3 id="heading-conclusion">Conclusion</h3>
<ul>
<li>Shebangs are also known as hashbangs</li>
<li>Shebangs are used to specify interpreters of the script</li>
<li>Shebangs start with <code>#!</code></li>
<li>To make shebangs work, the path needs to point to an executable and the file needs to have execute permissions</li>
<li><code>which</code> can be used to find path of our binary/executable</li>
<li><code>chmod +x filename</code> can be used to add execute permissions to the file</li>
<li><code>./filename</code> to run the file</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Naming Conventions: PascalCase , camelCase, snake_case, kebab-case]]></title><description><![CDATA[What are naming conventions?
Naming conventions, as the the name states, are rules that we use to name our variables, functions, classes, etc. They help us keep our names clean and consistent throughout our code.
The most popular naming conventions a...]]></description><link>https://blog.raahimfareed.com/naming-conventions</link><guid isPermaLink="true">https://blog.raahimfareed.com/naming-conventions</guid><category><![CDATA[naming]]></category><category><![CDATA[General Programming]]></category><category><![CDATA[Programming Tips]]></category><dc:creator><![CDATA[Raahim Fareed]]></dc:creator><pubDate>Thu, 02 Jun 2022 10:20:25 GMT</pubDate><content:encoded><![CDATA[<h3 id="heading-what-are-naming-conventions">What are naming conventions?</h3>
<p>Naming conventions, as the the name states, are rules that we use to name our variables, functions, classes, etc. They help us keep our names clean and consistent throughout our code.</p>
<p>The most popular naming conventions are the following with no particular order:</p>
<ul>
<li>PascalCase</li>
<li>camelCase</li>
<li>snake_case</li>
<li>kebab-case</li>
</ul>
<p>You may notice that the way I've written each of the above has a different "style" to it.</p>
<p>Let me explain</p>
<h4 id="heading-pascalcase">PascalCase</h4>
<p>PascalCase is generally used by classes, enums, etc.</p>
<p>In PascalCase, first letter of the word is uppercase and the rest are lowercase. Incase your name has multiple words in it, then each word's first letter will be uppercase and the rest of the letters will be lowercase.</p>
<p>Let's take a look at an example, usually when we make classes (in a programming language), it follows PascalCase convention.</p>
<p>The following example is in java</p>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HelloWorld</span> </span>{
    ...
}
</code></pre>
<p>The class name is <code>HelloWorld</code>. H of Hello and W of World are uppercase and the rest of the letters are lower case.</p>
<p>Another example that we can take a look at is <code>StarCraft</code>, name of a game and <code>MasterCard</code>, name of a company.</p>
<h4 id="heading-camelcase">camelCase</h4>
<p>camelCase is similar to PascalCase, with the only difference being that first letter of the name should lowercase, after that every word's first letter should be uppercase.</p>
<p>For example <code>HelloWorld</code> in PascalCase would change to <code>helloWorld</code> in camelCase.</p>
<p>Languages like JavaScript use camelCase in variable and function names.</p>
<p>The following example is in JavaScript.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">helloWorld</span>(<span class="hljs-params"></span>) </span>{
    ...
}

<span class="hljs-keyword">let</span> myVariable = <span class="hljs-string">"I'm in camelCase"</span>;
</code></pre>
<p>Our function <code>helloWorld</code> and our variable <code>myVariable</code> both have their first letters lowercase and after that every other word has a capital letter.</p>
<p>Another popular example is <code>iPhone</code>.</p>
<h4 id="heading-snakecase">snake_case</h4>
<p>Every word in snake_case is separated by an underscore.</p>
<p>snake_case is used by languages like Python.</p>
<p>Let's take a look at an example in Python</p>
<pre><code class="lang-py">my_name = <span class="hljs-string">"Raahim"</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello_world</span>:</span>
    ...
</code></pre>
<p>Our variable is <code>my_name</code> and our function <code>hello_world</code>. Each word is separated by an underscore.</p>
<p>snake_case is also used for naming our database tables and attributes.</p>
<h4 id="heading-kebab-case">kebab-case</h4>
<p>In kebab-case, each word is separated by a dash/minus (-).</p>
<p>Since it's a minus sign in programming languages, we don't use it to create variable names.</p>
<p>Instead it can be seen in urls.</p>
<p>For example my <em>Rust: a bare guide</em> URL looks like <a target="_blank" href="https://blog.raahim.com.pk/rust-a-bare-guide">https://blog.raahim.com.pk/rust-a-bare-guide</a>, notice <code>rust-a-bare-guide</code>. Every word is separated by a dash.</p>
<p>kebab-case is also used in css for property names and naming classes.</p>
<p>For example</p>
<pre><code class="lang-html"><span class="hljs-comment">&lt;!-- HTML --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"my-div"</span>&gt;</span>I am a div :D<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-comment">/* CSS */</span>
<span class="hljs-selector-class">.my-div</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#eeeeee</span>;
}
</code></pre>
<p>In <code>my-div</code> and <code>background-color</code>, we can see that each word is separated by a dash.</p>
<h3 id="heading-when-to-use-which-convention">When to use which convention?</h3>
<p>It depends on the language you're using.</p>
<p>For example in C++, people use <code>PascalCase</code>, <code>camelCase</code> and <code>snake_case</code> all three.</p>
<p>In JavaScript, <code>camelCase</code> is preferred for variables and functions and <code>PascalCase</code> for classes.</p>
<p>In Python, <code>snake_case</code> is used for functions and variables and <code>PascalCase</code> for classes.</p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>Naming conventions are used for our ease and to provide consistency to our code.</p>
<p>Differences between different naming conventions can be seen in the following table.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Name</td><td>Text</td></tr>
</thead>
<tbody>
<tr>
<td>PascalCase</td><td>HelloWorld</td></tr>
<tr>
<td>camelCase</td><td>helloWorld</td></tr>
<tr>
<td>snake_case</td><td>hello_world</td></tr>
<tr>
<td>kebab-case</td><td>hello-world</td></tr>
</tbody>
</table>
</div>]]></content:encoded></item><item><title><![CDATA[Rust: a bare guide]]></title><description><![CDATA[What is Rust?
Rust is a multi-paradigm general purpose language which provides memory safety out of the box. It's similar to C++.
It's a statically and strongly typed language and it's a compiled language.
It's a low level language and was created to...]]></description><link>https://blog.raahimfareed.com/rust-a-bare-guide</link><guid isPermaLink="true">https://blog.raahimfareed.com/rust-a-bare-guide</guid><category><![CDATA[Rust]]></category><category><![CDATA[guide]]></category><category><![CDATA[Tutorial]]></category><category><![CDATA[General Programming]]></category><dc:creator><![CDATA[Raahim Fareed]]></dc:creator><pubDate>Thu, 26 May 2022 20:13:50 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1653595809350/MQqk-zws5.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-what-is-rust">What is Rust?</h3>
<p>Rust is a multi-paradigm general purpose language which provides memory safety out of the box. It's similar to C++.</p>
<p>It's a statically and strongly typed language and it's a compiled language.</p>
<p>It's a low level language and was created to have similar performance as C and C++ with added benefits like code safety and memory safety.</p>
<h3 id="heading-installation">Installation</h3>
<ul>
<li><p>Windows</p>
<ol>
<li><p>Refer to <a target="_blank" href="https://www.rust-lang.org/tools/install">Rust Official Installation Page</a> and download 64bit or 32bit version for your system.</p>
</li>
<li><p>After downloading the installer, double click and follow the instructions on screen.</p>
</li>
<li><p>Download <a target="_blank" href="https://visualstudio.microsoft.com/visual-cpp-build-tools/">Microsoft C++ Build Tools Installer</a> and install the tools.</p>
</li>
<li><p>Finally, open your command prompt and run</p>
<pre><code class="lang-console">$ rustc
</code></pre>
<p>If you get an ouput from this command, congrats, rustc is installed.</p>
</li>
</ol>
</li>
<li><p>Mac/Linux</p>
<ol>
<li><p>Run the following command in your terminal</p>
<pre><code class="lang-console">$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
</code></pre>
</li>
<li><p>Follow the instructions on screen after running the command.</p>
</li>
<li><p>Once the installation is finished, restart your terminal</p>
</li>
<li><p>Finally, run</p>
<pre><code class="lang-console">$ rustc
</code></pre>
<p>If you get an ouput from this command, congrats, rustc is installed.</p>
</li>
</ol>
<p>Alternatively, you can use your package manager to install rust.</p>
</li>
</ul>
<h3 id="heading-text-editors">Text editors</h3>
<p>I personally use <a target="_blank" href="https://neovim.io/">neovim</a> and <a target="_blank" href="https://code.visualstudio.com/">VSCode</a> but you're free to use whichever text editor you want.
Some notable ones are</p>
<ul>
<li><a target="_blank" href="https://notepad-plus-plus.org/">Notepad++ (Windows Only)</a></li>
<li><a target="_blank" href="https://micro-editor.github.io/">Micro (Terminal based)</a></li>
<li><a target="_blank" href="https://brackets.io/">Brackets</a></li>
<li><a target="_blank" href="https://www.sublimetext.com/">Sublime Text</a></li>
<li><a target="_blank" href="https://atom.io/">Atom</a></li>
<li>Your native notepad application</li>
</ul>
<h3 id="heading-directory-structure">Directory structure</h3>
<p>I have created a folder named <code>rust</code> and two more folders inside that are named <code>src</code> and a <code>build</code></p>
<pre><code class="lang-console">rust
├── build
└── src
</code></pre>
<h3 id="heading-hello-world">Hello World</h3>
<p>Rust files end with <code>.rs</code> extension. So let's create a file named <code>main.rs</code> inside <code>src</code> folder</p>
<p>The main file should have only one main function (Just like C/C++)</p>
<ol>
<li><p>Let's create our main function</p>
<pre><code class="lang-rs"><span class="hljs-comment">// main.rs</span>
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() {

}
</code></pre>
<p><code>fn</code> is the keyword to declare our functions, it is followed by our function name <code>main</code> with parentheses and finally braces which are our function's body.</p>
<p>We'll get into functions later on, for now all you need to know is that a <code>main</code> function is required for us to compile the program; it's our entry point.</p>
</li>
<li><p>Now let's print <em>hello world</em></p>
<pre><code class="lang-rs"><span class="hljs-comment">// main.rs</span>
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() {
  <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Hello World"</span>);
}
</code></pre>
<p><code>println</code> is a macro hence why it's followed by an exclamation mark <code>!</code>.</p>
<p>Also, notice the semicolon at the end of the statement; every statement in rust needs to have a semicolon ending it.</p>
</li>
<li><p>To compile this, open your terminal in your main <code>rust</code> folder which contains <code>src</code> and <code>build</code> folders.</p>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main
</code></pre>
<p>This command should compile your <code>main.rs</code> file into a <code>main</code> executable in build folder.</p>
<p><code>rustc</code> is our compiler, followed by path of our <code>main.rs</code> file followed by <code>-o</code> flag which requires a path of our output file.</p>
<p>After running this command, my direcory now looks like</p>
<pre><code class="lang-console">rust
├── build
│   └── main
└── src
    └── main.rs
</code></pre>
</li>
<li><p>Finally to run the compiled file</p>
<pre><code class="lang-console">$ ./build/main
</code></pre>
<p>Our Output</p>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main
$ ./build/main
Hello World
</code></pre>
</li>
</ol>
<p>Congrats! You just created your first rust program</p>
<h3 id="heading-variables-and-their-types">Variables and their types</h3>
<p>Think of variables like a pocket, they store something for you. Variables have different types, for example there can be a variable for storing integers, there can be one for storing characters, etc.</p>
<h4 id="heading-primitive-variable-types">Primitive variable types</h4>
<pre><code class="lang-md"><span class="hljs-section"># Integers: numbers</span>

Signed

<span class="hljs-bullet">-</span> u8: [0 - 255]
<span class="hljs-bullet">-</span> u16: [0 - 65535]
<span class="hljs-bullet">-</span> u32: [0 - 4294967295]
<span class="hljs-bullet">-</span> u64: [0 - 18446744073709551615]
<span class="hljs-bullet">-</span> u128: [0 - 340282366920938463463374607431768211455]

Unsigned

<span class="hljs-bullet">-</span> i8: [-128 - 127]
<span class="hljs-bullet">-</span> i16: [-32768 - 32767]
<span class="hljs-bullet">-</span> i32: [-2147483648 - 2147483647]
<span class="hljs-bullet">-</span> i64: [-9223372036854775808 - 9223372036854775807]
<span class="hljs-bullet">-</span> i128: [-170141183460469231731687303715884105728 - 170141183460469231731687303715884105727]

<span class="hljs-section"># Floats: decimal numbers</span>

<span class="hljs-bullet">-</span> f32: single precision
<span class="hljs-bullet">-</span> f64: double precision

<span class="hljs-section"># Boolean: true or false</span>

<span class="hljs-bullet">-</span> bool

<span class="hljs-section"># Character: single digit, alphabet, unicode</span>

<span class="hljs-bullet">-</span> char

<span class="hljs-section"># Tuples</span>

<span class="hljs-section"># Arrays</span>

<span class="hljs-section"># Strings: a group of unicode characters</span>

<span class="hljs-bullet">-</span> str
</code></pre>
<p>Default integer type is <code>i32</code> and default float type is <code>f64</code>.</p>
<p>Now back to our <code>main.rs</code> file. Let's create a few variables inside our <code>main</code> function.</p>
<p>To declare a variable, we use <code>let</code> keyword followed by the variable name.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> age = <span class="hljs-number">20</span>;
<span class="hljs-keyword">let</span> name = <span class="hljs-string">"Raahim"</span>;
<span class="hljs-keyword">let</span> programmer = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">let</span> is_true = <span class="hljs-number">150</span> == <span class="hljs-number">100</span>;
<span class="hljs-keyword">let</span> face = '\u{<span class="hljs-number">1</span>F600}';
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{} {} {} {} {}"</span>, name, age, programmer, is_true, face);
</code></pre>
<p>Our Output</p>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main
$ ./build/main
Hello World
Raahim 20 true false 😀
</code></pre>
<p><code>{}</code> is a placeholder and gets replaced by the variables we provide <code>println</code></p>
<p>Let's try to change our variables</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> age = <span class="hljs-number">20</span>;
<span class="hljs-keyword">let</span> name = <span class="hljs-string">"Raahim"</span>;
<span class="hljs-keyword">let</span> programmer = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">let</span> is_true = <span class="hljs-number">150</span> == <span class="hljs-number">100</span>;
<span class="hljs-keyword">let</span> face = '\u{<span class="hljs-number">1</span>F600}';

<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{} {} {} {} {}"</span>, name, age, programmer, is_true, face);

age = <span class="hljs-number">21</span>;
</code></pre>
<p>Compiling this gives us an error</p>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main
$ ./build/main
error[E0384]: cannot assign twice to immutable variable `age`
  --&gt; src/main.rs:11:3
   |
3  |   let age = 20;
   |       ---
   |       |
   |       first assignment to `age`
   |       help: consider making this binding mutable: `mut age`
...
11 |   age = 21;
   |   ^^^^^^^^ cannot assign twice to immutable variable

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0384`.
</code></pre>
<p>That leads us to our next topic</p>
<h4 id="heading-mutable-variables">Mutable Variables</h4>
<p>By default, all variables declared are immutable, meaning they can't be reassigned.</p>
<p>In order to make our variables mutable, we need to use the keyword <code>mut</code></p>
<p>Let's change our <code>age</code> variable to mutable</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> age = <span class="hljs-number">20</span>;
age = <span class="hljs-number">21</span>;
</code></pre>
<p>This won't give us an error now.</p>
<p>What if we wanted to change these types? So far rust has automatically detected our variable types but sometimes we may require a different type to work with</p>
<h4 id="heading-explicit-type-annotation">Explicit Type Annotation</h4>
<p>To explicitly tell rust the type of our variable, we have to follow our variable name with colon and then provide the type of our variable</p>
<p>For example</p>
<pre><code class="lang-rs"><span class="hljs-comment">// main.rs</span>
<span class="hljs-keyword">let</span> age: <span class="hljs-built_in">u8</span> = <span class="hljs-number">20</span>;
<span class="hljs-keyword">let</span> pi: <span class="hljs-built_in">f32</span> = <span class="hljs-number">3.14</span>;
</code></pre>
<p><em>You should only use f32 when you absolutely need it, like when you need to save extra memory or such</em></p>
<h4 id="heading-declare-multiple-variables-at-the-same-time">Declare multiple variables at the same time</h4>
<p>Sometimes you want to declare variables in the same line, similar to <code>python</code>, like my name and age, I can do the following</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> (name, age) = (<span class="hljs-string">"Raahim"</span>, <span class="hljs-number">20</span>);
</code></pre>
<h4 id="heading-constants">Constants</h4>
<p>Constants are variables that cannot be changed.</p>
<p>To declare constants, we use <code>const</code> instead of <code>let</code> and the name of the constants should be all UPPERCASE</p>
<p>Like following</p>
<pre><code class="lang-rs"><span class="hljs-keyword">const</span> ID: <span class="hljs-built_in">i32</span> = <span class="hljs-number">1</span>;
</code></pre>
<p>So what's the difference between a normal variable that's immutable by default, and a constant?</p>
<p>There are two differences</p>
<ul>
<li>Constants need to have a datatype explicitly</li>
<li>Constants need to be intialized during declaration. This means that they need to have a value where they are getting declared. In comparison to normal variables, you can declare them without any value and initialize them later on.</li>
</ul>
<h3 id="heading-println-print-and-text-formatting"><code>println</code>, <code>print</code> and text formatting</h3>
<p>We've already used <code>println</code>, it's used to print a line to the terminal. We've also used it to print variables. Now let's look into print and various ways of formatting our texts</p>
<h4 id="heading-println">println</h4>
<pre><code class="lang-rs"><span class="hljs-built_in">println!</span>(<span class="hljs-string">"Hello Hashnode"</span>);
</code></pre>
<h4 id="heading-print">print</h4>
<pre><code class="lang-rs"><span class="hljs-built_in">print!</span>(<span class="hljs-string">"Hello Hashnode\n"</span>);
</code></pre>
<p>The difference between <code>println</code> and <code>print</code> is that, <code>println</code> automatically adds the line terminator (<code>\n</code>) whereas <code>print</code> does not add it at the end.</p>
<p>I'll be using <code>println</code> throughout this guide</p>
<h4 id="heading-arguments">Arguments</h4>
<p>To print our variables, we can use <code>{}</code> as placeholder and pass the variables to <code>println</code> or <code>print</code></p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> name = <span class="hljs-string">"Raahim"</span>;
<span class="hljs-keyword">let</span> age = <span class="hljs-number">20</span>;
<span class="hljs-keyword">const</span> PI: <span class="hljs-built_in">f64</span> = <span class="hljs-number">3.14</span>;
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"Hey, my name is {}, I am {} and Pi is {}"</span>,  name, age, pi);
</code></pre>
<p>This gives us</p>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
Hey, my name is Raahim, I am 20 and Pi is 3.14
</code></pre>
<h4 id="heading-positional-arguments">Positional Arguments</h4>
<p>To print our variables on specific positions in our statement we do</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> name = <span class="hljs-string">"Raahim"</span>;
<span class="hljs-keyword">let</span> age = <span class="hljs-number">20</span>;
<span class="hljs-keyword">const</span> PI: <span class="hljs-built_in">f64</span> = <span class="hljs-number">3.14</span>;
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"Hey, Pi is {2}, I am {1} and my name is {0}"</span>,  name, age, pi);
</code></pre>
<p>That will print</p>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
Hey, Pi is 3.14, I am 20 and my name is Raahim
</code></pre>
<p>Now let's go over what we actually did here</p>
<ul>
<li><code>name</code> is our first argument in <code>println</code> so it's at 0 index.</li>
<li><code>age</code> is our second argument in <code>println</code> so it's at 1 index.</li>
<li><code>PI</code> is our third argument in <code>println</code> so it's at 2 index.</li>
</ul>
<p>Notice how each argument's index is one number behind their position? That's because indices in Rust begin at 0 instead of 1</p>
<p>So in order to change the way our variables are formatted, we just write the index of our variable in between braces <code>{}</code></p>
<pre><code class="lang-rs"><span class="hljs-built_in">println!</span>(<span class="hljs-string">"Hey, Pi is {2}, I am {1} and my name is {0}"</span>,  name, age, pi);
</code></pre>
<h4 id="heading-named-arguments">Named Arguments</h4>
<p>Instead of using indices, we can use names for our arguments</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> name = <span class="hljs-string">"Raahim"</span>;
<span class="hljs-keyword">let</span> age = <span class="hljs-number">20</span>;
<span class="hljs-keyword">const</span> PI: <span class="hljs-built_in">f64</span> = <span class="hljs-number">3.14</span>;
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"Hey, Pi is {pi}, I am {myName} and my name is {myAge}"</span>,  myName=name, myAge=age, pi=PI);
</code></pre>
<p>We can also use the variable names as is for positions</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> name = <span class="hljs-string">"Raahim"</span>;
<span class="hljs-keyword">let</span> age = <span class="hljs-number">20</span>;
<span class="hljs-keyword">const</span> PI: <span class="hljs-built_in">f64</span> = <span class="hljs-number">3.14</span>;
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"Hey, Pi is {PI}, I am {name} and my name is {age}"</span>);
</code></pre>
<h4 id="heading-placeholder-traits">Placeholder traits</h4>
<p><code>println</code> and <code>print</code> can convert numbers from decimal to binary, octal or hex on it's own using placeholder traits</p>
<pre><code class="lang-rs"><span class="hljs-built_in">println!</span>(<span class="hljs-string">"Binary: {:b} Hex: {:x} Octal: {:o}"</span>, <span class="hljs-number">5</span>, <span class="hljs-number">10</span>, <span class="hljs-number">10</span>);
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
Binary: 101 Hex: a Octal: 12
</code></pre>
<h4 id="heading-debug-trait">Debug trait</h4>
<p>Some variables/data can't be formatted, so to print them to the terminal for debugging, we use <code>{:?}</code></p>
<pre><code class="lang-rs"><span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, (<span class="hljs-number">5</span>, <span class="hljs-string">"hello"</span>, <span class="hljs-literal">true</span>));
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
(5, "hello", true)
</code></pre>
<h3 id="heading-operators">Operators</h3>
<h4 id="heading-arithmetic">Arithmetic</h4>
<p>These operators are used for mathematical operations</p>
<h5 id="heading-addition">Addition +</h5>
<p><code>+</code> adds two operands together.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> a = <span class="hljs-number">10</span> + <span class="hljs-number">15</span>;
<span class="hljs-comment">// 25</span>
</code></pre>
<h5 id="heading-subtraction">Subtraction -</h5>
<p><code>-</code> subtracts second operand from first operand.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> a = <span class="hljs-number">10</span> - <span class="hljs-number">15</span>;
<span class="hljs-comment">// -5</span>
</code></pre>
<h5 id="heading-multiplication">Multiplication *</h5>
<p><code>*</code> multiplies two operands together.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> a = <span class="hljs-number">2</span> * <span class="hljs-number">3</span>;
<span class="hljs-comment">// 6</span>
</code></pre>
<h5 id="heading-division">Division /</h5>
<p><code>/</code> divides two operands together.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> a = <span class="hljs-number">15</span> / <span class="hljs-number">3</span>;
<span class="hljs-comment">// 5</span>
</code></pre>
<h5 id="heading-modulo">Modulo %</h5>
<p><code>%</code> returns remainder of division between two operands.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> a = <span class="hljs-number">15</span> % <span class="hljs-number">2</span>;
<span class="hljs-comment">// 1</span>
a = <span class="hljs-number">33</span> % <span class="hljs-number">5</span>;
<span class="hljs-comment">// 5</span>
a = <span class="hljs-number">10</span> % <span class="hljs-number">2</span>;
<span class="hljs-comment">// 0</span>
</code></pre>
<h4 id="heading-comparison-operators">Comparison Operators</h4>
<p>These operators are used to compare values, they always return a boolean, either <code>true</code> or <code>false</code>.</p>
<h5 id="heading-equal-to">Equal to ==</h5>
<p><code>==</code> is used to compare whether both operands are equal</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> a = <span class="hljs-number">10</span>;
<span class="hljs-keyword">let</span> b = <span class="hljs-number">11</span>;
<span class="hljs-keyword">let</span> c = a == b;
<span class="hljs-comment">// false</span>
</code></pre>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> a = <span class="hljs-number">10</span>;
<span class="hljs-keyword">let</span> b = <span class="hljs-number">10</span>;
<span class="hljs-keyword">let</span> c = a == b;
<span class="hljs-comment">// true</span>
</code></pre>
<h5 id="heading-greater-than-andgt">Greater than &gt;</h5>
<p><code>&gt;</code> used to compare whether first operand is greater than second operator</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> age = <span class="hljs-number">15</span>;
<span class="hljs-keyword">let</span> is_eligible = age &gt; <span class="hljs-number">17</span>;
<span class="hljs-comment">// false</span>
</code></pre>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> age = <span class="hljs-number">26</span>;
<span class="hljs-keyword">let</span> is_eligible = age &gt; <span class="hljs-number">17</span>;
<span class="hljs-comment">// true</span>
</code></pre>
<h5 id="heading-less-than-andlt">Less than &lt;</h5>
<p><code>&lt;</code> used to compare whether first operand is smaller than second operator</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> age = <span class="hljs-number">15</span>;
<span class="hljs-keyword">let</span> is_minor = age &lt; <span class="hljs-number">18</span>;
<span class="hljs-comment">// true</span>
</code></pre>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> age = <span class="hljs-number">26</span>;
<span class="hljs-keyword">let</span> is_minor = age &lt; <span class="hljs-number">18</span>;
<span class="hljs-comment">// false</span>
</code></pre>
<h5 id="heading-greater-than-or-equal-to-andgt">Greater than or equal to &gt;=</h5>
<p><code>&gt;=</code> used to compare whether first operand is greater than second operator, it's a mixture of <code>&gt;</code> and <code>==</code></p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> age = <span class="hljs-number">18</span>;
<span class="hljs-keyword">let</span> is_eligible = age &gt;= <span class="hljs-number">18</span>;
<span class="hljs-comment">// true</span>
</code></pre>
<h5 id="heading-lesser-than-or-equal-to">Lesser than or equal to &lt;=</h5>
<p><code>&lt;=</code> used to compare whether first operand is greater than second operator, it's a mixture of <code>&lt;</code> and <code>==</code></p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> age = <span class="hljs-number">17</span>;
<span class="hljs-keyword">let</span> is_minor = age &lt;= <span class="hljs-number">17</span>;
<span class="hljs-comment">// true</span>
</code></pre>
<h5 id="heading-not-equal-to">Not equal to !=</h5>
<p><code>!=</code> checks if first operand is not equal to second operator</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> name = <span class="hljs-string">"Raahim"</span>;
<span class="hljs-keyword">let</span> is_blocked = name != <span class="hljs-string">"Raahim"</span>;
<span class="hljs-comment">// false</span>
</code></pre>
<h4 id="heading-logical-operators">Logical Operators</h4>
<p>These operators also return either <code>true</code> or <code>false</code>. These operators combine two or more conditions together.</p>
<h5 id="heading-and-andampandamp">AND &amp;&amp;</h5>
<p><code>&amp;&amp;</code> returns <code>true</code> if both operands are <code>true</code></p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> a = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">let</span> b = <span class="hljs-literal">false</span>;
<span class="hljs-keyword">let</span> c = a &amp;&amp; b;
<span class="hljs-comment">// false</span>
</code></pre>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> a = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">let</span> b = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">let</span> c = a &amp;&amp; b;
<span class="hljs-comment">// true</span>
</code></pre>
<h5 id="heading-or-or">OR ||</h5>
<p><code>||</code> returns <code>true</code> if either of the operands are <code>true</code>. It returns <code>false</code> only if both operands are <code>false</code></p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> a = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">let</span> b = <span class="hljs-literal">false</span>;
<span class="hljs-keyword">let</span> c = a || b;
<span class="hljs-comment">// true</span>
</code></pre>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> a = <span class="hljs-literal">false</span>;
<span class="hljs-keyword">let</span> b = <span class="hljs-literal">false</span>;
<span class="hljs-keyword">let</span> c = a || b;
<span class="hljs-comment">// false</span>
</code></pre>
<h5 id="heading-not">NOT !</h5>
<p><code>!</code> returns inverse of the operand. <code>true</code> is converted to <code>false</code> and vice versa</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> a = <span class="hljs-literal">false</span>;
<span class="hljs-keyword">let</span> b = !a;
<span class="hljs-comment">// true</span>
</code></pre>
<h4 id="heading-compound-assignment-operators">Compound Assignment Operators</h4>
<p>These operators manipulate the value and then assign the value in the same statement.</p>
<h5 id="heading-add-and-assign">Add and Assign +=</h5>
<p><code>+=</code> adds the number with the variable and assigns the variable with the new value</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> a = <span class="hljs-number">1</span>;
a += <span class="hljs-number">1</span>;
<span class="hljs-comment">// 2</span>
</code></pre>
<p>The above code basically is the same as</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> a = <span class="hljs-number">1</span>;
a = a + <span class="hljs-number">1</span>;
<span class="hljs-comment">// 2</span>
</code></pre>
<h5 id="heading-subtract-and-assign">Subtract and Assign -=</h5>
<p><code>-=</code> subtracts the number with the variable and assigns the variable with the new value</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> a = <span class="hljs-number">1</span>;
a -= <span class="hljs-number">1</span>;
<span class="hljs-comment">// 0</span>
</code></pre>
<h5 id="heading-multiply-and-assign">Multiply and Assign -=</h5>
<p><code>*=</code> multiplies the number with the variable and assigns the variable with the new value</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> a = <span class="hljs-number">3</span>;
a *= <span class="hljs-number">2</span>;
<span class="hljs-comment">// 6</span>
</code></pre>
<h5 id="heading-divide-and-assign">Divide and Assign /=</h5>
<p><code>/=</code> divides the number with the variable and assigns the variable with the new value</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> a = <span class="hljs-number">4</span>;
a /= <span class="hljs-number">2</span>;
<span class="hljs-comment">// 2</span>
</code></pre>
<p>I've not included bitwise operators here as they may get too confusing for newcomers.</p>
<h3 id="heading-flow-control">Flow Control</h3>
<p>Flow control means, controlling the order in which our code executes.</p>
<h4 id="heading-if">If</h4>
<p><code>if</code> runs the code only if the condition provided to it is true</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> is_admin = <span class="hljs-literal">true</span>;

<span class="hljs-built_in">println!</span>(<span class="hljs-string">"User logged in"</span>);
<span class="hljs-keyword">if</span> is_admin == <span class="hljs-literal">true</span> {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"User is admin"</span>);
}
</code></pre>
<p>Output</p>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
User logged in
User is admin
</code></pre>
<p>What if, we change our variable from true to false</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> is_admin = <span class="hljs-literal">false</span>;
</code></pre>
<p>We get</p>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
User logged in
</code></pre>
<p>The <code>println</code> statement inside our if block didn't get printed, that's because our if statement got false.</p>
<h4 id="heading-else">Else</h4>
<p><code>else</code> runs when our <code>if</code> statement is not true.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> is_admin = <span class="hljs-literal">false</span>;

<span class="hljs-keyword">if</span> is_admin == <span class="hljs-literal">true</span> {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"User is admin"</span>);
} <span class="hljs-keyword">else</span> {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"User is not admin"</span>);
}
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
User is not admin
</code></pre>
<h4 id="heading-else-if">Else If</h4>
<p>We can have multiple if conditions chained together</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> is_admin = <span class="hljs-literal">false</span>;
<span class="hljs-keyword">let</span> is_mod = <span class="hljs-literal">true</span>;

<span class="hljs-keyword">if</span> is_admin == <span class="hljs-literal">true</span> {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"User is admin"</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> is_mod == <span class="hljs-literal">true</span> {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"User is mod"</span>);
} <span class="hljs-keyword">else</span> {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"User is neither admin nor mod"</span>);
}
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
User is mod
</code></pre>
<h4 id="heading-shorthand-if">Shorthand If</h4>
<p>We can use this to assign a variable some value depending on the if condition</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> age = <span class="hljs-number">18</span>;
<span class="hljs-keyword">let</span> is_eligible: <span class="hljs-built_in">bool</span> = <span class="hljs-keyword">if</span> age &gt;= <span class="hljs-number">21</span> { <span class="hljs-literal">true</span> } <span class="hljs-keyword">else</span> { <span class="hljs-literal">false</span> };
<span class="hljs-comment">// false</span>
</code></pre>
<h4 id="heading-match">Match</h4>
<p><code>match</code> statement is similar to having multiple else if statements</p>
<p>If you're coming from languages like C, C++, Java. You might be familiar with <code>switch</code> statement, this is essentially a <code>switch</code> statement.</p>
<p>It uses the keyword <code>match</code> followed by variable name and then braces <code>{}</code> which encapsulate the conditions and actions.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> role = <span class="hljs-string">"admin"</span>;

<span class="hljs-keyword">match</span> role {
   <span class="hljs-string">"admin"</span> =&gt; <span class="hljs-built_in">println!</span>(<span class="hljs-string">"The user is admin!"</span>),
   <span class="hljs-string">"mod"</span> =&gt; <span class="hljs-built_in">println!</span>(<span class="hljs-string">"The user is mod!"</span>),
   <span class="hljs-string">"guest"</span> =&gt; <span class="hljs-built_in">println!</span>(<span class="hljs-string">"The user needs to login"</span>),
   _ =&gt; <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Unknown user"</span>),
}
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
The user is admin!
</code></pre>
<p>If we were to change <code>role</code> variable, it would change the output of <code>match</code>.</p>
<p>Underscore <code>_</code> is used for fallback conditions. (Similar to <code>else</code>)</p>
<p>Another neat feature rust has over other languages is that it can combine cases together</p>
<p>for example</p>
<pre><code class="lang-rs"><span class="hljs-keyword">match</span> role {
   <span class="hljs-string">"owner"</span> | <span class="hljs-string">"admin"</span> =&gt; <span class="hljs-built_in">println!</span>(<span class="hljs-string">"The user is admin!"</span>),
   <span class="hljs-string">"mod"</span> =&gt; <span class="hljs-built_in">println!</span>(<span class="hljs-string">"The user is mod!"</span>),
   <span class="hljs-string">"guest"</span> =&gt; <span class="hljs-built_in">println!</span>(<span class="hljs-string">"The user needs to login"</span>),
   _ =&gt; <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Unknown user"</span>),
}
</code></pre>
<p>We can also provide a range to our <code>match</code></p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> age = <span class="hljs-number">17</span>;
<span class="hljs-keyword">match</span> age {
   <span class="hljs-number">13</span>..=<span class="hljs-number">19</span> =&gt; <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Teenagers"</span>),
   _ =&gt; <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Other age"</span>)
}
</code></pre>
<h3 id="heading-loops">Loops</h3>
<p>Loops are code blocks that keep on repeating</p>
<p>In rust, we have 3 types of loops</p>
<ul>
<li>loop</li>
<li>for</li>
<li>while</li>
</ul>
<h4 id="heading-loop">Loop</h4>
<p><code>loop</code> is an infinite loop. It runs forever until we explicitly tell it to stop.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> count = <span class="hljs-number">0</span>;

<span class="hljs-keyword">loop</span> {
   count += <span class="hljs-number">1</span>;
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{count}"</span>);

   <span class="hljs-keyword">if</span> count &gt;= <span class="hljs-number">5</span> {
      <span class="hljs-keyword">break</span>;
   }
}
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
1
2
3
4
5
</code></pre>
<p>Let's talk about what we did here,</p>
<ol>
<li><p>We first declared a mutable <code>count</code> variable and initialized it to 0.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> count = <span class="hljs-number">0</span>;
</code></pre>
</li>
<li><p>We started our loop</p>
<pre><code class="lang-rs"><span class="hljs-keyword">loop</span> {
</code></pre>
</li>
<li><p>On the start of our loop we added 1 to our variable</p>
<pre><code class="lang-rs">count += <span class="hljs-number">1</span>;
</code></pre>
</li>
<li><p>We printed our <code>count</code></p>
<pre><code class="lang-rs"><span class="hljs-built_in">println!</span>(<span class="hljs-string">"{count}"</span>);
</code></pre>
</li>
<li><p>We checked if our count is greater than or equal to 5, if it is, we <code>break</code> out of the loop, otherwise our loop goes back to the start and repeats from step 3.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">if</span> count &gt;= <span class="hljs-number">5</span> {
   <span class="hljs-keyword">break</span>;
}
</code></pre>
</li>
</ol>
<h4 id="heading-for">For</h4>
<p>For loops have a range, they start at a specific number we provide them and end before a specific number we provide them.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-number">0</span>..<span class="hljs-number">5</span> {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{x}"</span>);
}
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
0
1
2
3
4
</code></pre>
<p>Let's go through our code,</p>
<ol>
<li><p>We started our loop with range 0 to 5, which will get stored in <code>x</code> variable</p>
<pre><code class="lang-rs"><span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-number">0</span>..<span class="hljs-number">5</span> {
</code></pre>
</li>
<li><p>We print our <code>x</code> variable</p>
<pre><code class="lang-rs"><span class="hljs-built_in">println!</span>(<span class="hljs-string">"{x}"</span>);
</code></pre>
</li>
<li><p>and after that the loop goes back to the start, now what comes after 0? 1. So <code>x</code> turns into 1, and then it prints out to screen. This happens until we reach 4. The loop breaks 1 number before our final number 5.</p>
</li>
</ol>
<h4 id="heading-while">While</h4>
<p>While loops run until a condition is true</p>
<p>Let's do something exciting for this loop</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> count = <span class="hljs-number">0</span>;
<span class="hljs-keyword">while</span> count &lt;= <span class="hljs-number">100</span> {
   <span class="hljs-keyword">if</span> count % <span class="hljs-number">15</span> == <span class="hljs-number">0</span> {
      <span class="hljs-built_in">println!</span>(<span class="hljs-string">"fizzbuzz"</span>);
   }
   <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> count % <span class="hljs-number">3</span> == <span class="hljs-number">0</span>{
      <span class="hljs-built_in">println!</span>(<span class="hljs-string">"fizz"</span>);
   }
   <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> count % <span class="hljs-number">5</span> == <span class="hljs-number">0</span> {
      <span class="hljs-built_in">println!</span>(<span class="hljs-string">"buzz"</span>);
   }
   <span class="hljs-keyword">else</span> {
      <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{count}"</span>);
   }

   count += <span class="hljs-number">1</span>;
}
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
fizzbuzz
1
2
fizz
4
...
...
...
97
98
fizz
buzz
</code></pre>
<p>This is <code>fizzbuzz</code>, a simple programming task. In this task, we run a loop from 1 to 100, we print <code>fizz</code> if the number is divisible by 3, <code>buzz</code> if the number is divisible by 5, <code>fizzbuzz</code> if the number is divisible by both 3 and 5 and if neither of the above conditions are satisfied, it should just print the loop number.</p>
<p>That's exactly what's happening in our <code>while</code> loop.</p>
<ol>
<li><p>First we declared <code>count</code> variable to keep track of our loop iteration; we made it mutable as well.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> count = <span class="hljs-number">0</span>;
</code></pre>
</li>
<li><p>Then we started our while loop with the condition that it will keep running as long as our count is less than or equal to 100</p>
<pre><code class="lang-rs"><span class="hljs-keyword">while</span> count &lt;= <span class="hljs-number">100</span> {
</code></pre>
</li>
<li>Then comes our if statements<pre><code class="lang-rs"><span class="hljs-keyword">if</span> count % <span class="hljs-number">15</span> == <span class="hljs-number">0</span> {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"fizzbuzz"</span>);
}
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> count % <span class="hljs-number">3</span> == <span class="hljs-number">0</span>{
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"fizz"</span>);
}
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> count % <span class="hljs-number">5</span> == <span class="hljs-number">0</span> {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"buzz"</span>);
}
<span class="hljs-keyword">else</span> {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{count}"</span>);
}
</code></pre>
</li>
<li><p>Finally we add 1 to our count variable</p>
<pre><code class="lang-rs">count += <span class="hljs-number">1</span>;
</code></pre>
</li>
<li><p>After this, it goes back to start of the loop, if count is less than or equal to 100, the loop will run again, otherwise it will exit out of the loop.</p>
</li>
</ol>
<h3 id="heading-strings">Strings</h3>
<p>Let's talk about strings, we have 2 types of strings in Rust.</p>
<p><code>str</code> and <code>String</code></p>
<p><code>str</code> is a primitive datatype, it has fixed length and is immutable.</p>
<p><code>String</code> is a heap allocated data structure which is growable, meaning it's length can increase unlike <code>str</code></p>
<h4 id="heading-str">Str</h4>
<p>We can make string variables like this</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> name = <span class="hljs-string">"Hello There"</span>;
</code></pre>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> name: &amp;<span class="hljs-built_in">str</span> = <span class="hljs-string">"Hello There"</span>;
</code></pre>
<p>Both of the above are valid, they both make <code>str</code> type variable.</p>
<h4 id="heading-string">String</h4>
<p>To make <code>String</code></p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> hello = <span class="hljs-built_in">String</span>::from(<span class="hljs-string">"Hello"</span>);
</code></pre>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> hello: <span class="hljs-built_in">String</span> = <span class="hljs-built_in">String</span>::from(<span class="hljs-string">"Hello"</span>);
</code></pre>
<p>Both are correct but usually the first way is used as it already has <code>String</code> in the value.</p>
<p><code>String</code> also have a lot of built in functions.</p>
<h5 id="heading-push">push</h5>
<p>Let's say you want to append to the end of the string</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> hello = <span class="hljs-built_in">String</span>::from(<span class="hljs-string">"Hello"</span>);
hello.push(<span class="hljs-string">" world"</span>);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{hello}"</span>);
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
Hello world
</code></pre>
<h5 id="heading-len">len</h5>
<p><code>len</code> is used to get the length of the string. Length of the string is the number of characters it has</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> hello = <span class="hljs-built_in">String</span>::from(<span class="hljs-string">"Hello"</span>);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, hello.len());
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
5
</code></pre>
<p>Why did we get 5? We got 5 because "Hello" has 5 characters.</p>
<p><em>Note: Characters can be alphabets, numbers, spaces, symbols, etc</em></p>
<h5 id="heading-capacity">capacity</h5>
<p><code>capacity</code> gives us the size of the string in bytes</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> hello = <span class="hljs-built_in">String</span>::from(<span class="hljs-string">"Hello"</span>);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, hello.capacity());
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
5
</code></pre>
<h5 id="heading-isempty">is_empty</h5>
<p><code>is_empty</code>, sounds straight forward, it returns a boolean, it tells us if our string variable is empty or not</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> var = <span class="hljs-built_in">String</span>::new();
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, var.is_empty());

<span class="hljs-keyword">let</span> another_var = <span class="hljs-built_in">String</span>::from(<span class="hljs-string">"Hello my friend"</span>);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, another_var.is_empty());
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
true
false
</code></pre>
<h5 id="heading-contains">contains</h5>
<p>This returns a boolean. It tells us if a word or a piece of string exists in our variable or not.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> hello = <span class="hljs-built_in">String</span>::from(<span class="hljs-string">"Hello my friend"</span>);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, hello.contains(<span class="hljs-string">"my"</span>));
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
true
</code></pre>
<h5 id="heading-replace">replace</h5>
<p>We can also replace a specific piece of our string with a different string.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> hello = <span class="hljs-built_in">String</span>::from(<span class="hljs-string">"Hello my friend"</span>);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, hello.replace(<span class="hljs-string">"my"</span>, <span class="hljs-string">"your"</span>));
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
Hello your friend
</code></pre>
<h5 id="heading-splitwhitespace">split_whitespace</h5>
<p>It splits the string by whitespace. A whitespace can be defined as a space, a tab, etc.</p>
<p>It returns in iterator, which can't be formatted by our default formatting trait <code>{}</code>, so instead we'll be using the debug trait <code>{:?}</code></p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> hello = <span class="hljs-built_in">String</span>::from(<span class="hljs-string">"Hello my friend"</span>);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, hello.split_whitespace());
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
SplitWhitespace { inner: Filter { iter: Split(SplitInternal { start: 0, end: 15, matcher: CharPredicateSearcher { haystack: "Hello my friend", char_indices: CharIndices { front_offset: 0, iter: Chars(['H', 'e', 'l', 'l', 'o', ' ', 'm', 'y', ' ', 'f', 'r', 'i', 'e', 'n', 'd']) } }, allow_trailing_empty: true, finished: false }) } }
</code></pre>
<p>What's all this? This is a bit messy. It's all the information that variable contains right now.</p>
<p>Since it's an iterator, we can use it in a for loop.</p>
<p>If you remember, we gave a range to our for loop last time.</p>
<p>This time, let's give it our iterator</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> hello = <span class="hljs-built_in">String</span>::from(<span class="hljs-string">"Hello my friend"</span>);
<span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> hello.split_whitespace() {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{word}"</span>);
}
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
Hello
my
friend
</code></pre>
<p>Basically, our string gets split into an iterator which contains a list of our words that were separated by a space.</p>
<p>We printed it word by word using the for loop.</p>
<h5 id="heading-withcapacity">with_capacity</h5>
<p>We can also make a new string with our own capacity</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> s = <span class="hljs-built_in">String</span>::with_capacity(<span class="hljs-number">10</span>);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"Capacity: {}"</span>, s.capacity());

s.push(<span class="hljs-string">'a'</span>);
s.push(<span class="hljs-string">'b'</span>);

<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{s}"</span>);
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
Capacity: 10
ab
</code></pre>
<h3 id="heading-tuples">Tuples</h3>
<p>A tuple is a grouped value. It can have different types of data in it. A tuple can contain at max 12 elements.</p>
<p>Tuples can be created using parenthesis <code>()</code>.</p>
<p>To access the data in a tuple, we use indices. Like we learnt earlier about indices, they start at 0.</p>
<p>Let's look at an example</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> person: (&amp;<span class="hljs-built_in">str</span>, &amp;<span class="hljs-built_in">str</span>, <span class="hljs-built_in">u8</span>) = (<span class="hljs-string">"Raahim"</span>, <span class="hljs-string">"Pakistan"</span>, <span class="hljs-number">20</span>);

<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{} is from {} and is {}"</span>, person.<span class="hljs-number">0</span>, person.<span class="hljs-number">1</span>, person.<span class="hljs-number">2</span>);
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
Raahim is from Pakistan and is 20
</code></pre>
<h3 id="heading-arrays">Arrays</h3>
<p>Arrays can store multiple data of same data types. Their length are fixed. They are stack allocated data structures.</p>
<p>Let's create an array of single digit positive even numbers.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> numbers: [<span class="hljs-built_in">i32</span>; <span class="hljs-number">4</span>] = [<span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-number">8</span>];
</code></pre>
<p>The datatype of our array is <code>i32</code> and it contains <code>4</code> elements. Our elements are <code>2, 4, 6, 8</code></p>
<p>Since our array is mutable using <code>mut</code> keyword, we can change it's data.</p>
<p>To access an array, we use indices. First element is stored at 0 index, second at 1 index and last index is stored at n - 1 index, n being the length of the array.</p>
<pre><code class="lang-rs">numbers[<span class="hljs-number">1</span>] = -<span class="hljs-number">4</span>;
</code></pre>
<p>We can use loops to iterate over our arrays.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">for</span> number <span class="hljs-keyword">in</span> numbers {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, number);
}
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
2
-4
6
8
</code></pre>
<h4 id="heading-len">len</h4>
<p>We can get the length of an array using <code>len</code></p>
<pre><code class="lang-rs"><span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, numbers.len());
</code></pre>
<h4 id="heading-slicing-array">Slicing array</h4>
<p>We can slice an array as following</p>
<pre><code class="lang-rs"><span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, &amp;numbers[<span class="hljs-number">2</span>..<span class="hljs-number">4</span>]);
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
[6, 8]
</code></pre>
<p><code>2..4</code> means, our sliced array will start at index 2, and end before index 4.</p>
<p>Basically, 2 is included, 4 is not included, the one previous to 4 is included, which is 3.</p>
<h3 id="heading-vectors">Vectors</h3>
<p>Vectors are resizable arrays. Meaning, their length can be increased or decreased.</p>
<p>We can create vectors using <code>vec!</code> macro or using <code>Vec</code> as datatype.</p>
<p>For example</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> even_numbers: <span class="hljs-built_in">Vec</span>&lt;&amp;<span class="hljs-built_in">str</span>&gt;;
<span class="hljs-keyword">let</span> people = <span class="hljs-built_in">vec!</span>[<span class="hljs-string">"John"</span>, <span class="hljs-string">"Jane"</span>, <span class="hljs-string">"Doe"</span>];
</code></pre>
<p>Accessing elements in a vector is the same as in an array.</p>
<pre><code class="lang-rs"><span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, people[<span class="hljs-number">1</span>]);
<span class="hljs-comment">// Jane</span>
</code></pre>
<h4 id="heading-push-insert-into-vector">Push - Insert into vector</h4>
<p>Push is a function in vector, we can use it to insert data into our vector.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> people = <span class="hljs-built_in">vec!</span>[<span class="hljs-string">"John"</span>, <span class="hljs-string">"Jane"</span>, <span class="hljs-string">"Doe"</span>];

people.push(<span class="hljs-string">"Person"</span>);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, people);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, people[<span class="hljs-number">2</span>]);
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
["John", "Jane", "Doe", "Person"]
Doe
</code></pre>
<h4 id="heading-remove">remove</h4>
<p><code>remove</code> deletes a value at the given index.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> people = <span class="hljs-built_in">vec!</span>[<span class="hljs-string">"John"</span>, <span class="hljs-string">"Jane"</span>, <span class="hljs-string">"Doe"</span>];

people.remove(<span class="hljs-number">1</span>);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, people);
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
["John", "Doe"]
</code></pre>
<h4 id="heading-len">len</h4>
<p>Similar to array, returns the length of the vector</p>
<pre><code class="lang-rs">vector.len()
</code></pre>
<h4 id="heading-pop">pop</h4>
<p>Pop removes the last element in the vector and returns it as well</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> people = <span class="hljs-built_in">vec!</span>[<span class="hljs-string">"John"</span>, <span class="hljs-string">"Jane"</span>, <span class="hljs-string">"Doe"</span>];

<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, people.pop());
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
Some("Doe")
</code></pre>
<h4 id="heading-loops-with-vectors">loops with vectors</h4>
<p>Unlike arrays, with vectors we need to use methods in order for loops to work.</p>
<p>Let's take a look at a simple example</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> people = <span class="hljs-built_in">vec!</span>[<span class="hljs-string">"John"</span>, <span class="hljs-string">"Jane"</span>, <span class="hljs-string">"Doe"</span>];

<span class="hljs-keyword">for</span> person <span class="hljs-keyword">in</span> people.iter() {
  <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, person);
}
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
John
Jane
Doe
</code></pre>
<p>Notice how we're using <code>iter</code> method. Similarly, if you have a mutable vector and you want to change the values in the for loop as well, we can use <code>iter_mut</code></p>
<h3 id="heading-functions">Functions</h3>
<p>Functions are reusable code blocks. We can declare a function and then recall it as many times we want without rewriting all the code.</p>
<p>They're used to make our code more reusable.</p>
<p>You're already introduced to <code>main</code> function.
Just to recap, every rust program needs an entry point function named <code>main</code>.</p>
<p>It's usually in our <code>main.rs</code> file.</p>
<pre><code class="lang-rs"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() {
   ...
}
</code></pre>
<p>So far we've worked inside our <code>main</code> function but we'll make our own functions outside <code>main</code> function.</p>
<p>Now let's create our own function.</p>
<pre><code class="lang-rs"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() {
   ...
}

<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">hello</span></span>() {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Hello World!"</span>);
}
</code></pre>
<p>In the above example, I created a <code>hello</code> function outside <code>main</code> function.</p>
<p>But how do we use it now?</p>
<p>To call a function, we simple use it with parenthesis. For our <code>hello</code> function, we'd call it like <code>hello()</code></p>
<p>Let's try it</p>
<pre><code class="lang-rs"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() {
  hello();
}

<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">hello</span></span>() {
  <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Hello World!"</span>);
}
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
Hello World!
</code></pre>
<p>Notice how I called my <code>hello</code> function inside <code>main</code> function? Our function was declared outside <code>main</code> but we need to call it inside <code>main</code> or some other function.</p>
<p>The benefit of this function is that I can call it as many times as I want.</p>
<pre><code class="lang-rs">hello();
hello();
hello();
hello();
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
Hello World!
Hello World!
Hello World!
Hello World!
</code></pre>
<p>We can also pass arguments to our functions. Arguments are our variables or values that we want to use inside our functions.</p>
<p>Let's create an add function and let's pass 2 numbers to it.</p>
<pre><code class="lang-rs"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() {
  add(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>);
  add(<span class="hljs-number">15</span>, <span class="hljs-number">87</span>);
}

<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">add</span></span>(x: <span class="hljs-built_in">i32</span>, y: <span class="hljs-built_in">i32</span>) {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{x} + {y} = {}"</span>, x + y);
}
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
1 + 3 = 4
15 + 87 = 102
</code></pre>
<p>In this function we printed our values.</p>
<p>Alternatively, we can return our values from our functions to reuse later; we can store them in variables;</p>
<p>In order to do that, we have to define our return type of our function after our paraments and we have to use <code>return</code> statement.</p>
<pre><code class="lang-rs"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() {
  <span class="hljs-keyword">let</span> result = add(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>);
}

<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">add</span></span>(x: <span class="hljs-built_in">i32</span>, y: <span class="hljs-built_in">i32</span>) -&gt; <span class="hljs-built_in">i32</span> {
   <span class="hljs-keyword">return</span> x + y;
}
</code></pre>
<p>Now we learnt in the start that every statement in Rust needs to end with a semicplon <code>;</code>, but when we want to return a value in a function, we can exclude the semi colon and <code>return</code> keyword</p>
<p>Let me show you how, let's change our add function</p>
<pre><code class="lang-rs"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">add</span></span>(x: <span class="hljs-built_in">i32</span>, y: <span class="hljs-built_in">i32</span>) -&gt; <span class="hljs-built_in">i32</span> {
   x + y
}
</code></pre>
<p>This add function is the same as the previous one. The only difference is now we're not using <code>return</code> and <code>;</code>.</p>
<h4 id="heading-closures-or-anonymous-functions-or">Closures | Anonymous Functions |</h4>
<p>Closures in rust are anonymous functions that we can save in variables or pass to other functions</p>
<p>Let's convert our add function into a closer</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> add = |n1: <span class="hljs-built_in">i32</span>, n2: <span class="hljs-built_in">i32</span>| n1 + n2;
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, add(<span class="hljs-number">5</span>, <span class="hljs-number">3</span>));
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
8
</code></pre>
<h3 id="heading-structs">Structs</h3>
<p>Earlier, we talked about <code>tuples</code>; <code>tuples</code> are a type of Struct.</p>
<p>If you're coming from object-oriented languages, you may be familiar with classes. Structs are somewhat similar to classes.</p>
<p>If you're coming from C, you may be familiar with <code>struct</code> in C. Rust Struct is C styled.</p>
<h4 id="heading-structs-with-named-fields">Structs with named fields</h4>
<p>To create a struct, we use the <code>struct</code> keyword, followed by a name, and then braces which encapsulate named fields and their datatypes.</p>
<pre><code class="lang-rs"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Person</span></span> {
   first_name: <span class="hljs-built_in">String</span>,
   last_name: <span class="hljs-built_in">String</span>
}
</code></pre>
<p>How do we use this though? Let's create a variable with this struct.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> john = Person {
   first_name: <span class="hljs-string">"John"</span>.to_string(),
   last_name: <span class="hljs-string">"Doe"</span>.to_string()
};
</code></pre>
<p>Our fields are of <code>String</code> type, but we're assigning them <code>str</code>, that's why we called <code>to_string</code> methods, these are already created for us by the wonderful people working at rust.</p>
<p>Unlike <code>tuples</code> where we needed to use indices, here we can just access our struct values by their field name</p>
<pre><code class="lang-rs"><span class="hljs-built_in">println!</span>(<span class="hljs-string">"Full name: {} {}"</span>, john.first_name, john.last_name);
</code></pre>
<h4 id="heading-implementation">Implementation</h4>
<p>Implentation is basically adding functions to our structures. Just before last example, I mentioned that structures are somewhat similar to classes, what do classes have?</p>
<p>They have properties (variable) and methods (functions). What do our structures have? Named fields which we can consider as properties and through implementation, we can also add methods to our structures.</p>
<p><code>impl</code> keyword is used to make an implementation. It needs to have the same name as the struct you're implementing.</p>
<p>Let's take a look at how,</p>
<p>in our main function</p>
<pre><code class="lang-rs"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Color</span></span> {
   r: <span class="hljs-built_in">u8</span>,
   g: <span class="hljs-built_in">u8</span>,
   b: <span class="hljs-built_in">u8</span>
}

<span class="hljs-keyword">impl</span> Color {
   <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">new</span></span>(r: <span class="hljs-built_in">u8</span>, g: <span class="hljs-built_in">u8</span>, b: <span class="hljs-built_in">u8</span>) -&gt; Color {
      Color {
         r,
         g,
         b
      }
   }

   <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">to_tuple</span></span>(&amp;<span class="hljs-keyword">self</span>) -&gt; (<span class="hljs-built_in">u8</span>, <span class="hljs-built_in">u8</span>, <span class="hljs-built_in">u8</span>) {
      (<span class="hljs-keyword">self</span>.r, <span class="hljs-keyword">self</span>.g, <span class="hljs-keyword">self</span>.b)
   }
}

<span class="hljs-keyword">let</span> cyan = Color::new(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"R: {}, G: {}, B: {}"</span>, cyan.r, cyan.g, cyan.b);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, cyan.to_tuple());
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
R: 0, G: 255, B: 255
(0, 255, 255)
</code></pre>
<p>So what happened here?</p>
<ol>
<li><p>First we created our <code>Color</code> structure</p>
<pre><code class="lang-rs"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Color</span></span> {
   r: <span class="hljs-built_in">u8</span>,
   g: <span class="hljs-built_in">u8</span>,
   b: <span class="hljs-built_in">u8</span>
}
</code></pre>
</li>
<li><p>Then we created our implementation using <code>impl</code></p>
<pre><code class="lang-rs"><span class="hljs-keyword">impl</span> Color {
   ...
}
</code></pre>
</li>
<li><p>In our implementation, we created <code>new</code> function. That's used to create a new instance of our structure.</p>
<pre><code class="lang-rs"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">new</span></span>(r: <span class="hljs-built_in">u8</span>, g: <span class="hljs-built_in">u8</span>, b: <span class="hljs-built_in">u8</span>) -&gt; Color {
   Color {
      r,
      g,
      b
   }
}
</code></pre>
<p>It returns <code>Color</code></p>
</li>
<li><p>Then we created a <code>to_tuples</code>, which just returns our structure as a tuple</p>
<pre><code class="lang-rs"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">to_tuple</span></span>(&amp;<span class="hljs-keyword">self</span>) -&gt; (<span class="hljs-built_in">u8</span>, <span class="hljs-built_in">u8</span>, <span class="hljs-built_in">u8</span>) {
   (<span class="hljs-keyword">self</span>.r, <span class="hljs-keyword">self</span>.g, <span class="hljs-keyword">self</span>.b)
}
</code></pre>
</li>
<li><p>Finally, we create our variable and print out the values</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> cyan = Color::new(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"R: {}, G: {}, B: {}"</span>, cyan.r, cyan.g, cyan.b);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, cyan.to_tuple());
</code></pre>
</li>
</ol>
<h3 id="heading-enums">Enums</h3>
<p>Enums can be considered as custom data types.</p>
<p>Let's look at a few situations where enums may be used.</p>
<p>In games for instance, a player may be dead or alive. We can create an enum for that.</p>
<pre><code class="lang-rs"><span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">PlayerState</span></span> {
   Dead,
   Alive
}
</code></pre>
<p>Or for player movement, a player can move in 4 directions, forward, backward, right and left.</p>
<pre><code class="lang-rs"><span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">PlayerMovement</span></span> {
   Right,
   Left,
   Up,
   Down
}
</code></pre>
<p>Or the team of our player, is it in Red team, or Blue team, or is it a spectator?</p>
<pre><code class="lang-rs"><span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">PlayerTeam</span></span> {
   Red,
   Blue,
   Spectator
}
</code></pre>
<p>We can use it for days as well</p>
<pre><code class="lang-rs"><span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">Days</span></span> {
   Monday,
   Tuesday,
   Wednesday,
   Thursday,
   Friday,
   Saturday,
   Sunday
}
</code></pre>
<p>Now that we know how to define an enum, let's create some variables.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> today = Days::Tuesday;
</code></pre>
<p>Our <code>Days</code> enum has seven possible values. If we create a variable with <code>Days</code> datatype, it can be assigned one of those seven values.</p>
<p>Similarly for <code>PlayerState</code> we can do</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> alive = PlayerState::Alive;
<span class="hljs-keyword">let</span> dead = PlayerState::Dead;
</code></pre>
<h3 id="heading-comments">Comments</h3>
<p>Comments are a piece of code that get ignored by our compiler. We can use comments to leave messages in between our code for our future self or even other developers, explaining our code, so that it's easier to read our code when we pick it up after a few weeks or when we give it to someone else.</p>
<p>Comments can also be used to temporarily disabled a piece of code.</p>
<h4 id="heading-single-line-comments">Single line comments</h4>
<p>Single line comments can be made using double slash <code>//</code></p>
<pre><code class="lang-rs"><span class="hljs-comment">// This is a single lined comment</span>

<span class="hljs-comment">// Name variable</span>
<span class="hljs-keyword">let</span> my_name: &amp;<span class="hljs-built_in">str</span> = <span class="hljs-string">"Raahim"</span>;

<span class="hljs-comment">// Don't need admin variable for now</span>
<span class="hljs-comment">// let admin: &amp;str = "John Doe";</span>

<span class="hljs-comment">// If it's Raahim, do something</span>
<span class="hljs-keyword">if</span> my_name == <span class="hljs-string">"Raahim"</span> {
   ...
}
<span class="hljs-comment">// If it's not Raahim, do something else</span>
<span class="hljs-keyword">else</span> {
   ...
}
</code></pre>
<h4 id="heading-double-line-comments">Double line comments</h4>
<p>If we have multiple lines that we need to comment, it can be cumbersome to put '//' behind every line.</p>
<p>To overcome this, we use <code>/**/</code></p>
<pre><code class="lang-rs"><span class="hljs-comment">/*
This is a
multi line
comment,

It can span many lines
*/</span>
</code></pre>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> count = <span class="hljs-number">0</span>;
<span class="hljs-keyword">while</span> count &lt;= <span class="hljs-number">100</span> {
   <span class="hljs-comment">/*
   if count % 15 == 0 {
      println!("fizzbuzz");
   }
   else if count % 3 == 0{
      println!("fizz");
   }
   else if count % 5 == 0 {
      println!("buzz");
   }
   else {
      println!("{count}");
   }
   */</span>
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{count}"</span>);
   count += <span class="hljs-number">1</span>;
}
</code></pre>
<p>In the above code, we commented out our if statements.</p>
<h3 id="heading-generics">Generics</h3>
<p>Generics are used when we want to create the same function for different data types.</p>
<p>For example, an <code>add</code> function which adds two numbers together, you may want to create it for all types of numbers, <code>u8</code>, <code>i8</code>, <code>u16</code> ... <code>i128</code>, <code>f32</code>, <code>f64</code>. That's gonna be a lot of functions.</p>
<p>For functions like these, we use generics.</p>
<p>Let's create a generic function</p>
<pre><code class="lang-rs"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() {
  <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, function(<span class="hljs-string">"Hello there"</span>));
  <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, function(<span class="hljs-number">1</span>));
  <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, function(<span class="hljs-number">3.14</span>));
  <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, function(-<span class="hljs-number">100</span>));
  <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, function(<span class="hljs-string">'f'</span>));
  <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, function(<span class="hljs-literal">true</span>));
  <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, function([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]));
  <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, function((<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>)));
  <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, function(<span class="hljs-built_in">vec!</span>[<span class="hljs-number">0</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-number">8</span>]));
}

<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">function</span></span>&lt;T&gt;(var: T) -&gt; T {
  var
}
</code></pre>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
Hello there
1
3.14
-100
f
true
[1, 2, 3]
(4, 5, 6)
[0, 2, 4, 6, 8]
</code></pre>
<p>Let's see what we did here</p>
<ol>
<li><p>We created a function named <code>function</code> and besides the name, we added <code>&lt;T&gt;</code></p>
<p>Basically <code>&lt;T&gt;</code> is a placeholder for datatypes. It can be an integer, a float, boolean, any datatype, even custom datatypes such as structures and enums.</p>
<p>We also have an argument of the function with datatype <code>T</code> and our function also returns a value with datatype <code>T</code></p>
<pre><code class="lang-rs"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">function</span></span>&lt;T&gt;(var: T) -&gt; T {
   ...
}
</code></pre>
</li>
<li><p>In the function we simply returned our argument</p>
<pre><code class="lang-rs"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">function</span></span>&lt;T&gt;(var: T) -&gt; T {
   var
}
</code></pre>
</li>
<li><p>In our main functions, we had our print statements, we used many different data types for arguments in our function.</p>
<pre><code class="lang-rs"><span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, function(<span class="hljs-string">"Hello there"</span>));
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, function(<span class="hljs-number">1</span>));
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, function(<span class="hljs-number">3.14</span>));
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, function(-<span class="hljs-number">100</span>));
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, function(<span class="hljs-string">'f'</span>));
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, function(<span class="hljs-literal">true</span>));
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, function([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]));
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, function((<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>)));
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, function(<span class="hljs-built_in">vec!</span>[<span class="hljs-number">0</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-number">8</span>]));
</code></pre>
</li>
</ol>
<p>Now let's create an add function</p>
<pre><code class="lang-rs"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">add</span></span>&lt;T&gt;(one: T, two: T) -&gt; T {
  one + two
}
</code></pre>
<p>If we compile this, we get an error</p>
<pre><code class="lang-console">$ rustc src/main.rs -o build/main &amp;&amp; ./build/main
error[E0369]: cannot add `T` to `T`
  --&gt; src/main.rs:18:7
   |
18 |   one + two
   |   --- ^ --- T
   |   |
   |   T
   |
help: consider restricting type parameter `T`
   |
17 | fn add&lt;T: std::ops::Add&lt;Output = T&gt;&gt;(one: T, two: T) -&gt; T {
   |         +++++++++++++++++++++++++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0369`.
</code></pre>
<p>What's going on here?</p>
<p>The thing is, our datatype <code>T</code> can be a number, string, even custom datatypes like structures as we've established before but this comes with it's own problems. We can't add our custom datatypes together, in that case <code>+</code> will not work. So to avoid that later on, it gives us an error right now.</p>
<p>To fix that, we need to give a trait to our parameter type <code>T</code>.</p>
<p>If we read our error message, it also gives us a hint of what to do</p>
<pre><code class="lang-console">help: consider restricting type parameter `T`
   |
17 | fn add&lt;T: std::ops::Add&lt;Output = T&gt;&gt;(one: T, two: T) -&gt; T {
   |         +++++++++++++++++++++++++++
</code></pre>
<p>It even tells us the line number at which the error occurred, in my case it's line 17.</p>
<p>So let's add the trait <code>std::ops::Add&lt;Output = T&gt;</code> to our <code>T</code> parameter datatype.</p>
<pre><code class="lang-rs"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">add</span></span>&lt;T: std::ops::Add&lt;Output = T&gt;&gt;(one: T, two: T) -&gt; T {
  one + two
}
</code></pre>
<p>If we compile this now, it won't give us an error.</p>
<p>Let's use it</p>
<pre><code class="lang-rs"><span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, add(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>));
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, add(<span class="hljs-number">5</span>, <span class="hljs-number">10</span>));
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, add(<span class="hljs-number">3.12</span>, <span class="hljs-number">0.02</span>));
</code></pre>
<p>It should just give us</p>
<pre><code><span class="hljs-number">3</span>
<span class="hljs-number">15</span>
<span class="hljs-number">3.14</span>
</code></pre><p>Similarly for subtraction we would use <code>std::ops::Sub&lt;Output = T&gt;</code>,</p>
<p>for multiplication <code>std::ops::Mul&lt;Output = T&gt;</code>,</p>
<p>for division <code>std::ops::Div&lt;Output = T&gt;</code></p>
<p>You can find more information at <a target="_blank" href="https://doc.rust-lang.org/std/ops/index.html">Rust official website</a></p>
<h3 id="heading-cargo">Cargo</h3>
<p>Cargo is a rust's build system and package manager. It's used to install libraries, handle your project and compile your code.</p>
<p>It should be automatically installed in your system if have <code>rustc</code> installed.</p>
<p>Let's check</p>
<pre><code class="lang-console">$ cargo --version
cargo 1.58.0 (f01b232bc 2022-01-19)
</code></pre>
<p>Your cargo version may be a bit different but that's okay</p>
<h4 id="heading-creating-a-project-with-cargo">Creating a project with cargo</h4>
<p>To create a project we can simply run the command</p>
<pre><code class="lang-console">$ cargo new &lt;project_name&gt;
</code></pre>
<p>You need to replace <code>&lt;project_name&gt;</code> with the name of your project.</p>
<pre><code class="lang-console">$ cargo new hello_universe
Created binary (application) `hello_universe` package
</code></pre>
<p>Let's <code>cd</code> into our new project</p>
<pre><code class="lang-console">$ cd hello_universe
</code></pre>
<p>Your directory should look like</p>
<pre><code class="lang-console">.
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files
</code></pre>
<p>Cargo made a project folder for us, added <code>Cargo.toml</code> to it, created a <code>src</code> folder inside it and added <code>main.rs</code> to our <code>src</code> folder.</p>
<h4 id="heading-cargotoml">Cargo.toml</h4>
<p>This is the config file for our project.</p>
<p>For me it looks like</p>
<pre><code class="lang-toml"><span class="hljs-section">[package]</span>
<span class="hljs-attr">name</span> = <span class="hljs-string">"hello_universe"</span>
<span class="hljs-attr">version</span> = <span class="hljs-string">"0.1.0"</span>
<span class="hljs-attr">edition</span> = <span class="hljs-string">"2021"</span>

<span class="hljs-comment"># See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html</span>

<span class="hljs-section">[dependencies]</span>
</code></pre>
<p>The first section is <code>[package]</code>. It contains information about our project.</p>
<p>The second section is <code>[dependencies]</code>. It contains the information of other libraries that our project requires.</p>
<h4 id="heading-mainrs-file"><code>main.rs</code> file</h4>
<p>Opening our <code>main.rs</code> file, we see that cargo has already created a main function for us</p>
<pre><code class="lang-rs"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() {
    <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Hello, world!"</span>);
}
</code></pre>
<h4 id="heading-compiling-our-project-and-running-it">Compiling our project and running it</h4>
<p>So far we've used the command <code>rustc</code> to compile our program, but now we'll use <code>cargo</code> to handle it fo us.</p>
<pre><code class="lang-console">$ cargo build
</code></pre>
<p>If we run that, we get an output</p>
<pre><code class="lang-console">$ cargo build
   Compiling hello_universe v0.1.0 (/home/user/rust/hello_universe)
    Finished dev [unoptimized + debuginfo] target(s) in 0.28s
</code></pre>
<p>This command creates an executable file in _target/debug/hello<em>universe</em></p>
<p>We can run that file by either calling it in terminal</p>
<pre><code class="lang-console">$ ./target/debug/hello_universe
Hello, world!
</code></pre>
<p>Or we can use <code>cargo</code> to run it for us</p>
<pre><code class="lang-console">$ cargo run
</code></pre>
<p>Running that command gives us</p>
<pre><code class="lang-console">$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/hello_universe`
Hello, world!
</code></pre>
<p>We can also only use <code>cargo run</code> to compile and run our file instead of first running <code>build</code> and then <code>run</code></p>
<p>Let's change our print statement in <code>main.rs</code> to</p>
<pre><code class="lang-rs"><span class="hljs-built_in">println!</span>(<span class="hljs-string">"Hello, Universe!"</span>);
</code></pre>
<p>If we now run <code>cargo run</code> we get</p>
<pre><code class="lang-console">$ cargo run
   Compiling hello_universe v0.1.0 (/home/user/rust/hello_universe)
    Finished dev [unoptimized + debuginfo] target(s) in 0.25s
     Running `target/debug/hello_universe`
Hello, Universe!
</code></pre>
<p>Notice how it compiled our file and then ran it in the same command.</p>
<h4 id="heading-cargo-check">Cargo check</h4>
<p>If you're like me, you often save and compile your program to check for errors. The thing with <code>cargo build</code> is that even though it compiles our file, it creates an executable, which makes it a bit slower, at small projects it's not noticeable, but it becomes noticeable as our project goes.</p>
<p>In that case we use <code>cargo check</code>, this command compiles our project and tells us of errors, but it doesn't create an executable so it's faster than <code>cargo build</code></p>
<pre><code class="lang-console">$ cargo check
    Checking hello_universe v0.1.0 (/home/user/rust/hello_universe)
    Finished dev [unoptimized + debuginfo] target(s) in 0.14s
</code></pre>
<h4 id="heading-building-for-release">Building for release</h4>
<p>Once you're ready to release your project, there's <code>--release</code> flag that optimizes the code and removes debugging flags.</p>
<pre><code class="lang-console">$ cargo build --release
</code></pre>
<h4 id="heading-installing-dependencies">Installing dependencies</h4>
<p>Later on, you may need to install libraries and dependencies in order to help with you projects</p>
<p>For this, we'll need to list our dependencies in our <code>Cargo.toml</code> file.</p>
<p>When you open <code>Cargo.toml</code></p>
<p>It should have <code>[dependencies]</code>. You have to list your dependencies under this heading.</p>
<p>If you don't have this heading, just add it at the bottom of <code>Cargo.toml</code></p>
<p>Let's add <code>time</code> in our dependencies</p>
<pre><code class="lang-toml"><span class="hljs-section">[package]</span>
<span class="hljs-attr">name</span> = <span class="hljs-string">"hello_universe"</span>
<span class="hljs-attr">version</span> = <span class="hljs-string">"0.1.0"</span>
<span class="hljs-attr">edition</span> = <span class="hljs-string">"2021"</span>

<span class="hljs-comment"># See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html</span>

<span class="hljs-section">[dependencies]</span>
<span class="hljs-attr">time</span> = <span class="hljs-string">"*"</span>
</code></pre>
<p>Asterisk * means, cargo will install the latest possible version for us.</p>
<p>Let's now run <code>cargo build</code> and it'll automatically install the dependencies and build or program.</p>
<pre><code class="lang-console">$ cargo build
    Updating crates.io index
  Downloaded num_threads v0.1.6
  Downloaded time v0.3.9
  Downloaded libc v0.2.126
  Downloaded 3 crates (679.2 KB) in 3.39s
    Finished dev [unoptimized + debuginfo] target(s) in 5m 05s
</code></pre>
<p>Reading the output, we can see it installed <code>time</code> dependency. It's version is <code>v0.3.9</code></p>
<p>Now that we've installed it, how do we use it in our program?</p>
<p>That leads us to our next topic</p>
<h4 id="heading-use-crate"><code>use</code> crate</h4>
<p>Our dependencies and packages are called crates. To use them in our project, we use the <code>use</code> keyword.</p>
<p>Let's take a look at an example</p>
<pre><code class="lang-rs"><span class="hljs-keyword">use</span> time;

<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() {
    <span class="hljs-keyword">let</span> date = time::Date::from_calendar_date(<span class="hljs-number">2022</span>, time::Month::May, <span class="hljs-number">26</span>);
    <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, date.unwrap());
}
</code></pre>
<pre><code class="lang-console">$ cargo run
Date { year: 2022, ordinal: 146 }
</code></pre>
<ol>
<li><p>We added <code>time</code> create using <code>use</code> keyword.</p>
<pre><code class="lang-rs"><span class="hljs-keyword">use</span> time;
</code></pre>
</li>
<li><p>In our <code>time</code> crate, we have <code>Date</code> struct, and inside our <code>Date</code> struct we have a function named <code>from_calendar_date</code>, we're calling that function and creating a variable</p>
<pre><code class="lang-rs"><span class="hljs-keyword">let</span> date = time::Date::from_calendar_date(<span class="hljs-number">2022</span>, time::Month::May, <span class="hljs-number">26</span>);
</code></pre>
</li>
<li><p>Finally, we're printing our result after unwrapping it.</p>
<pre><code class="lang-rs"><span class="hljs-built_in">println!</span>(<span class="hljs-string">"{:?}"</span>, date.unwrap());
</code></pre>
</li>
</ol>
<h3 id="heading-pub-and-mod"><code>pub</code> and <code>mod</code></h3>
<p><code>pub</code> is used when we want to make our enums, structs and functions be externally available.</p>
<p>By externally available, it means that other files will be able to access these.</p>
<p>To use these in other files, we use <code>mod</code> in those external files.</p>
<p>Let's take a look at an example</p>
<pre><code class="lang-rs"><span class="hljs-comment">// functions.rs</span>
<span class="hljs-keyword">pub</span> <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">hello_world</span></span>() {
   <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Hello, World!"</span>);
}
</code></pre>
<pre><code class="lang-rs"><span class="hljs-comment">// main.rs</span>
<span class="hljs-keyword">mod</span> functions; <span class="hljs-comment">// name of the file without .rs</span>

<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() {
   functions::hello_world();
}
</code></pre>
<pre><code class="lang-console">$ cargo run
Hello, World!
</code></pre>
<p>Both files <code>main.rs</code> and <code>functions.rs</code> are in the same directory.</p>
<h3 id="heading-final-steps">Final Steps</h3>
<p>Now that you're familiar with the language, here are some resources to help you further</p>
<ul>
<li><a target="_blank" href="https://www.rust-lang.org/">Official Rust Website</a><ul>
<li><a target="_blank" href="https://play.rust-lang.org/">Online Rust Compiler</a></li>
</ul>
</li>
<li><a target="_blank" href="https://doc.rust-lang.org/book/">The Rust Programming Language</a>, book for reference and syntax</li>
<li><a target="_blank" href="https://crates.io/">crates.io</a>, rust community crate registry</li>
</ul>
<h4 id="heading-some-things-you-can-try-in-rust">Some things you can try in Rust</h4>
<ul>
<li>Make a website using Rust and WebAssembly</li>
<li>Try out <a target="_blank" href="https://www.piston.rs/">Piston</a>, a game engine</li>
<li>Make a SPA in Rust</li>
<li><a target="_blank" href="https://github.com/practical-tutorials/project-based-learning#rust">Some more project ideas</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Compiling C++ Using Terminal]]></title><description><![CDATA[Prerequisites

GCC compiler
For Windows, you will need to install mingw
For Linux, you will need to refer to your distributions package manager
For Mac, you will need to use brew



Compiling C++ Using Terminal

First, let's make a simple C++ file na...]]></description><link>https://blog.raahimfareed.com/compiling-cpp-using-terminal</link><guid isPermaLink="true">https://blog.raahimfareed.com/compiling-cpp-using-terminal</guid><category><![CDATA[C++]]></category><category><![CDATA[terminal]]></category><dc:creator><![CDATA[Raahim Fareed]]></dc:creator><pubDate>Tue, 10 May 2022 15:08:11 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1652194994743/PODSoVStV.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-prerequisites">Prerequisites</h3>
<ul>
<li>GCC compiler<ul>
<li>For Windows, you will need to install <code>mingw</code></li>
<li>For Linux, you will need to refer to your distributions package manager</li>
<li>For Mac, you will need to use <code>brew</code></li>
</ul>
</li>
</ul>
<h3 id="heading-compiling-c-using-terminal">Compiling C++ Using Terminal</h3>
<ol>
<li><p>First, let's make a simple C++ file named <code>main.cpp</code></p>
<pre><code class="lang-cpp"><span class="hljs-comment">// main.cpp</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Hello World!\n"</span>;
}
</code></pre>
</li>
<li><p>Now, open your terminal in the same directory where the file was saved.</p>
</li>
<li><p>To compile the file</p>
<pre><code class="lang-bash">$ g++ main.cpp
</code></pre>
<p>You can replace <code>main.cpp</code> with the filename you chose. This will compile your file into an executable called <code>a.out</code>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1652190691392/pFdF0OAaj.png" alt="image.png" /></p>
</li>
<li><p>Finally, to run the file</p>
<pre><code class="lang-bash">$ ./a.out
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1652190771990/Q_lfP1sCF.png" alt="image.png" /></p>
</li>
<li><p>Incase you want to name the executable file</p>
<pre><code class="lang-bash">$ g++ main.cpp -o main
</code></pre>
<p>This will make the executable file as <code>main</code></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1652191559057/-W7ZT1-0M.png" alt="image.png" /></p>
</li>
</ol>
<h3 id="heading-compiling-c-files-with-header-files">Compiling C++ Files with Header Files</h3>
<p>You've divided your main file into multiple files. Now what?</p>
<ol>
<li><p>I created a new file called <code>Universe.hpp</code> in <code>includes</code> directory and <code>main.cpp</code> into <code>src</code> directory
My directory now looks like</p>
<pre><code>.
├── includes
│   └── Universe.hpp
└── src
    └── main.cpp
</code></pre><pre><code class="lang-cpp"><span class="hljs-comment">// Universe.hpp</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">pragma</span> once</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Universe</span>
{</span>
  <span class="hljs-keyword">public</span>:
    Universe() {
      <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Hello Universe\n"</span>;
    }
};
</code></pre>
<p>and I changed my <code>main.cpp</code> to</p>
<pre><code><span class="hljs-comment">// main.cpp</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"Universe.hpp"</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Hello World!\n"</span>;

  Universe universe;
}
</code></pre><p>If I compile my main file now, I get an error</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1652194399706/v3Ohg6jK9.png" alt="image.png" /></p>
<p>What this means is that gcc can't find my <code>Universe.hpp</code> file, we need to tell where the file is.</p>
</li>
<li><p>To tell gcc where are header files are located, we will use <code>-I</code> flag</p>
<pre><code class="lang-bash">$ g++ src/main.cpp -I includes
</code></pre>
<p>It compiles just fine with no errors and runs as well!
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1652194579355/v8bETSqYl.png" alt="image.png" /></p>
</li>
</ol>
<h3 id="heading-conclusion">Conclusion</h3>
<ol>
<li><code>g++</code> is used to compile C++ on terminal</li>
<li><code>-o</code> flag is used to name output executable</li>
<li><code>-I</code> is used to include header files directory</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Enable Parallel Downloading in Pacman in Arch Linux]]></title><description><![CDATA[What is Parallel Downloading?
Parallel downloading means, downloading more than 1 packages at the same time.
By default Parallel downloading is disabled in Arch Linux, so whenever you download packages using Pacman, it downloads each package one by o...]]></description><link>https://blog.raahimfareed.com/arch-parallel-downloading</link><guid isPermaLink="true">https://blog.raahimfareed.com/arch-parallel-downloading</guid><category><![CDATA[Linux]]></category><dc:creator><![CDATA[Raahim Fareed]]></dc:creator><pubDate>Mon, 09 May 2022 17:59:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1652118889435/AvjPhmmLO.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-what-is-parallel-downloading">What is Parallel Downloading?</h3>
<p>Parallel downloading means, downloading more than 1 packages at the same time.</p>
<p>By default Parallel downloading is disabled in Arch Linux, so whenever you download packages using Pacman, it downloads each package one by one.</p>
<h3 id="heading-prerequisites">Prerequisites</h3>
<ul>
<li>Pacman version 6.0 or higher
You can check your pacman version by running<pre><code>$ pacman <span class="hljs-operator">-</span><span class="hljs-operator">-</span>version
</code></pre><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1652117964763/Md16JIZeP.png" alt="2022-05-09__001.png" /></li>
</ul>
<h3 id="heading-enabling-parallel-downloading">Enabling Parallel Downloading</h3>
<p>Open your terminal and type the following commands, I will explain what each command does step by step:</p>
<ol>
<li><p><code>/etc/pacman.conf</code> is Pacman configuration file, open it in your preferred text editor, I will be using <code>nano</code> for the sake of this guide:</p>
<pre><code>$ sudo nano <span class="hljs-operator">/</span>etc<span class="hljs-operator">/</span>pacman.conf
</code></pre></li>
<li><p>Find the following line and remove <code>#</code> from it's start; <code>#</code> means a line is commented</p>
<pre><code><span class="hljs-comment"># ParallelDownloads = 5</span>
</code></pre><p>It should now look like</p>
<pre><code><span class="hljs-attr">ParallelDownloads</span> = <span class="hljs-number">5</span>
</code></pre><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1652118241870/7L7bEk0mJ.png" alt="2022-05-09__002.png" />
5 means that pacman should be able to download 5 packages at the same time. You can change that number depending on your preference.</p>
</li>
<li><p>Press <code>CTRL + O</code> to save the file and <code>CTRL + X</code> to exit.</p>
</li>
</ol>
<p>And that's about it.</p>
<p>Now when you use pacman, it will download 5 packages concurrently.</p>
]]></content:encoded></item></channel></rss>