Use Declarations in Rust Programming

Mukul Latiyan
Updated on 05-Apr-2021 08:10:30

571 Views

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

227 Views

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

593 Views

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

654 Views

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

Slices in Rust Programming

Mukul Latiyan
Updated on 03-Apr-2021 14:43:14

234 Views

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

Path Struct in Rust Programming

Mukul Latiyan
Updated on 03-Apr-2021 14:40:28

411 Views

Path struct in Rust is used to represent the file paths in the underlying filesystem. It should also be noted that a Path in Rust is not represented as a UTF-8 string; instead, it is stored as a vector of bytes (Vec).ExampleConsider the example shown below − Live Demouse std::path::Path; fn main() {    // Create a `Path` from an `&'static str`    let path = Path::new(".");    // The `display` method returns a `Show`able structure    let display = path.display();    // Check if the path exists    if path.exists() {       println!("{} exists", display);    } ... Read More

Panic Macro in Rust Programming

Mukul Latiyan
Updated on 03-Apr-2021 14:38:40

295 Views

Handling critical errors in Rust is done with the help of panic! Macro. There are other ways to handle errors in Rust, but panic is unique in the sense that it is used to deal with unrecoverable errors.When we execute the panic! Macro, the whole program unwinds from the stack, and hence, it quits. Because of this manner with which the program quits, we commonly use panic! for unrecoverable errors.SyntaxThe syntax of calling a panic looks like this −panic!("An error was encountered");We usually pass a custom message inside the parentheses.ExampleConsider the code shown below as a reference − Live Demofn drink(beverage: ... Read More

Match in Rust Programming

Mukul Latiyan
Updated on 03-Apr-2021 14:32:57

301 Views

Rust provides us with a match keyword that can be used for pattern matching. It is similar to the switch statement in C, and the first arm that matches is evaluated.ExampleConsider the example shown below −fn main() {    let number = 17;    println!("Tell me about {}", number);    match number {       1 => println!("One!")       2 | 3 | 5 | 7 | 11 => println!("A prime"),       13..=19 => println!("A teen"),       _ => println!("Ain't special"),    } }In the above example, we are trying to use a ... Read More

Loop Keyword in Rust Programming

Mukul Latiyan
Updated on 03-Apr-2021 14:31:52

197 Views

Rust provides a loop keyword that allows us to run an infinite loop. The infinite loop indicated by the help of the loop keyword can be broken by the use of the break keyword. Also, we can exit an iteration and continue the infinite loop with the help of the continue keyword.ExampleConsider the example shown below − Live Demofn main() {    let mut count = 0u32;    println!("Infinite loop begins!!");    // Infinite loop    loop {       count += 1;       if count == 4 {          println!("four");         ... Read More

HashMap in Rust Programming

Mukul Latiyan
Updated on 03-Apr-2021 14:29:00

621 Views

HashMap is an important data structure, as it allows us to store data in key-value pairs. In Rust, HashMap stores values by key.HashMap keys can be Boolean, Integer, strings or any other data type that implements the Eq and Hash traits.HashMaps can grow in size, and when the space becomes too excessive, they can also shrink themselves.We can create a HashMap in multiple ways, we can use either HashMap::with_capacity(uint) or HashMap::new().Following are the methods that HashMaps support:insert()get()remove()iter()ExampleLet’s see an example where we build a HashMap and use all these operations stated above.Consider the example shown below. Live Demouse std::collections::HashMap; fn ... Read More

Advertisements