How to start a project in Rust

how-to-start-a-project-in-rust

Rust is a programming language that is becoming more and more popular among developers. But what would be the reason for such success? The language is basically recognized for its security and performance. So, if you are new to programming and want to know more about this language created by Mozilla, how about reading this content to the end?

The number of companies interested in Rust developers is growing. Just search the job openings for DEVs (Vaultree included). In addition, the language has become popular with DEVs because of its advantages.

What is Rust?

In this article I explained why Rust is the most loved programming language.

In short, Rust is a multi-paradigm and compiled programming language. It was developed by Mozilla Research. Its main principles are concurrency without data contention, abstraction without overhead, and security without garbage collector. It can be used in various types of applications, such as:

  • Command line tools;
  • Tools for DevOps;
  • Web services;
  • Games;
  • IoT (Internet of Things) applications.

Launched in 2010 with open source, the language is called like the new clothes C++, because the two technologies are very similar, although Rust is more efficient in terms of security.
Just out of curiosity, Rust is “rust”, in Portuguese, but in general it is not “rusty”, since it is constantly being updated and is quite competitive in the market.

How was Rust created?

Rust began to be developed in Graydon Hoare’s leisure time in mid-2006. At the time, he was working at Mozilla, and the company ended up taking an interest in the new language that was similar to C++, but had more advantages.

Thus, Mozilla decided to invest in the project and gave Hoare a team of technicians to dedicate himself more intensively to the new language. Its first version was released in 2010 and, in a short time, it became open-source. Shortly thereafter, Hoare left the technical leadership of the project, justifying that he was suffering from burnout (a syndrome linked to extreme fatigue) and began to dedicate himself to other smaller companies. But in mid-2016, he accepted a job offer from Apple Inc to work on Swift language development.

Who uses the Rust language?

As you have seen, the creation of the language was a great achievement for Mozilla, which was soon used by many other companies.
Thus, software that is widely used today, such as Firefox, Dropbox, Yelp, Firecracker (visualisation technology used by Amazon) and Cloudflare use Rust.
But, in addition, it is present in several projects of companies and startups that seek scalable results, prioritising security and performance.

What makes this language different?

Did you know that between 2016 and 2020, the Rust language was considered the preferred language among developers, according to Stack Overflow surveys? But what explains this predilection? Here are some of the benefits of Rust. Remember that this does not mean that it is unanimous among developers, but it indicates that it is very important to dedicate yourself to study this language to have this differential in your curriculum and belong to this powerful community.

Performance

During the compilation process, Rust itself performs memory management, freeing up space until then occupied by variables that are not being used. In other languages, this function is performed by a garbage collector, so Rust demonstrates greater performance, as it allows greater speed in development.

Safety

Whenever a variable is no longer used by the application, it is automatically deleted by the language’s memory management system. This avoids errors and poor performance.

Productivity

The developer who chooses Rust gains in productivity. Firstly, because the language has a very extensive documentation and full of details about its use. Secondly, because it is able to identify errors that other languages ​​do not, thanks to its intelligent compiler.

Updates

One of the advantages is also the constant update, which improves the features of the language. One of the most recent is from April 2019. It has brought improvements to the languages, making them easier to read and write.
In addition, the Rust language also runs very well in web applications due to its resourcefulness and high performance, giving projects more agility.

Career

Overall, the Rust developer space is hot in the market. This is because startups and companies are looking for more agile and secure systems to scale their results.

Installation and development environment

To get started with Rust we have two alternatives, or using the Play Rust site which is maintained by the Rust core and is a very nice and very complete tool or by downloading the Rustup tool which is the official Rust installer. The installation will depend on your operating system, but it is very easy, on the official website we can find these options mentioned above.

I will choose the installation because it gives us more options. I’ll be using VS Code as a code editor, the installation is very easy, on Windows it’s an executable, and if you’re using a Unix-based system, I advise you to download it through Snap.

Below are the extensions that I currently have installed within VS Code:

Rust Pack – this pack contains the 3 main extensions I use.
Rust Doc Viewer – to facilitate documentation.

For these studies I will be using the stable version of Rust, after installing Rustup, we run the command:

rustup default stable

Through this command we set the stable version as default. In addition to this, we have the option to use the nightly which is updated more frequently with the latest features in the language.

The stable version is released every 6 months.

To perform an update and download updates for Rust, Cargo, and Rustup tools, we run the command:

rustup update stable

And this way we have an environment ready to start coding.

Hello World

In a directory of your choice we will create a file called hello_world.rs (the .rs extension is the Rust file extension).
Inside this file will be added the following content:

fn main() {
    println!("Hello World!");
}

fn

→ reserved word of the language, every function creation within Rust we will use this keyword.

main()

→ every binary program in Rust has an entry point, where the program starts execution, every time we run a program, this will be the starting point. By default this function takes no parameters and returns nothing.

{ }

→ function scope – everything between the pair of braces is the body (scope) of this function.

println!

→ it’s a macro (we’ll see what a macro is in another post), but in a nutshell it would be kind of a code template for us to use, and the compiler will evaluate it in a pre-compile. Anything that has a !() at the end of the command is a macro. For better readability as we can see an indentation of 4 spaces was used in this line, the Rust style is indenting four spaces, not a tab.
(“Hello World!”);

→ Every string inside Rust must be enclosed in double quotes (“”), single quotes are used only in the character type (”). This line must be terminated with a semicolon to indicate that this expression has ended, and if we have a next one, it is ready to start. Most lines of Rust code end with a semicolon.

program execution

Before executing a program in Rust, we need to compile it, that is, generate a file that the computer knows how to execute, for that we go through the Unix terminal and enter the directory where we created the file, executing the following instruction:

cd /dev/workspace/rust

And from the command prompt on Windows:

cd devworkspacerust

This is the path where I created the file, you will change this line by adding the path you created.

After that, let’s compile the file by running the command below in the terminal:

rustc hello_world.rs

Inside the directory that the file we created is, another one will be generated, which will be the executable, for Windows it will be a hello_world.exe and for Unix systems a hello_world.sh. On Windows a file called hello_world.pbd will also be generated, this file contains debugging information.

To run the file on Windows, let’s run the command:

.hello_world.exe

To run the file on Unix, let’s run the command:

./hello_world

The output will look something like this:

Hello World!

So that this post doesn’t get too long, I’ll leave the creation of a Rust project for the next one, where we’ll start using the Cargo tool.

Voilà! You have your first Rust project. In the next article we will talk more in depth about this language and what you can build using it. See you!

About Vaultree

Image description

Vaultree’s Encryption-in-use enables businesses of all sizes to process (search and compute) fully end-to-end encrypted data without the need to decrypt. Easy to use and integrate, Vaultree delivers peak performance without compromising security, neutralising the weak spots of traditional encryption or other Privacy Enhancing Technology (PET) based solutions. Follow Vaultree on Twitter (@Vaultree), LinkedIn, Reddit (r/Vaultree) or dev.to. Visit www.vaultree.com, and sign up for a product demo and our newsletter to stay up to date on product development and company news.

Total
1
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
must-have-chrome-extensions-for-developers

Must have Chrome Extensions for developers

Next Post
field-guide-to-surviving-ddos-attacks-in-your-application

Field Guide to Surviving DDoS Attacks in your application

Related Posts