HomeLinuxWhats up World Program in Rust

Whats up World Program in Rust


The Rust programming language is among the quickest adopted programs programming languages by builders and tech corporations. Additionally it is voted as one of many most cherished programming languages by builders who use it each day. Rust has been getting this love for seven consecutive years now!

It’s so well-liked that there at the moment are two large efforts being carried out within the Linux ecosystem:

And that’s simply within the Linux ecosystem. Android’s Bluetooth implementation Gabeldorsche is now written in Rust.

Do you see the rising recognition of Rust? You’ll in all probability prefer to be taught coding in Rust.

Why must you think about Rust over different programming languages?

Rust is a programming language that has an extraordinarily strict sort system. Because of this, you’re “pressured” to not write dangerous code within the first place (effectively, often).

The Rust programming language has the next “objectives”:

  1. Velocity: Rust’s binaries are as quick as C binaries, generally outpacing C++ binaries!
  2. Reminiscence security: Rust has an enormous emphasis on reminiscence security.
  3. Concurrency: Specializing in reminiscence security eliminates loads of race condition-like situations and helps you introduce concurrency in your program.

Following are just a few errors errors one would possibly make in languages like C/C++ (however not with Rust):

  • Use after free
  • Double free
  • Accessing out-of-bound values
  • Utilizing NULL
  • Inappropriate pointer arithmetic and/or entry
  • Use of uninitialized variable(s)
  • Thread-unsafe multi-threading

Take a look on the points brought on by such points at main companies like Apple, Microsoft, Google, 0day and so forth,

Now that you understand why one would possibly need to select the Rust programming language over every other one, let’s begin with the Rust language tutorial collection!

Meant viewers

For the love of Rust, I’m scripting this collection of Rust tutorials that will help you get acquainted with the idea of Rust programming.

This tutorial collection is meant for people already conversant in programming languages like C and C++. I assume you understand fundamental phrases like variables, features, loops, and so forth.

The one conditions that I ask from you’re your time and a few effort.

Putting in the Rust compiler

I would favor that you’ve the Rust compiler put in regionally. You are able to do so by operating the next command:

curl --proto '=https' --tlsv1.3 -sSf https://sh.rustup.rs | sh
Installing Rust on Ubuntu Linux
Putting in Rust

Aside from the Rust Compiler, I additionally suggest putting in just a few extra instruments that may make it easier to within the improvement course of:

rustup part add rust-src rust-analyzer rust-analysis

💡

If you don’t want to set up the Rust compiler, no worries. You may run the Rust code instantly in your browser! Simply head over to the Rust Playgrounds web site and paste the code mentioned right here.

Whats up Rust!

Since Dennis Ritchie and Brian Kernighan launched the C programming language with the “Whats up world” program, it has develop into a customized within the UNIX world to take action with any new programming language you be taught.

So let’s write our Whats up World program in Rust as effectively.

I’ll create a mission listing known as learn-rust-its-foss in my residence listing. In there, I create one other listing known as hello-world. Inside that, I’ll create a most important.rs file:

// this code outputs the textual content
// "Whats up world!" to `stdout`

fn most important() {
    println!("Whats up world!");
}

📋

Similar to C, C++ and Java supply information have the extensions .c, .cpp and .java respectively, the Rust supply information have the .rs file extension.

As a C/C++ programmer, you may need used gcc on Linux, clang on macOS and MSVC on Home windows. However to compile Rust code, the language creators themselves present an official rustc compiler.

Working a Rust program is identical as executing C/C++ program. You compile the code to get the executable file after which run this executable to run the code.

$ ls
most important.rs

$ rustc most important.rs

$ ls
most important  most important.rs

$ ./most important
Whats up world!

Good!

Deciphering Rust code

Now that you just wrote, compiled and ran your first ever Rust program, let’s de-structure the “Whats up world” code and perceive every half.

fn most important() {
}

The fn key phrase is used to declare a perform in Rust. Following it, most important is the title of this explicit perform that was declared. Like many compiled programming languages, the most important is a particular perform used as your program’s entry level.

Any code written contained in the most important perform (between the curly brackets { }) will get executed upon program start-up.

println macro

Contained in the most important perform, there’s one assertion:

    println!("Whats up world!");

Just like the C language’s commonplace library has the printf perform, Rust language’s commonplace library has the println macro. A macro is much like a perform however it’s distinguished by the exclamation mark. You may find out about macros and features later on this collection.

The println macro takes in a format string and places it to this system’s output (in our case, that’s terminal). Since I want to output some textual content as an alternative of a variable, I’ll enclose the textual content inside double quotes ("). Lastly, I finish this assertion utilizing a semi-colon to indicate the top of the assertion.

📋

Simply know that something that appears like a perform name however has an exclamation mark (!) earlier than the opening parentheses is a macro within the Rust programming language.

Rust follows the identified commenting fashion of the C programming language. A single line remark begins with two ahead slashes (//) and a multi-line remark is began by /* and ends with */.

// this can be a multi-line remark
// however nothing stops me to doing the identical
// on the second or third line too!

/*
 * this can be a "true" mutli-line remark
 * as a result of it's _fancy_
 */

Conclusion

You simply took step one in the direction of coding in Rust with the Whats up World program.

As a observe, maybe you possibly can write and execute a Rust program that prints “Sure! I did Rust”.

Within the subsequent a part of the collection, you may be taught to make use of variables in your Rust program. Keep tuned!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments