Mukul Latiyan

Mukul Latiyan

363 Articles Published

Articles by Mukul Latiyan

Page 6 of 37

Casting in Rust Programming

Mukul Latiyan
Mukul Latiyan
Updated on 11-Mar-2026 455 Views

Casting or explicit conversion is only allowed in Rust, there’s no implicit conversion that the compiler of Rust does for us. It is known that, in many cases, implicit conversion can lead to data losses, which is not a good thing.Rules for converting between different types is pretty similar to C. In Rust though, we make use of as keyword when we want to convert from one type to another.Example:Consider the following example:// Suppress all warnings from casts which overflow. #![allow(overflowing_literals)] fn main() {    let decimal = 65.43_f32;    // Error! No implicit conversion    // let ...

Read More

From and Into Traits In Rust Programming

Mukul Latiyan
Mukul Latiyan
Updated on 11-Mar-2026 363 Views

From and Into are two traits that Rust provides us. They are internally linked.From TraitWe make use of From trait when we want to define a trait to how to create itself from any other type. It provides a very simple mechanism with which we can convert between several types.For example, we can easily convert str into a String.ExampleConsider the example shown below:fn main() {    let my_str = "hello";    let my_string = String::from(my_str);    println!("{}", my_string); }OutputhelloWe can even convert our own types.ExampleConsider the example shown below:use std::convert::From; #[derive(Debug)] struct Num {    value: i64, } impl From ...

Read More

HashMap in Rust Programming

Mukul Latiyan
Mukul Latiyan
Updated on 11-Mar-2026 686 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.use std::collections::HashMap; fn call(number: ...

Read More

Loop Keyword in Rust Programming

Mukul Latiyan
Mukul Latiyan
Updated on 11-Mar-2026 235 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 −fn main() {    let mut count = 0u32;    println!("Infinite loop begins!!");    // Infinite loop    loop {       count += 1;       if count == 4 {          println!("four");          continue; ...

Read More

Match in Rust Programming

Mukul Latiyan
Mukul Latiyan
Updated on 11-Mar-2026 343 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

Panic! Macro in Rust Programming

Mukul Latiyan
Mukul Latiyan
Updated on 11-Mar-2026 344 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 −fn drink(beverage: &str) ...

Read More

Path Struct in Rust Programming

Mukul Latiyan
Mukul Latiyan
Updated on 11-Mar-2026 461 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 −use 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

Unsafe Operation in Rust Programming

Mukul Latiyan
Mukul Latiyan
Updated on 11-Mar-2026 273 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

Vectors in Rust Programming

Mukul Latiyan
Mukul Latiyan
Updated on 11-Mar-2026 480 Views

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

While and For Range in Rust Programming

Mukul Latiyan
Mukul Latiyan
Updated on 11-Mar-2026 233 Views

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:fn main() {    let mut z = 1;    while z < 20 {   ...

Read More
Showing 51–60 of 363 articles
« Prev 1 4 5 6 7 8 37 Next »
Advertisements