Whats up, World!

Now that you simply’ve put in Rust, let’s write your first Rust program. It’s
conventional when studying a brand new language to put in writing somewhat program that prints
the textual content Whats up, world! to the display screen, so we’ll do the identical right here!

Observe: This e-book assumes fundamental familiarity with the command line. Rust makes
no particular calls for about your modifying or tooling or the place your code lives, so
if you happen to desire to make use of an built-in improvement setting (IDE) as a substitute of
the command line, be happy to make use of your favourite IDE. Many IDEs now have some
diploma of Rust assist; examine the IDE’s documentation for particulars. Lately,
the Rust staff has been specializing in enabling nice IDE assist, and progress
has been made quickly on that entrance!

Making a Venture Listing

You’ll begin by making a listing to retailer your Rust code. It doesn’t matter
to Rust the place your code lives, however for the workouts and tasks on this e-book,
we propose making a tasks listing in your house listing and maintaining all
your tasks there.

Open a terminal and enter the next instructions to make a tasks listing
and a listing for the “Hello, world!” challenge inside the tasks listing.

For Linux, macOS, and PowerShell on Home windows, enter this:

$ mkdir ~/tasks $ cd ~/tasks $ mkdir hello_world $ cd hello_world

For Home windows CMD, enter this:

> mkdir "%USERPROFILE%projects" > cd /d "%USERPROFILE%projects" > mkdir hello_world > cd hello_world

Writing and Working a Rust Program

Subsequent, make a brand new supply file and name it most important.rs. Rust recordsdata at all times finish with
the .rs extension. For those who’re utilizing a couple of phrase in your filename, use
an underscore to separate them. For instance, use hello_world.rs relatively than

Now open the most important.rs file you simply created and enter the code in Itemizing 1-1.

See also  How to fix GreedFall lags and to improve FPS?

Filename: most important.rs

fn most important() { println!("Hello, world!"); }

Itemizing 1-1: A program that prints Whats up, world!

Save the file and return to your terminal window. On Linux or macOS, enter
the next instructions to compile and run the file:

$ rustc most important.rs $ ./most important Whats up, world!

On Home windows, enter the command .most important.exe as a substitute of ./most important:

> rustc most important.rs > .most important.exe Whats up, world!

No matter your working system, the string Whats up, world! ought to print to
the terminal. For those who don’t see this output, refer again to the
“Troubleshooting” a part of the Set up
part for tactics to get assist.

If Whats up, world! did print, congratulations! You’ve formally written a Rust
program. That makes you a Rust programmer—welcome!

Anatomy of a Rust Program

Let’s evaluate intimately what simply occurred in your “Hello, world!” program.
Right here’s the primary piece of the puzzle:

fn most important() { }

These traces outline a operate in Rust. The most important operate is particular: it’s
at all times the primary code that runs in each executable Rust program. The primary
line declares a operate named most important that has no parameters and returns
nothing. If there have been parameters, they’d go contained in the parentheses, ().

Additionally, observe that the operate physique is wrapped in curly brackets, {}. Rust
requires these round all operate our bodies. It’s good fashion to put the opening
curly bracket on the identical line because the operate declaration, including one area in

If you wish to keep on with an ordinary fashion throughout Rust tasks, you should use an
automated formatter software referred to as rustfmt to format your code in a selected
fashion. The Rust staff has included this software with the usual Rust distribution,
like rustc, so it ought to already be put in in your pc! Test the
on-line documentation for extra particulars.

Contained in the most important operate is the next code:

#![allow(unused)] fn most important() { println!("Hello, world!"); }

This line does all of the work on this little program: it prints textual content to the
display screen. There are 4 vital particulars to note right here.

First, Rust fashion is to indent with 4 areas, not a tab.

Second, println! calls a Rust macro. If it referred to as a operate as a substitute, it
could be entered as println (with out the !). We’ll focus on Rust macros in
extra element in Chapter 19. For now, you simply have to know that utilizing a !
implies that you’re calling a macro as a substitute of a traditional operate.

Third, you see the "Hello, world!" string. We cross this string as an argument
to println!, and the string is printed to the display screen.

Fourth, we finish the road with a semicolon (;), which signifies that this
expression is over and the following one is able to start. Most traces of Rust code
finish with a semicolon.

Compiling and Working Are Separate Steps

You’ve simply run a newly created program, so let’s study every step within the
course of.

Earlier than operating a Rust program, it’s essential to compile it utilizing the Rust compiler by
getting into the rustc command and passing it the identify of your supply file, like

$ rustc most important.rs

In case you have a C or C++ background, you’ll discover that that is much like gcc
or clang. After compiling efficiently, Rust outputs a binary executable.

On Linux, macOS, and PowerShell on Home windows, you may see the executable by
getting into the ls command in your shell. On Linux and macOS, you’ll see two
recordsdata. With PowerShell on Home windows, you’ll see the identical three recordsdata that you simply
would see utilizing CMD.

$ ls most important most important.rs

With CMD on Home windows, you’ll enter the next:

> dir /B %= the /B choice says to solely present the file names =% most important.exe most important.pdb most important.rs

This exhibits the supply code file with the .rs extension, the executable file
(most important.exe on Home windows, however most important on all different platforms), and, when utilizing
Home windows, a file containing debugging data with the .pdb extension.
From right here, you run the most important or most important.exe file, like this:

$ ./most important # or .most important.exe on Home windows

If most important.rs was your “Hello, world!” program, this line would print Whats up, world! to your terminal.

For those who’re extra acquainted with a dynamic language, akin to Ruby, Python, or
JavaScript, you may not be used to compiling and operating a program as
separate steps. Rust is an ahead-of-time compiled language, that means you may
compile a program and provides the executable to another person, and so they can run it
even with out having Rust put in. For those who give somebody a .rb, .py, or
.js file, they should have a Ruby, Python, or JavaScript implementation
put in (respectively). However in these languages, you solely want one command to
compile and run your program. Every little thing is a trade-off in language design.

Simply compiling with rustc is ok for easy packages, however as your challenge
grows, you’ll wish to handle all of the choices and make it straightforward to share your
code. Subsequent, we’ll introduce you to the Cargo software, which is able to enable you write
real-world Rust packages.

Leave a Reply

Your email address will not be published.