{

Learn free rust tutorials


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

May 21, 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 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 disable unused code warnings in Rust example

May 15, 2022 ·  2 min read

In Rust code, Sometimes, You declared an variable or type or defined something, But never used in the code, It throws an error. For example, You declared an 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, variable declared, but never used....


Difference between iter, iter_mut, and into_iter in Rust example

May 11, 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() Above three provides 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...


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

May 3, 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 utility function or SystemClock. SystemTime::now() returns SystemTime object that contains current time ....


Difference between String and str in Rust example

May 1, 2022 ·  1 min read

In Rust, We have a 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 join a Strings in a Vector into a String ain Rust example

April 30, 2022 ·  1 min read

This tutorials explains how to join a strings with separator from a Vector. Rust Join strings in a Vector with 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 separator. Separator can be blank space or hyphen. Rust provides two methods as of version 1.56 join function...


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

April 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 character 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 Print Array and Vector data to console in Rust?

April 27, 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

April 27, 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. 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 capitalize the first letter of a string in Rust example

April 25, 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 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 the first letter, Append these two strings using +...


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

April 16, 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?...


How to split a Strings in Rust example

April 16, 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. split method split a string into multiple slices using spaces, separator or characters You can use for in loop to iterate the result or you can convert it to Vec<&str> using the collect method....


How to concatenate a Strings in Rust example

April 15, 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....


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

April 15, 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....


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