Files
docs-rust/ch01/ch01-02-hello-world.html
2026-06-22 21:27:36 +05:30

162 lines
9.5 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello, World!</title>
</head>
<body>
<h2 id="hello-world"><a class="header" href="#hello-world">Hello, World!</a></h2>
<p>Now that youve installed Rust, its time to write your first Rust program.
Its traditional when learning a new language to write a little program that
prints the text <code>Hello, world!</code> to the screen, so well do the same here!</p>
<section class="note" aria-role="note">
<p>Note: This book assumes basic familiarity with the command line. Rust makes
no specific demands about your editing or tooling or where your code lives, so
if you prefer to use an IDE instead of the command line, feel free to use your
favorite IDE. Many IDEs now have some degree of Rust support; check the IDEs
documentation for details. The Rust team has been focusing on enabling great
IDE support via <code>rust-analyzer</code>. See <a href="../appendix/appendix-04-useful-development-tools.html">Appendix D</a><!-- ignore -->
for more details.</p>
</section>
<!-- Old headings. Do not remove or links may break. -->
<p><a id="creating-a-project-directory"></a></p>
<h3 id="project-directory-setup"><a class="header" href="#project-directory-setup">Project Directory Setup</a></h3>
<p>Youll start by making a directory to store your Rust code. It doesnt matter
to Rust where your code lives, but for the exercises and projects in this book,
we suggest making a <em>projects</em> directory in your home directory and keeping all
your projects there.</p>
<p>Open a terminal and enter the following commands to make a <em>projects</em> directory
and a directory for the “Hello, world!” project within the <em>projects</em> directory.</p>
<p>For Linux, macOS, and PowerShell on Windows, enter this:</p>
<pre><code class="language-console">$ mkdir ~/projects
$ cd ~/projects
$ mkdir hello_world
$ cd hello_world
</code></pre>
<p>For Windows CMD, enter this:</p>
<pre><code class="language-cmd">&gt; mkdir "%USERPROFILE%\projects"
&gt; cd /d "%USERPROFILE%\projects"
&gt; mkdir hello_world
&gt; cd hello_world
</code></pre>
<!-- Old headings. Do not remove or links may break. -->
<p><a id="writing-and-running-a-rust-program"></a></p>
<h3 id="rust-program-basics"><a class="header" href="#rust-program-basics">Rust Program Basics</a></h3>
<p>Next, make a new source file and call it <em>main.rs</em>. Rust files always end with
the <em>.rs</em> extension. If youre using more than one word in your filename, the
convention is to use an underscore to separate them. For example, use
<em>hello_world.rs</em> rather than <em>helloworld.rs</em>.</p>
<p>Now open the <em>main.rs</em> file you just created and enter the code in Listing 1-1.</p>
<figure class="listing" id="listing-1-1">
<span class="file-name">Filename: main.rs</span>
<pre class="playground"><code class="language-rust edition2024">fn main() {
println!("Hello, world!");
}</code></pre>
<figcaption><a href="#listing-1-1">Listing 1-1</a>: A program that prints <code>Hello, world!</code></figcaption>
</figure>
<p>Save the file and go back to your terminal window in the
<em>~/projects/hello_world</em> directory. On Linux or macOS, enter the following
commands to compile and run the file:</p>
<pre><code class="language-console">$ rustc main.rs
$ ./main
Hello, world!
</code></pre>
<p>On Windows, enter the command <code>.\main</code> instead of <code>./main</code>:</p>
<pre><code class="language-powershell">&gt; rustc main.rs
&gt; .\main
Hello, world!
</code></pre>
<p>Regardless of your operating system, the string <code>Hello, world!</code> should print to
the terminal. If you dont see this output, refer back to the
<a href="ch01-01-installation.html#troubleshooting">“Troubleshooting”</a><!-- ignore --> part of the Installation
section for ways to get help.</p>
<p>If <code>Hello, world!</code> did print, congratulations! Youve officially written a Rust
program. That makes you a Rust programmer—welcome!</p>
<!-- Old headings. Do not remove or links may break. -->
<p><a id="anatomy-of-a-rust-program"></a></p>
<h3 id="the-anatomy-of-a-rust-program"><a class="header" href="#the-anatomy-of-a-rust-program">The Anatomy of a Rust Program</a></h3>
<p>Lets review this “Hello, world!” program in detail. Heres the first piece of
the puzzle:</p>
<pre class="playground"><code class="language-rust edition2024">fn main() {
}</code></pre>
<p>These lines define a function named <code>main</code>. The <code>main</code> function is special: It
is always the first code that runs in every executable Rust program. Here, the
first line declares a function named <code>main</code> that has no parameters and returns
nothing. If there were parameters, they would go inside the parentheses (<code>()</code>).</p>
<p>The function body is wrapped in <code>{}</code>. Rust requires curly brackets around all
function bodies. Its good style to place the opening curly bracket on the same
line as the function declaration, adding one space in between.</p>
<section class="note" aria-role="note">
<p>Note: If you want to stick to a standard style across Rust projects, you can
use an automatic formatter tool called <code>rustfmt</code> to format your code in a
particular style (more on <code>rustfmt</code> in
<a href="../appendix/appendix-04-useful-development-tools.html">Appendix D</a><!-- ignore -->). The Rust team has included this tool
with the standard Rust distribution, as <code>rustc</code> is, so it should already be
installed on your computer!</p>
</section>
<p>The body of the <code>main</code> function holds the following code:</p>
<pre class="playground"><code class="language-rust edition2024"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>println!("Hello, world!");
<span class="boring">}</span></code></pre>
<p>This line does all the work in this little program: It prints text to the
screen. There are three important details to notice here.</p>
<p>First, <code>println!</code> calls a Rust macro. If it had called a function instead, it
would be entered as <code>println</code> (without the <code>!</code>). Rust macros are a way to write
code that generates code to extend Rust syntax, and well discuss them in more
detail in <a href="../ch20/ch20-05-macros.html">Chapter 20</a><!-- ignore -->. For now, you just need to
know that using a <code>!</code> means that youre calling a macro instead of a normal
function and that macros dont always follow the same rules as functions.</p>
<p>Second, you see the <code>"Hello, world!"</code> string. We pass this string as an argument
to <code>println!</code>, and the string is printed to the screen.</p>
<p>Third, we end the line with a semicolon (<code>;</code>), which indicates that this
expression is over, and the next one is ready to begin. Most lines of Rust code
end with a semicolon.</p>
<!-- Old headings. Do not remove or links may break. -->
<p><a id="compiling-and-running-are-separate-steps"></a></p>
<h3 id="compilation-and-execution"><a class="header" href="#compilation-and-execution">Compilation and Execution</a></h3>
<p>Youve just run a newly created program, so lets examine each step in the
process.</p>
<p>Before running a Rust program, you must compile it using the Rust compiler by
entering the <code>rustc</code> command and passing it the name of your source file, like
this:</p>
<pre><code class="language-console">$ rustc main.rs
</code></pre>
<p>If you have a C or C++ background, youll notice that this is similar to <code>gcc</code>
or <code>clang</code>. After compiling successfully, Rust outputs a binary executable.</p>
<p>On Linux, macOS, and PowerShell on Windows, you can see the executable by
entering the <code>ls</code> command in your shell:</p>
<pre><code class="language-console">$ ls
main main.rs
</code></pre>
<p>On Linux and macOS, youll see two files. With PowerShell on Windows, youll
see the same three files that you would see using CMD. With CMD on Windows, you
would enter the following:</p>
<pre><code class="language-cmd">&gt; dir /B %= the /B option says to only show the file names =%
main.exe
main.pdb
main.rs
</code></pre>
<p>This shows the source code file with the <em>.rs</em> extension, the executable file
(<em>main.exe</em> on Windows, but <em>main</em> on all other platforms), and, when using
Windows, a file containing debugging information with the <em>.pdb</em> extension.
From here, you run the <em>main</em> or <em>main.exe</em> file, like this:</p>
<pre><code class="language-console">$ ./main # or .\main on Windows
</code></pre>
<p>If your <em>main.rs</em> is your “Hello, world!” program, this line prints <code>Hello, world!</code> to your terminal.</p>
<p>If youre more familiar with a dynamic language, such as Ruby, Python, or
JavaScript, you might not be used to compiling and running a program as
separate steps. Rust is an <em>ahead-of-time compiled</em> language, meaning you can
compile a program and give the executable to someone else, and they can run it
even without having Rust installed. If you give someone a <em>.rb</em>, <em>.py</em>, or
<em>.js</em> file, they need to have a Ruby, Python, or JavaScript implementation
installed (respectively). But in those languages, you only need one command to
compile and run your program. Everything is a trade-off in language design.</p>
<p>Just compiling with <code>rustc</code> is fine for simple programs, but as your project
grows, youll want to manage all the options and make it easy to share your
code. Next, well introduce you to the Cargo tool, which will help you write
real-world Rust programs.</p>
</body>
</html>