Getting Started - Beginning Rust - Get Started With Rust 2021 Edition
Getting Started - Beginning Rust - Get Started With Rust 2021 Edition
1. Getting Started
Carlo Milanesi1
(1) Bergamo, Italy
In this chapter, you will learn:
How to write and run your first program in the Rust language
How to print text and numbers on the terminal
How to write comments in your code
How to Start
To run this program, first you must install the Rust toolset. The offi-
cial Rust toolset can be downloaded for free from the website
www.rust-lang.org. Linux, Windows, and macOS platforms are sup-
ported. For each platform, there are three versions: stable, beta, and
nightly. The stable version is recommended; it is the oldest, but also the
most tested one and the one less likely to change. All of these program
versions should be used from the command line of a console. After in-
stallation, to check which version is installed, type at a command line
(with uppercase V): rustc -V. The code in this book has been checked
using version 1.56.0, but probably later versions will be OK too.
When you have a working installation, you can perform the following actions:
./main
You just run the program generated before. The prompt should be
printed immediately, as this program does nothing.
Hello, World!
Let’s see how to print some text on the terminal. Change the program of the previous
section to the following one:
fn main() {
print!("Hello, world!");
}
If such code is compiled and run as before, it will print: Hello,
world!.
Notice that the newly added line contains eight syntax items, aka tokens. Let’s
examine them:
Let’s examine the meaning of the literal string phrase. The word
string means finite sequence of characters, possibly including spaces and
punctuation. The word literal means specified directly in source code.
Therefore a literal string is a finite sequence of characters (possibly in-
cluding spaces and punctuation) specified directly in source code. A non-
literal string, instead, is a variable whose value at runtime is a string.
Such a string could be read from a file, or typed by the user, or copied
from a literal string, as we will see when string variables are explained.
The print macro simply inserts into the program some code that
prints on the terminal the text that it has received as an argument.
Rust always distinguishes between uppercase and lowercase letters
—it’s case sensitive. For all characters that are outside literal strings
and comments, if you replace an uppercase letter with a lowercase one
or conversely, typically you get a compilation error, or anyway a pro-
gram with a different behavior. Instead, making such changes inside
literal strings always allows a successful compilation, but it’s likely the
behavior of the program will be different.
For example:
fn Main() {}
If you compile this program, you get the compilation error main
function not found, as no main function (with a lowercase m) is de-
fined in the program.
From now on, except when specified, we will assume the example
code will be inside the braces of the main function, so the braces and
the text preceding them will be omitted.
Instead of using a single literal string, you can print several of them, even in a single
statement, in this way:
print!("{}, {}!", "Hello", "world");
This statement, put inside the braces of the main function , will print
again: Hello, world!.
So, the macro scans the arguments after the first one, and for each
of them it looks inside the first argument for a pair of braces and re-
places that pair of braces with the current argument.
So far, we wrote programs that print only one line, but a single statement can print
several lines. It is done in this way:
print!("First line\nSecond line\nThird line\n");
This will print:
First line
Second line
Third line
The sequence of characters \n (where n stands for new line) is trans-
formed by the compiler into the character sequence that represents the
line terminator for the currently used operating system.
Actually, it is very common to go to a new line exactly once for every printing
statement. You can do this by adding the two characters \n at the end of the line;
you can also use another macro of the standard library, println, whose name is to
be read “print line.” It’s used in this way:
println!("text of the line");
This statement is equivalent to:
print!("text of the line\n");
The print macro is also able to use integer numbers to replace the
corresponding placeholder inside its first argument.
This procedure explains, for example, why if the following program is written:
print!("My number: {}", 000140);
the compiler generates exactly the same executable program gener-
ated before. Actually, when the source string 000140 is converted to the
binary format, the leading zeros are ignored.
Comments
As any other programming language, Rust allows comments embedded
in code:
The first two lines start with a pair of slashes (//). Such a pair of
characters indicates the start of a line comment, which is a comment
ending at the end of the line. To write a comment on several lines, the
pair of slashes must be repeated at every line of the comment, as in the
second line of the preceding program.
Rust programmers use to leave a space character just after the dou-
ble slash, to improve readability.