128 lines
8.0 KiB
HTML
128 lines
8.0 KiB
HTML
<!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>
|