Skip to main content

Rust for absolute beginners Part 1: Introduction

Introduction to Rust

Table of content

  1. Introduction to Rust
  2. Installing of Rust and Environment setup
  3. Introduction to Cargo
    1. cargo new hello_world
    2. cargo build
    3. cargo run
    4. cargo build --release
    5. cargo clean
  4. Running Hello world application
  5. Understanding the code
    1. code block
    2. function
    3. macros
    4. cargo.toml / cargo.lock
  6. Debugging using codeLLDB

Introduction to Rust 

Rust is a multi-paradigm, general-purpose programming language designed for performance and safety, especially safe concurrency. Rust is syntactically similar to C++, but can guarantee memory safety. It was created at Mozilla foundations. 

Installing of Rust and Environment setup

Rust is easy to install. The most recommended way to install Rust programming language is through rustup tool. 

The command to install rustup tool on linux/mac is :

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

In the Rust development environment, all tools are installed to the ~/.cargo/bin directory, and this is where you will find the Rust toolchain, including rustc, cargo, and rustup

To check the installed rust version run  rustc --version command in the terminal to verify rust is installed successfully.

    $ rustc --version
    rustc 1.57.0 (f1edd0429 2021-11-29)
    $

Introduction to Cargo

Cargo is the Rust package manager. Cargo lets you download crates, compiles your code, make your own crates and upload them to https://crates.io . Crates IO is a centralized crate hosting site. We can check required crates and import them to use in our application.

Below are the common cargo commands :

cargo new <package_name> : to create a new application or package.

cargo build : is used to build and application. The new build is located in the target folder.

cargo run : is used to build and run the application.

cargo build --release : is used to release a production ready application. 

cargo clean : is used to clean the previous build and delete the target folder.

To create a new cargo application, run cargo new command with package name.

    $ cargo new hello_world
     Created binary (application) `hello_world` package
    $

 cargo new command creates a new rust application structure. 

/src  folder includes the rust source code,

/target folder contains the builds,

.gitignore file,

Cargo.toml and Cargo.lock file.

Overall structure of the application looks as below.

Running Hello world application

In the /src folder you see a main.rs file. This is the starting point of the application. This file contains the main method. When we run the Rust application, the rust runtime calls the main() function from main.rs file.

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

To run the application we can run the cargo run command in terminal in the root folder of the application. In the above example run the command in hello_world folder.

 
$ cargo run
Compiling hello_world v0.1.0 (/Users/shankernaik/code/rust/absolute-rust/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 1.97s
Running `target/debug/hello_world`
Hello, world!
$
 

As you can see the cargo compiles the application and run the application with the output as Hello, world!

Understanding the code

Code block in Rust programming language start with a { opening curly bracket and ends with a } closing curly bracket. Code block is used to define lifetime and scope for a variable.

Function in Rust programming language starts with the keyword fn followed by function main. In the hello_world application the main() is a function. It is the starting point of the application. We can define our own functions.

Macros in Rust programming are nothing but precompiled functions. Macros end with a bang character (!). We can define our own macros.In the above example println!() is a macro that prints information to the output stream (Default to console).

cargo.toml is dependency management file. It specifies the name, version and edition of the current application. It also lists down third party crate dependencies with version number if any. Below is the default content of cargo.toml file when a new application is created. As you can see there are no dependencies to the hello_world application.

 
[package]
name = "hello_world"
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 is an auto-generated file by cargo. Not intended to be modified manually. It locks the dependencies of the application.

Debugging using codeLLDB

You can use codeLLDB vscode extension to debug the application. Link to codeLLDB

Thank you folks, if you like my post do check my other posts on Django with Python 3 and Ruby on Rails  on SWE crunch

Most viewed

Ruby on rails part 6 - Blocks , lambda, procs and closure

 Blocks , lambda, procs  and closure Table of content  1. Blocks 2. Lambda 3. Procs 4. Closure Blocks  Ruby blocks are little anonymous functions that can be passed into methods. Blocks are enclosed in a do-end statement or between brackets {} 
. Blocks can have multiple arguments
. The argument names are defined between two pipe | characters. Blocks are typically used with ‘each’ method which iterates over a list. Syntax of block using {} ['List of items'].each { | block arguments|  block body }  Syntax of block using do-end ['List of items'].each do | block arguments |      # block body end Example of block declared as do-end with each method.   [ 1 , 2 , 3 ].each do |num| puts num end     Output   $ ruby block_with_each.rb 1 2 3 $    Blocks can also be saved in variables or passed as argument to another function.   yield is a Ruby keyword that is used to call a block. When you use the yield keyword, the code inside the block will run. Example of saving a bl