<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Blitty</title>
    <description>The latest articles on DEV Community by Blitty (@blitty).</description>
    <link>https://dev.to/blitty</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F643016%2Fce7811a6-0236-430e-ae83-45b57680d4cf.jpeg</url>
      <title>DEV Community: Blitty</title>
      <link>https://dev.to/blitty</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/blitty"/>
    <language>en</language>
    <item>
      <title>Rustlings V (Structs, enums)</title>
      <dc:creator>Blitty</dc:creator>
      <pubDate>Sat, 05 Feb 2022 11:51:19 +0000</pubDate>
      <link>https://dev.to/blitty/rustlings-v-structs-enums-5d76</link>
      <guid>https://dev.to/blitty/rustlings-v-structs-enums-5d76</guid>
      <description>&lt;p&gt;&lt;a href="https://i.giphy.com/media/l0IyjiXOXTX6Yemsg/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l0IyjiXOXTX6Yemsg/giphy.gif" alt="Lets go" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Structs 🥸
&lt;/h2&gt;

&lt;p&gt;Like C structs these types are a set of different fields with a type and it can be used for almost everything. A cool feature that Rust have, are &lt;code&gt;methods&lt;/code&gt; for structs which is different from a &lt;code&gt;function&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  "Normal" structs
&lt;/h3&gt;

&lt;p&gt;Also a cool thing about Rust, that is like Javascript (ECMA6), the &lt;strong&gt;spread&lt;/strong&gt; operator, which let us take the values from a struct and place them into another one in the same fields. Another feature like in ECMA6 is that when you have variables with the same name as a property from a struct, you can use the name instead of variable and value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// creating an Animal struct&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;death&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Josefina"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// instanciate a new Animal&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;bonnie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Bonnie"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;death&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"dog"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// normal re-use from a struct to a new one&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;josefina&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// instead of name: name (which is redundant)&lt;/span&gt;
  &lt;span class="c1"&gt;// also don't do these, this way, do it with the spread operator&lt;/span&gt;
  &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;bonnie&lt;/span&gt;&lt;span class="py"&gt;.age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;death&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;bonnie&lt;/span&gt;&lt;span class="py"&gt;.age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;bonnie&lt;/span&gt;&lt;span class="py"&gt;.type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// re-use properties from a struct rust way&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;guru&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Guru"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="n"&gt;bonnie&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Tuple struct
&lt;/h3&gt;

&lt;p&gt;You can use tuples as a struct instead of a normal struct. So instead of having a struct &lt;code&gt;Coordinates&lt;/code&gt; fields with names &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;. We can have a struct tuple, which has &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; without the names.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Coordinates_like_MEH&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// do this &lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;Coordinates&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Coordinates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"x: {}, y: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt;&lt;span class="na"&gt;.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt;&lt;span class="na"&gt;.1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is useful because sometimes the name of the attributes annoy more than helps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unit-Like structs
&lt;/h3&gt;

&lt;p&gt;This are structs that does not have fields. I mean this is cool, because if you have something you don't know what it gives you, you can use this structs. (We will see on later posts, because is chapter 10)&lt;/p&gt;

&lt;p&gt;Also, remember: Be careful with the context!!!! Because of ownership OwO&lt;/p&gt;

&lt;p&gt;An important thing about structs, and in general, is that when you use &lt;code&gt;println!()&lt;/code&gt; and types like integer, float, etc. they have implementd a &lt;code&gt;Display&lt;/code&gt; method.&lt;br&gt;
Structs does not have that, so you need to implement it or use &lt;code&gt;Debug&lt;/code&gt; that can be used to print data of a field or the hole struct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt; &lt;span class="c1"&gt;// u need this otherwise you won't be able to use debug&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Meeeep&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;wtf_level&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;meep007&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Meeeep&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;wtf_level&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"meep007: {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;meep007&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// output: meep007: Meeeep { wtf_level: 10000, }&lt;/span&gt;

&lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"meep007: {:#?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;meep007&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// output:&lt;/span&gt;
&lt;span class="c1"&gt;// meep007: Meeeep {&lt;/span&gt;
&lt;span class="c1"&gt;//   wtf_level: 10000,&lt;/span&gt;
&lt;span class="c1"&gt;// }&lt;/span&gt;

&lt;span class="nd"&gt;dbg!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;meep007&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// output:&lt;/span&gt;
&lt;span class="c1"&gt;// [src/main.rs:20] &amp;amp;meep007: Meeeep {&lt;/span&gt;
&lt;span class="c1"&gt;//   wtf_level: 10000,&lt;/span&gt;
&lt;span class="c1"&gt;// }&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;_miau&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Meeeep&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;wtf_level&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nd"&gt;dbg!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;meep007&lt;/span&gt;&lt;span class="py"&gt;.wtf_level&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;9086&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// output:&lt;/span&gt;
&lt;span class="c1"&gt;// [src/main.rs:25] -10 * meep007.wtf_level / 9086 = -11&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Methods
&lt;/h3&gt;

&lt;p&gt;The difference between a function and a method is that, a method is part of a struct, so it is defined on the context of that struct.&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="c1"&gt;// how to create a method&lt;/span&gt;
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;say_guau&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{} says FOLOOOOWWW MEEEEEE!!!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Josefina"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;bonnie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Bonnie"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;death&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"dog"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;say_guau&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bonnie&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;IMPORTANT!!!&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;&amp;amp;self&lt;/code&gt; means a variable called &lt;code&gt;self&lt;/code&gt; of type &lt;code&gt;Self&lt;/code&gt;. And what is self?? Well... I think it describes its self uwu, is a reference to an instance of type (in this case) Animal.&lt;/p&gt;

&lt;p&gt;If you read the &lt;a href="https://doc.rust-lang.org/book/ch05-03-method-syntax.html#wheres-the---operator"&gt;docs&lt;/a&gt;, you will see that it talks about &lt;code&gt;-&amp;gt;&lt;/code&gt; which I am not going to cover. Sorry UwU&lt;/p&gt;

&lt;p&gt;To end with structs, you can use multiple &lt;code&gt;impl&lt;/code&gt; if you want, but I don't see why you would use two &lt;code&gt;impl&lt;/code&gt; for the same struct when you can have everything in one.&lt;/p&gt;
&lt;h2&gt;
  
  
  Enums 🤪
&lt;/h2&gt;

&lt;p&gt;At this point, I am tired, I want to sleep and have dinner :v butttt I am going to finish the post. So:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/0A477fjlRXL1n2LVgJ/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/0A477fjlRXL1n2LVgJ/giphy.gif" alt="go" width="400" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enums are as you can think of, enumerators, this means that you have a set of values that can be matched. Also enums can have methods like structs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;AnimalType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;DOG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;CAT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;LION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;IDK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;AnimalType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;wtf_is_this_shit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tricky tricky lemons q-easy!!! {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;AnimalType&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;IDK&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;owo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;AnimalType&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;DOG&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;owo&lt;/span&gt;&lt;span class="nf"&gt;.wtf_is_this_shit&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can specify the type of a field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;AnimalType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;DOG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;CAT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;LION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;IDK&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the book there is an explanation about &lt;a href="https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html#the-option-enum-and-its-advantages-over-null-values"&gt;null values&lt;/a&gt; which I recommend reading.&lt;/p&gt;

&lt;p&gt;But what it says it that in Rust you cannot have null values never!! There is an enum &lt;code&gt;Option&amp;lt;T&amp;gt;&lt;/code&gt; being T a variable type which is used to handle not havind null values. This enum has &lt;code&gt;None&lt;/code&gt;, and &lt;code&gt;Some(T)&lt;/code&gt;. Also T is used for generalization, we will see it on another post.&lt;/p&gt;

&lt;p&gt;Take in consideration that you cannos operate with a variable which is &lt;code&gt;Option&amp;lt;T&amp;gt;&lt;/code&gt; and a &lt;code&gt;T&lt;/code&gt; variable type. There are ways to convert &lt;code&gt;Option&amp;lt;T&amp;gt;&lt;/code&gt; on &lt;code&gt;T&lt;/code&gt;. &lt;a href="https://doc.rust-lang.org/std/option/enum.Option.html"&gt;docs&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;some_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;some_number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Will fail because Option&amp;lt;i32&amp;gt; != i32&lt;/span&gt;

&lt;span class="c1"&gt;// using None&lt;/span&gt;
&lt;span class="c1"&gt;// we are saying that it will be an Option which type is String but we don't know its value right now.&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;some_value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using match with enums. &lt;code&gt;match&lt;/code&gt; is a useful and important keyword for enum, becuase is the way of having multiple actions depending on which one is true.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Drink&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;WATER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;BEER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;COKE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;WINE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="nn"&gt;Drink&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;WATER&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;Drink&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;WATER&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Nice :D"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nn"&gt;Drink&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;BEER&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"IDIOT^4"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nn"&gt;Drink&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;COKE&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"IDIOT^10"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nn"&gt;Drink&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;WINE&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"IDIOT^0"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference between match and if, is that "if" needs to have a boolean condition.&lt;br&gt;
When match is executes, it matches the value of the code associated with the pattern given, if the pattern does not match the value, it continues to the next arm, otherwise it executes what is on tje right side of "=&amp;gt;" (the arm).&lt;/p&gt;

&lt;p&gt;Match patterns are evaluated in order!!!! So be caredul when using the "catch-all" pattern.&lt;/p&gt;

&lt;p&gt;If you need to execute multiple statements, use &lt;code&gt;{}&lt;/code&gt; (curly braces).&lt;/p&gt;

&lt;p&gt;The code related with each arm (option) is an expression, and if you remember, that means that it "returns" that value.&lt;/p&gt;

&lt;p&gt;Using match with &lt;code&gt;Option&amp;lt;T&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// docs example&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;plus_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nb"&gt;None&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;five&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;six&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;plus_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;five&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// we will get 6 because Some(i) == Some(5)&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;none&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;plus_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// We will get None, because None == None&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case you need a pattern like an "else", because you don't need to check for 1000 cases that are the same code, you use the &lt;strong&gt;catch-all&lt;/strong&gt; patterns. You can use or not the value of the "catch-all" pattern, lets see:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="c1"&gt;// handling catch-all with a variable to use it, becuase be want&lt;/span&gt;
&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;dice_roll&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"haha"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hehe"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="n"&gt;other&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is s* {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// not handling cath-all&lt;/span&gt;
&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;dice_roll&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"haha"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hehe"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly there is something called &lt;code&gt;if let&lt;/code&gt; that is used when we want to match only one pattern and we don't care about them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// docs example&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;config_max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3u8&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;config_max&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The maximum is configured to be {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// equals&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;config_max&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The maximum is configured to be {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;I think you can use &lt;code&gt;if let&lt;/code&gt; when you have a huge enum with a lot of things and you just need to find one pattern and with the other don't need to do anything.&lt;/p&gt;

&lt;p&gt;So this is all... for now I wish you a good day!!&lt;/p&gt;




&lt;h2&gt;
  
  
  Follow me!
&lt;/h2&gt;

&lt;p&gt;🐦 &lt;a href="https://twitter.com/blit12_"&gt;Twitter&lt;/a&gt;&lt;br&gt;
🐙 &lt;a href="https://github.com/blitty-codes"&gt;GitHub&lt;/a&gt;&lt;br&gt;
👥 &lt;a href="https://www.linkedin.com/in/blitty/"&gt;LinkdIn&lt;/a&gt;&lt;/p&gt;




</description>
      <category>rust</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Rustlings IV (Ownership)</title>
      <dc:creator>Blitty</dc:creator>
      <pubDate>Mon, 31 Jan 2022 16:10:04 +0000</pubDate>
      <link>https://dev.to/blitty/rustlings-iv-ownership-498h</link>
      <guid>https://dev.to/blitty/rustlings-iv-ownership-498h</guid>
      <description>&lt;p&gt;This is the most difficult part in my opinion. Because is totaly different from other languages like C. And is kind of interesting too.&lt;/p&gt;

&lt;p&gt;So...&lt;br&gt;
&lt;a href="https://i.giphy.com/media/QvwCVnX9DWdlHCnix5/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/QvwCVnX9DWdlHCnix5/giphy.gif" alt="lets do it" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First of all, everything I write here can be found in the &lt;a href="https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html"&gt;docs&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  OwnerShip 👀
&lt;/h2&gt;

&lt;p&gt;What is &lt;strong&gt;ownership&lt;/strong&gt;?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Ownership is a set of rules that governs how a Rust program manages memory.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I think the definition is easy, also as you will see, this "rules" prevents us from using the debugger too much which is cool because u've controlled "stupid" things before running!!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html"&gt;Docs&lt;/a&gt; talks about &lt;strong&gt;Heap&lt;/strong&gt; and &lt;strong&gt;Stack&lt;/strong&gt; which are important!! I am going to talk a little about them.&lt;br&gt;
As u should know both, the stack and the heap are data structures.&lt;/p&gt;

&lt;p&gt;First the &lt;strong&gt;stack&lt;/strong&gt;. What is it?&lt;/p&gt;

&lt;p&gt;Well the name says everything, a stack is a data structure which can either &lt;strong&gt;pop&lt;/strong&gt; or &lt;strong&gt;push&lt;/strong&gt;. When u push a new value, it is stacked at the top and when u pop a value, it removes the top value. You can only access the top value, so to get the first value you need to pop everything that is over it.&lt;/p&gt;

&lt;p&gt;Something like this:&lt;br&gt;
&lt;a href="https://i.giphy.com/media/1TSHTZvHoblaCLm6vM/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/1TSHTZvHoblaCLm6vM/giphy.gif" alt="stack like" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why do we need to know about it??&lt;br&gt;
Well... because things like, when you call a function, its parameters and local variables are pushed to the stack. When finished they are poped.&lt;/p&gt;

&lt;p&gt;Maybe you don't see why you need this, but is not "you need or not". It is done for simplicity.&lt;/p&gt;

&lt;p&gt;If you have had program in assembler, you can understand why we need it. You would have used it to push a value you will need later, but we don't do this in rust.&lt;/p&gt;

&lt;p&gt;Also each process has its own stack and heap at run time.&lt;/p&gt;

&lt;p&gt;Okey, hope everything is:&lt;br&gt;
&lt;a href="https://i.giphy.com/media/xT9DPIZXsgAYE3SNvW/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/xT9DPIZXsgAYE3SNvW/giphy.gif" alt="okey" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now... what is the &lt;strong&gt;heap&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;As we said earlier, it is a data structure, so we can use it to store "values". It is less efficient than stack for some things, because when you create a new varible, you need to search for a space that is not being used to store that value.&lt;/p&gt;

&lt;p&gt;So like docs says "is a little messy", and when you need to get a value, you have to follow the pointer. It is unefficient but, another point of view, it is good because is much more flexible than the stack.&lt;/p&gt;

&lt;p&gt;Also the heap is used for dynamic allocating.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap"&gt;More info about Heap and Stack&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/NHUONhmbo448/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/NHUONhmbo448/giphy.gif" alt="Continue" width="500" height="414"&gt;&lt;/a&gt;&lt;br&gt;
(I don't really drink coffe...)&lt;/p&gt;

&lt;p&gt;This are the &lt;strong&gt;owner ship rules&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each value in Rust has a variable that’s called its owner.&lt;/li&gt;
&lt;li&gt;There can only be one owner at a time.&lt;/li&gt;
&lt;li&gt;When the owner goes out of scope, the value will be dropped.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Remember them because you will need it uwu&lt;/p&gt;

&lt;p&gt;When we are doing --&amp;gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we are creating something like this --&amp;gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ptr&lt;/td&gt;
&lt;td&gt;-&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;len&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;capacity&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;index&lt;/th&gt;
&lt;th&gt;value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;-&amp;gt; 0&lt;/td&gt;
&lt;td&gt;h&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;e&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;l&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;l&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;o&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;(ptr points to index 0)&lt;/p&gt;

&lt;p&gt;If you come from C, you know that you are able to allocate dynamic memory with &lt;code&gt;malloc, calloc, etc&lt;/code&gt; but you are responsible for "freeing" that memory, and you use &lt;code&gt;free&lt;/code&gt;. Here comes one thing, if you allocate memory and also make a reference to it (a pointer), when you free that pointer, you are "freeing" that memory which is pointing. You have to be careful with this because if you free the variable (not the pointer), you will be "freeing" something that is already free, so you will collapse the program.&lt;/p&gt;

&lt;p&gt;So it is a bit difficult because you are, maybe wasting a lot of memory or you are just de-allocating memory everywhere, so it's kind of difficult and it takes experience. But once you get it, everything is okey.... I think....&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// test.c&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d - %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d - %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// error&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This is the output:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l6fkQNyB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/703hv6e82o67iz6lsi5s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l6fkQNyB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/703hv6e82o67iz6lsi5s.png" alt="test.c solve" width="422" height="122"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also you can try this code on &lt;a href="https://replit.com/languages/c"&gt;Repl.it&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I think this is clear enough.&lt;br&gt;
Now lets talk about languages like Java that has a Garbage Collector (GC) which free the memory when that part of memory is not used. Of course having a GC is okey (?) if you don't want to play with memory and make your prorgam explode. But... meh.&lt;/p&gt;

&lt;p&gt;What &lt;strong&gt;Rust&lt;/strong&gt; does is simply de-allocate that memory that has been used when the scope has end. A scope is, for example, a function, or the main, as well coding blocks.&lt;/p&gt;

&lt;p&gt;If you don't know what a block is, here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// start of block and a new scope&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// end of the scope, so x is being "free" (de-allocated)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need to take that in consideration that. In depth, what Rust does is call a function &lt;a href="https://doc.rust-lang.org/std/ops/trait.Drop.html#tymethod.drop"&gt;drop&lt;/a&gt; which basically de-allocates memory.&lt;/p&gt;

&lt;p&gt;In my opinion, this approach is much better because you just have to control when you need that value from the scope.&lt;/p&gt;

&lt;p&gt;Remember the c code whe saw later? Well, now you will see what Rust do if you make something similar, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rust compiler will tell you:&lt;br&gt;
&lt;a href="https://i.giphy.com/media/TIWYqtzg5wCCtc10Bi/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/TIWYqtzg5wCCtc10Bi/giphy.gif" alt="Rust says" width="480" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because in Rust you cannot have two variables which points to the same "thing" unless they are integers, booleans, etc, primitives.&lt;/p&gt;

&lt;p&gt;This is better explained in the book:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you’ve heard the terms shallow copy and deep copy while working with other languages, the concept of copying the pointer, length, and capacity without copying the data probably sounds like making a shallow copy. But because Rust also invalidates the first variable, instead of calling it a shallow copy, it’s known as a move.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means that our x will be "disabled". And I think this is cool because it prevents us to make junk code xd.&lt;/p&gt;

&lt;p&gt;But if you really need a copy of it, you can use &lt;a href="https://doc.rust-lang.org/std/clone/index.html"&gt;clone&lt;/a&gt;. I think the docs are okey, so not gonna explain anything.&lt;/p&gt;

&lt;p&gt;OOOOOHHHH ALSO you can "copy" tuples whiout the "clone" method, when it is formed of primitives, like we said earlier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// implicit Copy&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// no implicit Copy because of String&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ownership in functions should be easy to understand with what I have explained, have a &lt;a href="https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html#ownership-and-functions"&gt;read&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;em&gt;Borrowing&lt;/em&gt; is the action of creating a Reference. Becuase once you get that reference, you return it.&lt;/p&gt;

&lt;p&gt;When you "borrow" a variable, you cannot change its value. Unless is a "mutable borrow".&lt;/p&gt;

&lt;p&gt;According to the book&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Unlike a pointer, a reference is guaranteed to point to a valid value of a particular type.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// example from book&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculate_length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The length of '{}' is {}."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;calculate_length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// s is a reference&lt;/span&gt;
  &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#references-and-borrowing"&gt;Look at the image&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By using references, you don't get the &lt;strong&gt;ownership&lt;/strong&gt; of that variable, so it is cool. Less mess.&lt;/p&gt;

&lt;p&gt;You can have mutable references, but you have to be careful, because you can only have one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// creating a mutable reference&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;r1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we said that you cannot have 2 mutable references, that is not quite true. You can have more than one mutable reference, but they have to be either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Different scopes&lt;/li&gt;
&lt;li&gt;The mutable reference is not used after the next mutable reference is created
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// this is valid&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;r1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;r1&lt;/span&gt;&lt;span class="nf"&gt;.push_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;", world"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;r2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;r2&lt;/span&gt;&lt;span class="nf"&gt;.push_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"!!!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// this is invalid&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;r1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;r2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;r1&lt;/span&gt;&lt;span class="nf"&gt;.push_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;", world"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;r2&lt;/span&gt;&lt;span class="nf"&gt;.push_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;", world"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}, {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maybe this is weird and you don't get it. Rust says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The benefit of having this restriction is that Rust can prevent data races at compile time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And a data race is like race conditions when using paralelism. A data race can appear:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Two or more pointers access the same data at the same time.&lt;/li&gt;
&lt;li&gt;At least one of the pointers is being used to write to the data.&lt;/li&gt;
&lt;li&gt;There’s no mechanism being used to synchronize access to the data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can have a lot of inmutable references. BUTTTT be careful, when using references, if you have inmutable, you cannot have mutable ones on the same scope. The answer to what you are thinking is that you are not able to change the data while someone is reading it.&lt;/p&gt;

&lt;p&gt;It is like when using databases. There is always (if it is not well done) a way of having an error value. Imagine that &lt;strong&gt;Paula&lt;/strong&gt; wants to read, and indeed is reading but then suddenly at the same time &lt;strong&gt;Jose&lt;/strong&gt; is writting, the value has change, so &lt;strong&gt;Paula&lt;/strong&gt; has an obsolete value, which is not cool.&lt;/p&gt;

&lt;p&gt;(Is it clear? I wish it is)&lt;/p&gt;

&lt;p&gt;The least: &lt;strong&gt;Dangling References&lt;/strong&gt;. A Dangling reference, is something like a value being lost when it shouldn't because you want that value. This happens when you want to return a value from a scope, but not actually returning the value, instead you are returning the reference. As we said earlier, when a scope ends, the variables inside it are "dropped" (removed), so you cannot return a reference to something that is going to be dropped.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;reference_to_nothing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dangle&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// F&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;dangle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// this is better :D&lt;/span&gt;
&lt;span class="c1"&gt;// you are retturning the value not the reference, so nice.&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;no_dangle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;If you have code in python, this is going to be familiar. Types of slices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;String literals&lt;/li&gt;
&lt;li&gt;Arrays&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First of all, we need to clarify that slices does NOT have Ownership, so you can do almost whatever. If you remember, we couldn't have more than 1 reference to something. But with slices, we can have lots of references!!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello world"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;word0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// from index 0 to index 6&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;word1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// from 0 to the length&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;word1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// from index 0 to index 3&lt;/span&gt;

  &lt;span class="c1"&gt;// this is bad&lt;/span&gt;
  &lt;span class="c1"&gt;// .clear uses a mutable reference, and we have inmutable references&lt;/span&gt;
  &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="nf"&gt;.clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// error!&lt;/span&gt;

  &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"the first word is: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;word0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The concepts of ownership, borrowing, and slices ensure memory safety in Rust programs at compile time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/7yojoQtevjOCI/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/7yojoQtevjOCI/giphy.gif" alt="Done" width="480" height="342"&gt;&lt;/a&gt;&lt;br&gt;
Whish you understood everything!! Because is one of the most fundamental parts in my opinion and I am still learning it uwu&lt;/p&gt;




&lt;h2&gt;
  
  
  Follow me!
&lt;/h2&gt;

&lt;p&gt;🐦 &lt;a href="https://twitter.com/blit12_"&gt;Twitter&lt;/a&gt;&lt;br&gt;
🐙 &lt;a href="https://github.com/blitty-codes"&gt;GitHub&lt;/a&gt;&lt;br&gt;
👥 &lt;a href="https://www.linkedin.com/in/blitty/"&gt;LinkdIn&lt;/a&gt;&lt;/p&gt;




</description>
      <category>rust</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>RustLings III (conditions and convert)</title>
      <dc:creator>Blitty</dc:creator>
      <pubDate>Sun, 30 Jan 2022 15:44:58 +0000</pubDate>
      <link>https://dev.to/blitty/rustlings-iii-conditions-and-convert-57ij</link>
      <guid>https://dev.to/blitty/rustlings-iii-conditions-and-convert-57ij</guid>
      <description>&lt;p&gt;Bonjour!!!&lt;/p&gt;

&lt;p&gt;I am back... it's been a while, but lets continue!!!&lt;/p&gt;

&lt;p&gt;Next thing in rustlings is control flow which is very easy but if u come from C maybe a bit tricky. But nothing difficult!!!&lt;/p&gt;

&lt;p&gt;The keywords are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// first condition&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// other condition&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// none condition is true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;nothing from another world. But you need to know that, &lt;code&gt;&amp;lt;condition&amp;gt;&lt;/code&gt; has to be a &lt;strong&gt;bool&lt;/strong&gt;, otherwise you will get an error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ternary conditions&lt;/strong&gt; are weird... I like more C.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need those &lt;code&gt;{}&lt;/code&gt; otherwise the compiler will say something like&lt;br&gt;
&lt;a href="https://i.giphy.com/media/4cQSQYz0a9x9S/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/4cQSQYz0a9x9S/giphy.gif" alt="Not cool" width="460" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next thing I had to do was a quiz, very easy you don't miss nothing, except for one thing I have discovered!!&lt;/p&gt;

&lt;p&gt;When u are using, say i8 and u want to return or you need it to be i32. There are two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;into&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="nf"&gt;.into&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;from&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// u don't even need to specify here xd&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;from&lt;/strong&gt; is recommended by the &lt;a href="https://doc.rust-lang.org/std/convert/trait.From.html"&gt;docs&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Follow me!
&lt;/h2&gt;

&lt;p&gt;(I liked more the red bird :_()&lt;br&gt;
🐦 &lt;a href="https://twitter.com/blit12_"&gt;Twitter&lt;/a&gt;&lt;br&gt;
🐙 &lt;a href="https://github.com/blitty-codes"&gt;GitHub&lt;/a&gt;&lt;br&gt;
👥 &lt;a href="https://www.linkedin.com/in/blitty/"&gt;LinkdIn&lt;/a&gt;&lt;/p&gt;




</description>
      <category>rust</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>RustLings II</title>
      <dc:creator>Blitty</dc:creator>
      <pubDate>Fri, 01 Oct 2021 18:05:18 +0000</pubDate>
      <link>https://dev.to/blitty/rustlings-ii-3dk4</link>
      <guid>https://dev.to/blitty/rustlings-ii-3dk4</guid>
      <description>&lt;p&gt;Willkommen!!&lt;/p&gt;

&lt;p&gt;Sorry is German :)&lt;/p&gt;

&lt;p&gt;Re-writing the post, I've learned that rust is &lt;strong&gt;expression-based&lt;/strong&gt; language HAHAHAH and I was like a friend does "AAAJJJAAAAAMMMMMMM...." that is a well I don't know how to translate it.&lt;/p&gt;

&lt;p&gt;So what is an expression-based language??? Well I searched it on DuckDuckGo ;)&lt;/p&gt;

&lt;p&gt;and for me the best result was the wikipedia page hahahha, yeah so this is what it says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An expression-oriented programming language is a programming language in which every (or nearly every) construction is an expression and thus yields a value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But... so this means that everything or nearly every construction is an expression and it produces a value. Wish you understood it :V&lt;/p&gt;

&lt;p&gt;Rust is made of Statements and Expressions&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Statements&lt;/em&gt; are &lt;em&gt;Instructions&lt;/em&gt; that perform some action and do not return a value. &lt;em&gt;Expressions&lt;/em&gt; evaluate to a resulting value. (from docs)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Maybe with an example???&lt;br&gt;
So in the book (that are the docs) it has this example:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;So here we would say that &lt;code&gt;x=6&lt;/code&gt; and &lt;code&gt;y=6&lt;/code&gt;, no?&lt;/p&gt;

&lt;p&gt;Well this is not true with rust and there is an explanation... the errors in terminal... Ok no HAHAHAHHAHA&lt;br&gt;
I was kidding.&lt;/p&gt;

&lt;p&gt;You cannot do this in Rust and the reason is because the &lt;code&gt;let&lt;/code&gt; keyword is a Statement, and as the book says, a Statement does not return a value!!!!&lt;br&gt;
So it is impossible to do that in Rust!!&lt;/p&gt;

&lt;p&gt;Then we have Expression, as the book says, those "evaluate to a resulting value", we could say that is something like "return a value". So... how can we do the last code and add 5 to work???&lt;br&gt;
We can use &lt;code&gt;blocks&lt;/code&gt;, this are statements you can have a piece of code, and return a value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="c1"&gt;// returns the value (also is an expression)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// if you print "y", you get 5 not 10, go and search why :D&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, &lt;em&gt;Expressions DO NOT INCLUDE SEMICOLONS AT THE END&lt;/em&gt;, and this is important.&lt;br&gt;
Other Expressions -&amp;gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;calling a function&lt;/li&gt;
&lt;li&gt;calling a macro&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let x = 6&lt;/code&gt;, here &lt;code&gt;6&lt;/code&gt; is an Expression, because &lt;code&gt;6&lt;/code&gt; evaluates to &lt;code&gt;6&lt;/code&gt; (AHAAHHAHAHAHAHAHA, love this explanations from the doc)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PD1: I think there are more, but I cannot think of them right now, those are examples from the docs, but everything that can evaluate to a value, is an Expression, otherwise an Instruction or Statement.&lt;/p&gt;

&lt;p&gt;So that was the difficult part of this chapter, wish you understood everything and I wrote it well. Anything tell me :D&lt;/p&gt;

&lt;p&gt;Here you have the easy part uwu&lt;/p&gt;

&lt;p&gt;You create a function like with &lt;code&gt;fn&lt;/code&gt; keyword&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;bonjour&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// If you've watched Modern Family, you will understand it :D&lt;/span&gt;
  &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Yelooouuuhhh!!!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if the function does not specify the return data type, then it returns nothing. You specify a function to return something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;example&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now as we are not in python, we need to specify the type of the parameter (&lt;a href="https://doc.rust-lang.org/book/ch03-02-data-types.html"&gt;doc&lt;/a&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;new_user&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"New user {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In rust you can return the value in 2 ways.&lt;/p&gt;

&lt;p&gt;1 -&amp;gt; without the &lt;code&gt;return&lt;/code&gt; keyword&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;is_even&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And.... why does this work??? Well... very easy, you are specifying the type you want to return and you have no &lt;code&gt;;&lt;/code&gt; in the instruction. Which means, as we said later, that this is an Expression so it evaluates a value.&lt;/p&gt;

&lt;p&gt;Another example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;uwu&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;-&amp;gt; using &lt;code&gt;return&lt;/code&gt;
You just use the keyword and &lt;code&gt;;&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;is_even&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PD: I have read the docs like 10 times to understand that jajajaj&lt;br&gt;
&lt;a href="https://i.giphy.com/media/NFA61GS9qKZ68/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/NFA61GS9qKZ68/giphy.gif" alt="reading" width="500" height="281"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Follow me!
&lt;/h2&gt;

&lt;p&gt;(I liked more the red bird :_()&lt;br&gt;
🐦 &lt;a href="https://twitter.com/blit12_"&gt;Twitter&lt;/a&gt;&lt;br&gt;
🐙 &lt;a href="https://github.com/blitty-codes"&gt;GitHub&lt;/a&gt;&lt;br&gt;
👥 &lt;a href="https://www.linkedin.com/in/blitty/"&gt;LinkdIn&lt;/a&gt;&lt;/p&gt;




</description>
      <category>functional</category>
      <category>rust</category>
      <category>learning</category>
    </item>
    <item>
      <title>RustLings I</title>
      <dc:creator>Blitty</dc:creator>
      <pubDate>Sun, 26 Sep 2021 23:08:09 +0000</pubDate>
      <link>https://dev.to/blitty/rustlings-i-486n</link>
      <guid>https://dev.to/blitty/rustlings-i-486n</guid>
      <description>&lt;p&gt;Hello again! I had a nice sleep :))&lt;/p&gt;

&lt;p&gt;Today we'll learn how to use variables in Rust :D&lt;/p&gt;

&lt;p&gt;Oh yeah! Something you need to know is that all exercises of variables have been done by my little sister of 10 years xd&lt;/p&gt;

&lt;p&gt;Just for you to know SJSJJSSJJSJSJ&lt;/p&gt;

&lt;p&gt;In the first file of &lt;a href="https://github.com/rust-lang/rustlings"&gt;rustlings&lt;/a&gt; we get an error that simply means, &lt;code&gt;x does not exist&lt;/code&gt; and that's because we haven't defined the variable.&lt;/p&gt;

&lt;p&gt;So... the way to define a variable is using the keyword &lt;code&gt;let&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Something you need to know is that when you create a variable it has to be defined, otherwise you will get an error from the Compile.&lt;/p&gt;

&lt;p&gt;OH YES!!! Another important thing is that every variable you create is &lt;code&gt;inmutable&lt;/code&gt;. What does it mean? That you cannot change the value of the variable. To make a variable &lt;code&gt;mutable&lt;/code&gt; you have to use the keyword &lt;code&gt;mut&lt;/code&gt; before the name of the variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Something you can do is telling a variable what type it is. (You have to do this in the second variable file)&lt;/p&gt;

&lt;p&gt;So if you go to the docs -&amp;gt; &lt;a href="https://doc.rust-lang.org/book/ch03-02-data-types.html"&gt;https://doc.rust-lang.org/book/ch03-02-data-types.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go and read :)&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;That is an example :D&lt;/p&gt;

&lt;p&gt;Now an interesting "tool" is that you can change the type of a variable, is what they call "shadowed".&lt;/p&gt;

&lt;p&gt;I think you will understand if I give you an example -&amp;gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"OH YEAH I AM A STRING"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yeah is... strange??? I mean, you change the type of the variable just using the &lt;code&gt;let&lt;/code&gt; keyword and the same name of the variable. And is super weird (for me at least) but I think is useful, but IDK, we'll see.&lt;/p&gt;

&lt;p&gt;Also it is called &lt;em&gt;shadowed&lt;/em&gt; because is like the first initialization of the variable gets "shadowed" by the last one.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;constants&lt;/em&gt; in Rust... you have to specify the type, otherwise it won't work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;PI&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.14&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the best for the end... Why the f* I have to use &lt;code&gt;mut&lt;/code&gt; if I have constants???&lt;/p&gt;

&lt;p&gt;Well... let me read the docs...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/8dYmJ6Buo3lYY/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/8dYmJ6Buo3lYY/giphy.gif" alt="Reading" width="282" height="207"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You cannot use &lt;code&gt;mut&lt;/code&gt; with &lt;code&gt;const&lt;/code&gt; I think is something obvious.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;const&lt;/code&gt; needs to have a type, &lt;code&gt;let&lt;/code&gt; no&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;const&lt;/code&gt; can be used in any scope, including &lt;code&gt;global&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let&lt;/code&gt; can get the result of a function or something like that, but &lt;code&gt;const&lt;/code&gt; can't, it has to be a value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Something like this, I think it convinced me, I mean, having variables that are immutable is "top" you can do a lot of things with it, some times we have variables that never change their value, and this is like a "plus" for rust, I cannot think about any example now, sry.&lt;/p&gt;

&lt;p&gt;So this is all for today, I wish you a nice sleep or end of reading, or whatever you want.&lt;/p&gt;

&lt;p&gt;Bye.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/xT8qBvH1pAhtfSx52U/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/xT8qBvH1pAhtfSx52U/giphy.gif" alt="Sleeping" width="260" height="146"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Follow me!
&lt;/h2&gt;

&lt;p&gt;(I liked more the red bird :_()&lt;br&gt;
🐦 &lt;a href="https://twitter.com/blit12_"&gt;Twitter&lt;/a&gt;&lt;br&gt;
🐙 &lt;a href="https://github.com/blitty-codes"&gt;GitHub&lt;/a&gt;&lt;br&gt;
👥 &lt;a href="https://www.linkedin.com/in/blitty/"&gt;LinkdIn&lt;/a&gt;&lt;/p&gt;




</description>
      <category>rust</category>
      <category>learning</category>
    </item>
    <item>
      <title>Learning Rust with rustlings</title>
      <dc:creator>Blitty</dc:creator>
      <pubDate>Sun, 26 Sep 2021 01:03:10 +0000</pubDate>
      <link>https://dev.to/blitty/learning-rust-with-rustlings-40ji</link>
      <guid>https://dev.to/blitty/learning-rust-with-rustlings-40ji</guid>
      <description>&lt;p&gt;So.... first thing is first&lt;/p&gt;

&lt;p&gt;Hello again!!!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/l0MYGb1LuZ3n7dRnO/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l0MYGb1LuZ3n7dRnO/giphy.gif" alt="Welcome pal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I am writing and learning while I am in a call on discord :)&lt;/p&gt;

&lt;p&gt;So what you would be wondering now:&lt;br&gt;
Q: Why Rust (lang)? Do you love your self?&lt;br&gt;
A: Why not? And no... I just UwU&lt;/p&gt;

&lt;p&gt;Now I have answered your questions, I am going to learn Rust, the programming language not the game OwO&lt;/p&gt;

&lt;p&gt;So, I am learning the syntax here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/rust-lang/rustlings/" rel="noopener noreferrer"&gt;https://github.com/rust-lang/rustlings/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can install it or you use it in repl.it, read the README :D&lt;/p&gt;

&lt;p&gt;Next... when you open the repo, you see something like this&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpm611m1vy5ow20jnqkgo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpm611m1vy5ow20jnqkgo.png" alt="Files and folders"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I've read the REAMD.md, so I know what and where things are jsjsjssjsjsjsjs&lt;/p&gt;

&lt;p&gt;And the first thing we have to do is type &lt;code&gt;rustlings&lt;/code&gt; in the terminal, if you get an error, probably you don't have the binaries in the &lt;em&gt;PATH&lt;/em&gt;, so be careful!!!&lt;/p&gt;

&lt;p&gt;So with that we get a little of information, the real problem starts with &lt;code&gt;rustlings watch&lt;/code&gt; that we will use, since it makes us follow everything, from beginning.&lt;/p&gt;

&lt;p&gt;Each time we fix (or maybe not, and we just bug it more :D), the command will display for us the error or a "Congratulation" for doing it well.&lt;/p&gt;

&lt;p&gt;And this is everything for today :D&lt;/p&gt;

&lt;p&gt;Have a nice sleep. With love B&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/ReycznqpTfwuwefOl4/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ReycznqpTfwuwefOl4/giphy.gif" alt="Good night"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Follow me!
&lt;/h2&gt;

&lt;p&gt;(I liked more the red bird :_()&lt;br&gt;
🐦 &lt;a href="https://twitter.com/blit12_" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;br&gt;
🐙 &lt;a href="https://github.com/blitty-codes" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;br&gt;
👥 &lt;a href="https://www.linkedin.com/in/blitty/" rel="noopener noreferrer"&gt;LinkdIn&lt;/a&gt;&lt;/p&gt;




</description>
      <category>rust</category>
      <category>learning</category>
    </item>
    <item>
      <title>Having the rick power!!! Pt.4</title>
      <dc:creator>Blitty</dc:creator>
      <pubDate>Mon, 09 Aug 2021 17:06:55 +0000</pubDate>
      <link>https://dev.to/blitty/having-the-rick-power-pt-4-6gd</link>
      <guid>https://dev.to/blitty/having-the-rick-power-pt-4-6gd</guid>
      <description>&lt;p&gt;WOWOWOWOWWO a lot of time since I posted!!!! Well... 4 days or so...???&lt;/p&gt;

&lt;p&gt;Don't know, but lets continue, I want to finish, I think this is going to be the last one. Or I'll try to make it the last one at least hahahahaha&lt;/p&gt;

&lt;p&gt;First thing is first, we need to do a little of recap, we did some routes, make some Apollo magic in the home page, so we have 5 random characters, also we have an ugly Errors page :)&lt;/p&gt;

&lt;p&gt;So, to finish we have to do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Search to work&lt;/li&gt;
&lt;li&gt;Button "more info" working&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don't know how I am gonna make this with apollo, 💩💩💩💩💩💩 (or maybe I know)&lt;/p&gt;

&lt;p&gt;Maybe we'll use &lt;code&gt;useContext&lt;/code&gt; because I want the results of the search bar in the &lt;code&gt;cardContainer&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
npm start

&lt;ol&gt;
&lt;li&gt;Fixing errors&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;

Continuing with Apollo

&lt;ol&gt;
&lt;li&gt;Show more button&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;Styling&lt;/li&gt;

&lt;li&gt;Uploading...&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/cdNSp4L5vCU7aQrYnV/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/cdNSp4L5vCU7aQrYnV/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  npm start &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;First thing we do... it 🤯🤯🤯🤯🤯 yeppp&lt;br&gt;
why? Because my computer decided to shut down before. So the changes were not fixed....&lt;/p&gt;
&lt;h3&gt;
  
  
  » Fixing errors &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;15 minutes later and realizing that we don't want &lt;code&gt;useContext&lt;/code&gt; because we don't want to share the global variable to be displayed (rendered) in another element. That's why we don't use &lt;code&gt;useContext&lt;/code&gt;, and that is why we want to pass the value as a prop, from one child to the other child, we do this using the "Father" element as a "Controller" of the value we want to pass.&lt;/p&gt;

&lt;p&gt;So first is first, we prepare the "father" element which is &lt;code&gt;Home&lt;/code&gt;, we make it use &lt;code&gt;useState&lt;/code&gt;, this way we can keep track and update the value easily.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Home.jsx&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setT&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nf"&gt;callBack &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;P&lt;/span&gt; &lt;span class="na"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;callBack&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;O&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we know that the element that changes the value of the element is &lt;code&gt;Search&lt;/code&gt; which modifies the value when the we submit or click the icon. This way we pass the prop &lt;code&gt;callBack&lt;/code&gt; which uses the function from the useState, &lt;code&gt;setT&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;callBack&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// to keep track of the value from the input, when changed&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
        &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;
        &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Use power to search..."&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;
        &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;callBack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Click
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way we have the new value when the button is pressed.&lt;/p&gt;

&lt;p&gt;Next and last step is to pass the value to the one which is going to use it for the query. So that we can search a character by Name.&lt;/p&gt;

&lt;p&gt;In this example we are not going to use it to query something, we are going to use it to display, is just an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;O&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that is everything, I think you have understand why we do this and why we don't use &lt;code&gt;useContext&lt;/code&gt;. If you haven't understand it well, I'll update the post!!! So comment!!!&lt;/p&gt;

&lt;h2&gt;
  
  
  Continuing with Apollo &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Apollo....&lt;br&gt;
&lt;a href="https://i.giphy.com/media/69mUSKBujnpgmxcqlg/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/69mUSKBujnpgmxcqlg/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;YUHUUUUUUU!! I love Apollo hahahhaha, I mean... is very easy and super powerful!!!&lt;/p&gt;

&lt;p&gt;In the component &lt;code&gt;cardContainer&lt;/code&gt;, we mostly use Apollo, so we are going to change the place of &lt;code&gt;ApolloProvider&lt;/code&gt; just to wrap &lt;code&gt;CardContainer&lt;/code&gt;, because nothing inside &lt;code&gt;Home.jsx&lt;/code&gt; is going to use Apollo as well, so there is no need to have it to wrap everything.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/26u4lOMA8JKSnL9Uk/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/26u4lOMA8JKSnL9Uk/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We had some errors in the queries we saved in &lt;code&gt;queries.js&lt;/code&gt;, those who use the variables, so the correct way to use variables inside a query with Apollo is this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getCharactersByName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gql&lt;/span&gt;&lt;span class="s2"&gt;`
  query getCharactersByName($charName: String!) {
    characters(filter: { name: $charName }) {
      results {
        id
      }
    }
  }
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with the variable name inside the query of the name, as an argument.&lt;/p&gt;

&lt;p&gt;So then, when we use it in the &lt;code&gt;cardContainer&lt;/code&gt;, we use Apollo to request the query, we use the &lt;code&gt;hook&lt;/code&gt; provided by the library called &lt;code&gt;useQuery&lt;/code&gt;. It has multiple parameters, the one that is required, the query we can also add options like "variables", "onError", "pollInterval" that fetches each time (the one you specify) the query and much more!!!&lt;/p&gt;

&lt;p&gt;What we want from the return are some states like &lt;code&gt;loading, error and data&lt;/code&gt;, this way we can display components depending on the state we have.&lt;/p&gt;

&lt;p&gt;An example with the query above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;Query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;variables&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;charName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rick&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  » "Show more" button &lt;a href=""&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;I am no sure how I want to do this one, because I thought about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;creating a new window&lt;/li&gt;
&lt;li&gt;a pop up&lt;/li&gt;
&lt;li&gt;a route&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I think I am going to do the 3rd one, because this way is easier, I just send the id of the character when the button is pressed. &lt;/p&gt;

&lt;p&gt;PRESS THE BUTTTONNNN!!!!!&lt;br&gt;
&lt;a href="https://i.giphy.com/media/gKr21u11F3hyRyJHqa/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/gKr21u11F3hyRyJHqa/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now lets continue&lt;/p&gt;

&lt;p&gt;For this we call &lt;code&gt;react-router-dom&lt;/code&gt; again, and we create a route which accepts a parameter, which we are going to get in the component who will fetch the data using a query.&lt;/p&gt;

&lt;p&gt;To create the route, is like when using &lt;a href="https://expressjs.com/es/" rel="noopener noreferrer"&gt;express&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// import CharInfo&lt;/span&gt;

&lt;span class="c1"&gt;// &amp;lt;Switch&amp;gt;&lt;/span&gt;
     &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/charInfo/:id"&lt;/span&gt; &lt;span class="na"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;CharInfo&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// &amp;lt;/Switch&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is everything, now using in &lt;code&gt;CharInfo&lt;/code&gt; a hook provided from &lt;code&gt;react-router-dom&lt;/code&gt; called &lt;code&gt;useParams&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This way we can intercept that param and fetch the data with apollo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;CharInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useParams&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now we are going to use the hook from &lt;code&gt;Apollo&lt;/code&gt; to use the query and the &lt;code&gt;id&lt;/code&gt; and fetch the data to display it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getFullDataByID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="na"&gt;variables&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of using the &lt;code&gt;ApolloProvider&lt;/code&gt;, we use the option from the hook &lt;code&gt;client&lt;/code&gt;, why we do it like this?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We don't have a root for the component &lt;code&gt;CharInfo&lt;/code&gt; because is a component for a Route. So we cannot use "ApolloProvider"&lt;/li&gt;
&lt;li&gt;I am not going to create a "root" component for that component, when I have the option of using that paramenter.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once we got this, we have everything!!! Now we just need to make CSS, which I'll do later (but you will see them in this post UwU)&lt;/p&gt;

&lt;p&gt;Before styling... I got an error while doing the basic web of the information from the character, and I got an "Internal Server Error" which was very weird (BTW I wasted like 15min with this error until I figured it out) and It all was because sometimes an attribute called "origin" was null, so this was causing all that error, which was not returning the data.... (Summary: An error in the Back)&lt;br&gt;
Now I have removed that prop, so that I get no error.&lt;/p&gt;

&lt;p&gt;[I am going to sleep, I am very tired... so... continue reading ;) and be happy!!]&lt;/p&gt;

&lt;h2&gt;
  
  
  Styling!! &lt;a href=""&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Omg... this is going to be tough... F*&lt;br&gt;
I hate starting, but when I have everything as I want, is like "OH MY GOOOOOODDDD, I LOVE IITTTT", then someone just come and says "What a F* shit, is that supposed to responsive?" :_(&lt;/p&gt;

&lt;p&gt;Don't hate okey?¿?¿? UwU&lt;br&gt;
Let's be loved and love :D&lt;/p&gt;

&lt;p&gt;Now, continuing with styling... I am not going to do another GIMP, sorry but don't feel like doing another :(&lt;/p&gt;

&lt;p&gt;BUTTTT I am going to use the FIREFOX TOOLSSSS TO MAKE THE CSS AND THEN CONVERT IT INTO TAILWINDCSS CLASSES!!!! OHHHHH YEAHHH BBYYYYY. Does that make any sense?? Don't think so... but I go fast as a fart like this 😎.&lt;br&gt;
(Why a fart? IDK, why not?)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/du6kQYcgBQE2t9wR3p/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/du6kQYcgBQE2t9wR3p/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I just want to show you what I have to deal with:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvk8ah04n6yu0vz3ihhgm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvk8ah04n6yu0vz3ihhgm.png" alt="Mobile progress"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you seen that vertical bar??? :') I think I am going to... have a good fun :')&lt;/p&gt;

&lt;p&gt;As I said, no GIMP, I am lazzy, but I'll write what I want to do, this way you know what is in my mind :O&lt;/p&gt;

&lt;p&gt;We have 3 sections&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Main part, fundamental information for the character:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;name&lt;/li&gt;
&lt;li&gt;status&lt;/li&gt;
&lt;li&gt;species&lt;/li&gt;
&lt;li&gt;type&lt;/li&gt;
&lt;li&gt;gender&lt;/li&gt;
&lt;li&gt;image&lt;/li&gt;
&lt;li&gt;created&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Episodes section, these are Episodes where the character has been.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;id&lt;/li&gt;
&lt;li&gt;name&lt;/li&gt;
&lt;li&gt;air_date&lt;/li&gt;
&lt;li&gt;episode&lt;/li&gt;
&lt;li&gt;created&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Of course is an Array, so we are going to create a new component &lt;code&gt;Episode&lt;/code&gt; to have everything better for my health :')&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Location part, here we have information about

&lt;ul&gt;
&lt;li&gt;id&lt;/li&gt;
&lt;li&gt;name&lt;/li&gt;
&lt;li&gt;type&lt;/li&gt;
&lt;li&gt;dimension&lt;/li&gt;
&lt;li&gt;residents&lt;/li&gt;
&lt;li&gt;created&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here we have a subsection, "residents", which is an Array &lt;br&gt;
  of other characters, I am going to do it a single row with &lt;br&gt;
  horizontal scroll (What do you think?? DON'T TELL ME!!! Is &lt;br&gt;
  a great idea, I know, you know, we don't really know xd) &lt;br&gt;
  Sometimes there are a lot of residents, so this might be a &lt;br&gt;
  problem, but as it renders like "fast and furious", is &lt;br&gt;
  going to be every single character of the array rendered :)&lt;br&gt;
  Also, we are going to &lt;em&gt;reuse&lt;/em&gt; the &lt;code&gt;Card&lt;/code&gt; component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Uploading... &lt;a href=""&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We can upload a react project to GitHub Pages. Using the module &lt;code&gt;gh-pages&lt;/code&gt;, we install it doing:&lt;br&gt;
&lt;code&gt;npm install --save-dev gh-pages&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;With it, we can upload it, so you can go and try it &lt;a href="https://blitty-codes.github.io/rick-power/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Tell me what else you want to learn, I'll make a series of it :D&lt;/p&gt;

&lt;p&gt;I want to learn more things!!!&lt;/p&gt;




&lt;h2&gt;
  
  
  Follow me or contact!
&lt;/h2&gt;

&lt;p&gt;(I liked more the red bird :_()&lt;br&gt;
🐦 &lt;a href="https://twitter.com/blit12_" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;br&gt;
🐙 &lt;a href="https://github.com/blitty-codes" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;br&gt;
👥 &lt;a href="https://www.linkedin.com/in/blitty/" rel="noopener noreferrer"&gt;LinkdIn&lt;/a&gt;&lt;/p&gt;




</description>
      <category>react</category>
      <category>webdev</category>
      <category>graphql</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Having the rick power!!! Pt.3</title>
      <dc:creator>Blitty</dc:creator>
      <pubDate>Sat, 31 Jul 2021 18:53:24 +0000</pubDate>
      <link>https://dev.to/blitty/having-the-rick-power-pt-3-2f0k</link>
      <guid>https://dev.to/blitty/having-the-rick-power-pt-3-2f0k</guid>
      <description>&lt;p&gt;Hello again!!! 🙌&lt;/p&gt;

&lt;p&gt;Here I am, I've just open vscode, and the first thing I did was styling the container of the cards, now is amazing how it is!! (Maybe you don't like it, but I DO :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;A bit of react-router&lt;/li&gt;
&lt;li&gt;
Starting with Apollo

&lt;ol&gt;
&lt;li&gt;A little explanation&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;Returning to the project&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  A bit of react-router &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Then I did 'the change', I converted &lt;code&gt;App.jsx&lt;/code&gt; into a Switch for Routes, using &lt;code&gt;react-router-dom&lt;/code&gt;, which I have neves used, but is super simple!!&lt;/p&gt;

&lt;p&gt;(Mini tutorial)&lt;br&gt;
To use &lt;strong&gt;React Router&lt;/strong&gt;, the first thing we have to do is install it :) of course... &lt;a href="https://reactrouter.com/web/guides/quick-start"&gt;docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install react-router-dom&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now that we have it installed, the first thing we have to do, is to say to React that we are using it!!! So we tell &lt;code&gt;index.js&lt;/code&gt; (the one that is god, because it controls everything) to have something called &lt;code&gt;BrowserRouter&lt;/code&gt;, which is like a &lt;code&gt;Route&lt;/code&gt; but a bit special, because is the one that keeps a history.&lt;/p&gt;

&lt;p&gt;Next step is to go to out &lt;code&gt;App.jsx&lt;/code&gt; where we used to have all of our things. But now we have it as a Router for Routes. Okey okey... if you have not understand me, we have a &lt;code&gt;Switch&lt;/code&gt; that has &lt;code&gt;Route&lt;/code&gt;s which redirect to the component you want. So we have something like this -&amp;gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/06k0m?module=/src/App.jsx"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;So if you try to go to another path that is not &lt;code&gt;/&lt;/code&gt; (home), you will be displayed &lt;code&gt;Error!!&lt;/code&gt;. And why? Because when we used the &lt;code&gt;Switch&lt;/code&gt;, we created a &lt;code&gt;Route&lt;/code&gt; which has no path, when a &lt;code&gt;Route&lt;/code&gt; has no path, it means that any other path will go to that one, so for example if we search: &lt;strong&gt;/blitty&lt;/strong&gt; we will have the same error, or &lt;code&gt;OwO&lt;/code&gt;, and so on.&lt;/p&gt;

&lt;p&gt;But how do we do, if for example we want &lt;strong&gt;/owo&lt;/strong&gt; to work?&lt;br&gt;
Well.. that's pretty simple, we just create a new &lt;code&gt;Route&lt;/code&gt; and we add the attribute &lt;code&gt;path="/owo"&lt;/code&gt;, also &lt;code&gt;component={Component}&lt;/code&gt;. And that's all!!! So we would have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Switch&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
   // other routes
   &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;exact&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/owo"&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Owo&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Switch&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And... what does &lt;code&gt;exact&lt;/code&gt; mean???&lt;br&gt;
It means that it will only match the &lt;em&gt;exact&lt;/em&gt; route, so for example, if we search for &lt;code&gt;/owo/uwu&lt;/code&gt; we wouldn't get rendered the component &lt;code&gt;Owo&lt;/code&gt;, only when &lt;code&gt;/owo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That's all for todays route, that is all I needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Starting with Apollo &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We are f*... Okey no hahahahaa, that was a joke!!!!&lt;/p&gt;

&lt;p&gt;We saw what &lt;strong&gt;GraphQL&lt;/strong&gt; and *&lt;em&gt;Apollo&lt;/em&gt; was, now is time to touch it, so I'll come back... (well you will read this at the same time, but meh, I like doing this, is more... entertaining (?) wish so...).&lt;/p&gt;

&lt;p&gt;Now that I have done the tutorial and an approach of fetching data from &lt;a href="https://rickandmortyapi.com/documentation"&gt;rick and morty API&lt;/a&gt;, is time to explain to you 👊.&lt;/p&gt;

&lt;p&gt;Apollo is very very easy, first time I use it and I got everything!! (I'll give you the sandbox, but is the same one).&lt;/p&gt;

&lt;p&gt;What I used was, &lt;code&gt;ApolloProvider&lt;/code&gt;, &lt;code&gt;ApolloClient&lt;/code&gt;, &lt;code&gt;useQuery&lt;/code&gt;, &lt;code&gt;InMemoryCache&lt;/code&gt; and &lt;code&gt;gql&lt;/code&gt;. All from the library &lt;code&gt;@apollo/client&lt;/code&gt; that you can install using:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i @apollo/client&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So... what is this all about B?&lt;/li&gt;
&lt;li&gt;Well... you can go to the Docs :)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nah, I am going to explain it!!! Don't worry!! I'll try it at least. (The &lt;a href="https://codesandbox.io/s/solitary-wood-06k0m?file=/src/apolloExample.jsx:113-127"&gt;codesandbox&lt;/a&gt; is commented)&lt;/p&gt;

&lt;h3&gt;
  
  
  » A little explanation &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.apollographql.com/docs/react/api/react/hooks/#the-apolloprovider-component"&gt;&lt;code&gt;ApolloProvider&lt;/code&gt;&lt;/a&gt; is like &lt;code&gt;useContext&lt;/code&gt; of React, you can use it to access data from every component inside that &lt;em&gt;context&lt;/em&gt;. So we use it to wrap components that (in this case) needs data from a GraphQL API, so they can access that data. It hhas one prop which is &lt;code&gt;client&lt;/code&gt; that is important because is the object generated by &lt;code&gt;ApolloClient&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.apollographql.com/docs/react/api/core/ApolloClient/#gatsby-focus-wrapper"&gt;&lt;code&gt;ApolloClient&lt;/code&gt;&lt;/a&gt; is a class provided by Apollo Client, which we need to communicate to the GraphQL API. This class has a lot of arguments to use in the constructor, but the ones required, are &lt;code&gt;uri&lt;/code&gt; or &lt;code&gt;link&lt;/code&gt; (of course) that is the link ti the GrapQL API. And &lt;code&gt;cache&lt;/code&gt; which is required, and is the cache that Apollo will use to store the fetch results.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.apollographql.com/docs/react/caching/overview/"&gt;&lt;code&gt;InMemoryCache&lt;/code&gt;&lt;/a&gt; is a type of cache provided by Apollo.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.apollographql.com/docs/resources/graphql-glossary/#gql-function"&gt;&lt;code&gt;gql&lt;/code&gt;&lt;/a&gt; a JavaScript template literal tag that parses GraphQL queries into an abstract syntax tree (AST). (I have copy the docs, because is very well explained)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.apollographql.com/docs/react/data/queries/"&gt;&lt;code&gt;useQuery&lt;/code&gt;&lt;/a&gt; is the way of fetching data, when we call this hook, we get a JSON with 3 keys: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;loading&lt;/code&gt; which is a boolean that gives us information about the request, so, if the request is pending or loading, then the loading argument is &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;error&lt;/code&gt; if the query was loaded and there was an error fetching or collecting the data, we are returned that error.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;data&lt;/code&gt; when it is loaded, and there is no error, we get the data (of the query we passed to the useQuery) in this key of the json.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, it is very powerful, because we didn't really did something, and we have 3 states that we can easily use, to know how our fetch of the data has done.&lt;/p&gt;

&lt;h2&gt;
  
  
  Returning to the project &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Since we want to fetch data from the component &lt;code&gt;Search&lt;/code&gt; and &lt;code&gt;CardContainer&lt;/code&gt;.&lt;br&gt;
In &lt;code&gt;Search&lt;/code&gt; to get the characters with the matched name, and in &lt;code&gt;CardContainer&lt;/code&gt; to get 5 random characters.&lt;/p&gt;

&lt;p&gt;So nice everything... I am not sure about giving &lt;code&gt;ApolloProvider&lt;/code&gt; in &lt;code&gt;home.jsx&lt;/code&gt;, which would be a destruction to my self from a lot of developers, thinkers and philosophers. Why I said that? IDK :)&lt;/p&gt;

&lt;p&gt;But I think is the nice place for it, because we have both components which fetches data from the same GraphQL API, but with different queries. (If you think I am wrong, please explain to me).&lt;/p&gt;

&lt;p&gt;I am going to code, I'll be right back.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/1VuZTjTISUvZGJ1Fih/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/1VuZTjTISUvZGJ1Fih/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So... it has been just 30min, of doing it and thinking...&lt;br&gt;
The main problem was that the &lt;code&gt;Client&lt;/code&gt; I was creating, didn't let me use &lt;code&gt;.query&lt;/code&gt; to test the queries, but now it works fine. I just changed the &lt;code&gt;require&lt;/code&gt; with &lt;code&gt;import ... from ...&lt;/code&gt;. Don't know why it happened, if you it, tell me in comments 🙏.&lt;/p&gt;

&lt;p&gt;Then I added a lot of random, to get 5 random characters from the GraphQL API, every time you render.&lt;/p&gt;

&lt;p&gt;The Loading or Error, are not pretty, I haven't even make nothing, just some &lt;code&gt;h3&lt;/code&gt; hahahahahaha, but don't worry I'll chage it (if you are reading this and the changes have been applied... you are lucky :])&lt;/p&gt;

&lt;p&gt;And this is all bby!!! Well, I didn't say it, but I created one folder called &lt;code&gt;apollo&lt;/code&gt; where I have the &lt;code&gt;Client&lt;/code&gt; and the &lt;code&gt;Queries&lt;/code&gt;, so everything is separated and nice :)&lt;/p&gt;

&lt;p&gt;Wish you like it!!! Here you have the source code &lt;a href="https://github.com/blitty-codes/rick-power"&gt;rick-power&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Follow me or contact!
&lt;/h2&gt;

&lt;p&gt;(I liked more the red bird :_()&lt;br&gt;
🐦 &lt;a href="https://twitter.com/blit12_"&gt;Twitter&lt;/a&gt;&lt;br&gt;
🐙 &lt;a href="https://github.com/blitty-codes"&gt;GitHub&lt;/a&gt;&lt;br&gt;
👥 &lt;a href="https://www.linkedin.com/in/blitty/"&gt;LinkdIn&lt;/a&gt;&lt;/p&gt;




</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>apollo</category>
    </item>
    <item>
      <title>Having the rick power!!! Pt.2</title>
      <dc:creator>Blitty</dc:creator>
      <pubDate>Thu, 29 Jul 2021 18:33:47 +0000</pubDate>
      <link>https://dev.to/blitty/having-the-rick-power-pt-2-k0</link>
      <guid>https://dev.to/blitty/having-the-rick-power-pt-2-k0</guid>
      <description>&lt;p&gt;HUHUHUHU let's continueeeeeee :)&lt;/p&gt;

&lt;p&gt;What are we going to do today? Don't know, but something like this:&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
Making home

&lt;ol&gt;
&lt;li&gt;Design&lt;/li&gt;
&lt;li&gt;Components&lt;/li&gt;
&lt;li&gt;Process&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Making home &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;So... this is what we have from yesterday...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MEQIX8Y2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hmeixkcq8r94hrqnxnwm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MEQIX8Y2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hmeixkcq8r94hrqnxnwm.png" alt="Photo from yesterday"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Is it a shit???&lt;br&gt;
&lt;a href="https://i.giphy.com/media/Lbp2zfSdpo2viXMtt3/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/Lbp2zfSdpo2viXMtt3/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now is time to change it :D&lt;/p&gt;

&lt;h3&gt;
  
  
  » Desing &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Well... I am going to show you how I want it to be, or at least, what I see in my mind... I know there are a lot of programs where I can design the web like &lt;em&gt;figma&lt;/em&gt;, but I like more paint!!! I am waiting for &lt;strong&gt;GIMP&lt;/strong&gt; to install, so that I can draw it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TYg0blCD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pubrp324u2898yu35z8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TYg0blCD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pubrp324u2898yu35z8.png" alt="Home page image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Something like this... (?) Maybe (?) Don't know, we will see how everything goes.&lt;/p&gt;

&lt;p&gt;what, we can see that we have 3 components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Welcome (heading)&lt;/li&gt;
&lt;li&gt;Search bar&lt;/li&gt;
&lt;li&gt;Cards place&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Everything is going inside &lt;code&gt;App.jsx&lt;/code&gt;, so that we live &lt;code&gt;index.js&lt;/code&gt; cleaner.&lt;/p&gt;

&lt;h3&gt;
  
  
  » Components &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Now we know how many and which components we have. The next step is to place the components the right way. As we see, we want the &lt;strong&gt;Search bar&lt;/strong&gt; at the top, so it is going to be the first element, then the &lt;strong&gt;heading&lt;/strong&gt; and the last one the Cards place which we will create a new component, called &lt;strong&gt;CardContainer&lt;/strong&gt; (Is it a good name?).&lt;/p&gt;

&lt;h3&gt;
  
  
  » Process &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;I've finished for today, so I'll explain how it has been...&lt;/p&gt;

&lt;p&gt;A lot of fight with CSS, nothing from another world, but I like it how it is, well not everything, because the colors doesn't match too much, but I am not too worried about it, because it can be changed later.&lt;/p&gt;

&lt;p&gt;The web is fully responsive.&lt;/p&gt;

&lt;p&gt;The worst thing was styling the &lt;strong&gt;CardContainer&lt;/strong&gt; because it was a lot of work, first I thought about &lt;code&gt;flex&lt;/code&gt; and &lt;strong&gt;rows&lt;/strong&gt; and &lt;strong&gt;cols&lt;/strong&gt; but I didn't like it, because it was not what I wanted, so I dig in my head, and found a solution, a &lt;code&gt;grid&lt;/code&gt;!!!! and there it is, a perfect grid (or I think so).&lt;/p&gt;

&lt;p&gt;And... nothing more... I was like 1h+ with the f* &lt;code&gt;CardContainer&lt;/code&gt; styling it.&lt;/p&gt;

&lt;p&gt;So here you have your screenshot&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fD_3lnPr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/en7e2wo552avxlj6aeq7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fD_3lnPr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/en7e2wo552avxlj6aeq7.png" alt="Home page image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So... tell me!!! How do you see it??&lt;/p&gt;

&lt;p&gt;--- Repeating...&lt;br&gt;
I know the colors match are a shit, but we will see, if we change it or not 😁&lt;br&gt;
--- Shutting down...&lt;/p&gt;

&lt;p&gt;PD: See you tomorrow... I guess&lt;/p&gt;

&lt;p&gt;OOOOHHHH waittttt......... I have to push it to &lt;a href="https://github.com/blitty-codes/rick-power.git"&gt;GitHub&lt;/a&gt; 😵 I just want to go to the sofa :(&lt;/p&gt;

&lt;p&gt;Done.&lt;br&gt;
See you!&lt;/p&gt;




&lt;h2&gt;
  
  
  Follow me!
&lt;/h2&gt;

&lt;p&gt;(I liked more the red bird :_()&lt;br&gt;
🐦 &lt;a href="https://twitter.com/blit12_"&gt;Twitter&lt;/a&gt;&lt;br&gt;
🐙 &lt;a href="https://github.com/blitty-codes"&gt;GitHub&lt;/a&gt;&lt;br&gt;
👥 &lt;a href="https://www.linkedin.com/in/blitty/"&gt;LinkdIn&lt;/a&gt;&lt;/p&gt;




</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>Having the rick power!!! Pt.1</title>
      <dc:creator>Blitty</dc:creator>
      <pubDate>Thu, 29 Jul 2021 00:06:09 +0000</pubDate>
      <link>https://dev.to/blitty/having-the-rick-power-pt-1-jl1</link>
      <guid>https://dev.to/blitty/having-the-rick-power-pt-1-jl1</guid>
      <description>&lt;p&gt;WOOOHOOOOOO we are going to have a nice time doing this :O&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/efHpdducCDKUMMyzte/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/efHpdducCDKUMMyzte/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
Creating the project

&lt;ol&gt;
&lt;li&gt;Installing dependencies&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;
Creating Home page

&lt;ol&gt;
&lt;li&gt;Making a nice entry&lt;/li&gt;
&lt;li&gt;Making cards&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I will be using as I said in the first post, React and Apollo with the API of &lt;a href="https://rickandmortyapi.com/documentation/"&gt;rick and morty&lt;/a&gt; using the graphQL one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the project &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Everything is going to be uploaded on my &lt;a href="https://github.com/blitty-codes/rick-power"&gt;GitHub&lt;/a&gt; with some comments (I guess... 😶‍🌫️).&lt;/p&gt;

&lt;p&gt;First thing is first, lets start our react project using&lt;br&gt;
&lt;code&gt;npx create-react-app my-app&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;YESSSS we are...&lt;br&gt;
&lt;a href="https://i.giphy.com/media/kgahdSixevDLsgeuur/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/kgahdSixevDLsgeuur/giphy.gif"&gt;&lt;/a&gt;&lt;br&gt;
DONEEE!!!&lt;/p&gt;

&lt;p&gt;Okey... no :/ it was a joke...&lt;/p&gt;

&lt;p&gt;just... continue reading and ignore it :D&lt;/p&gt;

&lt;h3&gt;
  
  
  » Installing dependencies &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;We need &lt;strong&gt;Apollo client&lt;/strong&gt;, &lt;strong&gt;GraphQL&lt;/strong&gt; and &lt;strong&gt;Tailwind&lt;/strong&gt; (why not?) So installing them...&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i @apollo/client graphql tailwindcss&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(Since my WiFi is like...&lt;br&gt;
&lt;a href="https://i.giphy.com/media/26uf10GWGaKqhD9Is/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/26uf10GWGaKqhD9Is/giphy.gif"&gt;&lt;/a&gt;&lt;br&gt;
here I am, waiting for it to finish, BTW is with the react project AHAHHAHAHAHAHA okey... okey... sorry you don't care about this, sorry 😬)&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Home page &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We are going to get rid of... everything?? (I know I did the first commit with everything, sorry xd).&lt;/p&gt;

&lt;p&gt;We don't need almost anything, so we will have only the &lt;code&gt;index.js&lt;/code&gt; and &lt;code&gt;app.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;PERFECT!!! NOW WE HAVE... nothing :|&lt;/p&gt;

&lt;p&gt;But don't cry, we are going to start now.&lt;/p&gt;

&lt;h3&gt;
  
  
  » Making a nice entry &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;For this... well... I will need &lt;strong&gt;Apollo client&lt;/strong&gt; here, because I'll be showing 5 cards of characters.&lt;/p&gt;

&lt;p&gt;And I thought... What about making a folder named &lt;code&gt;components&lt;/code&gt; that is what everyone would use, and create a component &lt;code&gt;card.jsx&lt;/code&gt;. And that is what I've done :).&lt;/p&gt;

&lt;p&gt;And at this point I thought: "What about doing it in typescript...?" And I said: "Noup... too much for now".&lt;/p&gt;

&lt;p&gt;Also... I realized that is the first time I use &lt;strong&gt;tailwind&lt;/strong&gt; for &lt;strong&gt;React&lt;/strong&gt;, so I needed to do the things that were in the &lt;a href="https://tailwindcss.com/docs/guides/create-react-app"&gt;docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And installed ESLint HEHE, sorry 😬😥 &lt;code&gt;restarting now...&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;-- Restarting&lt;br&gt;
-- Here we go!&lt;/p&gt;

&lt;h3&gt;
  
  
  » Making cards &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;So far so good, I've made the cards, I think they are okey... (also responsive)&lt;br&gt;
I got some problems with react, because I wasn't aware of some errors, like using &lt;code&gt;{{}}&lt;/code&gt; instead of &lt;code&gt;{}&lt;/code&gt; for strings :=.&lt;/p&gt;

&lt;p&gt;So.. I wanted to make a nice styling card but simple and I decided to have what you see in the image behind, I don't really know what to explain here, because is simple and there is no Apollo 😬.&lt;/p&gt;

&lt;p&gt;Well... I'll do a push ⬆ and go to sleep, see you tomorrow (?)!&lt;/p&gt;

&lt;p&gt;This tailwind is dizzying me 😵.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bSe4B5s---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0s525au8ahu337h64r08.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bSe4B5s---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0s525au8ahu337h64r08.png" alt="How is looking"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Follow me!
&lt;/h2&gt;

&lt;p&gt;(I liked more the red bird :_()&lt;br&gt;
🐦 &lt;a href="https://twitter.com/blit12_"&gt;Twitter&lt;/a&gt;&lt;br&gt;
🐙 &lt;a href="https://github.com/blitty-codes"&gt;GitHub&lt;/a&gt;&lt;br&gt;
👥 &lt;a href="https://www.linkedin.com/in/blitty/"&gt;LinkdIn&lt;/a&gt;&lt;/p&gt;




</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>Now... Let's learn Apollo!!!</title>
      <dc:creator>Blitty</dc:creator>
      <pubDate>Wed, 28 Jul 2021 21:04:39 +0000</pubDate>
      <link>https://dev.to/blitty/now-let-s-learn-apollo-38eg</link>
      <guid>https://dev.to/blitty/now-let-s-learn-apollo-38eg</guid>
      <description>&lt;p&gt;So... good morning???? Okey, I don't know. UwU&lt;/p&gt;

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

&lt;p&gt;The official definition is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Apollo is the industry-standard GraphQL implementation, providing the data graph layer that connects modern apps to the cloud.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What does it mean?&lt;br&gt;
&lt;a href="https://i.giphy.com/media/bPTXcJiIzzWz6/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/bPTXcJiIzzWz6/giphy.gif"&gt;&lt;/a&gt;&lt;br&gt;
JEJEJEJEJEJJEE&lt;/p&gt;

&lt;p&gt;Let's try and resume this definition... is a way of using GraphQL in every place, in Front and Back. I guess this is right.&lt;/p&gt;

&lt;p&gt;We have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Apollo client&lt;/strong&gt;, to use in the Front (something like, fetching data)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Apollo server&lt;/strong&gt; for the Back&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Apollo Federation&lt;/strong&gt;, which I kind of like it because is a way of segregation of a monolithic graph, which is cool, because you don't have just one graph.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Apollo Studio&lt;/strong&gt; a place to build graphs and be more secure and some good stuff if you really need it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  » What will we use? &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Of course!!! Apollo client 😁. Why?&lt;br&gt;
Because is what we need, the Back is done, we just need to make a good website.&lt;/p&gt;




&lt;h2&gt;
  
  
  Follow me!
&lt;/h2&gt;

&lt;p&gt;(I liked more the red bird :_()&lt;br&gt;
🐦 &lt;a href="https://twitter.com/blit12_"&gt;Twitter&lt;/a&gt;&lt;br&gt;
🐙 &lt;a href="https://github.com/blitty-codes"&gt;GitHub&lt;/a&gt;&lt;br&gt;
👥 &lt;a href="https://www.linkedin.com/in/blitty/"&gt;LinkdIn&lt;/a&gt;&lt;/p&gt;




</description>
      <category>apollo</category>
      <category>graphql</category>
    </item>
    <item>
      <title>First steps... GraphQL. What?</title>
      <dc:creator>Blitty</dc:creator>
      <pubDate>Tue, 27 Jul 2021 17:57:07 +0000</pubDate>
      <link>https://dev.to/blitty/first-steps-graphql-what-4dd5</link>
      <guid>https://dev.to/blitty/first-steps-graphql-what-4dd5</guid>
      <description>&lt;p&gt;We need this... and you know it!!! 🤓&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
What is GraphQL?

&lt;ol&gt;
&lt;li&gt;Query&lt;/li&gt;
&lt;li&gt;Mutations&lt;/li&gt;
&lt;li&gt;Scalars&lt;/li&gt;
&lt;li&gt;Arguments&lt;/li&gt;
&lt;li&gt;Type&lt;/li&gt;
&lt;li&gt;Interface&lt;/li&gt;
&lt;li&gt;Enum&lt;/li&gt;
&lt;li&gt;Non-null-values&lt;/li&gt;
&lt;li&gt;Arrays&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;Full Example&lt;/li&gt;
&lt;li&gt;Follow me!&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;According to the definition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So... in simple words, &lt;code&gt;is a tool to request the necessary and what you want, instead of sending all the information.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Nice! Now... How do we use it?¿&lt;/p&gt;

&lt;h3&gt;
  
  
  » Query &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;This is special, because it is the entry point of every GQL query.&lt;/p&gt;

&lt;p&gt;(Example from the docs)&lt;br&gt;
For example._&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Query {
  hero(episode: Episode): Character
  droid(id: ID!): Droid
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can get/send 2 types of &lt;em&gt;type&lt;/em&gt; (jeje), by using those Arguments, if we try to get a Character, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query {
  character {
    name
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we will get an error 🚧 !!! Because in the Query type, we didn't specify it.&lt;/p&gt;

&lt;h3&gt;
  
  
  » Mutations &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;They are another special type, is like Query, but the Arguments are used to modify or create new data in the Data base.&lt;/p&gt;

&lt;h3&gt;
  
  
  » Scalars &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;They are the basic (or primitive (?)) data type in GQL. The main ones are &lt;code&gt;Int, Float, String, Boolean, ID&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be human‐readable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  » Arguments &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;(I am not sure if this is the way of describe it, comment if not)&lt;br&gt;
Arguments are like "functions" that a &lt;em&gt;type&lt;/em&gt; can have, so we can have a way of sending data depending on what we want. (Using the example from the doc)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Starship {
  id: ID!
  name: String!
  length(unit: LengthUnit = METER): Float
}

enum LengthUnit {
  METER
  FOOT
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have &lt;code&gt;length&lt;/code&gt; that has a parameter which is by default METER and of type (enum) LengthUnit. Here is where magic happens, if we use METER we will have a value in meters, but when we get FOOT as argument, surprise!!, we will have a result in foot. Which is amazing, no? 🤯&lt;/p&gt;

&lt;h3&gt;
  
  
  » Type &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;To create &lt;em&gt;types&lt;/em&gt; which is a form of describing the data, if you know how SQL data bases work, then you will compare this as tables and rows.&lt;/p&gt;

&lt;h3&gt;
  
  
  » Interface &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;An interface we can think of it like in OOP (Object Oriented Programming) but with another idea. It is an abstract type that has fields, which can be &lt;strong&gt;implemented&lt;/strong&gt; by types, this way we can have fields that more than one type has in common. Also, the type that &lt;strong&gt;implements&lt;/strong&gt; an interface, needs to have the fields of that interface.&lt;/p&gt;

&lt;p&gt;Example._&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Monkie {
  name: String!
}

type Human implements Monkie {
  name: String!
  isDev: Boolean!
}

type HumanPlus implements Monkie {
  name: String!
  isSuper: Boolean
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  » Enum &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Is a way of creating an enumeration, for the people who does not know what an enum is, it is a way of creating some kind of "just these values can be accepted in the field.&lt;/p&gt;

&lt;h3&gt;
  
  
  » Non-null values &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;To determine fields that cannot be null, we use the char &lt;code&gt;!&lt;/code&gt;, this way, if the non-null field is null, then &lt;strong&gt;graphql&lt;/strong&gt; will trigger an error.&lt;/p&gt;

&lt;h3&gt;
  
  
  » List or Arrays (?) &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;We represent a field as an Array or List, using the &lt;code&gt;[]&lt;/code&gt; operator. Inside it we determine the type. When we use the non-null value in Arrays, we use it in two different ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;[type]!
Here we can say that GraphQL will trigger an error when the field value is null, so it will admit &lt;code&gt;[], [values], [null, values]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;[type!]
It will admit &lt;code&gt;null, [], [value]&lt;/code&gt;, but will trigger an error when given an Array with a null value: &lt;code&gt;[value, null]&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Full Example &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Animal {
  id: ID!
  name: String!
  age: Int!
  breed: [String]!
  colorEyes: ColorEyes!
  typeEyes: TypesEyes!
  father: Animal
  mother: Animal
  isStrong: Boolean
}

type AnimalFromMars implements Animal {
  id: ID!
  name: String!
  age: Int!
  breed: [String]!
  colorEyes: ColorEyes!
  typeEyes: TypesEyes!
  father: Animal
  mother: Animal
  isStrong: Boolean
  isIntelligent: Boolean
}

type AnimalFromOnePiece implements Animal {
  id: ID!
  name: String!
  age: Int!
  breed: [String]!
  colorEyes: ColorEyes!
  typeEyes: TypesEyes!
  father: Animal
  mother: Animal
  isStrong: Boolean
  size: Float
}

enum ColorEyes {
  GREEN # my favourites :D
  BLUE
  BLACK
  BROWN
}

enum TypesEyes { # didn't know how to describe this xd
  SHARINGAN
  SEA
  FIRE
  LINES
}

type Query {
  getAnimalByID (id: ID = 0): Animal
  getAnimalByName (name: String!): Animal
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when we want to make a request, we create a query&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# one way
query {
  getAnimalByID () { # by default is 0
    name
    age
    # we need to make this because size is just in AnimalFromOnePiece type, so we need to say what we want, when it is that type
    ... on AnimalFromOnePiece {
      size
    }
    ... on AnimalFromMars {
      isIntelligent
    }
  }
}
# another way
query getParents ($name: String) {
  getAnimalByID (name: $name) { # by default is 0
    father
    mother
    ... on AnimalFromOnePiece {
      size
    }
    ... on AnimalFromMars {
      isIntelligent
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the variables place, we need to have for the second query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "GQL"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So.... this is all!! Now I am an expert of GQL HUASHUASHUASHUAS&lt;/p&gt;

&lt;p&gt;Okey. . . . . Just joking, but now I know how all these stuff works!!!&lt;/p&gt;

&lt;p&gt;Wish this helps you 😅 (It helped me, doing it!)&lt;/p&gt;




&lt;h2&gt;
  
  
  Follow me! &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;(I liked more the red bird :_()&lt;br&gt;
🐦 &lt;a href="https://twitter.com/blit12_"&gt;Twitter&lt;/a&gt;&lt;br&gt;
🐙 &lt;a href="https://github.com/blitty-codes"&gt;GitHub&lt;/a&gt;&lt;br&gt;
👥 &lt;a href="https://www.linkedin.com/in/blitty/"&gt;LinkdIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PD: I think I missed some things 😬 Sorry&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
