Getting started with Rust language from JavaScript  knowledge

Photo by Doan Tuan on Unsplash

Getting started with Rust language from JavaScript knowledge

Β·

3 min read

I have no experience with Rust language at all. So when I try Rust, maybe something I don't understand at the moment I write this. But I want to give it a try!

Preferences

🌱 Getting started

After installing Rush language based on your using OS. To verify the successful installation:

rustc --version
# output: rustc 1.67.0 (fc594f156 2023-01-24)
cargo --version
# output: cargo 1.67.0 (8ecd4f20a 2023-01-10)

🍏 Generating a new project

cargo new hello-rust
cd hello-rust
ls
code .

Next, we need to prepare the development environment. For VSCode, I'm using Rust analyzer extension. Other extensions:

  • even-better-toml - Syntax highlighting and validation for TOML documents (Cargo.toml)
cargo run

Output:

  • Compiling - hello-rust v0.1.0 (path\to\hello-rust)

  • Finished - dev [unoptimized + debuginfo] target(s) in 0.71s

  • Running - target\debug\hello-rust.exe

cargo build

Output:

  • Finished dev [unoptimized + debuginfo] target(s) in 0.00s
cargo test

Output:

  • Compiling hello-rust v0.1.0 (path\to\hello-rust)

  • Finished test [unoptimized + debuginfo] target(s) in 0.32s

  • Running unittests src\main.rs (target\debug\deps\hello_rust-e2e48f3e969733ad.exe)

🌽 Source code

Cargo.toml:

[package]
name = "hello-rust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

Cargo.lock :

# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "hello-rust"
version = "0.1.0"

.gitignore :

/target

src\main.rs :

fn main() {
    println!("Hello, world!");
}

Let's view deeper in ignored folder:

tree .

My running OS is Windows, so the output is:

PATH\TO\HELLO-RUST
β”œβ”€β”€β”€src
└───target
    └───debug
        β”œβ”€β”€β”€.fingerprint
        β”‚   β”œβ”€β”€β”€hello-rust-56ddd413b0849874
        β”‚   β”œβ”€β”€β”€hello-rust-5820db0b29385df2
        β”‚   β”œβ”€β”€β”€hello-rust-71cc4f1d00a50391
        β”‚   └───hello-rust-e2e48f3e969733ad
        β”œβ”€β”€β”€build
        β”œβ”€β”€β”€deps
        β”œβ”€β”€β”€examples
        └───incremental
            β”œβ”€β”€β”€hello_rust-13xvhef7ifhwo
            β”‚   └───s-gi0ldsfjao-e7nyh-1a9bmzoyjcn30
            β”œβ”€β”€β”€hello_rust-2mpimy89ql4lt
            β”‚   └───s-gi0ldsfj7a-rw39ce-2a18ew5zcrcaz
            β”œβ”€β”€β”€hello_rust-3e9t63lxisjk4
            β”‚   └───s-gi0lv1s4s4-14t6l41-9wekp6y89fms
            └───hello_rust-p3ci3gxgh4dj
                └───s-gi0lvjh8bc-sqyxds-3eqvyiv620wj4

The main folder structure:

β”œβ”€β”€ Cargo.toml
└── src
    └── main.rs

🍍 Compared categories

Here are the JS equivalents:

categoryRustJSdifference
Package managercargonpm, pnpm, yarncargo is also a build tool
Package manager file configurationCargo.tomlpackage.jsoncargo is using the TOML format
Package library dependencies[dependencies]"dependencies"?
Package library dependencies for development[dev-dependencies], [build-dependencies]"devDependencies"With npm, other option is peerDependencies, peerDependenciesMeta, bundleDependencies, optionalDependencies
community’s crate registrycrates.ionpmjs.com?
Add dependenciescargo addyarn add?
Remove dependenciescargo removeyarn remove?
run your project withcargo runpnpm startcargo used compiler - rustc and execute the compiled file
compiling scriptcargo buildnoneJavaScript is an interpreted language, not a compiled language
coding conventiontab size is 4 spacestab size is 2 spaces
function syntax keywordfnfunction
............

I think I need to rest now, too many things to learn!