Open In App

Rust Basics

In the last 2 decades, computers and the internet have made an increasing demand, and with the evolution of new technologies, devices, and protocols the programming languages are also being updated regularly but still most of the early programming languages like C, and C++ have shown some drawbacks. These drawbacks motivated others to create new programming languages like Go, Rust, Python, and more. In these tutorials, we will talk about one of these programming languages. Rust language is intended for a highly concurrent and highly safe system. Rust language has an emphasis on safety, control of memory layout, and concurrency.

Rust Programming Language:

Rust is a multi-paradigm programming language like C++ syntax that was designed for performance and safety, especially safe concurrency by using a borrow checker and ownership to validate references. Rust was developed by Graydon Hoare at Mozilla research with contributions from Dave Herman, Brendan Eich, and others. Which achieves memory safety without garbage collection. Rust is a compiled system programming language.



Why Rust?

Rust has many reasons for being popular among programmers. Below are the reasons are :

An example program in rust saved with extension .rs






fn main() {println!("geeks for geeks");}

Output:

geeks for geeks

We can Install Rust by using terminal. For Linux and macOS open our terminal and use the curl which can automatically install rust for us, we can refer Rust docs for windows installation

$ curl –proto ‘=https’ –tlsv1.2 https://sh.rustup.rs -sSf | sh

We can check whether we have Rust installed correctly, open a shell and enter this command

$ rustc –version

Features of Rust Programming Language

Rust Programming Language creating and maintaining boundaries that preserve large-system integrity. Rust is a multi-paradigm programming language. it is designed for safety and performance. There are some features which makes it different.

  1. Ownership: InRust Programming Language all values have a unique owner, and the scope of the value is same as scope of the owner That’s why It has an ownership system. Values can be passed by immutable reference and mutable reference, using &T and &mut T, or by value, using T. there are either be multiple immutable references or one mutable reference.
  2. Memory Safety: When it comes to memory safety, Rust inspires developer to write safe code. Unlike C, it does not provide memory unsafe thing like dangling pointer, uninitialized pointer and NULL pointer. Which in result, code becomes more safe and stable. It has define format to initialize data value. And similar to C, it does provide control to handle lifetime of a variable through added syntax. Apart from this it also provide flexibility to write unsafe code with unsafe keyword which Ideally should be avoided until there is no other way.
  3. Memory Management: A programmer’s performance also depends on how the language manages memory internally. Rust works on RAII , unlike java’s garbage collection. Add on to this reference counting is also available to developers but that is optional.

What is Cargo?

Cargo is Rust’s build system and package manager, like pip for Python, gem for Ruby and npm for Javascript. Cargo handles a lot of tasks, such as building and compiling your code, downloading the libraries your code depends on, and building those libraries(dependencies). Cargo mostly comes pre-installed with Rust.

Cargo handles calling rustc (Rust compiler) with correct parameters.




$ rustc main.rs
$ ./main
Hello, GFG

You can check cargo from the below command, if you don’t see version number that means you haven’t installed cargo

$ cargo –version

We can create a new rust project using cargo, for that use the below commands.

cargo new gfg

cd gfg

cargo new command creates a new cargo project in the specified directory. the directory contains, cargo.lock, cargo.toml, src files of the project.

[package]

name = “gfg”

version = “0.1.0”

authors = [“Your Name <you@example.com>”]

edition = “2018”

[dependencies]

We can run the project using any of the below commands.

// compiled out put

cargo build

// runs the compiled output

cargo run

// check the output

cargo check

rust and cargo


Article Tags :