<?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: Danh Võ</title>
    <description>The latest articles on Forem by Danh Võ (@datavadoz).</description>
    <link>https://forem.com/datavadoz</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%2F736361%2Fbe6f165a-1e87-4ae8-876a-6f168868ab63.JPG</url>
      <title>Forem: Danh Võ</title>
      <link>https://forem.com/datavadoz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/datavadoz"/>
    <language>en</language>
    <item>
      <title>[Rust] Place to store values</title>
      <dc:creator>Danh Võ</dc:creator>
      <pubDate>Thu, 22 Dec 2022 10:09:14 +0000</pubDate>
      <link>https://forem.com/datavadoz/rust-place-to-store-values-566j</link>
      <guid>https://forem.com/datavadoz/rust-place-to-store-values-566j</guid>
      <description>&lt;p&gt;&lt;em&gt;Like other programing languages, to hold data we need some things called constant and variable.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  CONSTANT
&lt;/h2&gt;

&lt;p&gt;We could not change value of a constant. Good choice to store value that is not changed over time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const SECONDS_OF_ONE_MINUTE: i32 = 60;
const SECONDS_OF_ONE_HOUR: i32 = SECONDS_OF_ONE_MINUTE * 60;
const SECONDS_OF_ONE_DAY: i32 = SECONDS_OF_ONE_HOUR * 24;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  VARIABLE
&lt;/h2&gt;

&lt;p&gt;1) &lt;strong&gt;Immutable variable&lt;/strong&gt; (default and favor by Rust): &lt;br&gt;
After assigning a value to a variable by keyword let, we could not re-assign it. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let var = 1;
println!("value: {var}");
var = 2;  // Will cause error at compile time.
println!("value: {var}");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, it is quite similar to constant, right?&lt;br&gt;
But there are 2 things different between immutable variable and constant:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can use &lt;em&gt;variable shadowing&lt;/em&gt; to change variable value. Just repeat the &lt;code&gt;let&lt;/code&gt; keyword. But there is a small thing behind the scene. Rust creates another new variable with a new memory slot and with a same name. Not the original one. IMO, its functionality seems to run contrary to its name (immutable but changeable by somehow)
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let var = 1;
println!("value: {var}");
let var = 2;  // Is OK
println!("value: {var}");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Mutable was born to serve this purpose.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2) &lt;strong&gt;Mutable variable&lt;/strong&gt;:&lt;br&gt;
Just put &lt;code&gt;mut&lt;/code&gt; keyword after &lt;code&gt;let&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mut name = "Unknown";
name = "Danh";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy huh?&lt;/p&gt;

&lt;p&gt;Reference: &lt;a href="https://doc.rust-lang.org/stable/book/ch03-01-variables-and-mutability.html" rel="noopener noreferrer"&gt;https://doc.rust-lang.org/stable/book/ch03-01-variables-and-mutability.html&lt;/a&gt; &lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>First taste on Rust!</title>
      <dc:creator>Danh Võ</dc:creator>
      <pubDate>Thu, 15 Dec 2022 14:27:20 +0000</pubDate>
      <link>https://forem.com/datavadoz/hello-world-56gm</link>
      <guid>https://forem.com/datavadoz/hello-world-56gm</guid>
      <description>&lt;p&gt;&lt;em&gt;I have just finished the &lt;a href="https://doc.rust-lang.org/stable/book/ch01-00-getting-started.html"&gt;Chapter 1&lt;/a&gt; of The Rust Programming Language book. It is quite easy to follow. Below are several things to take away from what I have just learnt.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Rust is a compiled language
&lt;/h2&gt;

&lt;p&gt;Similar to C/C++, Rust needs an entry point to run the whole program (&lt;code&gt;main&lt;/code&gt; function) and a compiler to transform human readable text into binary file. To compile Rust source code, we use &lt;code&gt;rustc&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;danh@friday:~/workspace/self-study/hello-rust/src$ cat hell_world.rs
fn main() {
    println!("Hello world!");
}
danh@friday:~/workspace/self-study/hello-rust/src$ rustc hell_world.rs
danh@friday:~/workspace/self-study/hello-rust/src$ ll
total 3956
drwxrwxr-x 2 danh danh    4096 Dec 15 17:16 ./
drwxrwxr-x 5 danh danh    4096 Dec 15 17:16 ../
-rwxrwxr-x 1 danh danh 4036560 Dec 15 17:08 hell_world*
-rw-rw-r-- 1 danh danh      44 Dec 15 17:08 hell_world.rs
danh@friday:~/workspace/self-study/hello-rust/src$ ./hell_world
Hello world!

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

&lt;/div&gt;



&lt;p&gt;As Rust says, its brilliant compiler will handle and caution us if there is any vulnerable code sections and suggest a hint during compile time. Great!&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the life easy by using Cargo
&lt;/h2&gt;

&lt;p&gt;Rust provides &lt;code&gt;cargo&lt;/code&gt; to create, build, run and release our Rust project in a convenient way. Over than that, &lt;code&gt;cargo&lt;/code&gt; help us in package dependency management (similar to &lt;code&gt;npm&lt;/code&gt; in Javascript world).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To create new project&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;danh@friday:~/workspace/self-study$ cargo new hello-world
     Created binary (application) `hello-world` package
danh@friday:~/workspace/self-study$ ll hello-world/
total 24
drwxrwxr-x 4 danh danh 4096 Dec 15 17:26 ./
drwxrwxr-x 7 danh danh 4096 Dec 15 17:26 ../
-rw-rw-r-- 1 danh danh  180 Dec 15 17:26 Cargo.toml
drwxrwxr-x 6 danh danh 4096 Dec 15 17:26 .git/
-rw-rw-r-- 1 danh danh    8 Dec 15 17:26 .gitignore
drwxrwxr-x 2 danh danh 4096 Dec 15 17:26 src/
danh@friday:~/workspace/self-study$

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

&lt;/div&gt;



&lt;p&gt;There are 2 major new things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;Cargo.toml&lt;/code&gt;: Cargo config file which contains the current Rust version information and package dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;src&lt;/code&gt;: directory contains Rust source code.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;To build project&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;danh@friday:~/workspace/self-study/hello-world$ cargo build
   Compiling hello-world v0.1.0 (/home/danh/workspace/self-study/hello-world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.31s
danh@friday:~/workspace/self-study/hello-world$ ll target/debug/hello-world
-rwxrwxr-x 2 danh danh 4046280 Dec 15 17:33 target/debug/hello-world

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

&lt;/div&gt;



&lt;p&gt;Note: The binary file is located at &lt;code&gt;./target/debug/&lt;/code&gt; so we can call it directly from there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To build &amp;amp; run in one command&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;danh@friday:~/workspace/self-study/hello-world$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/hello-world`
Hello, world!

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;To build for production grade binary&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;danh@friday:~/workspace/self-study/hello-world$ cargo build --release
   Compiling hello-world v0.1.0 (/home/danh/workspace/self-study/hello-world)
    Finished release [optimized] target(s) in 0.15s
danh@friday:~/workspace/self-study/hello-world$ ll target/
total 24
drwxrwxr-x 4 danh danh 4096 Dec 15 17:38 ./
drwxrwxr-x 5 danh danh 4096 Dec 15 17:33 ../
-rw-rw-r-- 1 danh danh  177 Dec 15 17:33 CACHEDIR.TAG
drwxrwxr-x 7 danh danh 4096 Dec 15 17:33 debug/
drwxrwxr-x 7 danh danh 4096 Dec 15 17:38 release/
-rw-rw-r-- 1 danh danh 1138 Dec 15 17:33 .rustc_info.json

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

&lt;/div&gt;



&lt;p&gt;Note: It differs from the previous build in the &lt;code&gt;--release&lt;/code&gt; keyword. The generated binary is not at &lt;code&gt;./target/debug/&lt;/code&gt; anymore. It is at &lt;code&gt;./target/release/&lt;/code&gt; instead. Moreover, Rust compiler does some optimization stuffs during compiling time to output a faster and better binary. So, if we want to benchmark, perform testing on this production grade binary.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
