 
 Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1262 of 3366
 
 
			
			332 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: Live Demofn 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 ... Read More
 
 
			
			326 Views
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
 
 
			
			187 Views
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
 
 
			
			259 Views
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
 
 
			
			761 Views
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
 
 
			
			408 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: 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
 
 
			
			636 Views
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
 
 
			
			564 Views
PHP 8 uses a new built-in exception ValueError. PHP throws this exception when we pass a value to a function, which has a valid type but cannot be used for operation. In the earlier versions of PHP, we used to get a Warning error in such cases, but PHP 8 will show a ValueError.Example: ValueError in PHP 8OutputFatal error: Uncaught ValueError: array_rand(): Argument #1 ($array) cannot be emptyExample Live DemoOutputbool(false)Example: ValueError in PHP 8OutputFatal error: Uncaught ValueError: array_rand(): Argument #1 ($array) cannot be empty
 
 
			
			328 Views
The resource is a type of variable that holds a reference to an external resource. The resource can be a filehandle, a database connection, or a URL handle. Every resource is identified by a unique id. In the previous versions of PHP, we needed to cast a resource to int to get the resource id.Example: get_recource_id using int.Output1In PHP 8, the get_resource_id() function always returns an int. It is used to get the ID for a given resource. This function always guarantees type safety.Example: Using get_recource_id in PHP 8.Output1
 
 
			
			319 Views
In the previous versions of PHP, if we wanted to catch an exception, then we needed it to store in a variable to check whether that variable is used or not.Before PHP 8, to handle the exception catch block, we needed to catch the exception (thrown by the try block) to a variable.Example: Capturing Exception Catches in PHPExplanation − In the above program, the exception is being caught by the catch block to a variable $e. Now the $e variable can hold any information about the exception as code, message, etc.PHP 8 introduced non-capturing catches. Now, it is possible to ... Read More