mirror of https://github.com/rust-lang/book
Update chapter 1 from latest print edits
This commit is contained in:
parent
16ba58d00d
commit
f801008f55
|
@ -10,18 +10,18 @@ an internet connection for the download.
|
||||||
The following steps install the latest stable version of the Rust compiler.
|
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
|
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
|
compile will continue to compile with newer Rust versions. The output might
|
||||||
differ slightly between versions, because Rust often improves error messages
|
differ slightly between versions because Rust often improves error messages and
|
||||||
and warnings. In other words, any newer, stable version of Rust you install
|
warnings. In other words, any newer, stable version of Rust you install using
|
||||||
using these steps should work as expected with the content of this book.
|
these steps should work as expected with the content of this book.
|
||||||
|
|
||||||
> ### Command Line Notation
|
> ### Command Line Notation
|
||||||
>
|
>
|
||||||
> In this chapter and throughout the book, we’ll show some commands used in the
|
> 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 `$`. You
|
> terminal. Lines that you should enter in a terminal all start with `$`. You
|
||||||
> don’t need to type in the `$` character; it’s the command line prompt shown
|
> don’t need to type the `$` character; it’s the command line prompt shown to
|
||||||
> to indicate the start of each command. Lines that don’t start with `$`
|
> indicate the start of each command. Lines that don’t start with `$` typically
|
||||||
> typically show the output of the previous command. Additionally,
|
> show the output of the previous command. Additionally, PowerShell-specific
|
||||||
> PowerShell-specific examples will use `>` rather than `$`.
|
> examples will use `>` rather than `$`.
|
||||||
|
|
||||||
### Installing `rustup` on Linux or macOS
|
### Installing `rustup` on Linux or macOS
|
||||||
|
|
||||||
|
@ -60,13 +60,14 @@ the `build-essential` package.
|
||||||
On Windows, go to [https://www.rust-lang.org/tools/install][install] and follow
|
On Windows, go to [https://www.rust-lang.org/tools/install][install] and follow
|
||||||
the instructions for installing Rust. At some point in the installation, you’ll
|
the instructions for installing Rust. At some point in the installation, you’ll
|
||||||
receive a message explaining that you’ll also need the MSVC build tools for
|
receive a message explaining that you’ll also need the MSVC build tools for
|
||||||
Visual Studio 2013 or later. To acquire the build tools, you’ll need to install
|
Visual Studio 2013 or later.
|
||||||
[Visual Studio 2022][visualstudio]. When asked which workloads to install,
|
|
||||||
include:
|
|
||||||
|
|
||||||
- “Desktop Development with C++”
|
To acquire the build tools, you’ll need to install [Visual Studio
|
||||||
- The Windows 10 or 11 SDK
|
2022][visualstudio]. When asked which workloads to install, include:
|
||||||
- The English language pack component, along with any other language pack of
|
|
||||||
|
* “Desktop Development with C++”
|
||||||
|
* The Windows 10 or 11 SDK
|
||||||
|
* The English language pack component, along with any other language pack of
|
||||||
your choosing
|
your choosing
|
||||||
|
|
||||||
The rest of this book uses commands that work in both *cmd.exe* and PowerShell.
|
The rest of this book uses commands that work in both *cmd.exe* and PowerShell.
|
||||||
|
@ -82,15 +83,15 @@ $ rustc --version
|
||||||
```
|
```
|
||||||
|
|
||||||
You should see the version number, commit hash, and commit date for the latest
|
You should see the version number, commit hash, and commit date for the latest
|
||||||
stable version that has been released in the following format:
|
stable version that has been released, in the following format:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
rustc x.y.z (abcabcabc yyyy-mm-dd)
|
rustc x.y.z (abcabcabc yyyy-mm-dd)
|
||||||
```
|
```
|
||||||
|
|
||||||
If you see this information, you have installed Rust successfully! If you don’t
|
If you see this information, you have installed Rust successfully! If you don’t
|
||||||
see this information, check that Rust is in your `%PATH%`
|
see this information, check that Rust is in your `%PATH%` system variable as
|
||||||
system variable as follows.
|
follows.
|
||||||
|
|
||||||
In Windows CMD, use:
|
In Windows CMD, use:
|
||||||
|
|
||||||
|
@ -100,27 +101,24 @@ In Windows CMD, use:
|
||||||
|
|
||||||
In PowerShell, use:
|
In PowerShell, use:
|
||||||
|
|
||||||
```console
|
```powershell
|
||||||
> echo $env:Path
|
> echo $env:Path
|
||||||
```
|
```
|
||||||
|
|
||||||
In Linux and macOS, use:
|
In Linux and macOS, use:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
echo $PATH
|
$ echo $PATH
|
||||||
```
|
```
|
||||||
|
|
||||||
If that’s all correct and Rust still isn’t working, there are a number of
|
If that’s all correct and Rust still isn’t working, there are a number of
|
||||||
places you can get help. The easiest is the #beginners channel on [the official
|
places you can get help. Find out how to get in touch with other Rustaceans (a
|
||||||
Rust Discord][discord]. There, you can chat with other Rustaceans (a silly
|
silly nickname we call ourselves) on [the community page][community].
|
||||||
nickname we call ourselves) who can help you out. Other great resources include
|
|
||||||
[the Users forum][users] and [Stack Overflow][stackoverflow].
|
|
||||||
|
|
||||||
### Updating and Uninstalling
|
### Updating and Uninstalling
|
||||||
|
|
||||||
Once Rust is installed via `rustup`, when a new version of Rust is released,
|
Once Rust is installed via `rustup`, updating to a newly released version is
|
||||||
updating to the latest version is easy. From your shell, run the following
|
easy. From your shell, run the following update script:
|
||||||
update script:
|
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ rustup update
|
$ rustup update
|
||||||
|
@ -135,9 +133,9 @@ $ rustup self uninstall
|
||||||
|
|
||||||
### Local Documentation
|
### Local Documentation
|
||||||
|
|
||||||
The installation of Rust also includes a local copy of the documentation, so
|
The installation of Rust also includes a local copy of the documentation so
|
||||||
you can read it offline. Run `rustup doc` to open the local documentation in
|
that you can read it offline. Run `rustup doc` to open the local documentation
|
||||||
your browser.
|
in your browser.
|
||||||
|
|
||||||
Any time a type or function is provided by the standard library and you’re not
|
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
|
sure what it does or how to use it, use the application programming interface
|
||||||
|
@ -146,6 +144,4 @@ sure what it does or how to use it, use the application programming interface
|
||||||
[otherinstall]: https://forge.rust-lang.org/infra/other-installation-methods.html
|
[otherinstall]: https://forge.rust-lang.org/infra/other-installation-methods.html
|
||||||
[install]: https://www.rust-lang.org/tools/install
|
[install]: https://www.rust-lang.org/tools/install
|
||||||
[visualstudio]: https://visualstudio.microsoft.com/downloads/
|
[visualstudio]: https://visualstudio.microsoft.com/downloads/
|
||||||
[discord]: https://discord.gg/rust-lang
|
[community]: https://www.rust-lang.org/community
|
||||||
[users]: https://users.rust-lang.org/
|
|
||||||
[stackoverflow]: https://stackoverflow.com/questions/tagged/rust
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
## Hello, World!
|
## Hello, World!
|
||||||
|
|
||||||
Now that you’ve installed Rust, let’s write your first Rust program. It’s
|
Now that you’ve installed Rust, it’s time to write your first Rust program.
|
||||||
traditional when learning a new language to write a little program that prints
|
It’s traditional when learning a new language to write a little program that
|
||||||
the text `Hello, world!` to the screen, so we’ll do the same here!
|
prints the text `Hello, world!` to the screen, so we’ll do the same here!
|
||||||
|
|
||||||
> Note: This book assumes basic familiarity with the command line. Rust makes
|
> 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
|
> no specific demands about your editing or tooling or where your code lives, so
|
||||||
|
@ -10,7 +10,7 @@ the text `Hello, world!` to the screen, so we’ll do the same here!
|
||||||
> the command line, feel free to use your favorite IDE. Many IDEs now have some
|
> 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
|
> degree of Rust support; check the IDE’s documentation for details. The Rust
|
||||||
> team has been focusing on enabling great IDE support via `rust-analyzer`. See
|
> team has been focusing on enabling great IDE support via `rust-analyzer`. See
|
||||||
> [Appendix D][devtools]<!-- ignore --> for more details!
|
> [Appendix D][devtools]<!-- ignore --> for more details.
|
||||||
|
|
||||||
### Creating a Project Directory
|
### Creating a Project Directory
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ line as the function declaration, adding one space in between.
|
||||||
> use an automatic formatter tool called `rustfmt` to format your code in a
|
> use an automatic formatter tool called `rustfmt` to format your code in a
|
||||||
> particular style (more on `rustfmt` in
|
> particular style (more on `rustfmt` in
|
||||||
> [Appendix D][devtools]<!-- ignore -->). The Rust team has included this tool
|
> [Appendix D][devtools]<!-- ignore -->). The Rust team has included this tool
|
||||||
> with the standard Rust distribution, like `rustc`, so it should already be
|
> with the standard Rust distribution, as `rustc` is, so it should already be
|
||||||
> installed on your computer!
|
> installed on your computer!
|
||||||
|
|
||||||
The body of the `main` function holds the following code:
|
The body of the `main` function holds the following code:
|
||||||
|
@ -126,7 +126,7 @@ First, Rust style is to indent with four spaces, not a tab.
|
||||||
Second, `println!` calls a Rust macro. If it had called a function instead, it
|
Second, `println!` calls a Rust macro. If it had called a function instead, it
|
||||||
would be entered as `println` (without the `!`). We’ll discuss Rust macros in
|
would be entered as `println` (without the `!`). We’ll discuss Rust macros in
|
||||||
more detail in Chapter 19. For now, you just need to know that using a `!`
|
more detail in Chapter 19. For now, you just need to know that using a `!`
|
||||||
means that you’re calling a macro instead of a normal function, and that macros
|
means that you’re calling a macro instead of a normal function and that macros
|
||||||
don’t always follow the same rules as functions.
|
don’t always follow the same rules as functions.
|
||||||
|
|
||||||
Third, you see the `"Hello, world!"` string. We pass this string as an argument
|
Third, you see the `"Hello, world!"` string. We pass this string as an argument
|
||||||
|
@ -153,16 +153,16 @@ If you have a C or C++ background, you’ll notice that this is similar to `gcc`
|
||||||
or `clang`. After compiling successfully, Rust outputs a binary executable.
|
or `clang`. After compiling successfully, Rust outputs a binary executable.
|
||||||
|
|
||||||
On Linux, macOS, and PowerShell on Windows, you can see the executable by
|
On Linux, macOS, and PowerShell on Windows, you can see the executable by
|
||||||
entering the `ls` command in your shell. On Linux and macOS, you’ll see two
|
entering the `ls` command in your shell:
|
||||||
files. With PowerShell on Windows, you’ll see the same three files that you
|
|
||||||
would see using CMD.
|
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ ls
|
$ ls
|
||||||
main main.rs
|
main main.rs
|
||||||
```
|
```
|
||||||
|
|
||||||
With CMD on Windows, you would enter the following:
|
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:
|
||||||
|
|
||||||
```cmd
|
```cmd
|
||||||
> dir /B %= the /B option says to only show the file names =%
|
> dir /B %= the /B option says to only show the file names =%
|
||||||
|
|
|
@ -17,7 +17,7 @@ assumes that you’re using Cargo too. Cargo comes installed with Rust if you
|
||||||
used the official installers discussed in the
|
used the official installers discussed in the
|
||||||
[“Installation”][installation]<!-- ignore --> section. If you installed Rust
|
[“Installation”][installation]<!-- ignore --> section. If you installed Rust
|
||||||
through some other means, check whether Cargo is installed by entering the
|
through some other means, check whether Cargo is installed by entering the
|
||||||
following into your terminal:
|
following in your terminal:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ cargo --version
|
$ cargo --version
|
||||||
|
@ -30,9 +30,9 @@ determine how to install Cargo separately.
|
||||||
### Creating a Project with Cargo
|
### Creating a Project with Cargo
|
||||||
|
|
||||||
Let’s create a new project using Cargo and look at how it differs from our
|
Let’s create a new project using Cargo and look at how it differs from our
|
||||||
original “Hello, world!” project. Navigate back to your *projects* directory (or
|
original “Hello, world!” project. Navigate back to your *projects* directory
|
||||||
wherever you decided to store your code). Then, on any operating system, run
|
(or wherever you decided to store your code). Then, on any operating system,
|
||||||
the following:
|
run the following:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ cargo new hello_cargo
|
$ cargo new hello_cargo
|
||||||
|
@ -74,8 +74,8 @@ edition = "2021"
|
||||||
<span class="caption">Listing 1-2: Contents of *Cargo.toml* generated by `cargo
|
<span class="caption">Listing 1-2: Contents of *Cargo.toml* generated by `cargo
|
||||||
new`</span>
|
new`</span>
|
||||||
|
|
||||||
This file is in the [*TOML*](https://toml.io)<!-- ignore --> (*Tom’s Obvious,
|
This file is in the [*TOML*][toml]<!-- ignore --> (*Tom’s Obvious, Minimal
|
||||||
Minimal Language*) format, which is Cargo’s configuration format.
|
Language*) format, which is Cargo’s configuration format.
|
||||||
|
|
||||||
The first line, `[package]`, is a section heading that indicates that the
|
The first line, `[package]`, is a section heading that indicates that the
|
||||||
following statements are configuring a package. As we add more information to
|
following statements are configuring a package. As we add more information to
|
||||||
|
@ -102,7 +102,7 @@ fn main() {
|
||||||
|
|
||||||
Cargo has generated a “Hello, world!” program for you, just like the one we
|
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
|
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 *src* directory,
|
project Cargo generated are that Cargo placed the code in the *src* directory
|
||||||
and we have a *Cargo.toml* configuration file in the top directory.
|
and we have a *Cargo.toml* configuration file in the top directory.
|
||||||
|
|
||||||
Cargo expects your source files to live inside the *src* directory. The
|
Cargo expects your source files to live inside the *src* directory. The
|
||||||
|
@ -147,7 +147,7 @@ manages its contents for you.
|
||||||
|
|
||||||
We just built a project with `cargo build` and ran it with
|
We just built a project with `cargo build` and ran it with
|
||||||
`./target/debug/hello_cargo`, but we can also use `cargo run` to compile the
|
`./target/debug/hello_cargo`, but we can also use `cargo run` to compile the
|
||||||
code and then run the resulting executable all in one command:
|
code and then run the resultant executable all in one command:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ cargo run
|
$ cargo run
|
||||||
|
@ -184,7 +184,7 @@ $ cargo check
|
||||||
```
|
```
|
||||||
|
|
||||||
Why would you not want an executable? Often, `cargo check` is much faster than
|
Why would you not want an executable? Often, `cargo check` is much faster than
|
||||||
`cargo build`, because it skips the step of producing an executable. If you’re
|
`cargo build` because it skips the step of producing an executable. If you’re
|
||||||
continually checking your work while writing the code, using `cargo check` will
|
continually checking your work while writing the code, using `cargo check` will
|
||||||
speed up the process of letting you know if your project is still compiling! As
|
speed up the process of letting you know if your project is still compiling! As
|
||||||
such, many Rustaceans run `cargo check` periodically as they write their
|
such, many Rustaceans run `cargo check` periodically as they write their
|
||||||
|
@ -236,9 +236,7 @@ $ cd someproject
|
||||||
$ cargo build
|
$ cargo build
|
||||||
```
|
```
|
||||||
|
|
||||||
For more information about Cargo, check out [its documentation].
|
For more information about Cargo, check out [its documentation][cargo].
|
||||||
|
|
||||||
[its documentation]: https://doc.rust-lang.org/cargo/
|
|
||||||
|
|
||||||
## Summary
|
## Summary
|
||||||
|
|
||||||
|
@ -257,4 +255,6 @@ If you would rather start by learning how common programming concepts work in
|
||||||
Rust, see Chapter 3 and then return to Chapter 2.
|
Rust, see Chapter 3 and then return to Chapter 2.
|
||||||
|
|
||||||
[installation]: ch01-01-installation.html#installation
|
[installation]: ch01-01-installation.html#installation
|
||||||
|
[toml]: https://toml.io
|
||||||
[appendix-e]: appendix-05-editions.html
|
[appendix-e]: appendix-05-editions.html
|
||||||
|
[cargo]: https://doc.rust-lang.org/cargo/
|
||||||
|
|
Loading…
Reference in New Issue