{"componentChunkName":"component---src-templates-blog-post-js","path":"/learning-rust/","result":{"data":{"site":{"siteMetadata":{"title":"Undefined","author":"Anirudh Varma"}},"markdownRemark":{"id":"cd6f8286-de0d-5092-a8ef-8326ea7e7dd9","excerpt":"I have been programming for some time now and have been fortunate enough to have worked on a bunch of different languages like Java, C#, Python, JavaScript…","html":"<p>I have been programming for some time now and have been fortunate enough to have worked on a bunch of different languages like Java, C#, Python, JavaScript (TypeScript).</p>\n<p>I have been thinking of picking up a new language for sometime now and the choice came down to Go &#x26; Rust. As the title indicates, I went with Rust.</p>\n<h2 id=\"why-rust\" style=\"position:relative;\"><a href=\"#why-rust\" aria-label=\"why rust permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why Rust?</h2>\n<ol>\n<li><strong>The ecosystem</strong>: As a frontend/JavaScript developer, I am seeing more and more tools being re-written in Rust for performance benefits, and so hopefully, I would be able to contribute or at least learn from those code bases in the context of my day to day work.</li>\n<li><strong>Its Different?!</strong>: All of my previous experiences have been in application-oriented, managed languages. Rust is similar, but has some interesting concepts like <a href=\"https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html\">Immutability-by-default</a>, <a href=\"https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html\">Ownership</a> etc.</li>\n</ol>\n<h2 id=\"what-is-this-post\" style=\"position:relative;\"><a href=\"#what-is-this-post\" aria-label=\"what is this post permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is this post?</h2>\n<p>This post is supposed to be just a journal of things that I might like, dislike or find interesting enough to document as I learn Rust enough to build a simple CLI app. I am following the <a href=\"https://doc.rust-lang.org/book\">Rust Programming Language Book</a> and many snippets here are picked from there.</p>\n<h2 id=\"the-setup-rust-and-sublime-text-3-macos\" style=\"position:relative;\"><a href=\"#the-setup-rust-and-sublime-text-3-macos\" aria-label=\"the setup rust and sublime text 3 macos permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The Setup: Rust and Sublime Text 3 (MacOS)</h2>\n<h3 id=\"packages-required\" style=\"position:relative;\"><a href=\"#packages-required\" aria-label=\"packages required permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Packages Required</h3>\n<ul>\n<li><a href=\"https://rust-lang.github.io/rust-enhanced\">Rust Enhanced</a>: The official Rust package for ST3.</li>\n<li><a href=\"https://rust-analyzer.github.io/\">rust-analyzer</a>: Language Server implementation for Rust.</li>\n</ul>\n<h3 id=\"setup-the-language-server\" style=\"position:relative;\"><a href=\"#setup-the-language-server\" aria-label=\"setup the language server permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Setup the Language Server</h3>\n<ul>\n<li>Install the <a href=\"https://github.com/sublimelsp/LSP\">LSP Package</a></li>\n<li>Download the right package from releases.</li>\n<li>Extract the package.</li>\n<li>Rename to <code class=\"language-text\">rust-analyzer</code></li>\n<li>Add to <code class=\"language-text\">$PATH</code></li>\n<li>Make it executable <code class=\"language-text\">chmod +x rust-analyzer</code></li>\n</ul>\n<h3 id=\"enable-the-language-server-in-sublime\" style=\"position:relative;\"><a href=\"#enable-the-language-server-in-sublime\" aria-label=\"enable the language server in sublime permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Enable the Language Server in Sublime</h3>\n<ul>\n<li>Open Command Pallete (Cmd + Shift + P)</li>\n<li>Select <code class=\"language-text\">LSP: Enable Language Server Globally</code></li>\n<li>Select <code class=\"language-text\">rust-analyzer</code></li>\n</ul>\n<h2 id=\"the-interesting-parts\" style=\"position:relative;\"><a href=\"#the-interesting-parts\" aria-label=\"the interesting parts permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The Interesting Parts</h2>\n<h3 id=\"match\" style=\"position:relative;\"><a href=\"#match\" aria-label=\"match permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Match</h3>\n<p>Rust has an expressive <code class=\"language-text\">match</code> syntax that can be used to compare values. A match expression is written like -</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\"><span class=\"token keyword\">use</span> <span class=\"token namespace\">std<span class=\"token punctuation\">::</span>cmp<span class=\"token punctuation\">::</span></span><span class=\"token class-name\">Ordering</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">let</span> guess <span class=\"token operator\">=</span> <span class=\"token number\">4</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// some sample value, can be input</span>\n    <span class=\"token keyword\">let</span> secret <span class=\"token operator\">=</span> <span class=\"token number\">5</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// another value to compare against.</span>\n\n    <span class=\"token keyword\">match</span> guess<span class=\"token punctuation\">.</span><span class=\"token function\">cmp</span><span class=\"token punctuation\">(</span><span class=\"token operator\">&amp;</span>secret<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n            <span class=\"token class-name\">Ordering</span><span class=\"token punctuation\">::</span><span class=\"token class-name\">Less</span> <span class=\"token operator\">=></span> <span class=\"token macro property\">println!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Under\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n            <span class=\"token class-name\">Ordering</span><span class=\"token punctuation\">::</span><span class=\"token class-name\">Equal</span> <span class=\"token operator\">=></span> <span class=\"token macro property\">println!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Match!!\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n            <span class=\"token class-name\">Ordering</span><span class=\"token punctuation\">::</span><span class=\"token class-name\">Greater</span> <span class=\"token operator\">=></span> <span class=\"token macro property\">println!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Over\"</span><span class=\"token punctuation\">)</span>\n        <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Each line in the <code class=\"language-text\">match</code> block is called an <code class=\"language-text\">arm</code>. In my opinion this makes the code very easy to read and understand at a glance.</p>\n<h3 id=\"built-in-error-handling\" style=\"position:relative;\"><a href=\"#built-in-error-handling\" aria-label=\"built in error handling permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Built-In Error Handling</h3>\n<p>Some operations, like I/O always require error handling. Rust’s <code class=\"language-text\">Result</code> type represents either a success (<code class=\"language-text\">OK</code>) or an error (<code class=\"language-text\">Err</code>).</p>\n<p>The great thing here is that if a function returns a <code class=\"language-text\">Result</code>, then the caller must handle both success and failure cases.</p>\n<p>Consider this code that converts a string to an int -</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\"><span class=\"token keyword\">let</span> guess<span class=\"token punctuation\">:</span> <span class=\"token keyword\">u32</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">match</span> guess<span class=\"token punctuation\">.</span><span class=\"token function\">trim</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">parse</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n            <span class=\"token class-name\">Ok</span><span class=\"token punctuation\">(</span>num<span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> num<span class=\"token punctuation\">,</span>\n            <span class=\"token class-name\">Err</span><span class=\"token punctuation\">(</span>_<span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n                <span class=\"token macro property\">eprintln!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"{} is not a legit number, try again;\"</span><span class=\"token punctuation\">,</span> guess<span class=\"token punctuation\">.</span><span class=\"token function\">trim</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n                <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n            <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n        <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></code></pre></div>\n<h3 id=\"immutable-variables\" style=\"position:relative;\"><a href=\"#immutable-variables\" aria-label=\"immutable variables permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Immutable Variables</h3>\n<p>One major difference in Rust vs the other langugages I mentioned above is that variables in Rust are immutable by default.</p>\n<p>In cases where we need to re-assign a value to a variable, we need to explicitly mark that variable as mutable by using the <code class=\"language-text\">mut</code> keyword.</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\"><span class=\"token keyword\">let</span> a <span class=\"token operator\">=</span> <span class=\"token number\">5</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">//This is immutable, `a` cannot be re-assigned.</span>\n<span class=\"token keyword\">let</span> <span class=\"token keyword\">mut</span> b <span class=\"token operator\">=</span> <span class=\"token number\">10</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// `b` can be reassigned.</span></code></pre></div>\n<h3 id=\"shadowing\" style=\"position:relative;\"><a href=\"#shadowing\" aria-label=\"shadowing permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Shadowing</h3>\n<p>Rust allows developers to re-declare a variable with the same name in a scope. This is called Shadowing.</p>\n<p>Consider the following snippet -</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\"><span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">let</span> x <span class=\"token operator\">=</span> <span class=\"token number\">5</span><span class=\"token punctuation\">;</span>\n\n    <span class=\"token keyword\">let</span> x <span class=\"token operator\">=</span> x <span class=\"token operator\">+</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\n\n    <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">let</span> x <span class=\"token operator\">=</span> x <span class=\"token operator\">*</span> <span class=\"token number\">2</span><span class=\"token punctuation\">;</span>\n        <span class=\"token macro property\">println!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"The value of x in the inner scope is: {}\"</span><span class=\"token punctuation\">,</span> x<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// This will print 12</span>\n    <span class=\"token punctuation\">}</span>\n\n    <span class=\"token macro property\">println!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"The value of x is: {}\"</span><span class=\"token punctuation\">,</span> x<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// This is still 6.</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Honestly, the first time I saw this, I was pretty confused. Alot of other languages just refuse this kind of re-declaring of variables in scope.</p>\n<p>Technically, this variable is still immutable, i.e. this snippet would fail -</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\"><span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">let</span> x <span class=\"token operator\">=</span> <span class=\"token number\">5</span><span class=\"token punctuation\">;</span>\n\n    x <span class=\"token operator\">=</span> x <span class=\"token operator\">+</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// will fail</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The usecase for shadowing defined in the <a href=\"https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing\">Rust Book</a> is for running multiple transforms on the same variable without having to declare a bunch of variables names that we don’t care about.</p>\n<p>It will interesting to see if this causes any issues on larger Rust codebases.</p>\n<h3 id=\"arrays\" style=\"position:relative;\"><a href=\"#arrays\" aria-label=\"arrays permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Arrays</h3>\n<ul>\n<li>Arrays in Rust are more like Java than JavaScript. While an array in JavaScript can grow in size after initialization, arrays in Rust are fixed size.</li>\n<li>An array maybe initialized using the type of its elements and the the length like - <code class=\"language-text\">let x:[char, 5] = ['a','b','c','d','e']</code></li>\n<li>Rust also provides a shorthand syntax when we want to create an array with the same elements like <code class=\"language-text\">let x = [3;'a']</code>. This creates an array like <code class=\"language-text\">['a','a','a']</code></li>\n</ul>\n<h3 id=\"expressions-vs-statements\" style=\"position:relative;\"><a href=\"#expressions-vs-statements\" aria-label=\"expressions vs statements permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Expressions vs Statements</h3>\n<p>Statements do not return a value. An assignemnt is a statement. Consider the following JavaScript snippet;</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> a <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span>b <span class=\"token operator\">=</span> <span class=\"token number\">12</span><span class=\"token punctuation\">)</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>a<span class=\"token punctuation\">)</span> <span class=\"token comment\">// prints 12</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>b<span class=\"token punctuation\">)</span> <span class=\"token comment\">// prints 12</span></code></pre></div>\n<p>Here the variable <code class=\"language-text\">a</code> gets a value 12, because <code class=\"language-text\">b=12</code> returns 12. However, assignments in Rust don’t return a value.</p>\n<p>Expressions return a value, Rust is an <a href=\"https://en.wikipedia.org/wiki/Expression-oriented_programming_language\">Expression oriented</a> language.</p>\n<p>The block <code class=\"language-text\">{}</code> created for new scopes is an expression and thus can be assigned to variable like -</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\">   <span class=\"token keyword\">let</span> y <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">let</span> x <span class=\"token operator\">=</span> <span class=\"token number\">3</span><span class=\"token punctuation\">;</span>\n        x <span class=\"token operator\">+</span> <span class=\"token number\">1</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Here the last line in the block does not end with a semi-colon (;), thats because expressions do not include ending semicolons, if we add a semicolon, then it becomes a statement and hence it does not return a value.</p>\n<h3 id=\"return-values\" style=\"position:relative;\"><a href=\"#return-values\" aria-label=\"return values permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Return Values</h3>\n<p>In Rust, the by default, the return value of the function is the value returned by the last expression in the function. Using <code class=\"language-text\">return</code> can be used to exit early, but otherwise its optional.</p>\n<h3 id=\"conditional-assignemnt\" style=\"position:relative;\"><a href=\"#conditional-assignemnt\" aria-label=\"conditional assignemnt permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conditional Assignemnt</h3>\n<p>Since <code class=\"language-text\">if</code> is an expression, conditional assignemnts don’t require additional syntax.</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\"><span class=\"token keyword\">let</span> condition <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">let</span> number <span class=\"token operator\">=</span> <span class=\"token keyword\">if</span> condition <span class=\"token punctuation\">{</span> <span class=\"token number\">5</span> <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">6</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></code></pre></div>\n<h3 id=\"infinite-loops-there-is-a-keyword-for-that\" style=\"position:relative;\"><a href=\"#infinite-loops-there-is-a-keyword-for-that\" aria-label=\"infinite loops there is a keyword for that permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Infinite Loops? There is a keyword for that</h3>\n<p>Rust has built-in support for infinite loops <a href=\"https://doc.rust-lang.org/book/ch03-05-control-flow.html#repeating-code-with-loop\">using the <code class=\"language-text\">loop</code> keyword</a>. This is the first language where I am seeing this.</p>\n<h4>Returning with break;</h4>\n<p>Another first for me, was the ability to return values with the <code class=\"language-text\">break</code> keyword.</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\">    <span class=\"token keyword\">let</span> <span class=\"token keyword\">mut</span> counter <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\n\n    <span class=\"token keyword\">let</span> result <span class=\"token operator\">=</span> <span class=\"token keyword\">loop</span> <span class=\"token punctuation\">{</span>\n        counter <span class=\"token operator\">+=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\n\n        <span class=\"token keyword\">if</span> counter <span class=\"token operator\">==</span> <span class=\"token number\">10</span> <span class=\"token punctuation\">{</span>\n            <span class=\"token keyword\">break</span> counter <span class=\"token operator\">*</span> <span class=\"token number\">2</span><span class=\"token punctuation\">;</span>\n        <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n\n    <span class=\"token macro property\">println!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"The result is {}\"</span><span class=\"token punctuation\">,</span> result<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<ul>\n<li>Note that since <code class=\"language-text\">loop</code> is treated as an expression that yields a value, we can put it on the right side of assignment.</li>\n<li>The <code class=\"language-text\">break counter * 2</code> statement not only stops the loop, but assigns the value to the variable <code class=\"language-text\">result</code></li>\n</ul>\n<p><em>Source</em> <a href=\"https://doc.rust-lang.org/book/ch03-05-control-flow.html#returning-values-from-loops\">https://doc.rust-lang.org/book/ch03-05-control-flow.html#returning-values-from-loops</a></p>\n<h3 id=\"ownership\" style=\"position:relative;\"><a href=\"#ownership\" aria-label=\"ownership permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Ownership</h3>\n<p>Just refer - <a href=\"https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html\">https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html</a></p>\n<h4>Rules of ownership</h4>\n<ul>\n<li>Each value in Rust has a variable that’s called its owner.</li>\n<li>There can only be one owner at a time.</li>\n<li>When the owner goes out of scope, the value will be dropped.</li>\n</ul>\n<h4>Reassigning ‘objects’</h4>\n<p>Consider the following JS Code:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> x <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">foo</span><span class=\"token operator\">:</span> <span class=\"token number\">1</span> <span class=\"token punctuation\">}</span>\n<span class=\"token keyword\">const</span> y <span class=\"token operator\">=</span> x\ny<span class=\"token punctuation\">.</span>foo <span class=\"token operator\">=</span> <span class=\"token number\">2</span> <span class=\"token comment\">// this works and sets x.foo = 2</span></code></pre></div>\n<p>Here JavaScript assigns the reference to the variable x to y. So when some code alters <code class=\"language-text\">y.foo</code>, it’s actually changing <code class=\"language-text\">x.foo</code>.</p>\n<p>Consider the following rust snippet</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\">    <span class=\"token keyword\">let</span> s1 <span class=\"token operator\">=</span> <span class=\"token class-name\">String</span><span class=\"token punctuation\">::</span><span class=\"token function\">from</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"hello\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">let</span> s2 <span class=\"token operator\">=</span> s1<span class=\"token punctuation\">;</span>\n    <span class=\"token macro property\">println!</span><span class=\"token punctuation\">(</span>s1<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// this errors.</span></code></pre></div>\n<p>In this case, Rust too copies the pointer to where “Hello” is stored, but does not copy the data itself. However, due to how ownership works, and to keep things simple, <code class=\"language-text\">let s2=s1</code> actually invalidates <code class=\"language-text\">s1</code> and transfers the ownership of <code class=\"language-text\">Hello</code> to <code class=\"language-text\">s2</code>. After the re-assignment <code class=\"language-text\">s1</code> can no longer be used. This is called <code class=\"language-text\">move</code>.</p>\n<p>As explained <a href=\"https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html#ways-variables-and-data-interact-move\">here</a>, this is done so that <code class=\"language-text\">hello</code> only has one owner (s2) and when s2 goes out of scope, Rust can easily free up the memory.</p>\n<p><strong>Note</strong>: Fixed length data like integers and floats, that is stored on the stack can be re-assiged without invalidating the old variable. <a href=\"https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html#stack-only-data-copy\">Read More Here</a></p>\n<h4>Ownership and Functions</h4>\n<p>Passing data to functions as arguments also transfers the ownership of that data. Assume a regular <code class=\"language-text\">say_hello</code> function like -</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\"><span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">say_hello</span><span class=\"token punctuation\">(</span>name<span class=\"token punctuation\">:</span> <span class=\"token class-name\">String</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">-></span><span class=\"token class-name\">String</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token class-name\">String</span><span class=\"token punctuation\">::</span><span class=\"token function\">from</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Hello!, \"</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">+</span> <span class=\"token operator\">&amp;</span>name\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">let</span> s1 <span class=\"token operator\">=</span> <span class=\"token class-name\">String</span><span class=\"token punctuation\">::</span><span class=\"token function\">from</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"foo\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token macro property\">println!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"{}\"</span><span class=\"token punctuation\">,</span> <span class=\"token function\">say_hello</span><span class=\"token punctuation\">(</span>s1<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token macro property\">println!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"{}\"</span><span class=\"token punctuation\">.</span> s1<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// This would complain about `s1` having moved</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Passing variables to a function and then storing its return value back in a different variable will obviously be tedios and not always desirable. To get around this Rust has the concept of <a href=\"https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html\">References</a></p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\"><span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">say_hello</span><span class=\"token punctuation\">(</span>name<span class=\"token punctuation\">:</span> <span class=\"token operator\">&amp;</span><span class=\"token class-name\">String</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">-></span><span class=\"token class-name\">String</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token class-name\">String</span><span class=\"token punctuation\">::</span><span class=\"token function\">from</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Hello!, \"</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">+</span> <span class=\"token operator\">&amp;</span>name\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">let</span> s1 <span class=\"token operator\">=</span> <span class=\"token class-name\">String</span><span class=\"token punctuation\">::</span><span class=\"token function\">from</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"foo\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token macro property\">println!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"{}\"</span><span class=\"token punctuation\">,</span> <span class=\"token function\">say_hello</span><span class=\"token punctuation\">(</span><span class=\"token operator\">&amp;</span>s1<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token macro property\">println!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"{}\"</span><span class=\"token punctuation\">.</span> s1<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The <code class=\"language-text\">&amp;</code> syntax creates a reference to the value of <code class=\"language-text\">s1</code>, but it does not own it. Creating references is called <strong>borrowing</strong>.</p>\n<p><em>Notes</em></p>\n<ul>\n<li>References, just like variables are mutable by default. In order to change a reference, it needs to be marked with <code class=\"language-text\">&amp;mut</code>.</li>\n<li>There can only be one mutable reference at a time.</li>\n</ul>\n<h3 id=\"slices\" style=\"position:relative;\"><a href=\"#slices\" aria-label=\"slices permalink\" class=\"anchor-hint before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Slices</h3>\n<p>Refer: <a href=\"https://doc.rust-lang.org/book/ch04-03-slices.html\">https://doc.rust-lang.org/book/ch04-03-slices.html</a></p>\n<hr>\n<p><strong>Last Updated</strong>: 15 December 2021.</p>","frontmatter":{"title":"Learning Rust - The Interesting Parts","date":"December 10, 2021","ogimage":null}}},"pageContext":{"slug":"/learning-rust/","previous":{"fields":{"slug":"/gitlab-todos-hammerspoon/"},"frontmatter":{"title":"Tracking GitLab Todos with Hammerspoon"}},"next":{"fields":{"slug":"/book-notes-psychology-of-money/"},"frontmatter":{"title":"Book Notes: Psychology of Money"}}}},"staticQueryHashes":["426816048","983108779"]}