[rust official language Chinese translation] preparation — Installation

Time:2021-9-20

Let’s start rust’s learning journey! There is a lot to learn, but just as every journey begins somewhere. In this chapter, we will discuss:

  • Install rust on Linux, MacOS, windows
  • Write an outputHello, world!Procedure
  • usecargo, perform the package management and system compilation of rust

install

Step 1: install rust. We fromrustup–A command line tool for managing rust versions and associated tools — download rust. You need to connect to the network to download.

Note: if you choose not to pass for various reasonsrustupThe way, then go toRust official websiteSee other ways.

The next steps describe how to install the latest version of the rust compiler. The stability of rust ensures that all examples in this book can be compiled in the latest version of rust. Since rust often improves the output experience of error messages and warning messages, the output of editing results of each version may be slightly different. In other words, any updated, stable version of rust you install will have expected output for the examples in this book.

Command line tag
In this chapter or in various parts of the book, we will show some commands used at the terminal. Everything displayed on the command line will be$start. You don’t need to enter it manually$Symbol, which only represents the beginning of each command line. Those who don’t$The first content line represents the output of the previous command. In addition, special PowerShell examples use>instead of$

Installing rustup on Linux and MacOS

If you are using Linux or MacOS, please open the terminal and enter the following command

curl https://sh.rustup.rs -sSf | sh
This command will download a script and start the installation of the latest version of the tool ` rustup ‘. You may be prompted to enter a password during installation. If the installation is successful, the following will appear:
Rust is installed now. Great!
If you like, you can check the script content before downloading the script and running it.

The whole installation process will automatically add rust to your system variable path when you log in next time. If you want to start using rust directly without restarting the terminal, please run the following command in the terminal to add rust to your system variable:

source $HOME/.cargo/env

Or, add the following to ~ /. Bash_ In profile:

export PATH=”$HOME/.cargo/bin:$PATH”

Besides, you need some kind of linker. It is likely that your system has already installed the linker. However, if an error occurs when you try to compile a rust program and you are prompted that the linker is not executed, it means that your system has not installed the linker, and you need to install it manually.
C compilers usually have the correct linker. You can check the documentation of your system to learn how to install the C compiler. of course,
Some common rust packages rely on C language code and often require a C compiler. So you can install it now.

Installing rustup on Windows

In windows, openhttps://www.rust-lang.org/too…, install according to the instructions. During the installation process, you will receive a message that you need to install visual studio 2013 or later C + + build tools. The easiest way to get the build tool is to install itBuild Tools for Visual Studio 2017, the tool can be used in web pagesOther tools and frameworksGet from.

The commands shown in the rest of this book can be found incmd.exeandPowerShellUsed in. If there are differences for a certain scene, we will clearly point out in the book.

Update and uninstall

When you passrustupAfter installing rust, you can easily update the version of rust. Open your terminal and run the following command:

rustup update

Uninstall rust andrustup, please run the following uninstall script in your terminal

rustup self uninstall

common problem

Open the terminal and enter the following command to check whether you have installed rust correctly

rustc –version

After that, you should see the output version number with the latest stable version, the submission hash value and the submission date. The format is as follows:

rustc x.y.x (abcabcabc yyyy-mm-dd)

If you see the message, you have successfully installed rust. If you don’t see this information, please check whether rust is in your Windows environment%PATH%In the system variable. If all steps are OK, but rust still doesn’t work properly, you can get help in many places. The easiest way is throughMibbitvisitthe #rust IRC channel on irc.mozilla.org 。 In this place, you can get help by chatting with other rustaceans (our nickname for ourselves). You can also learn fromthe Users forumandStack OverflowGet help.

Local document

The installation package contains a copy of the local documentation so that you can read the documentation offline after installation. functionrustup docYou can open a document in the explorer to explore.
At any time, if you don’t know how to use the types or functions provided by the standard library, you can find the corresponding instructions through the API documents.