Rust: starting with blazingly fast programming language

0
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 rust programming lang, which talks about what is rust programming language 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 rust is blazingly fast.

What is it that makes Rust so fast?

  1. Static typing:  rust is 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.
  2. Runtime: Rust doesn't use virtual machine. It directly  runs 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.
  3. 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 the rust programming language. 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:

  1. 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.
  2. Run RUSTUP-INIT.EXE and follow the further instructions. 
Note: Rust needs Visual C++ build tools which are large in size so, the first installation may takes some time to install. This took lot of time downloading and installing on my system. It's huge in size.

Installing Rust on mac/linux/unix based systems:

 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh      

Running first Rust program:

Rust is similar to java in basic program run. In java, we compile the program using javac and then run using java command. Similar two steps are required for running Rust program.
  1. compile program using
     rustc hello_world.rs
  2. Above command generates 2 more files in current directory along with original source file.
  3. The hello_world.exe is the final executable file that we have to run.

Cargo:

Cargo is the build tool and rust package manager similar to what is maven for java developers. It makes developers life easy starting from creating new project, managing dependency and building/running the final executables. rust cargo is Rust’s build system and package manager. I will not write in every detail command about rust cargo in this article,  may be in another article, but its super cool and looks familiar, may be because I have worked with maven/gradle in past.

Cargo comes bundled with official rust installation. 

Basic cargo commands:

  1. Check cargo version:
  2. C:\Users\SBELOSE> cargo --version
           cargo 1.47.0 (f3c7e066a 2020-08-28)     
  3. Create new rust project:
  4. C:\Users\SBELOSE> cargo new hello_cargo
          	Created binary (application) `hello_cargo` package    
    Open the created project and you will see the below folder structure: The best part of cargo package manager is it gives you git as default version control system. No need to initialize new git repo. It follows principle of Convention over configuration. It also creates something called cargo.toml which is kind of pom.xml for maven. You can add/edit basic information about your project along with dependencies information.



  5. Building Rust project:
  6. 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     
  7. Build and Run Rust project in one step:
  8. C:\Users\SBELOSE\hello_cargo> cargo run
            Finished dev [unoptimized + debuginfo] target(s) in 0.02s
         	Running `target\debug\hello_cargo.exe`
    		Hello, world!     
  9. Cargo check:
  10. 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     
    This is interesting command. rust cargo check neither produce resulting executable nor run your project. Its just check correctness of your code. Something like how our code is checked in real time in IDE's. So this command is faster than cargo build command.

  11. Building for release:
  12. C:\Users\SBELOSE\hello_cargo>cargo build --release
    This command is used for building your project for release with applying optimization on your code. This makes your code run faster in production environment. But it takes some time to build your executable. The resulting executable is also present in target/release/ instead of target/debug.

Post a Comment

0Comments

Please Select Embedded Mode To show the Comment System.*

Cookies Consent

This website uses cookies to offer you a better Browsing Experience. By using our website, You agree to the use of Cookies

Privacy Policy