Rust: starting with blazingly fast programming language
How I heard about Rust.
As a regular user/contributor of one of popular tech forum stack overflow, I read almost all the blog post on stack-overflow. Every year they hold programming survey and Rust is on top since last 4-5 years. you can read about this survey here.
First look:
As a curious user, I tried to take a look at the language website, which talks about why Rust and many other things. First question came to my mind was, why another language? what are we trying to solve? there are many other languages out there along with popular frameworks and thriving developer communities .
I come from Java development background and have been building web-apps since 2012. So the first statement that caught my attention, is that its blazingly fast.
What is it that makes Rust so fast?
-
Static typing:
Its statically typed language. So Rust compiler knows what type of
data is going to be stored in particular type. So the compiler can optimize many things beforehand and
less surprises at runtime.
-
Runtime: Rust doesn't use virtual machine. It directly run on target system.
You don't need any runtime to run executable built by Rust. This is not
the case with other virtual machine based languages like Java. So no
abstraction, no intermediate system means closer to the system.
-
No Garbage collector: Coming from GC based languages, first I was kind of shocked by this
feature. No GC? So does that mean I have to allocate and
deallocate the memory and scratch my head solving segmentation fault
problems. But its very beautifully handled in Rust. Rust have
concept of memory ownership model. It takes cares of the memory
allocation and deallocation so well, that you don't need GC.
So there are many such features but I will write those in detail sometime in future.
Installing Rust on windows:
- Go to https://www.rust-lang.org/tools/install to install latest Rust version. Download RUSTUP-INIT.EXE (32-bit or 64-bit) as per your system.
- Run RUSTUP-INIT.EXE and follow the further instructions.
Installing Rust on mac/linux/unix based systems:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Running first Rust program:
-
compile program using
rustc hello_world.rs
-
Above command generates 2 more files in current directory along with
original source file.
-
The hello_world.exe is the final executable file that we have to run.
Cargo:
Basic cargo commands:
- Check cargo version:
- Create new rust project:
- Building Rust project:
- Build and Run Rust project in one step:
- Cargo check:
- Building for release:
C:\Users\SBELOSE> cargo --version cargo 1.47.0 (f3c7e066a 2020-08-28)
C:\Users\SBELOSE> cargo new hello_cargo Created binary (application) `hello_cargo` package
C:\Users\SBELOSE\hello_cargo> cargo build Compiling hello_cargo v0.1.0 (C:\Users\SBELOSE\hello_cargo) Finished dev [unoptimized + debuginfo] target(s) in 2.31s
C:\Users\SBELOSE\hello_cargo> cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.02s Running `target\debug\hello_cargo.exe` Hello, world!
C:\Users\SBELOSE\hello_cargo> cargo check Checking hello_cargo v0.1.0 (C:\Users\SBELOSE\hello_cargo) Finished dev [unoptimized + debuginfo] target(s) in 0.17s
C:\Users\SBELOSE\hello_cargo>cargo build --release
Comments