{

Learn free rust tutorials


Difference between iter, iter_mut, and into_iter in Rust example

February 28, 2022 ·  2 min read

Rust provides different iterators to iterate an element in a Collection. It provides three types of methods to iterate a vector or array. iter() iter_mut() into_iter() The above three provide an iterator to navigate each element of a collection of objects. Then, What is the difference between those? Difference between iter, into_iter, and iter_mut? iter() function These functions iterate elements using reference or pointer. Let’s see an example of how to iterate a vector v...


Difference between String and str in Rust example

February 28, 2022 ·  1 min read

In Rust, We have different below string objects. Let’s see the difference between String and Str types in Rust. What is the difference between String and Str in Rust? String: It is std::String, String data type, and data stored on Heap str: It is a primitive String type in Rust used as a String slice or literal that point to a Fixed UTF-8 byte array, Represents as *Char. str accessed using &str The string has capacity and length...


How to capitalize the first letter of a string in Rust example

February 28, 2022 ·  1 min read

This tutorial shows you how to capitalize the first letter of a string. For example, if the given string is welcome, Output is Welcome. Rust capitalizes the first character of a string Here is an example program to capitalize the case of a first character. In this example, Create a function capitalize_first_letter that takes a string argument and returns a string. Inside a function, the first letter slice is extracted and converted to uppercase using to_uppercase next, takes a string slice except for the first letter, Append these two strings using +...


How to concatenate a Strings in Rust example

February 28, 2022 ·  2 min read

In this example, You’ll find multiple ways to concatenate a string in rust with examples. For example, you have two strings declared in Rust. let first = "First"; let second = "Second"; After appending the strings, the Result is First Second How to append a string in Rust There are multiple ways we can use to concatenate Strings in Rust use join method join function concatenates with separator string. The separator can be space, tab(\t), or our new line (\n)....


How to Convert String to Int and int to String in Rust example

February 28, 2022 ·  2 min read

This tutorial explains about following things How to parse a string to int in Rust? How to convert an int into a String How to read the input from the console and convert it to Integer? For example, if the string is “123”, It converts to an integer type 123 value. There are multiple ways we can do the conversion. How to convert a String to int in Rust? This example converts a string to int with an example using the parse() method....


How to Convert Strings into Uppercase and Lower case in Rust example

February 28, 2022 ·  2 min read

This program shows you multiple ways to convert String into Uppercase in Rust with examples. For example, if the given string is Hello, Output is HELLO for uppercase. if the given string is Hello, Output is hello for lowercase. How Convert lower case string to Upper case in Rust? In this, Let’s see how to convert string to upper case in Rust. There are multiple ways to convert a string to an uppercase....


How to disable unused code warnings in Rust example

February 28, 2022 ·  2 min read

In Rust code, Sometimes, You declared a variable or type or defined something, But never used it in the code, It throws an error. For example, You declared a struct, but never constructed with fields struct Employee; fn main() { println!("Hello World") } Running the above program throws warning: struct is never constructed: warning: struct is never constructed: `Employee` --> code.rs:1:8 | 1 | struct Employee; | ^^^^^^^^ | = note: `#[warn(dead_code)]` on by default warning: 1 warning emitted Hello World Let’s see another example In this example, the variable is declared but never used....


How to find variable type in Rust example

February 28, 2022 ·  2 min read

This tutorial shows how to get the data type of a variable in Rust Rust gets Variable Type There are multiple ways to get variable types use std::any::type_name function use std::intrinsics::type_name std::any::type_name function to get type of an variable: std::any::type_name function returns type of an variable and return string slice value. The below example program get printing the variable type of Integer types Boolean type String flat character tuples array vector fn main() { let str = "test"; let number = 11; let price= 21....


How to Get Current Timestamp in Milliseconds in Rust| Rust epoch time example

February 28, 2022 ·  2 min read

In this post, You will learn multiple ways to get the current epoch timestamp in Rust. epoch time is Unix style in milliseconds since 01/01/1971, which means it returns a long number in milliseconds. It is epoch time or Unix timestamp Rust provides the Date object provides the date and time-related things. Rust current time in Milliseconds using SystemTime SystemTime struct provides a utility function or SystemClock. SystemTime::now() returns SystemTime object that contains current time ....


How to get the current date and time in local and UTC in Rust example

February 28, 2022 ·  1 min read

This program prints the current date and time in the system and UTC timezone. It uses the Chrono library to access current timestamp-related information. Let’s create a new rust project using the cargo command. cargo new dateapp Next, Change the directory to dateapp cd dateapp Add the Chrono dependency in cargo.toml file [dependencies] chrono = "0.4" Now, Run the cargo build command to install all dependencies. cargo build Let’s write a code for the printing date in local and UTC format....


How to join a Strings in a Vector into a String in Rust example

February 28, 2022 ·  1 min read

This tutorial explains how to join a strings with separator from a Vector. Rust Join strings in a Vector with a separator For example, We have a list of strings in a Vector object. let numbers = vec!["One", "Two", "Three"]; You want to iterate the list of strings and join using a separator. The separator can be a blank space or a hyphen. Rust provides two methods as of version 1....


How to match a String against string literals in Rust example

February 28, 2022 ·  2 min read

This explains compare the comparison string with the string literal in with example in Rust. String and string literals are two different objects. String literals are string slices that can be declared. let name = "one"; String object type can be created as follows. let name1 = String::from("one"); Rust String matching with String literals Rust provides a matching case, similar to a switch in java, used to match an expression with a set of values and executes the matching case....


How to Print Array and Vector data to console in Rust?

February 28, 2022 ·  2 min read

This tutorial shows How to print array and vector data to console in Rust. In Rust, {} is used display trait to display the normal string. display trait is not implemented for Array and Vector. fn main() { let vector = vec![5; 30]; println!("{}", vector); } It throws error[E0277]: Vec<{integer}> doesn’t implement std::fmt::Display and Vec<{integer}> cannot be formatted with the default formatter error[E0277]: `Vec<{integer}>` doesn't implement `std::fmt::Display` --> test.rs:3:20 | 3 | println!...


How to Read a file into string, vector, and line by line Rust example

February 28, 2022 ·  2 min read

This tutorial shows multiple ways to read a file in Rust with examples. Rust File read content into a string Rust provides a standard library std package that provides an fs module with file read and writes operations. The read_to_string function in the fs module takes the path of a file and gives file content into a string. if the file does not exist, It gives an error. Here is a syntax...


How to Remove first and last characters of a Strings in Rust example

February 28, 2022 ·  1 min read

This tutorial explains multiple ways to remove the first and last characters of a string in Rust. How to remove the first and last characters of a String There are multiple ways we can do it. String slice range This example removes the first and last characters and returns the string. using a range of a string length starting from 1..string.length-1 fn main() { let name = "Welcome"; let result = &name[1....


How to split a Strings in Rust example

February 28, 2022 ·  1 min read

This tutorial shows you multiple ways to split a string with a separator. How to Split a String with space in Rust? There are multiple ways we can do it. use split function split function in String object split the string using a pattern, returns an iterator. The split method split a string into multiple slices using spaces, separators or characters You can use for in loop to iterate the result or you can convert it to Vec<&str> using the collect method....


Range Operator in Rust with example

February 28, 2022 ·  3 min read

This tutorial explains the available range of operators in Rust programming. The range operator is also called two periods(..) in Rust. Here is a syntax start .. end the start is a starting value the end is an end value. A range of operators can be used in Array and vectors to get the Slice of an object. Rust Range Operator Rust provides multiple variations of range operators. Range It includes the start value and excludes the end value....


Rust frequently used programs tutorials|Rust by example

February 28, 2022 ·  1 min read

This is a home page of rust tutorials and examples. Rust by examples The following examples categorized based on feature Strings Comparision of String and str in Rust String captilize first letter Append a string Upper and Lower case String split by delimeter Vector strings join as String String compare with literal Remove First,Last letter from a string Conversion programs Parse String to/from Int Date and time code Current timestamp or epoch Current Local and UTC Date and time Arrays and vector programs Difference between iter, iter_mut, and into_iter Range operators in Rust Print Array and Vector File and directory-related code Read File into line,string and vector Errors Fix for disable unused code warnings ...


Subscribe
You'll get a notification every time a post gets published here.