]> git.proxmox.com Git - rustc.git/blame - src/doc/book/src/ch01-02-hello-world.md
New upstream version 1.55.0+dfsg1
[rustc.git] / src / doc / book / src / ch01-02-hello-world.md
CommitLineData
13cf67c4
XL
1## Hello, World!
2
3Now that you’ve installed Rust, let’s write your first Rust program. It’s
4traditional when learning a new language to write a little program that prints
5the text `Hello, world!` to the screen, so we’ll do the same here!
6
7> Note: This book assumes basic familiarity with the command line. Rust makes
8> no specific demands about your editing or tooling or where your code lives, so
9> if you prefer to use an integrated development environment (IDE) instead of
10> the command line, feel free to use your favorite IDE. Many IDEs now have some
11> degree of Rust support; check the IDE’s documentation for details. Recently,
12> the Rust team has been focusing on enabling great IDE support, and progress
13> has been made rapidly on that front!
14
15### Creating a Project Directory
16
17You’ll start by making a directory to store your Rust code. It doesn’t matter
18to Rust where your code lives, but for the exercises and projects in this book,
19we suggest making a *projects* directory in your home directory and keeping all
20your projects there.
21
22Open a terminal and enter the following commands to make a *projects* directory
e74abb32 23and a directory for the “Hello, world!” project within the *projects* directory.
13cf67c4 24
9fa01778 25For Linux, macOS, and PowerShell on Windows, enter this:
13cf67c4 26
f035d41b 27```console
13cf67c4
XL
28$ mkdir ~/projects
29$ cd ~/projects
30$ mkdir hello_world
31$ cd hello_world
32```
33
34For Windows CMD, enter this:
35
36```cmd
37> mkdir "%USERPROFILE%\projects"
38> cd /d "%USERPROFILE%\projects"
39> mkdir hello_world
40> cd hello_world
41```
42
13cf67c4
XL
43### Writing and Running a Rust Program
44
45Next, make a new source file and call it *main.rs*. Rust files always end with
46the *.rs* extension. If you’re using more than one word in your filename, use
47an underscore to separate them. For example, use *hello_world.rs* rather than
48*helloworld.rs*.
49
50Now open the *main.rs* file you just created and enter the code in Listing 1-1.
51
52<span class="filename">Filename: main.rs</span>
53
54```rust
55fn main() {
56 println!("Hello, world!");
57}
58```
59
60<span class="caption">Listing 1-1: A program that prints `Hello, world!`</span>
61
62Save the file and go back to your terminal window. On Linux or macOS, enter
63the following commands to compile and run the file:
64
f035d41b 65```console
13cf67c4
XL
66$ rustc main.rs
67$ ./main
68Hello, world!
69```
70
71On Windows, enter the command `.\main.exe` instead of `./main`:
72
73```powershell
74> rustc main.rs
75> .\main.exe
76Hello, world!
77```
78
79Regardless of your operating system, the string `Hello, world!` should print to
9fa01778
XL
80the terminal. If you don’t see this output, refer back to the
81[“Troubleshooting”][troubleshooting]<!-- ignore --> part of the Installation
82section for ways to get help.
13cf67c4
XL
83
84If `Hello, world!` did print, congratulations! You’ve officially written a Rust
85program. That makes you a Rust programmer—welcome!
86
87### Anatomy of a Rust Program
88
e74abb32 89Let’s review in detail what just happened in your “Hello, world!” program.
13cf67c4
XL
90Here’s the first piece of the puzzle:
91
92```rust
93fn main() {
94
95}
96```
97
98These lines define a function in Rust. The `main` function is special: it is
99always the first code that runs in every executable Rust program. The first
100line declares a function named `main` that has no parameters and returns
101nothing. If there were parameters, they would go inside the parentheses, `()`.
102
103Also, note that the function body is wrapped in curly brackets, `{}`. Rust
104requires these around all function bodies. It’s good style to place the opening
105curly bracket on the same line as the function declaration, adding one space in
106between.
107
6a06907d 108If you want to stick to a standard style across Rust projects, you can use an
fc512014
XL
109automatic formatter tool called `rustfmt` to format your code in a particular
110style. The Rust team has included this tool with the standard Rust distribution,
6a06907d
XL
111like `rustc`, so it should already be installed on your computer! Check the
112online documentation for more details.
13cf67c4
XL
113
114Inside the `main` function is the following code:
115
116```rust
117 println!("Hello, world!");
118```
119
120This line does all the work in this little program: it prints text to the
f035d41b
XL
121screen. There are four important details to notice here.
122
123First, Rust style is to indent with four spaces, not a tab.
13cf67c4
XL
124
125Second, `println!` calls a Rust macro. If it called a function instead, it
126would be entered as `println` (without the `!`). We’ll discuss Rust macros in
127more detail in Chapter 19. For now, you just need to know that using a `!`
136023e0
XL
128means that you’re calling a macro instead of a normal function, and that macros
129don't always follow the same rules as functions.
13cf67c4
XL
130
131Third, you see the `"Hello, world!"` string. We pass this string as an argument
132to `println!`, and the string is printed to the screen.
133
134Fourth, we end the line with a semicolon (`;`), which indicates that this
135expression is over and the next one is ready to begin. Most lines of Rust code
136end with a semicolon.
137
138### Compiling and Running Are Separate Steps
139
140You’ve just run a newly created program, so let’s examine each step in the
141process.
142
143Before running a Rust program, you must compile it using the Rust compiler by
144entering the `rustc` command and passing it the name of your source file, like
145this:
146
f035d41b 147```console
13cf67c4
XL
148$ rustc main.rs
149```
150
151If you have a C or C++ background, you’ll notice that this is similar to `gcc`
152or `clang`. After compiling successfully, Rust outputs a binary executable.
153
9fa01778 154On Linux, macOS, and PowerShell on Windows, you can see the executable by
532ac7d7 155entering the `ls` command in your shell. On Linux and macOS, you’ll see two
9fa01778
XL
156files. With PowerShell on Windows, you’ll see the same three files that you
157would see using CMD.
13cf67c4 158
6a06907d 159```console
13cf67c4
XL
160$ ls
161main main.rs
162```
163
13cf67c4
XL
164With CMD on Windows, you would enter the following:
165
166```cmd
167> dir /B %= the /B option says to only show the file names =%
168main.exe
169main.pdb
170main.rs
171```
172
173This shows the source code file with the *.rs* extension, the executable file
174(*main.exe* on Windows, but *main* on all other platforms), and, when using
9fa01778
XL
175Windows, a file containing debugging information with the *.pdb* extension.
176From here, you run the *main* or *main.exe* file, like this:
13cf67c4 177
f035d41b 178```console
13cf67c4
XL
179$ ./main # or .\main.exe on Windows
180```
181
e74abb32 182If *main.rs* was your “Hello, world!” program, this line would print `Hello,
13cf67c4
XL
183world!` to your terminal.
184
185If you’re more familiar with a dynamic language, such as Ruby, Python, or
186JavaScript, you might not be used to compiling and running a program as
187separate steps. Rust is an *ahead-of-time compiled* language, meaning you can
188compile a program and give the executable to someone else, and they can run it
189even without having Rust installed. If you give someone a *.rb*, *.py*, or
190*.js* file, they need to have a Ruby, Python, or JavaScript implementation
191installed (respectively). But in those languages, you only need one command to
192compile and run your program. Everything is a trade-off in language design.
193
194Just compiling with `rustc` is fine for simple programs, but as your project
195grows, you’ll want to manage all the options and make it easy to share your
196code. Next, we’ll introduce you to the Cargo tool, which will help you write
197real-world Rust programs.
9fa01778
XL
198
199[troubleshooting]: ch01-01-installation.html#troubleshooting