Found 34 Articles for Rust Programming

File Operations in Rust Programming

Mukul Latiyan
Updated on 03-Apr-2021 12:50:15


File operations normally include I/O operations. Rust provides us with different methods to handle all these operations.OpenRust provides an open static method that we can use to open a file in read-only mode.ExampleConsider the code shown below:use std::fs::File; use std::io::prelude::*; use std::path::Path; fn main() {    // Create a path to the desired file    let path = Path::new("out.txt");    let display = path.display();    let mut file = match File::open(&path) {       Err(why) => panic!("unable to open {}: {}", display, why),       Ok(file) => file,    };    let mut s = String::new();    match ... Read More

Enums in Rust Programing

Mukul Latiyan
Updated on 03-Apr-2021 14:25:50


Also referred to as enumerations, enums are very useful in certain cases. In Rust, we use enums, as they allow us to define a type that may be one of a few different variants.Enumerations are declared with the keyword enum.Example Live Demo#![allow(unused)] #[derive(Debug)] enum Animal {    Dog,    Cat, } fn main() {    let mut b : Animal = Animal::Dog;    b = Animal::Cat;    println!("{:?}", b); }OutputCatZero-variant EnumsEnums in Rust can also have zero variants, hence the name Zero-variant enums. Since they do not have any valid values, they cannot be instantiated.Zero-variant enums are equivalent to the never ... Read More

Constants in Rust Programming

Mukul Latiyan
Updated on 03-Apr-2021 14:09:33


Rust provides us with two types of constants. These are −const − an unchangeable valuestatic − possible mutable value with static lifetime.If we try to assign another value to an already declared const value, the compiler will throw an error.ExampleConsider the example shown below − Live Demostatic LANGUAGE: &str = "TutorialsPoint-Rust"; const THRESHOLD: i32 = 10; fn is_small(n: i32) -> bool {    n < THRESHOLD } fn main() {    // Error! Cannot modify a `const`.    THRESHOLD = 5;    println!("Everything worked fine!"); }In the above code, we are trying to modify the value of a variable that is ... Read More

Channels in Rust Programming

Mukul Latiyan
Updated on 03-Apr-2021 14:06:35


Channels are a medium that allow communication to take place between two or more threads. Rust provides asynchronous channels that enable communication between threads.Channels in Rust allow a unidirectional flow of communication between two endpoints. These two endpoints are Sender and Receiver.ExampleConsider the example shown below −use std::sync::mpsc::{Sender, Receiver}; use std::sync::mpsc; use std::thread; static NTHREADS: i32 = 3; fn main() {    let (tx, rx): (Sender, Receiver) = mpsc::channel();    let mut children = Vec::new();    for id in 0..NTHREADS {       let thread_tx = tx.clone();       let child = thread::spawn(move || {   ... Read More

Casting in Rust Programming

Mukul Latiyan
Updated on 03-Apr-2021 14:05:17


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: Live Demo// Suppress all warnings from casts which overflow. #![allow(overflowing_literals)] fn main() {    let decimal = 65.43_f32;    // Error! No implicit conversion    // ... Read More

Arrays in Rust Programming

Mukul Latiyan
Updated on 03-Apr-2021 13:58:35


An array is a data structure that is used to contain objects of the same type T. These objects are stored in contiguous memory locations.In Rust, the arrays are created by making the use of [ ] brackets. It is important that the size of the arrays must be known at the compile time. Also, unlike other languages like Java and C++, the length of the array is part of its type in Rust.[T; length].This means that an array of signature say, [i32, 4], is different from an array with the signature [i32, 5].Arrays can be immutable in nature and there ... Read More

Functions in Rust programming language

Mukul Latiyan
Updated on 20-Feb-2021 08:09:31


Functions are building blocks of code. They allow us to avoid writing similar code and also help in organizing large section of code into reusable components.In Rust, functions can be found everywhere. Even function definitions are also statements in Rust.One of the most important function in Rust is the main function, which is the entry point of any software or application.We make use of the fn keyword to declare a function in Rust.In Rust, the names of the functions make use of snake case as the conventional style. In snake case, all the letters of the word are in lower ... Read More

Rust programming language – Getting Started

Mukul Latiyan
Updated on 20-Feb-2021 06:53:00


The first part of getting hands-on experience with Rust is installing Rust. In order to install Rust, we need a Rust installer.Rustup is a version management tool and also an installer that helps in installing Rust in your local machine.If you’re running Linux, macOS, or another Unix-like OS, then we simply need to run the following command in the terminal −curl --proto ‘=https’ --tlsv1.2 -sSf | shThe above command will install Rust on your local machine.If you are on Windows, then you can download the .exe file from this link rustup-init.exeKeeping Rust UpdatedThough Rust is updated frequently, but you ... Read More

Why is Rust Programming language loved so much?

Mukul Latiyan
Updated on 20-Feb-2021 06:49:30


Being voted as the most loved language for four consecutive years, Rust has come a long way.Originally designed as a low-level language, best suited for embedded, systems and critical performance code, it has gained a lot of traction and not stopping yet.Rust also has found its way in web developments and also provides a great opportunity for game developers.So, why is Rust so much loved? Let’s explore some of the reasons why it is so popular and loved.Fearless ConcurrencyConcurrency and parallelism have become more significant in today’s scenario. The number of cores is increasing in computers to support concurrency, but ... Read More

Downsides of Rust Programming Language

Mukul Latiyan
Updated on 20-Feb-2021 06:48:17

2K+ Views

Every programming language has some downsides attached to it, it’s not all roses when it comes to Rust as well. Some of the noticeable downsides of Rust programming language are highlighted here −Compile TimeYes, compile time. Rust is fast, no doubt. But when it comes to compiling code, then it’s a bit slower as compared to its peer languages. The reason for its slow compile time is that its “unit of compilation” is not an individual file, it’s instead a whole package (known as a crate). Crates in Rust can include multiple modules, thus they can be large units of ... Read More