feat: added cleanscript
This commit is contained in:
17
ch01/ch01-00-getting-started.html
Normal file
17
ch01/ch01-00-getting-started.html
Normal file
@@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Getting Started</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1 id="getting-started"><a class="header" href="#getting-started">Getting Started</a></h1>
|
||||
<p>Let’s start your Rust journey! There’s a lot to learn, but every journey starts
|
||||
somewhere. In this chapter, we’ll discuss:</p>
|
||||
<ul>
|
||||
<li>Installing Rust on Linux, macOS, and Windows</li>
|
||||
<li>Writing a program that prints <code>Hello, world!</code></li>
|
||||
<li>Using <code>cargo</code>, Rust’s package manager and build system</li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
127
ch01/ch01-01-installation.html
Normal file
127
ch01/ch01-01-installation.html
Normal file
@@ -0,0 +1,127 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Installation</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2 id="installation"><a class="header" href="#installation">Installation</a></h2>
|
||||
<p>The first step is to install Rust. We’ll download Rust through <code>rustup</code>, a
|
||||
command line tool for managing Rust versions and associated tools. You’ll need
|
||||
an internet connection for the download.</p>
|
||||
<section class="note" aria-role="note">
|
||||
<p>Note: If you prefer not to use <code>rustup</code> for some reason, please see the
|
||||
<a href="https://forge.rust-lang.org/infra/other-installation-methods.html">Other Rust Installation Methods page</a> for more options.</p>
|
||||
</section>
|
||||
<p>The following steps install the latest stable version of the Rust compiler.
|
||||
Rust’s stability guarantees ensure that all the examples in the book that
|
||||
compile will continue to compile with newer Rust versions. The output might
|
||||
differ slightly between versions because Rust often improves error messages and
|
||||
warnings. In other words, any newer, stable version of Rust you install using
|
||||
these steps should work as expected with the content of this book.</p>
|
||||
<section class="note" aria-role="note">
|
||||
<h3 id="command-line-notation"><a class="header" href="#command-line-notation">Command Line Notation</a></h3>
|
||||
<p>In this chapter and throughout the book, we’ll show some commands used in the
|
||||
terminal. Lines that you should enter in a terminal all start with <code>$</code>. You
|
||||
don’t need to type the <code>$</code> character; it’s the command line prompt shown to
|
||||
indicate the start of each command. Lines that don’t start with <code>$</code> typically
|
||||
show the output of the previous command. Additionally, PowerShell-specific
|
||||
examples will use <code>></code> rather than <code>$</code>.</p>
|
||||
</section>
|
||||
<h3 id="installing-rustup-on-linux-or-macos"><a class="header" href="#installing-rustup-on-linux-or-macos">Installing <code>rustup</code> on Linux or macOS</a></h3>
|
||||
<p>If you’re using Linux or macOS, open a terminal and enter the following command:</p>
|
||||
<pre><code class="language-console">$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
|
||||
</code></pre>
|
||||
<p>The command downloads a script and starts the installation of the <code>rustup</code>
|
||||
tool, which installs the latest stable version of Rust. You might be prompted
|
||||
for your password. If the install is successful, the following line will appear:</p>
|
||||
<pre><code class="language-text">Rust is installed now. Great!
|
||||
</code></pre>
|
||||
<p>You will also need a <em>linker</em>, which is a program that Rust uses to join its
|
||||
compiled outputs into one file. It is likely you already have one. If you get
|
||||
linker errors, you should install a C compiler, which will typically include a
|
||||
linker. A C compiler is also useful because some common Rust packages depend on
|
||||
C code and will need a C compiler.</p>
|
||||
<p>On macOS, you can get a C compiler by running:</p>
|
||||
<pre><code class="language-console">$ xcode-select --install
|
||||
</code></pre>
|
||||
<p>Linux users should generally install GCC or Clang, according to their
|
||||
distribution’s documentation. For example, if you use Ubuntu, you can install
|
||||
the <code>build-essential</code> package.</p>
|
||||
<h3 id="installing-rustup-on-windows"><a class="header" href="#installing-rustup-on-windows">Installing <code>rustup</code> on Windows</a></h3>
|
||||
<p>On Windows, go to <a href="https://www.rust-lang.org/tools/install">https://www.rust-lang.org/tools/install</a><!-- ignore
|
||||
--> and follow the instructions for installing Rust. At some point in the
|
||||
installation, you’ll be prompted to install Visual Studio. This provides a
|
||||
linker and the native libraries needed to compile programs. If you need more
|
||||
help with this step, see
|
||||
<a href="https://rust-lang.github.io/rustup/installation/windows-msvc.html">https://rust-lang.github.io/rustup/installation/windows-msvc.html</a><!--
|
||||
ignore -->.</p>
|
||||
<p>The rest of this book uses commands that work in both <em>cmd.exe</em> and PowerShell.
|
||||
If there are specific differences, we’ll explain which to use.</p>
|
||||
<h3 id="troubleshooting"><a class="header" href="#troubleshooting">Troubleshooting</a></h3>
|
||||
<p>To check whether you have Rust installed correctly, open a shell and enter this
|
||||
line:</p>
|
||||
<pre><code class="language-console">$ rustc --version
|
||||
</code></pre>
|
||||
<p>You should see the version number, commit hash, and commit date for the latest
|
||||
stable version that has been released, in the following format:</p>
|
||||
<pre><code class="language-text">rustc x.y.z (abcabcabc yyyy-mm-dd)
|
||||
</code></pre>
|
||||
<p>If you see this information, you have installed Rust successfully! If you don’t
|
||||
see this information, check that Rust is in your <code>%PATH%</code> system variable as
|
||||
follows.</p>
|
||||
<p>In Windows CMD, use:</p>
|
||||
<pre><code class="language-console">> echo %PATH%
|
||||
</code></pre>
|
||||
<p>In PowerShell, use:</p>
|
||||
<pre><code class="language-powershell">> echo $env:Path
|
||||
</code></pre>
|
||||
<p>In Linux and macOS, use:</p>
|
||||
<pre><code class="language-console">$ echo $PATH
|
||||
</code></pre>
|
||||
<p>If that’s all correct and Rust still isn’t working, there are a number of
|
||||
places you can get help. Find out how to get in touch with other Rustaceans (a
|
||||
silly nickname we call ourselves) on <a href="https://www.rust-lang.org/community">the community page</a>.</p>
|
||||
<h3 id="updating-and-uninstalling"><a class="header" href="#updating-and-uninstalling">Updating and Uninstalling</a></h3>
|
||||
<p>Once Rust is installed via <code>rustup</code>, updating to a newly released version is
|
||||
easy. From your shell, run the following update script:</p>
|
||||
<pre><code class="language-console">$ rustup update
|
||||
</code></pre>
|
||||
<p>To uninstall Rust and <code>rustup</code>, run the following uninstall script from your
|
||||
shell:</p>
|
||||
<pre><code class="language-console">$ rustup self uninstall
|
||||
</code></pre>
|
||||
<!-- Old headings. Do not remove or links may break. -->
|
||||
<p><a id="local-documentation"></a></p>
|
||||
<h3 id="reading-the-local-documentation"><a class="header" href="#reading-the-local-documentation">Reading the Local Documentation</a></h3>
|
||||
<p>The installation of Rust also includes a local copy of the documentation so
|
||||
that you can read it offline. Run <code>rustup doc</code> to open the local documentation
|
||||
in your browser.</p>
|
||||
<p>Any time a type or function is provided by the standard library and you’re not
|
||||
sure what it does or how to use it, use the application programming interface
|
||||
(API) documentation to find out!</p>
|
||||
<!-- Old headings. Do not remove or links may break. -->
|
||||
<p><a id="text-editors-and-integrated-development-environments"></a></p>
|
||||
<h3 id="using-text-editors-and-ides"><a class="header" href="#using-text-editors-and-ides">Using Text Editors and IDEs</a></h3>
|
||||
<p>This book makes no assumptions about what tools you use to author Rust code.
|
||||
Just about any text editor will get the job done! However, many text editors and
|
||||
integrated development environments (IDEs) have built-in support for Rust. You
|
||||
can always find a fairly current list of many editors and IDEs on <a href="https://www.rust-lang.org/tools">the tools
|
||||
page</a> on the Rust website.</p>
|
||||
<h3 id="working-offline-with-this-book"><a class="header" href="#working-offline-with-this-book">Working Offline with This Book</a></h3>
|
||||
<p>In several examples, we will use Rust packages beyond the standard library. To
|
||||
work through those examples, you will either need to have an internet connection
|
||||
or to have downloaded those dependencies ahead of time. To download the
|
||||
dependencies ahead of time, you can run the following commands. (We’ll explain
|
||||
what <code>cargo</code> is and what each of these commands does in detail later.)</p>
|
||||
<pre><code class="language-console">$ cargo new get-dependencies
|
||||
$ cd get-dependencies
|
||||
$ cargo add rand@0.8.5 trpl@0.2.0
|
||||
</code></pre>
|
||||
<p>This will cache the downloads for these packages so you will not need to
|
||||
download them later. Once you have run this command, you do not need to keep the
|
||||
<code>get-dependencies</code> folder. If you have run this command, you can use the
|
||||
<code>--offline</code> flag with all <code>cargo</code> commands in the rest of the book to use these
|
||||
cached versions instead of attempting to use the network.</p>
|
||||
</body>
|
||||
</html>
|
||||
161
ch01/ch01-02-hello-world.html
Normal file
161
ch01/ch01-02-hello-world.html
Normal file
@@ -0,0 +1,161 @@
|
||||
<!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 you’ve installed Rust, it’s time to write your first Rust program.
|
||||
It’s traditional when learning a new language to write a little program that
|
||||
prints the text <code>Hello, world!</code> to the screen, so we’ll 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 IDE’s
|
||||
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>You’ll start by making a directory to store your Rust code. It doesn’t 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">> mkdir "%USERPROFILE%\projects"
|
||||
> cd /d "%USERPROFILE%\projects"
|
||||
> mkdir hello_world
|
||||
> 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 you’re 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">> rustc main.rs
|
||||
> .\main
|
||||
Hello, world!
|
||||
</code></pre>
|
||||
<p>Regardless of your operating system, the string <code>Hello, world!</code> should print to
|
||||
the terminal. If you don’t 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! You’ve 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>Let’s review this “Hello, world!” program in detail. Here’s 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. It’s 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 we’ll 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 you’re calling a macro instead of a normal
|
||||
function and that macros don’t 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>You’ve just run a newly created program, so let’s 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, you’ll 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, you’ll see two files. With PowerShell on Windows, you’ll
|
||||
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">> 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 you’re 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, you’ll want to manage all the options and make it easy to share your
|
||||
code. Next, we’ll introduce you to the Cargo tool, which will help you write
|
||||
real-world Rust programs.</p>
|
||||
</body>
|
||||
</html>
|
||||
202
ch01/ch01-03-hello-cargo.html
Normal file
202
ch01/ch01-03-hello-cargo.html
Normal file
@@ -0,0 +1,202 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Hello, Cargo!</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2 id="hello-cargo"><a class="header" href="#hello-cargo">Hello, Cargo!</a></h2>
|
||||
<p>Cargo is Rust’s build system and package manager. Most Rustaceans use this tool
|
||||
to manage their Rust projects because Cargo handles a lot of tasks for you,
|
||||
such as building your code, downloading the libraries your code depends on, and
|
||||
building those libraries. (We call the libraries that your code needs
|
||||
<em>dependencies</em>.)</p>
|
||||
<p>The simplest Rust programs, like the one we’ve written so far, don’t have any
|
||||
dependencies. If we had built the “Hello, world!” project with Cargo, it would
|
||||
only use the part of Cargo that handles building your code. As you write more
|
||||
complex Rust programs, you’ll add dependencies, and if you start a project
|
||||
using Cargo, adding dependencies will be much easier to do.</p>
|
||||
<p>Because the vast majority of Rust projects use Cargo, the rest of this book
|
||||
assumes that you’re using Cargo too. Cargo comes installed with Rust if you
|
||||
used the official installers discussed in the
|
||||
<a href="ch01-01-installation.html#installation">“Installation”</a><!-- ignore --> section. If you installed Rust
|
||||
through some other means, check whether Cargo is installed by entering the
|
||||
following in your terminal:</p>
|
||||
<pre><code class="language-console">$ cargo --version
|
||||
</code></pre>
|
||||
<p>If you see a version number, you have it! If you see an error, such as <code>command not found</code>, look at the documentation for your method of installation to
|
||||
determine how to install Cargo separately.</p>
|
||||
<h3 id="creating-a-project-with-cargo"><a class="header" href="#creating-a-project-with-cargo">Creating a Project with Cargo</a></h3>
|
||||
<p>Let’s create a new project using Cargo and look at how it differs from our
|
||||
original “Hello, world!” project. Navigate back to your <em>projects</em> directory
|
||||
(or wherever you decided to store your code). Then, on any operating system,
|
||||
run the following:</p>
|
||||
<pre><code class="language-console">$ cargo new hello_cargo
|
||||
$ cd hello_cargo
|
||||
</code></pre>
|
||||
<p>The first command creates a new directory and project called <em>hello_cargo</em>.
|
||||
We’ve named our project <em>hello_cargo</em>, and Cargo creates its files in a
|
||||
directory of the same name.</p>
|
||||
<p>Go into the <em>hello_cargo</em> directory and list the files. You’ll see that Cargo
|
||||
has generated two files and one directory for us: a <em>Cargo.toml</em> file and a
|
||||
<em>src</em> directory with a <em>main.rs</em> file inside.</p>
|
||||
<p>It has also initialized a new Git repository along with a <em>.gitignore</em> file.
|
||||
Git files won’t be generated if you run <code>cargo new</code> within an existing Git
|
||||
repository; you can override this behavior by using <code>cargo new --vcs=git</code>.</p>
|
||||
<section class="note" aria-role="note">
|
||||
<p>Note: Git is a common version control system. You can change <code>cargo new</code> to
|
||||
use a different version control system or no version control system by using
|
||||
the <code>--vcs</code> flag. Run <code>cargo new --help</code> to see the available options.</p>
|
||||
</section>
|
||||
<p>Open <em>Cargo.toml</em> in your text editor of choice. It should look similar to the
|
||||
code in Listing 1-2.</p>
|
||||
<figure class="listing" id="listing-1-2">
|
||||
<span class="file-name">Filename: Cargo.toml</span>
|
||||
<pre><code class="language-toml">[package]
|
||||
name = "hello_cargo"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
</code></pre>
|
||||
<figcaption><a href="#listing-1-2">Listing 1-2</a>: Contents of <em>Cargo.toml</em> generated by <code>cargo new</code></figcaption>
|
||||
</figure>
|
||||
<p>This file is in the <a href="https://toml.io"><em>TOML</em></a><!-- ignore --> (<em>Tom’s Obvious, Minimal
|
||||
Language</em>) format, which is Cargo’s configuration format.</p>
|
||||
<p>The first line, <code>[package]</code>, is a section heading that indicates that the
|
||||
following statements are configuring a package. As we add more information to
|
||||
this file, we’ll add other sections.</p>
|
||||
<p>The next three lines set the configuration information Cargo needs to compile
|
||||
your program: the name, the version, and the edition of Rust to use. We’ll talk
|
||||
about the <code>edition</code> key in <a href="../appendix/appendix-05-editions.html">Appendix E</a><!-- ignore -->.</p>
|
||||
<p>The last line, <code>[dependencies]</code>, is the start of a section for you to list any
|
||||
of your project’s dependencies. In Rust, packages of code are referred to as
|
||||
<em>crates</em>. We won’t need any other crates for this project, but we will in the
|
||||
first project in Chapter 2, so we’ll use this dependencies section then.</p>
|
||||
<p>Now open <em>src/main.rs</em> and take a look:</p>
|
||||
<p><span class="filename">Filename: src/main.rs</span></p>
|
||||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||||
println!("Hello, world!");
|
||||
}</code></pre>
|
||||
<p>Cargo has generated a “Hello, world!” program for you, just like the one we
|
||||
wrote in Listing 1-1! So far, the differences between our project and the
|
||||
project Cargo generated are that Cargo placed the code in the <em>src</em> directory,
|
||||
and we have a <em>Cargo.toml</em> configuration file in the top directory.</p>
|
||||
<p>Cargo expects your source files to live inside the <em>src</em> directory. The
|
||||
top-level project directory is just for README files, license information,
|
||||
configuration files, and anything else not related to your code. Using Cargo
|
||||
helps you organize your projects. There’s a place for everything, and
|
||||
everything is in its place.</p>
|
||||
<p>If you started a project that doesn’t use Cargo, as we did with the “Hello,
|
||||
world!” project, you can convert it to a project that does use Cargo. Move the
|
||||
project code into the <em>src</em> directory and create an appropriate <em>Cargo.toml</em>
|
||||
file. One easy way to get that <em>Cargo.toml</em> file is to run <code>cargo init</code>, which
|
||||
will create it for you automatically.</p>
|
||||
<h3 id="building-and-running-a-cargo-project"><a class="header" href="#building-and-running-a-cargo-project">Building and Running a Cargo Project</a></h3>
|
||||
<p>Now let’s look at what’s different when we build and run the “Hello, world!”
|
||||
program with Cargo! From your <em>hello_cargo</em> directory, build your project by
|
||||
entering the following command:</p>
|
||||
<pre><code class="language-console">$ cargo build
|
||||
Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo)
|
||||
Finished dev [unoptimized + debuginfo] target(s) in 2.85 secs
|
||||
</code></pre>
|
||||
<p>This command creates an executable file in <em>target/debug/hello_cargo</em> (or
|
||||
<em>target\debug\hello_cargo.exe</em> on Windows) rather than in your current
|
||||
directory. Because the default build is a debug build, Cargo puts the binary in
|
||||
a directory named <em>debug</em>. You can run the executable with this command:</p>
|
||||
<pre><code class="language-console">$ ./target/debug/hello_cargo # or .\target\debug\hello_cargo.exe on Windows
|
||||
Hello, world!
|
||||
</code></pre>
|
||||
<p>If all goes well, <code>Hello, world!</code> should print to the terminal. Running <code>cargo build</code> for the first time also causes Cargo to create a new file at the top
|
||||
level: <em>Cargo.lock</em>. This file keeps track of the exact versions of
|
||||
dependencies in your project. This project doesn’t have dependencies, so the
|
||||
file is a bit sparse. You won’t ever need to change this file manually; Cargo
|
||||
manages its contents for you.</p>
|
||||
<p>We just built a project with <code>cargo build</code> and ran it with
|
||||
<code>./target/debug/hello_cargo</code>, but we can also use <code>cargo run</code> to compile the
|
||||
code and then run the resultant executable all in one command:</p>
|
||||
<pre><code class="language-console">$ cargo run
|
||||
Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
|
||||
Running `target/debug/hello_cargo`
|
||||
Hello, world!
|
||||
</code></pre>
|
||||
<p>Using <code>cargo run</code> is more convenient than having to remember to run <code>cargo build</code> and then use the whole path to the binary, so most developers use <code>cargo run</code>.</p>
|
||||
<p>Notice that this time we didn’t see output indicating that Cargo was compiling
|
||||
<code>hello_cargo</code>. Cargo figured out that the files hadn’t changed, so it didn’t
|
||||
rebuild but just ran the binary. If you had modified your source code, Cargo
|
||||
would have rebuilt the project before running it, and you would have seen this
|
||||
output:</p>
|
||||
<pre><code class="language-console">$ cargo run
|
||||
Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo)
|
||||
Finished dev [unoptimized + debuginfo] target(s) in 0.33 secs
|
||||
Running `target/debug/hello_cargo`
|
||||
Hello, world!
|
||||
</code></pre>
|
||||
<p>Cargo also provides a command called <code>cargo check</code>. This command quickly checks
|
||||
your code to make sure it compiles but doesn’t produce an executable:</p>
|
||||
<pre><code class="language-console">$ cargo check
|
||||
Checking hello_cargo v0.1.0 (file:///projects/hello_cargo)
|
||||
Finished dev [unoptimized + debuginfo] target(s) in 0.32 secs
|
||||
</code></pre>
|
||||
<p>Why would you not want an executable? Often, <code>cargo check</code> is much faster than
|
||||
<code>cargo build</code> because it skips the step of producing an executable. If you’re
|
||||
continually checking your work while writing the code, using <code>cargo check</code> will
|
||||
speed up the process of letting you know if your project is still compiling! As
|
||||
such, many Rustaceans run <code>cargo check</code> periodically as they write their
|
||||
program to make sure it compiles. Then, they run <code>cargo build</code> when they’re
|
||||
ready to use the executable.</p>
|
||||
<p>Let’s recap what we’ve learned so far about Cargo:</p>
|
||||
<ul>
|
||||
<li>We can create a project using <code>cargo new</code>.</li>
|
||||
<li>We can build a project using <code>cargo build</code>.</li>
|
||||
<li>We can build and run a project in one step using <code>cargo run</code>.</li>
|
||||
<li>We can build a project without producing a binary to check for errors using
|
||||
<code>cargo check</code>.</li>
|
||||
<li>Instead of saving the result of the build in the same directory as our code,
|
||||
Cargo stores it in the <em>target/debug</em> directory.</li>
|
||||
</ul>
|
||||
<p>An additional advantage of using Cargo is that the commands are the same no
|
||||
matter which operating system you’re working on. So, at this point, we’ll no
|
||||
longer provide specific instructions for Linux and macOS versus Windows.</p>
|
||||
<h3 id="building-for-release"><a class="header" href="#building-for-release">Building for Release</a></h3>
|
||||
<p>When your project is finally ready for release, you can use <code>cargo build --release</code> to compile it with optimizations. This command will create an
|
||||
executable in <em>target/release</em> instead of <em>target/debug</em>. The optimizations
|
||||
make your Rust code run faster, but turning them on lengthens the time it takes
|
||||
for your program to compile. This is why there are two different profiles: one
|
||||
for development, when you want to rebuild quickly and often, and another for
|
||||
building the final program you’ll give to a user that won’t be rebuilt
|
||||
repeatedly and that will run as fast as possible. If you’re benchmarking your
|
||||
code’s running time, be sure to run <code>cargo build --release</code> and benchmark with
|
||||
the executable in <em>target/release</em>.</p>
|
||||
<!-- Old headings. Do not remove or links may break. -->
|
||||
<p><a id="cargo-as-convention"></a></p>
|
||||
<h3 id="leveraging-cargos-conventions"><a class="header" href="#leveraging-cargos-conventions">Leveraging Cargo’s Conventions</a></h3>
|
||||
<p>With simple projects, Cargo doesn’t provide a lot of value over just using
|
||||
<code>rustc</code>, but it will prove its worth as your programs become more intricate.
|
||||
Once programs grow to multiple files or need a dependency, it’s much easier to
|
||||
let Cargo coordinate the build.</p>
|
||||
<p>Even though the <code>hello_cargo</code> project is simple, it now uses much of the real
|
||||
tooling you’ll use in the rest of your Rust career. In fact, to work on any
|
||||
existing projects, you can use the following commands to check out the code
|
||||
using Git, change to that project’s directory, and build:</p>
|
||||
<pre><code class="language-console">$ git clone example.org/someproject
|
||||
$ cd someproject
|
||||
$ cargo build
|
||||
</code></pre>
|
||||
<p>For more information about Cargo, check out <a href="https://doc.rust-lang.org/cargo/">its documentation</a>.</p>
|
||||
<h2 id="summary"><a class="header" href="#summary">Summary</a></h2>
|
||||
<p>You’re already off to a great start on your Rust journey! In this chapter, you
|
||||
learned how to:</p>
|
||||
<ul>
|
||||
<li>Install the latest stable version of Rust using <code>rustup</code>.</li>
|
||||
<li>Update to a newer Rust version.</li>
|
||||
<li>Open locally installed documentation.</li>
|
||||
<li>Write and run a “Hello, world!” program using <code>rustc</code> directly.</li>
|
||||
<li>Create and run a new project using the conventions of Cargo.</li>
|
||||
</ul>
|
||||
<p>This is a great time to build a more substantial program to get used to reading
|
||||
and writing Rust code. So, in Chapter 2, we’ll build a guessing game program.
|
||||
If you would rather start by learning how common programming concepts work in
|
||||
Rust, see Chapter 3 and then return to Chapter 2.</p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user