<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mohit Karekar</title>
    <description>The latest articles on Forem by Mohit Karekar (@mohitk05).</description>
    <link>https://forem.com/mohitk05</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%2F227453%2Fc60d7e70-b07f-4d6c-8a9a-5f80bb0b3560.jpeg</url>
      <title>Forem: Mohit Karekar</title>
      <link>https://forem.com/mohitk05</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mohitk05"/>
    <language>en</language>
    <item>
      <title>Venturing into Rust Land 🤺</title>
      <dc:creator>Mohit Karekar</dc:creator>
      <pubDate>Mon, 30 Mar 2020 06:15:40 +0000</pubDate>
      <link>https://forem.com/mohitk05/venturing-into-rust-land-5dg0</link>
      <guid>https://forem.com/mohitk05/venturing-into-rust-land-5dg0</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This post was originally published &lt;a href="https://mohitkarekar.com/posts/2020/venturing-into-rust-land/"&gt;on my blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Rust is a compiled, type-strict, performant language that is aimed to build fail-safe applications. After my failed attempt at learning C++, I decided to try Rust this weekend. Rust is promising as it opens up a lot of interesting domains for me - systems programming, WebAssembly, web servers (also on Raspberry Pi). Hence it is more exciting to learn as compared to C++. One can learn Rust from the official Rust book available online for free. Let's begin.&lt;/p&gt;

&lt;p&gt;I have been reading the official book for the past couple of weeks and I'll try to note down the important pointers from chapter 1 through 8 here. These chapters mainly introduce you to the syntax, core concepts and inbuilt data structures like arrays, enums and structs. Chapter 7 explains in detail about code organisation and the modules system. The main purpose of listing down these points is to revise whatever I read in these couple of weeks, and for future reference. You may go through these for quick understanding of Rust's syntax and working.&lt;/p&gt;

&lt;h2&gt;
  
  
  1
&lt;/h2&gt;

&lt;p&gt;Rust uses &lt;code&gt;rustup&lt;/code&gt; as its toolchain. It makes the process of installing Rust and getting started with it really smooth. It installs all the necessary tools like &lt;code&gt;rustc&lt;/code&gt; - the Rust compiler, &lt;code&gt;cargo&lt;/code&gt; - the Rust package manager, and the official Rust docs. It is also useful for future version upgrades. To download, run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;curl https://sh.rustup.rs &lt;span class="nt"&gt;-sSf&lt;/span&gt; | sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  2
&lt;/h2&gt;

&lt;p&gt;Unlike JavaScript, Rust is a compiled language and hence once something is developed, we create an executable out of our code. Rust is particularly known for its powerful compiler as it wouldn't allow you to make popular mistakes and will simply won't compile your code. I'll speak about this in a point separately.&lt;/p&gt;

&lt;h2&gt;
  
  
  3
&lt;/h2&gt;

&lt;p&gt;Since Rust is compiled, it requires an entry point to your code. Like C and Java, Rust requires a &lt;code&gt;main&lt;/code&gt; function which it considers by default as the entry point to your code. We define functions as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;square&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;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;fn&lt;/code&gt; is the keyword used to define a function, followed by the function name &lt;code&gt;square&lt;/code&gt;. If a function expects arguments, each argument should have a type defined for it. Here the argument &lt;code&gt;n&lt;/code&gt; is of &lt;code&gt;i32&lt;/code&gt; type. Inside the curly braces &lt;code&gt;{}&lt;/code&gt; is the function body. In Rust, we have expressions and statements; expressions return some value and statements don't. E.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&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="c"&gt;// This is a statement&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="c"&gt;// This is an expression, returns 5 (Notice that it does not have a semi-colon)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above &lt;code&gt;square&lt;/code&gt; function returns the square of &lt;code&gt;n&lt;/code&gt;, and in Rust, by default, the function returns the last expression. Here the function body has only one line and it is an expression. Hence the function returns &lt;code&gt;n * n&lt;/code&gt; for whatever value &lt;code&gt;n&lt;/code&gt; holds.&lt;/p&gt;

&lt;h2&gt;
  
  
  4
&lt;/h2&gt;

&lt;p&gt;Rust is statically typed and has a strong type system. At places, the Rust compiler can infer the type of a variable according to the value stored in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;// a has type i32&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;// b has inferred type: i32&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Basic rules of types apply here, like you can add only similar types etc, and if some function expects some type, you cannot pass other typed variable to it.&lt;/p&gt;

&lt;p&gt;Immutability is an important feature in Rust. By default, variables are immutable, i.e. you cannot set the value of a variable after it is set once. This is an important factor in memory-safe patterns. To make a variable mutable, you need to explicitly state this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&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;Note above that while resetting the value of &lt;code&gt;b&lt;/code&gt;, we used the &lt;code&gt;let&lt;/code&gt; keyword again.&lt;/p&gt;

&lt;h2&gt;
  
  
  5
&lt;/h2&gt;

&lt;p&gt;There is no garbage collector in Rust and this is the most amazing thing I find about it. I was writing about a similar concept in the article &lt;a href="https://mohitkarekar.com/posts/2020/knowing-what-changed-fast"&gt;Knowing what changed, really fast&lt;/a&gt;. It is about knowing what depends on what at compile time itself and following a concept of ownership and borrowing.&lt;/p&gt;

&lt;p&gt;Before this, let's talk in brief about how memory is allocated. There are two types of data structures used by Rust to allocate memory - the stack and the heap. Stack essentially stores all the variable information and also the memory content in the case of primitive data-types like integers, boolean, floating point numbers, characters, tuples of these types etc. In case of non-primitive data structures like String or structs, the content is stored somewhere on the heap and a pointer to this memory is stored on the stack.&lt;/p&gt;

&lt;p&gt;Each variable has a scope inside which it is valid, and becomes invalid when the scope ends. E.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;disp&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="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="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;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c"&gt;// the function ends here, and so does the scope of the local variable s. After this s is invalid.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Whenever some variable goes out of scope, Rust calls a &lt;code&gt;drop&lt;/code&gt; method defined for each variable. This method frees the memory associated with the variable.&lt;/p&gt;

&lt;p&gt;In Rust, it is important that a memory is &lt;strong&gt;owned&lt;/strong&gt; by a single owner at any point of time. For example, in case of a String variable, when we assign it some value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;name&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;"Mohit"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Memory is allocated on the heap to store &lt;code&gt;Mohit&lt;/code&gt;. An entry is pushed on the stack with the pointer to the String &lt;code&gt;Mohit&lt;/code&gt; on the heap. Variable &lt;code&gt;name&lt;/code&gt; owns this memory now. When we try to copy this variable into some other variable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;name&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;"Mohit"&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_2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;One would expect, like it happens in other languages like Java and C, that Rust would create a reference to the same memory and &lt;code&gt;name_2&lt;/code&gt; will hold this reference. This is partially true for what Rust does in this case. It does create a new entry with a pointer to the same memory on the heap and pushes it to stack, but with this, &lt;strong&gt;it also removes the first reference to the memory which was held by &lt;code&gt;name&lt;/code&gt;&lt;/strong&gt;. Hence if we try to use name after making a copy, the compiler will throw an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;name&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;"Mohit"&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_2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&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;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name_2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c"&gt;// Error: name_2 - value used here after move&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This prevents &lt;code&gt;use after free&lt;/code&gt; errors, where two items try to access the same memory and one of them clears the memory since it goes out of scope first, and then the second one tries to use it. These are the common &lt;code&gt;NullPointerExceptions&lt;/code&gt; in Java.&lt;/p&gt;

&lt;p&gt;When passing values to a function, the ownership is passed to the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;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="c"&gt;// Ownership passed to go_to_function, s invalid after this&lt;/span&gt;
&lt;span class="nf"&gt;go_to_function&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="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;go_to_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&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="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;string&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c"&gt;// scope of string ends and memory is freed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we do not want the variable to become invalid, we will have to return it at the end of function and assign it to a new variable so that we can use it after we have called the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;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="c"&gt;// Ownership passed to go_to_function, s invalid after this&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="nf"&gt;go_to_function&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="c"&gt;//Now s1 has the value of s and is valid hereafter&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;go_to_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&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="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;string&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="c"&gt;// returned back, ownership passed to s1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The second concept is &lt;strong&gt;borrowing&lt;/strong&gt;. Whenever we move variables from one place to another, e.g. when we pass them to functions like mentioned above, passing ownership is not always the right option. We might not want the memory to be freed. Instead of passing the values directly to the function, we can pass references to the variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;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="c"&gt;// Reference to s passed to go_to_function&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="nf"&gt;go_to_function&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This can be pictured as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;ptr     &lt;span class="nt"&gt;-----&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; stack ptr &lt;span class="nt"&gt;-----&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; heap value
string         s                Hello
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When inside the function, after the function ends, the local function variable goes out of scope. But this time, it does not free the memory as it does not have ownership of the memory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;go_to_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&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;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c"&gt;// string goes out of scope but it does not free memory as it did not own the memory&lt;/span&gt;
    &lt;span class="c"&gt;// Also, we did not have to return anything as we never had the ownership&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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



&lt;h2&gt;
  
  
  6
&lt;/h2&gt;

&lt;p&gt;Rust has some compositional data structures built-in, like structs and enums. A &lt;code&gt;struct&lt;/code&gt; is similar to that in C - a data structure that can hold a set of properties of different data types with each property being named. This makes it flexible enough such that the property can be accessed with the name and order isn't important, unlike tuples.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Person&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;i8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;address&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="c"&gt;// We can create instances of struct by providing right values&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Person&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;"Mohit"&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;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;address&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;"Mumbai, India"&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;For mutability, the entire struct instance has to be mutable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Person&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;"Nitish"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="c"&gt;// This adds rest of the values from p1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Possible, because p2 is mutable&lt;/span&gt;
&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="py"&gt;.name&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;"Nitu"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can also define methods associated with a struct. For this, we will have to use the &lt;code&gt;impl&lt;/code&gt; keyword. Each method receives a &lt;code&gt;&amp;amp;self&lt;/code&gt; reference to the struct instance on which the method is being called. Other function parameters can be added after &lt;code&gt;&amp;amp;self&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Person&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;i8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;address&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;impl&lt;/span&gt; &lt;span class="n"&gt;Person&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;display_age&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;"{}"&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;.age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;enum&lt;/code&gt; is used to store a list of items that are possible values of some entity, such that the entity will hold only one of those values at a particular time. This is particularly useful at places where there are multiple results/inputs possible and we need a way to group them and operate all of the variations as one. For example, consider we have a multi-user chat room, and we have implemented a function that displays a new message on the terminal. The message to be displayed can be a result of a variety of situations - a new user joined the room, a user left the room, a new room was created, a new message, a message for all users etc.&lt;/p&gt;

&lt;p&gt;We want that one single function should display all these types of messages, but each of the message has some different property like &lt;code&gt;new user joined&lt;/code&gt; has the name of the user to be displayed. &lt;code&gt;new room created&lt;/code&gt; has name of the room attached to it etc. In short, each message needs to be printed/handled in a different way. Here, creating an enum called &lt;code&gt;Message&lt;/code&gt; would be very useful.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Message&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;NewUser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;NewRoom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;UserLeft&lt;/span&gt;
    &lt;span class="n"&gt;Broadcast&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;display_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;Message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// handle different messages&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Usually, the best way to handle enum types is using the &lt;code&gt;match&lt;/code&gt; block. It is similar to switch block in other languages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;display_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;Message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nn"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NewUser&lt;/span&gt; &lt;span class="k"&gt;=&amp;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;message&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="nn"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NewRoom&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c"&gt;// Specific code&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="nn"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;UserLeft&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c"&gt;// Specific code&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="nn"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Broadcast&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c"&gt;// Specific code&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;Enums can also hold data, and each item can be of different type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Message&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;NewUser&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="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="nf"&gt;NewRoom&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="nf"&gt;UserLeft&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="nf"&gt;Broadcast&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  7
&lt;/h2&gt;

&lt;p&gt;There is no &lt;code&gt;null&lt;/code&gt; data type in Rust. It does not allow the concept of a variable not having any memory as it leads to a lot of problems later. Instead, there is a &lt;code&gt;Option&amp;lt;T&amp;gt;&lt;/code&gt; enum. This enum can have two values, one if the variable has some value and second if it doesn't have any value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;T&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;square&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;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;n&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="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;sq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;square&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c"&gt;// sq will have Some(4)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, the function square isn't sure if the value sent to it will surely be an &lt;code&gt;i32&lt;/code&gt;, so it covers the case for &lt;code&gt;None&lt;/code&gt; by accepting a &lt;code&gt;Option&amp;lt;i32&amp;gt;&lt;/code&gt;. Inside the body, we match if the value of the argument is &lt;code&gt;None&lt;/code&gt; or &lt;code&gt;Some(i32)&lt;/code&gt; and accordingly return.&lt;/p&gt;

&lt;p&gt;I personally found this very similar to &lt;code&gt;Promise&lt;/code&gt; in JavaScript. Whenever we are unsure of the output of some asynchronous function, we return a Promise, which can either resolve or reject according to the output of the async operation.&lt;/p&gt;




&lt;p&gt;I have been writing this article since a few days and have run through the documentation several times. I may have missed some topics and might even be wrong at some points but I believe this will get better with more Rust. Currently I'm looking into &lt;strong&gt;&lt;a href="https://neon-bindings.com/docs/intro"&gt;Neon&lt;/a&gt;&lt;/strong&gt;, which provides ways to embed Rust inside Nodejs. It allows you to write native modules in Rust and expose them as JavaScript functions. This can be pretty interesting to speed up parts of your Node app which face performance bottlenecks. I'll document this once I have substantial work done. Till then, keep learning and stay safe!&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programminglanguage</category>
      <category>node</category>
    </item>
    <item>
      <title>Building a Smart Mirror - 1</title>
      <dc:creator>Mohit Karekar</dc:creator>
      <pubDate>Sun, 01 Mar 2020 16:55:57 +0000</pubDate>
      <link>https://forem.com/mohitk05/building-a-smart-mirror-1-p96</link>
      <guid>https://forem.com/mohitk05/building-a-smart-mirror-1-p96</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This post was originally published on &lt;a href="https://mohitkarekar.com/posts/2020/building-a-smart-mirror-1/"&gt;my personal blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Almost a month back we started the work for renovating our house in Panvel, a decision my parents had taken long back, but was finally brought into action the previous month. This included a lot of breaking, building, cutting and painting. The house was going to be a mess for sure. This was a trigger for me and my brother, Nitish, because this was the right time to experiment with building some stuff. Nitish wanted to build a smart mirror for quite some time, so without any delay, I ordered a Raspberry Pi 3B and got to work.&lt;/p&gt;

&lt;p&gt;Building a smart mirror as an actual product requires quite an effort. More than actually building the thing, acquiring materials is a tougher job. Before the Pi had arrived, Nitish was out searching for a monitor screen! He got one at a local electronics repair shop, for a very low price of Rs. 1500. He got it home and disassembled it, ready to be installed in the mirror. The Pi was still on the way, meanwhile, I researched for the software stuff that would be required for the mirror.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rr0CP3IC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/iwo3m0lh3r0owic58fux.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rr0CP3IC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/iwo3m0lh3r0owic58fux.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The monitor screen&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So basically, Raspberry Pi is a mini-computer. It can do anything that your laptop/mobile/server can. You can develop stuff, run local servers, serve webpages, run scripts, connect to Wi-Fi and much more! So my initial idea of running a smart mirror was to boot up the Pi in kiosk mode, which means that it would always display a specified UI when booted up. This UI can be a simple HTML page opened in Chromium, the default browser in Pi. The HTML page would be connected to a remote server, which would serve data required to render things on the mirror. Plus it would be sockets enabled. If I had to refresh my screen on the mirror, I'd click a button on my phone, and the screen would refresh.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does a Magic Mirror work?
&lt;/h2&gt;

&lt;p&gt;A magic mirror is called so because text and images magically appear on the mirror glass, somewhere you wouldn't expect at all. This is achieved by using a two-way glass surface. This surface behaves according to the amount of light on each side of the surface - reflective where the light is more, and transparent where it is dark.&lt;/p&gt;

&lt;p&gt;Leveraging this property, we can black out the monitor screen behind the glass and display white text which would appear as if floating on the mirror. Once this is done, we can add numerous other features because now our mirror is nothing but a computer!&lt;/p&gt;

&lt;h2&gt;
  
  
  Gathering Materials
&lt;/h2&gt;

&lt;p&gt;A basic smart mirror requires the following items to become alive:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Raspberry Pi&lt;/li&gt;
&lt;li&gt;Mirror screen/glass&lt;/li&gt;
&lt;li&gt;A monitor&lt;/li&gt;
&lt;li&gt;Wooden frame&lt;/li&gt;
&lt;li&gt;Black cardboard&lt;/li&gt;
&lt;li&gt;32 GB micro SD card&lt;/li&gt;
&lt;li&gt;USB Mic&lt;/li&gt;
&lt;li&gt;Power adapter (5V 2.5-3A)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Getting all of these is a bit of an effort because you won't necessarily find them in one go. Since I had carpentry work going on at my home, I asked the carpenter to build a frame. Initially, he did not get the idea of what we were building. So we explained it to him patiently, showing him YouTube videos of people who had already built the mirror. Finally, he got the crux and built the beautiful frame which you see in the cover image of this post.&lt;/p&gt;

&lt;p&gt;The frame was in two pieces, the actual frame which was going to hold all the parts, and the cover, which was going to cover up the open back of the frame and hang up on the wall. Simultaneously we were looking out for the mirror screen. We were trying to find the acrylic sheet which behaved like a mirror. We found it nowhere. Visiting various glass shops and roaming around hopelessly in scorch heat, we could not get what we were looking for. Amazon has this sheet, also the glass, but the price was way too much. Buying it from there would ruin the purpose of this being a side project.&lt;/p&gt;

&lt;p&gt;Throughout the glass search, we had a backup - &lt;strong&gt;a two-way reflective glass&lt;/strong&gt; as the one on sliding windows. I wasn't quite sure how it would perform though; the reflective properties were really good, but all of them had a tint which made the light coming in a bit dull. I doubted if the UI on the monitor screen would look good through this glass. Eventually, after failed attempts at finding the acrylic sheet, we resorted to the two-way glass.&lt;/p&gt;

&lt;p&gt;The rest of the components - black cardboard, USB mic, power adapter were easily available at a local stationery and electronics shop.&lt;/p&gt;

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

&lt;h4&gt;
  
  
  Raspbian
&lt;/h4&gt;

&lt;p&gt;There are a lot of good tutorials to get started with Raspberry Pi. The first step to begin development is to flash an OS on the memory card. I installed the official OS Raspbian as I did not require any customization there. The process is well documented on the &lt;a href="https://www.raspberrypi.org/documentation/installation/installing-images/"&gt;official website&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  MagicMirror
&lt;/h4&gt;

&lt;p&gt;Once I had the OS installed, it was all the same! It was a computer. So the basic idea was clear, I had to run a web-page in full-screen mode with a sufficiently black background for it to look &lt;em&gt;magical&lt;/em&gt;. I knew this was an easy task, so I left that for the end. Moreover, there's this amazing open-source library to set up the interface called &lt;a href="https://github.com/MichMich/MagicMirror"&gt;MagicMirror&lt;/a&gt;. I moved to the next task of setting up Google Assistant, the mic was added for this particular purpose.&lt;/p&gt;

&lt;h4&gt;
  
  
  Google Assistant
&lt;/h4&gt;

&lt;p&gt;Adding Google Assistant to Raspberry Pi is somewhat tedious, Google recently deprecated a widely used Python library and instead promotes using its Assistant Service. But still, several tutorials and blogs mention the old library and hence leads to a bit of confusion. I followed the &lt;a href="https://developers.google.com/assistant/sdk/guides/service/python"&gt;latest official documentation&lt;/a&gt; after wandering a bit. But before moving ahead, if you are referring this article to set up a magic mirror, I finally decided to not go with Google Assistant for reasons I've &lt;a href=""&gt;mentioned below&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once all the basic stuff was ready, we decided to compile everything and put the mirror on the wall, the software part could be worked on later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bringing Everything Together
&lt;/h2&gt;

&lt;p&gt;All of this boiled down to the day when all of us took the task of putting up the mirror seriously. Before this, we were randomly trying things out and there were no solid improvements. Honestly, I've always been bad at carpentry or civil work. My dad is amazing at it - I remember the time when I was around 15 years old, we had a wooden TV cabinet that had stood the test of time and showed signs of old age. So my dad took up the task of cutting the almost 8 feet tall cabinet into two halves and use one as a tabletop for the TV and the other one as a computer table. I and my brother used to help in the occasional cutting of wood and painting.&lt;/p&gt;

&lt;p&gt;We started with me finishing off the final software installs on the Pi and getting the board ready to be put behind the mirror. &lt;em&gt;Then we hit a roadblock&lt;/em&gt;. The monitor screen we were using for the project died a natural death. When connected to the Pi for final checks, it started flickering and finally blacked out. This was a major hit given we had planned to get this project done. So we decided to wait for the next day and get all the paraphernalia ready so that the project reaches completion the next day.&lt;/p&gt;

&lt;p&gt;First thing next morning, Nitish and I got outside in search of second-hand monitor screens. We were looking for cheap yet good ones and ones that would fit in the wooden frame as it was built to the exact size of the previous screen. We did find a good one, a second hand 19'' Dell monitor. This was also perfect because it had a metal casing for all the circuitry. The monitor was back!&lt;/p&gt;

&lt;p&gt;Next, it was time to place the monitor inside the frame and put all the electronics in place.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zhWfAsLT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/edc5fepg7ldyxj1qmqlt.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zhWfAsLT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/edc5fepg7ldyxj1qmqlt.jpg" alt="Layout"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is always better to pull out the USB ports for future debugging. We also extended the analog audio (aux) port so that we could connect a speaker outside. The mic was brought just out of one of the ventilation holes we had drilled in the wooden frame.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QwC54mbv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fba3uytogxnexul31mo7.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QwC54mbv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fba3uytogxnexul31mo7.jpeg" alt="Mirror"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We then put on the back cover and the mirror was ready to be hung! It looked stunning and we honestly did not think it would look that way. I set up a basic MagicMirror UI and it really looked magical!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5TYYg-S6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o2s9mst9zqjhvospp1fb.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5TYYg-S6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o2s9mst9zqjhvospp1fb.jpeg" alt="Magic"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While building the mirror, I was reminded of an app that I and Kunal had built some time back. It was called &lt;a href="https://playthis.netlify.com"&gt;Play This&lt;/a&gt; and it allowed playing music on one device and controlling it from others. Several devices could join the 'room' and add their favorite music to a single playlist. We used to play a lot of music while at TTT. What struck me was that this was perfect for the mirror! I could play music on the mirror using my phone! And after a few CSS changes, it worked perfectly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zTcjSyEU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3fyag23i5l6dvsss0m8n.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zTcjSyEU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3fyag23i5l6dvsss0m8n.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was a long post, but there's more to this. I'll write one more piece to document how I failed to get Google Assistant running and to my rescue was none other than Amazon Alexa.&lt;/p&gt;

</description>
      <category>raspberrypi</category>
      <category>smartmirror</category>
      <category>diy</category>
      <category>alexa</category>
    </item>
  </channel>
</rss>
