Open In App

Rust Basics

Improve
Improve
Like Article
Like
Save
Share
Report

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 :

  • Rust is Fast: Rust Programming Language is a multi-paradigm programming language similar to C++ syntax. Thus it becomes very easy to learn Rust for anyone. Across multiple platforms, Rust code compiles to native machine code.
  • Rust is Memory Safe: Rust inspires developers to write safe code. Unlike C, it does not provide memory unsafe thing like dangling pointers, uninitialized pointers, and NULL pointers.
  • Rust is Low-Overhead: InRust Programming Language all values have a unique owner, and the scope of the value is the same as the scope of the owner That’s why It has an ownership system.
  • Rust is flexible: Rust is designed for performance and safety, especially safe concurrency by using a borrow checker and ownership to validate references. 
  • Rust is easy to use: Rust Programming Language syntax is similar to C++ language syntax so it is easy to use or easy to understand.
  • Rust is statically and strongly typed:  Rust is built in such a way that the code can is checked at compile time and if the compilation fails then there would be no accompanying extra memory usage.
  • Binding with C programs: Rust provides a C API that provides memory safety and just like vectors it makes use of high-level functions.
  • Threads without Data race: A condition where more than two or more threads access the memory is called Data race. As Rust uses ownership concepts, a data race condition is not applicable here.

An example program in rust saved with extension .rs

RUST




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.

Rust




$ 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.

  • cargo.lock – lock file for the project
  • cargo.toml – contains details and dependencies of the project, am example file is shown below.

[package]

name = “gfg”

version = “0.1.0”

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

edition = “2018”

[dependencies]

  • src – directory containing the source files of the project, main.rs file is main file for the project which will be created by default

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



Last Updated : 04 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads