Rust Basics

04 Jul, 2022
In last 2 decades the computers and the internet has made an increasing demand, and with evolution of new technologies, devices and protocols the programming languages also being updated regularly but still most of the early programming languages like C, 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 highly concurrent and highly safe system. Rust language has an emphasis on safety, control of memory layout and concurrency.
Rust is a multi-paradigm programming language like C++ syntax was designed for performance and safety, especially safe concurrency  by using a borrow checker and ownership to validate references. Rust was developed byGraydon 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.
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 developer to write safe code. Unlike C, it does not provide memory unsafe thing like dangling pointer, uninitialized pointer and NULL pointer.
Rust is Low-Overhead: 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.
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.
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
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.
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.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.Memory Management: A programmer’s performance also depends on how language manage memory internally. Rust works on RAII , unlike java’s garbage collection. Add on to this reference counting is also available to developer but that is optional.
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.
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.
Memory Management: A programmer’s performance also depends on how language manage memory internally. Rust works on RAII , unlike java’s garbage collection. Add on to this reference counting is also available to developer but that is optional.
Cargo is the 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.
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
sayanc170
Rust-basics
Rust
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Generate Random Numbers in Rust Language?
Rust - HashMaps
Rust - Creating a Library
Variables in Rust
Standard I/O in Rust
How to Install Rust on Termux?
Rust - Array
Rust - Traits
How to Install Rust in MacOS?
Rust - Generic Function