Found 33 Articles for Rust Programming

Comments in Rust Programming

Mukul Latiyan
Updated on 21-May-2021 12:26:33
Comments in Rust are statements that are ignored by both the rust compiler and interpreter. They are mainly used for human understanding of the code.Generally, in programming, we write comments to explain the working of different functions or variables or methods to whosoever is reading our code.Comments enhance the code readability, especially when the identifiers in the code are not named properly.In Rust, there are multiple ways in which we can declare comments. Mainly these are −Single-line commentsMulti-line commentsDoc commentsIn this article, we will explore all the three comments.Single-Line commentSingle line comments in Rust are comments that extend up to ... Read More

While and For Range in Rust Programming

Mukul Latiyan
Updated on 05-Apr-2021 08:29:42
We know that Rust provides a loop keyword to run an infinite loop. But the more traditional way to run loops in any programming language is with either making use of the while loop or the for range loop.While LoopThe while loop is used to execute a code of block until a certain condition evaluates to true. Once the condition becomes false, the loop breaks and anything after the loop is then evaluated. In Rust, it is pretty much the same.ExampleConsider the example shown below:Live Demofn main() {    let mut z = 1;    while z < 20 { ... Read More

Vectors in Rust Programming

Mukul Latiyan
Updated on 05-Apr-2021 08:23:50
Vectors in Rust are like re-sizable arrays. They are used to store objects that are of the same type and they are stored contiguously in memoryLike Slices, their size is not known at compile-time and can grow or shrink accordingly. It is denoted by Vec in RustThe data stored in the vector is allocated on the heap.ExampleIn the below example, a vector named d is created using the Vec::new(); function that Rust provides.fn main() {    let mut d: Vec = Vec::new();    d.push(10);    d.push(11);    println!("{:?}", d);    d.pop();    println!("{:?}", d); }We push the elements into a ... Read More

Using Threads in Rust Programming

Mukul Latiyan
Updated on 05-Apr-2021 08:13:32
We know that a process is a program in a running state. The Operating System maintains and manages multiple processes at once. These processes are run on independent parts and these independent parts are known as threads.Rust provides an implementation of 1:1 threading. It provides different APIs that handles the case of thread creation, joining, and many such methods.Creating a new thread with spawnTo create a new thread in Rust, we call the thread::spawn function and then pass it a closure, which in turn contains the code that we want to run in the new thread.ExampleConsider the example shown below:use ... Read More

Use Declarations in Rust Programming

Mukul Latiyan
Updated on 05-Apr-2021 08:10:30
Use Declarations in Rust are used to bind a full path to a new name. It can be very helpful in cases where the full path is a bit long to write and invoke.In normal cases, we were used to doing something like this:use crate::deeply::nested::{    my_function,    AndATraitType }; fn main() {    my_function(); }We invoked the use declaration function by the name of the function my_function. Use declaration also allows us to bind the full path to a new name of our choice.ExampleConsider the example shown below:// Bind the `deeply::nested::function` path to `my_function`. use deeply::nested::function as my_function; ... Read More

Unsafe Operation in Rust Programming

Mukul Latiyan
Updated on 05-Apr-2021 08:06:59
Unsafe Operations are done when we want to ignore the norm that Rust provides us. There are different unsafe operations that we can use, mainly:dereferencing raw pointersaccessing or modifying static mutable variablescalling functions or methods which are unsafeThough it is not recommended by Rust that we should use unsafe operations at all, we should use them only when we want to bypass the protections that are put in by the compiler.Raw Pointers In Rust, the raw pointers * and the references &T perform almost the same thing, but references are always safe, as they are guaranteed by the compiler to point ... Read More

Super and Self Keywords in Rust Programming

Mukul Latiyan
Updated on 05-Apr-2021 07:44:24
Whenever we want to remove the tedious long importing paths of functions that we want to invoke, either from the same function or from a different module, we can make use of the super and self keywords provided in Rust.These keywords help in removing the ambiguity when we want to access the items and also prevent unnecessary hardcoding of paths.ExampleConsider a simple example shown below:fn function() {    println!("called `function()`"); } mod cool {    pub fn function() {       println!("called `cool::function()`");    } } mod my {    fn function() {       println!("called `my::function()`");   ... Read More

Structs in Rust Programming

Mukul Latiyan
Updated on 03-Apr-2021 14:46:42
Structs in Rust are user-defined data types. They contain fields that are used to define their particular instance.We defined structs with the help of the struct keyword followed by the name we want for the struct. The name of the struct should describe the significance of the pieces of data that we are grouping together inside it.Syntaxstruct Employee {    id: i32,    name: String,    leaves: i8, }The above syntax contains the name of the struct and inside the curly braces, we have different fields, namely, id which is of type i32, name and leaves.Creating an InstanceTo create an ... Read More

Struct Visibility in Rust Programming

Mukul Latiyan
Updated on 03-Apr-2021 13:31:24
Structs in Rust contains an extra level of visibility. These can be modified by the developers as per his/her convenience.In a normal scenario, the visibility of the struct in Rust is private and it can be made public by making use of the pub modifier.It should be noted that this case of visibility only makes sense when we are trying to access the struct fields from outside the module, from where it is defined.When we are hiding the fields of the struct, we are simply trying to encapsulate the data.ExampleConsider the example shown below −mod my {    // A ... Read More

Slices in Rust Programming

Mukul Latiyan
Updated on 03-Apr-2021 14:43:14
Slices in Rust are a collection of elements of the same data type T, but unlike arrays, it is not necessary that their length is known at compile time.In Rust, a slice is a two-word object, where the first word is actually a pointer to the data and the second word is simply the length of the slice.Slices are much safer to use than arrays, and they allow efficient access to an array without copying. Slices are created from an array, string. They can be both mutable and not. A slice generally refers to slice of an array or a ... Read More
Advertisements