<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>rust on A Blog with No Name</title><link>/tags/rust/</link><description>A Blog with No Name (rust)</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Thu, 04 Mar 2021 20:39:45 -0800</lastBuildDate><atom:link href="/tags/rust/index.xml" rel="self" type="application/rss+xml"/><item><title>Rust, Part One</title><link>/posts/rust-part-one/</link><pubDate>Thu, 04 Mar 2021 20:39:45 -0800</pubDate><guid>/posts/rust-part-one/</guid><description>&lt;h2 id="what-is-rust">What is Rust?&lt;/h2>
&lt;p>In short, a programming language focused on memory safety. Compiled
like C/C++. Has a lot of neat features. Let&amp;rsquo;s talk about that.&lt;/p>
&lt;h2 id="why-rust">Why Rust?&lt;/h2>
&lt;p>Among other things, Rust is built to be modern. It has its own dependency
manager (cargo) which allows you to easily get the modules and components you need.&lt;/p>
&lt;p>I used to thoroughly dislike Rust, but that was because
I wasn&amp;rsquo;t approaching it the right way. You see, Rust&amp;rsquo;s whole
philosophy is built around making safe code from the get-go.
Evidently, the C++ code I was writing wasn&amp;rsquo;t safe, at least
from Rust&amp;rsquo;s perspective. So I suppose I ought to explain Rust&amp;rsquo;s
whole approach to memory management.&lt;/p>
&lt;p>Suppose you have two variables, each initialized with the same
value.&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-rust" data-lang="rust">&lt;span style="color:#66d9ef">let&lt;/span> x &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#ae81ff">2&lt;/span>;
&lt;span style="color:#66d9ef">let&lt;/span> y &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#ae81ff">2&lt;/span>;
&lt;/code>&lt;/pre>&lt;/div>&lt;p>Both &lt;code>x&lt;/code> and &lt;code>y&lt;/code> now own their own respective values of 2. Now,
say we do this:&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-rust" data-lang="rust">&lt;span style="color:#66d9ef">let&lt;/span> y &lt;span style="color:#f92672">=&lt;/span> x;
&lt;/code>&lt;/pre>&lt;/div>&lt;p>At this point, &lt;code>x&lt;/code> may as well have gone out of scope. Rust has a
particularly fickle way of dealing with memory, and insists that
when you use a variable somewhere, unless you &amp;lsquo;borrow&amp;rsquo; it, that
variable is no longer of use outside of where it got used. This
confused and frustrated me greatly; C and C++ don&amp;rsquo;t have this problem,
because memory isn&amp;rsquo;t constantly treated like a resource that needs
to be tracked (unless you&amp;rsquo;re doing things with threads, in which
case if you&amp;rsquo;re not using mutexes or semaphores, I would highly recommend
looking into how that works).&lt;/p>
&lt;p>So, if you wish to give a variable another variable&amp;rsquo;s value without
moving said value, you must explicitly declare so:&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-rust" data-lang="rust">&lt;span style="color:#66d9ef">let&lt;/span> x &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#ae81ff">2&lt;/span>;
&lt;span style="color:#66d9ef">let&lt;/span> y &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#f92672">&amp;amp;&lt;/span>x;
&lt;/code>&lt;/pre>&lt;/div>&lt;p>As a programmer coming from C++ to Rust, this convention is not immediately
clear.&lt;/p>
&lt;h2 id="classes">Classes&lt;/h2>
&lt;p>Rust doesn&amp;rsquo;t have object-oriented support in the &amp;lsquo;typical&amp;rsquo; sense (typical
meaning having tokens such as &lt;code>class&lt;/code>). Much like C, &amp;lsquo;classes&amp;rsquo; are simply structs.
Coming from a C++ perspective, the inability to define proper classes becomes
jarring, but realistically, Rust&amp;rsquo;s structs are effectively the same thing.&lt;/p>
&lt;p>Defining a struct in Rust is somewhat similar to JavaScript, except that instead
of defining values when you define the struct, you define the types:&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-rust" data-lang="rust">&lt;span style="color:#66d9ef">struct&lt;/span> &lt;span style="color:#a6e22e">computer&lt;/span> {
ram: &lt;span style="color:#66d9ef">i8&lt;/span>,
cpu: String,
gpu: String
};
&lt;/code>&lt;/pre>&lt;/div>&lt;p>Then, when initializing an instance (object) of said struct, you can then
define the values:&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-rust" data-lang="rust">&lt;span style="color:#66d9ef">let&lt;/span> p &lt;span style="color:#f92672">=&lt;/span> computer {
ram: &lt;span style="color:#ae81ff">16&lt;/span>,
cpu: String::from(&lt;span style="color:#e6db74">&amp;#34;some cpu&amp;#34;&lt;/span>),
gpu: String::from(&lt;span style="color:#e6db74">&amp;#34;some gpu&amp;#34;&lt;/span>)
};
&lt;/code>&lt;/pre>&lt;/div>&lt;p>Here&amp;rsquo;s where we run into another fun (frustrating) aspect of Rust: types.&lt;/p>
&lt;h2 id="types">Types&lt;/h2>
&lt;p>C++ and like languages don&amp;rsquo;t usually care how you deal with types because,
for the most part, everything&amp;rsquo;s the same. Numbers can be interchangably compared with
other numbers of different types, and strings are not considered different types
(unless you&amp;rsquo;re using C-style strings, but maybe that will be for the next post).
The developers of Rust, in their infinite wisdom and endless pedantry, decided
that the strings &lt;code>&amp;quot;this&amp;quot;&lt;/code> and &lt;code>&amp;quot;this&amp;quot;&lt;/code> are not the same if the former is of type &lt;code>&amp;amp;str&lt;/code> (a
string slice), and the latter of type &lt;code>String&lt;/code> (a string object). The difference
here comes down to how each is allocated in memory. A string slice, much like a C-style
string, is a fixed size allocated on the stack and cannot be expanded or shrunk,
only altered in its original size. A string object, on the other hand is allocated
on the heap, meaning that you can expand or shrink it as you please, messing around
with it without fear of messing around with other memory. This has the practial
upshot of &lt;code>???&lt;/code>.&lt;/p>
&lt;p>I haven&amp;rsquo;t even gotten into the different integer types in Rust. There are separate
bit sizes for each amount of memory you could want to allocate to a given integer
variable. I presume this is a memory-saving measure, since sometimes you don&amp;rsquo;t
need to use 16 bits just for one number. However, this has the effect of making
my life harder because I just want to type &lt;code>int&lt;/code> when specifying a struct
data member&amp;rsquo;s type. Wah.&lt;/p>
&lt;p>Comparing data between variables of different types is only a slight hassle, if
only because you have to explicitly cast one variable to the other&amp;rsquo;s type. Most other
languages make some base assumptions about comparing types, but as Rust wants to
be safe and not cause bugs that can occur in these situations, it forces explicit
casting.&lt;/p>
&lt;p>That&amp;rsquo;s about all I have to say for Rust for the time being. Since I started writing this
post, I moved on to building a website using Node. Keep an eye out for a post about
that.&lt;/p></description></item></channel></rss>