<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Programming on spaghettidev 🦀</title><link>https://spaghettidev.netlify.app/posts/programming/</link><description>Recent content in Programming on spaghettidev 🦀</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><copyright>&lt;a href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank" rel="noopener">CC BY-NC 4.0&lt;/a></copyright><lastBuildDate>Sun, 24 Apr 2022 15:04:20 -0400</lastBuildDate><atom:link href="https://spaghettidev.netlify.app/posts/programming/index.xml" rel="self" type="application/rss+xml"/><item><title>Arrays: A beginner's approach 😀</title><link>https://spaghettidev.netlify.app/posts/arrays-a-beginners-approach/</link><pubDate>Sun, 25 Sep 2022 08:48:13 -0400</pubDate><guid>https://spaghettidev.netlify.app/posts/arrays-a-beginners-approach/</guid><description>Overview I was touching grass with the tip of my feet, jumping around the meadow, then I saw a herding of wild variables and I thought: Why are they all coming and going by a group if each of them all owns a value by themselves.
— Cut it dude, looks like I&amp;rsquo;m crazy — Sometime we want to store a bunch of data, but nobody wants to write a bunch of variables just for the sake of it.</description><content type="html"><![CDATA[<h2 id="overview">Overview</h2>
<p>I was touching grass with the tip of my feet, jumping around the meadow, then I saw a herding of wild variables and I thought: Why are they all coming and going by a group if each of them all owns a value by themselves.</p>
<p>—  Cut it dude, looks like I&rsquo;m crazy — </p>
<p>Sometime we want to store a bunch of data, but nobody wants to write a bunch of variables just for the sake of it. Here comes collections, in this post we&rsquo;ll be talking about arrays.</p>
<p>As we know a collection is an abstract data type that consist in a group of variables that holds a relation between them, e.g: You want to store the first 10 unsigned integers, the days of the week or even the months of the year, all and each them holds a shared significance and needs to be operated upon together to solve the given problem.</p>
<p>There are different type of collections out there, hashmaps, sets, maps and so on, but for now we&rsquo;ll focus on static and dynamic arrays, how to represent it (visually) and while doing it and I&rsquo;m sure we&rsquo;ll spot some differences as well.</p>
<h2 id="introduction-to-static-arrays">Introduction to static arrays</h2>
<p>An static array is a fixed-size contiguous Data Structure, that means the values will be stored one next to other. First, let&rsquo;s take a deeper look at how variables works to spot some differences:</p>
<p>A variable is like a box that will hold your value ready to be read or changed. The compiler &amp; the linker will work together to assign you variable&rsquo;s name to a memory address, it&rsquo;s the compiler&rsquo;s job to keep track of all the references (calls to that variable) and the linker to put the right location in it. So anytime you see an indentifier <code>foo</code> the compiler will see a <code>0xnnnnnnnn</code> instead.</p>
<p>Something similar happens to static arrays, they let you keep multiple values under the same name but accessed through an unique identifier, and the array must have a known size at compile time, since they are stored in the stack memory — we&rsquo;ll be digging into that topic soon. Another cool thing about arrays is they are homogeneous: That means that each element is of the same type, e.g. an array of integers or an array of chars.</p>
<h3 id="visually-explained">Visually explained</h3>
<p>A good way to get a better grasp of it is to explain it visually so I&rsquo;m going make a <del>horrible</del> drawing and then we jump right into it.</p>
<p><img src="/visually-explained.png#center" alt="Visually explained"></p>
<p>In the center we find a box with a few cells in it, each cell contains a value and each value can be accessed through an <code>index</code>. I did create a small box below the array to show how indexing is being made. By the way you can notice that as the definition stated before every element is of the same type.</p>
<p>But an array with data unable to being read isn&rsquo;t really handy, right? So let&rsquo;s take a look at the next topic.</p>
<h3 id="indexing">Indexing</h3>
<p>In the vast majority of programming languages indexing uses <a href="https://en.wikipedia.org/wiki/Zero-based_numbering">zero-based numbering</a>. This means that in order to get the data stored in the 3rd spot in our example array, we do have to get it through the <code>index 2</code>. In the picture below I did represent each index within its value.</p>
<blockquote>
<p>📍Note: Array&rsquo;s last index will always be (length - 1). In our example the length is 4, so the last element will be indexed by ( 4 - 1 ) index 3.</p>
</blockquote>
<p><img src="/Indexing.png#center" alt="Indexing"></p>
<blockquote>
<p>📍Note: It&rsquo;s worth to remark that the squared brackets notation used here <code>Array[Index]</code> is <em>convention</em> among the most popular programming languages. By the way, if you&rsquo;re interested in how it became <em>standart</em> you can read whether <a href="https://softwareengineering.stackexchange.com/questions/368256/how-did-the-custom-of-using-square-brackets-for-array-elements-develop">the discussion here</a>  <a href="https://drmarkclewis.medium.com/syntax-for-indexing-why-is-an-antiquated-standard-798aa81f01bb">or a summary here</a>.</p>
</blockquote>
<p>But now a question raises from the depths of our minds. What if we try to access to <code>index -1</code> or <code>index 4</code>, <strong>What will we get?</strong></p>
<h3 id="index-out-of-bounds">Index out of bounds</h3>
<p>Whether trying to access to negative index or a index greater or equal to the array&rsquo;s length you&rsquo;re accessing to an <code>illegal index</code>. In the modern programming languages this will result in an error labeled <code>Index out of bounds</code>. But in older languages such as C the compiler neither provide compile-time checks nor runtime checks for out of bounds access. So you&rsquo;re likely to compile an error prone program and run into <a href="https://en.wikipedia.org/wiki/Undefined_behavior">Undefined Behaviour</a>.</p>
<p><img src="/index-out-of-bounds.png#center" alt="index out of bounds"></p>
<h2 id="introduction-to-dynamic-arrays">Introduction to dynamic arrays</h2>
<p>A big throwback when working with  <code>static sized array</code> is that there&rsquo;s no good way to add an element<code>( n + 1)</code> if the array is of size <code>n</code>. So instead of allocating a enormous <em>capacity</em> —  Which is a horrible decision when it comes to performance —  for the array, we can use <code>dynamic arrays</code>.</p>
<blockquote>
<p>❔ Let&rsquo;s take a slight look at what&rsquo;s the logic behind it.</p>
</blockquote>
<p>Dynamic arrays are, in fact, a static array with some magic tricks going underlying. In short, when the array runs out of space to store more values in it the compiler allocates another array with the double of size and copying the elements to the new array.</p>
<p>Look the at the picture bellow.</p>
<p><img src="/allocating-dynamic-array.png#center" alt="Allocating a dynamic array"></p>
<p>Imagine having an array of <code>size 1</code>, if we add a single value, it&rsquo;ll have no longer space so the compiler allocates another array of <code>size 2</code>, copies the values to it and deallocates the old array. The same proccess will happen as long as we fulfill the available space.</p>
<h2 id="conclusion">Conclusion</h2>
<p>Arrays are a very straightforward data structures and is one of the most popular ways to relate data. In further posts I&rsquo;ll be talking about how are arrays implemented in C++ (and Rust), how to use them and some useful examples to get seasoned. Hope you enjoyed ❤️</p>
]]></content></item><item><title>Creating a CLI with Rust</title><link>https://spaghettidev.netlify.app/posts/creating-a-cli-with-rust/</link><pubDate>Mon, 07 Mar 2022 15:35:15 -0400</pubDate><guid>https://spaghettidev.netlify.app/posts/creating-a-cli-with-rust/</guid><description>Setup ⚠ Warning
This post requires basic understanding of some programming concepts as structs, functions, modules, and I/O handling. It is also recommended to have Rust knowledge, if you are new to programming I highly recommend you to check out this video and/or if you are new to Rust, you should read the official book.
Before getting into our hacker fashion, let&amp;rsquo;s create a cargo project and install the packages we&amp;rsquo;ll need:</description><content type="html"><![CDATA[<h2 id="setup">Setup</h2>
<p>⚠ Warning</p>
<p>This post requires basic understanding  of some programming concepts as structs, functions, modules, and I/O handling. It is also recommended to have Rust knowledge, if you are new to programming I highly recommend you to check out this <a href="https://youtu.be/zOjov-2OZ0E">video</a> and/or if you are new to Rust, you should read the <a href="https://youtu.be/zOjov-2OZ0E">official book</a>.</p>
<p>Before getting into our <strong>hacker</strong> fashion,
let&rsquo;s create a cargo project and install the packages we&rsquo;ll need:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Bash" data-lang="Bash"><span style="display:flex;"><span>cargo new ferris-say
</span></span></code></pre></div><p>Now, open your Cargo.toml, and add this below <code>[dependencies]</code>:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-toml" data-lang="toml"><span style="display:flex;"><span>[<span style="color:#a6e22e">dependencies</span>]
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">ansi_term</span> = <span style="color:#e6db74">&#34;0.12.1&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">clap</span> = { <span style="color:#a6e22e">version</span> = <span style="color:#e6db74">&#34;3.1.6&#34;</span>, <span style="color:#a6e22e">features</span> = [<span style="color:#e6db74">&#34;derive&#34;</span>] }
</span></span></code></pre></div><p>Or, if you have <a href="https://github.com/killercup/cargo-edit">cargo edit</a> (which I prefer using), write right in your terminal:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Bash" data-lang="Bash"><span style="display:flex;"><span>cargo add ansi_term 
</span></span><span style="display:flex;"><span>cargo add clap --features derive
</span></span></code></pre></div><p>After it&rsquo;s installed, we&rsquo;re ready to go to the next step.</p>
<h2 id="getting-input-from-arguments">Getting input from arguments</h2>
<p>The crate <a href="https://github.com/clap-rs/clap">clap</a> that we just installed and used in the snippet below, helps us not only to parse but to validate the input given by the user. Let&rsquo;s see how to implement it in our application.</p>
<p>First thing is to create a function named <code>input</code> here we&rsquo;ll handle and parse the arguments provided; Our Cli will request only two arguments to work with: <code>quote</code> and <code>color</code> but for <code>color</code> we need an enum as we&rsquo;ll only allow to use a few types of colors (but a cool ones).</p>
<h3 id="the-function-signature">The function signature</h3>
<p>The function signatures let us to know how many parameters will it receive and what will be the output generated, and let&rsquo;s bring into scope both of the crate we&rsquo;ll be using along this tutorial:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Rust" data-lang="Rust"><span style="display:flex;"><span><span style="color:#75715e">// Imports
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">use</span> ansi_term::{self, Colour};
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">use</span> clap::{ArgEnum, Parser};
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">fn</span> <span style="color:#a6e22e">input</span>() -&gt; (String, Colour)
</span></span></code></pre></div><h3 id="parsing-the-input">Parsing the input</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Rust" data-lang="Rust"><span style="display:flex;"><span>  <span style="color:#75715e">#[derive(Parser, Debug)]</span>
</span></span><span style="display:flex;"><span>  <span style="color:#75715e">#[clap(author, version, about = </span><span style="color:#e6db74">&#34;cowsay rusty version&#34;</span><span style="color:#75715e">)]</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">struct</span> <span style="color:#a6e22e">Args</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">/// Quote that ferris will say
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"></span>        <span style="color:#75715e">#[clap(short, long)]</span>
</span></span><span style="display:flex;"><span>        quote: String,
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">/// Colors to choose
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"></span>        <span style="color:#75715e">#[clap(arg_enum)]</span>
</span></span><span style="display:flex;"><span>        color: <span style="color:#a6e22e">Colors</span>,
</span></span><span style="display:flex;"><span>    }
</span></span></code></pre></div><p>The <code>Parser</code> trait that our <code>struct</code> derives, parses all the arguments provided by the user and turns it into that struct fields. As you can see, we only have two fields, so it means, we&rsquo;ll only receive two arguments, as we state before.</p>
<p>Note that <code>quote</code> is the type of <code>String</code> and <code>color</code> is the type of <code>Colors</code>, but it doesn&rsquo;t exist yet, so let&rsquo;s create it.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Rust" data-lang="Rust"><span style="display:flex;"><span>  <span style="color:#75715e">#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum, Debug)]</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">enum</span> <span style="color:#a6e22e">Colors</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// Not black because it&#39;s the background color
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        Red,
</span></span><span style="display:flex;"><span>        Green,
</span></span><span style="display:flex;"><span>        Yellow,
</span></span><span style="display:flex;"><span>        Blue,
</span></span><span style="display:flex;"><span>        Purple,
</span></span><span style="display:flex;"><span>        Cyan,
</span></span><span style="display:flex;"><span>        White,
</span></span><span style="display:flex;"><span>    }
</span></span></code></pre></div><p>Focus on that <code>ArgEnum</code> derived trait, that means: &ldquo;Hey, this enum&rsquo;s variants are the only few valid types for a argument&rdquo; and clap will ensure that the argument provided fits into it.</p>
<p>Once we have our <code>Colors</code> enum, let&rsquo;s handle each variant using the classic <strong>match</strong> statement.</p>
<h3 id="handling-the-input">Handling the input</h3>
<p>At this point we already brought into scope our <code>ansi_term</code> crate so it&rsquo;s time to use it</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Rust" data-lang="Rust"><span style="display:flex;"><span><span style="color:#75715e">// Imports
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">use</span> clap::{ArgEnum, Parser};
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">use</span> ansi_term::{self, Colour} <span style="color:#75715e">// -&gt; This one
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// fn input(){...} 
</span></span></span></code></pre></div><p>So we can crate a match statement to filter through the <code>Colors</code>(our enum) variants, return a <code>Colour</code>(ansi_term&rsquo;s enum) variant and bind it to a variale named <code>color_matched</code>.</p>
<p>First, create a new instance of <code>Args</code>. To access to its field (<code>quote</code> and <code>color</code>) we need to use the <strong>dot notation</strong>:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Rust" data-lang="Rust"><span style="display:flex;"><span><span style="color:#75715e">// ...
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">let</span> args <span style="color:#f92672">=</span> Args::parse();
</span></span><span style="display:flex;"><span><span style="color:#75715e">// to access to the quote:
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>args.quote
</span></span><span style="display:flex;"><span><span style="color:#75715e">// to access to the color:
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>args.color <span style="color:#75715e">// If you want to show through terminal remember using &#34;:?&#34;
</span></span></span></code></pre></div><p>Now we are up to handle:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Rust" data-lang="Rust"><span style="display:flex;"><span><span style="color:#75715e">// ...
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">let</span> color_matched <span style="color:#f92672">=</span> <span style="color:#66d9ef">match</span> args.color {
</span></span><span style="display:flex;"><span>        Colors::Red <span style="color:#f92672">=&gt;</span> Colour::Red,
</span></span><span style="display:flex;"><span>        Colors::Green <span style="color:#f92672">=&gt;</span> Colour::Green,
</span></span><span style="display:flex;"><span>        Colors::Yellow <span style="color:#f92672">=&gt;</span> Colour::Yellow,
</span></span><span style="display:flex;"><span>        Colors::Blue <span style="color:#f92672">=&gt;</span> Colour::Blue,
</span></span><span style="display:flex;"><span>        Colors::Purple <span style="color:#f92672">=&gt;</span> Colour::Purple,
</span></span><span style="display:flex;"><span>        Colors::Cyan <span style="color:#f92672">=&gt;</span> Colour::Cyan,
</span></span><span style="display:flex;"><span>        Colors::White <span style="color:#f92672">=&gt;</span> Colour::White,
</span></span><span style="display:flex;"><span>    };
</span></span><span style="display:flex;"><span>    println!(<span style="color:#e6db74">&#34;The user picked the color {:?}&#34;</span>, color_matched);
</span></span></code></pre></div><p>Last thing to do is return the tuple:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Rust" data-lang="Rust"><span style="display:flex;"><span><span style="color:#75715e">//...
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>(args.quote, color_matched)
</span></span></code></pre></div><p>Time to test it!</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>cargo build
</span></span><span style="display:flex;"><span>target/debug/ferris_say.exe -h
</span></span><span style="display:flex;"><span>ferris-say 0.1.0
</span></span><span style="display:flex;"><span>cowsay rusty version
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>USAGE:
</span></span><span style="display:flex;"><span>    ferris_say.exe --quote &lt;QUOTE&gt; &lt;COLOR&gt;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>ARGS:
</span></span><span style="display:flex;"><span>    &lt;COLOR&gt;    Colors to choose <span style="color:#f92672">[</span>possible values: red, green, yellow, blue, purple,
</span></span><span style="display:flex;"><span>               cyan, white<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>OPTIONS:
</span></span><span style="display:flex;"><span>    -h, --help             Print help information
</span></span><span style="display:flex;"><span>    -q, --quote &lt;QUOTE&gt;    Quote that ferris will say
</span></span><span style="display:flex;"><span>    -V, --version          Print version information
</span></span></code></pre></div><p>passing the <code>-h</code> or <code>--help</code> shows the arguments we can use.</p>
<p>Once we know what are the arguments, we&rsquo;re able to use it correctly.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>target/debug/ferris_say.exe -q <span style="color:#e6db74">&#34;Hello world!&#34;</span> red
</span></span><span style="display:flex;"><span>The user picked the color Red
</span></span></code></pre></div><p>But&hellip;what if I pass a color that isn&rsquo;t valid?</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>target/debug/ferris_say.exe -q <span style="color:#e6db74">&#34;hello world!&#34;</span> black
</span></span><span style="display:flex;"><span>error: <span style="color:#e6db74">&#34;black&#34;</span> isn<span style="color:#e6db74">&#39;t a valid value for &#39;</span>&lt;COLOR&gt;<span style="color:#960050;background-color:#1e0010">&#39;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">[</span>possible values: red, green, yellow, blue, purple, cyan, white<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>USAGE:
</span></span><span style="display:flex;"><span>    ferris_say.exe --quote &lt;QUOTE&gt; &lt;COLOR&gt;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>For more information try --help
</span></span></code></pre></div><p>We are ready to go to the next step: drawing the ferris.</p>
<h2 id="draw-the-ferris">Draw the ferris</h2>
<p>Here, I highly recommend you just to copy and paste the drawing. But first, let&rsquo;s create another function <code>draw</code>:</p>
<h3 id="the-function-signature-1">The function signature</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Rust" data-lang="Rust"><span style="display:flex;"><span><span style="color:#66d9ef">fn</span> <span style="color:#a6e22e">draw</span>(quote: <span style="color:#66d9ef">&amp;</span><span style="color:#66d9ef">str</span>, color: <span style="color:#66d9ef">&amp;</span><span style="color:#a6e22e">Colour</span>)
</span></span></code></pre></div><p>Inside the <code>draw</code> function:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Rust" data-lang="Rust"><span style="display:flex;"><span><span style="color:#75715e">// Ferris drawing
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">const</span> FERRIS: <span style="color:#66d9ef">&amp;</span><span style="color:#f92672">&#39;</span>static <span style="color:#66d9ef">str</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">r&#34;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">    .
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">     .
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      .
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">       █ █           █ █
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        ▀█  ▄█████▄  █▀
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">         ▀▄███▀█▀███▄▀ 
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">         ▄▀███▀▀▀███▀▄ 
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">         █ ▄▀▀▀▀▀▀▀▄ █
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">    &#34;</span>;
</span></span><span style="display:flex;"><span>    println!(<span style="color:#e6db74">&#34;{}&#34;</span>, format!(<span style="color:#e6db74">&#34;\&#34;{}\&#34;{}&#34;</span>, quote, color.paint(FERRIS)));
</span></span></code></pre></div><p>A <code>r&quot;&quot;</code> String is called as &ldquo;raw string&rdquo;, in short, it tells the compiler to ignore every scape character, It&rsquo;s easier for us to draw our ferris that way. On the other hand we have a <code>format!</code>macro, we use it as we&rsquo;ll place the text right above the ferris. It will be better illustrated as we run the program.</p>
<p><strong>Last but not least</strong> color is a Colour instance passed by reference, that means we can use the <code>paint</code> that receives a &amp;str params. It &ldquo;paints&rdquo; the text to a given color.</p>
<p>Finally inside our <code>main function</code>:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Rust" data-lang="Rust"><span style="display:flex;"><span><span style="color:#66d9ef">fn</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">let</span> (q, c) <span style="color:#f92672">=</span> input();
</span></span><span style="display:flex;"><span>    draw(<span style="color:#f92672">&amp;</span>q, <span style="color:#f92672">&amp;</span>c)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h2 id="final-code">Final code</h2>
<p>We&rsquo;ll split it into parts to understand how it works.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Rust" data-lang="Rust"><span style="display:flex;"><span><span style="color:#66d9ef">use</span> ansi_term::{self, Colour}; <span style="color:#75715e">// 0.12.1
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">use</span> clap::{ArgEnum, Parser};
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">fn</span> <span style="color:#a6e22e">draw</span>(quote: <span style="color:#66d9ef">&amp;</span><span style="color:#66d9ef">str</span>, color: <span style="color:#66d9ef">&amp;</span><span style="color:#a6e22e">Colour</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// Ferris drawing
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">const</span> FERRIS: <span style="color:#66d9ef">&amp;</span><span style="color:#f92672">&#39;</span>static <span style="color:#66d9ef">str</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">r&#34;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">    .
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">     .
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      .
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">       █ █           █ █
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        ▀█  ▄█████▄  █▀
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">         ▀▄███▀█▀███▄▀ 
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">         ▄▀███▀▀▀███▀▄ 
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">         █ ▄▀▀▀▀▀▀▀▄ █
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">    &#34;</span>;
</span></span><span style="display:flex;"><span>    println!(<span style="color:#e6db74">&#34;{}&#34;</span>, format!(<span style="color:#e6db74">&#34;\&#34;{}\&#34;{}&#34;</span>, quote, color.paint(FERRIS)));
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">fn</span> <span style="color:#a6e22e">input</span>()-&gt; (String, Colour) {
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">#[derive(Parser, Debug)]</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">#[clap(author, version, about = </span><span style="color:#e6db74">&#34;cowsay rusty version&#34;</span><span style="color:#75715e">)]</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">struct</span> <span style="color:#a6e22e">Args</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">/// Quote that ferris will say
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"></span>        <span style="color:#75715e">#[clap(short, long)]</span>
</span></span><span style="display:flex;"><span>        quote: String,
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">/// Colors to choose
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"></span>        <span style="color:#75715e">#[clap(arg_enum)]</span>
</span></span><span style="display:flex;"><span>        color: <span style="color:#a6e22e">Colors</span>,
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum, Debug)]</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">enum</span> <span style="color:#a6e22e">Colors</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// Not black because it&#39;s the our background color
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        Red,
</span></span><span style="display:flex;"><span>        Green,
</span></span><span style="display:flex;"><span>        Yellow,
</span></span><span style="display:flex;"><span>        Blue,
</span></span><span style="display:flex;"><span>        Purple,
</span></span><span style="display:flex;"><span>        Cyan,
</span></span><span style="display:flex;"><span>        White,
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">let</span> args <span style="color:#f92672">=</span> Args::parse();
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// validate colors argument:
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">let</span> color_matched <span style="color:#f92672">=</span> <span style="color:#66d9ef">match</span> args.color {
</span></span><span style="display:flex;"><span>        Colors::Red <span style="color:#f92672">=&gt;</span> Colour::Red,
</span></span><span style="display:flex;"><span>        Colors::Green <span style="color:#f92672">=&gt;</span> Colour::Green,
</span></span><span style="display:flex;"><span>        Colors::Yellow <span style="color:#f92672">=&gt;</span> Colour::Yellow,
</span></span><span style="display:flex;"><span>        Colors::Blue <span style="color:#f92672">=&gt;</span> Colour::Blue,
</span></span><span style="display:flex;"><span>        Colors::Purple <span style="color:#f92672">=&gt;</span> Colour::Purple,
</span></span><span style="display:flex;"><span>        Colors::Cyan <span style="color:#f92672">=&gt;</span> Colour::Cyan,
</span></span><span style="display:flex;"><span>        Colors::White <span style="color:#f92672">=&gt;</span> Colour::White,
</span></span><span style="display:flex;"><span>    };
</span></span><span style="display:flex;"><span>    (args.quote, color_matched)
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">fn</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">let</span> (q, c) <span style="color:#f92672">=</span> input();
</span></span><span style="display:flex;"><span>    draw(<span style="color:#f92672">&amp;</span>q, <span style="color:#f92672">&amp;</span>c)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h2 id="result-photos">Result photos</h2>
<p><img src="/creating-a-CLI-result.jpg" alt="Result photo"></p>
<p>I hope you guys enjoyed this tutorial, any comment or suggestion my email is opened!</p>
]]></content></item></channel></rss>