Photo by Aaron Thomas on Unsplash

Formatting in Rust

In my previous stories, we discussed RUST!! installation, we also ran our first “Hello, World!” program.

Link to the previous stories:

Let’s move forward in our journey of learning rust !!!!

Every programming language has a different way of formatting like in javascript we can directly use console.log(1) or C we use printf(“%d”, number); like that.

If we write the statement println!(1) it is illegal in RUST, but let me show you what error it gives.

To test this we will create a new file in the source folder and name it format.rs

and also define a new function run in that file.

created a file named format.rs
Make these changes to run the format .rs

Now we are able to run a new file from our main function.

Let's try to print something else and see.

You can clearly see that compiler is yelling to me that it is illegal. The beauty of rust is that its compiler acts as a gatekeeper and also suggest you the correct way to enter so let's make a correction in the previous statement and see.

Multiple arguments can also be used

Now, suppose we have a situation where we want to print the same argument twice. Rust comes with the facility of the positional argument so let's see how it works. If I want to print the sentence “Jimmy is a teacher and Jimmy loves grapes.” Let’s see how the positional argument will make our task easy.

We can easily assign the argument position to retrieve its value.

We also have named argument in rust which you can use if needed.

How cool it is rust is a most modern programming language with tons of features.

Let’s continue our formatting data in rust like a positional and named formatting we also have placeholder traits, sounds confusing let make it easy with an example in our placeholder “{}” we can explicitly define different traits to get a different result.

Suppose we have a task we are provided with a number and asked to find it binary, hexadecimal and octal values. Yes, you can definitely do it but you have to write a separate function for each task. Now see in rust it is just a piece of cake.

Waooo, that so simple in rust.. just by using “:b — binary ”, “:x — hexadecimal”, “:o — octal”, Sooo simple it is…. 😊

Also, you can do basic maths.

That enough for formatting in the next part we will learn new thing about rust and explore it more.

Happy Coding!!!!!!!!!!! 👦 🔥