First taste on Rust!
Tags: rust, beginners
Published: 2022-12-15 14:27:20

_I have just finished the [Chapter 1](https://doc.rust-lang.org/stable/book/ch01-00-getting-started.html) 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._

## Rust is a compiled language
Similar to C/C++, Rust needs an entry point to run the whole program (`main` function) and a compiler to transform human readable text into binary file. To compile Rust source code, we use `rustc`:
```
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!

```
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!
## Make the life easy by using Cargo
Rust provides `cargo` to create, build, run and release our Rust project in a convenient way. Over than that, `cargo` help us in package dependency management (similar to `npm` in Javascript world).

**To create new project**:
```
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$

```
There are 2 major new things:
1. `Cargo.toml`: Cargo config file which contains the current Rust version information and package dependencies.
2. `src`: directory contains Rust source code.

**To build project**:
```
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

```
Note: The binary file is located at `./target/debug/` so we can call it directly from there.

**To build & run in one command**:
```
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!

```

**To build for production grade binary**:
```
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

```
Note: It differs from the previous build in the `--release` keyword. The generated binary is not at `./target/debug/` anymore. It is at `./target/release/` 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.