mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-01-24 16:10:25 +08:00
parent
b0126e2c97
commit
e63c645c76
@ -29,22 +29,24 @@ Rust is installed now. Great!
|
||||
|
||||
### Installing on Windows
|
||||
|
||||
If you're on Windows, please download the appropriate [installer][install-page].
|
||||
If you're on Windows, please go to [rustup.rs](https://rustup.rs/) and follow
|
||||
the instructions to download rustup-init.exe. Run that and follow the rest of
|
||||
the instructions.
|
||||
|
||||
[install-page]: https://www.rust-lang.org/install.html
|
||||
The rest of the Windows-specific commands in the book will assume that you are
|
||||
using `cmd` as your shell. If you use a different shell, you may be able to run
|
||||
the same commands that Linux and Mac users do. If neither work, consult the
|
||||
documentation for the shell you are using.
|
||||
|
||||
### Uninstalling
|
||||
|
||||
Uninstalling Rust is as easy as installing it. On Linux or Mac, just run
|
||||
Uninstalling Rust is as easy as installing it. From your shell, run
|
||||
the uninstall script:
|
||||
|
||||
```bash
|
||||
$ rustup self uninstall
|
||||
```
|
||||
|
||||
If you used the Windows installer, you can re-run the `.msi` and it will give
|
||||
you an uninstall option.
|
||||
|
||||
### Troubleshooting
|
||||
|
||||
If you've got Rust installed, you can open up a shell, and type this:
|
||||
@ -64,12 +66,11 @@ If you see this, Rust has been installed successfully!
|
||||
Congrats!
|
||||
|
||||
If you don't and you're on Windows, check that Rust is in your `%PATH%` system
|
||||
variable. If it isn't, run the installer again, select "Change" on the "Change,
|
||||
repair, or remove installation" page and ensure "Add to PATH" is checked.
|
||||
variable.
|
||||
|
||||
If it still isn't working, there are a number of places where you can get help.
|
||||
The easiest is [the #rust IRC channel on irc.mozilla.org][irc], which you can
|
||||
access through [Mibbit][mibbit]. Click that link, and you'll be chatting with
|
||||
access through [Mibbit][mibbit]. Go to that address, and you'll be chatting with
|
||||
other Rustaceans (a silly nickname we call ourselves) who can help you out.
|
||||
Other great resources include [the user’s forum][users] and [Stack
|
||||
Overflow][stackoverflow].
|
||||
@ -82,6 +83,5 @@ Overflow][stackoverflow].
|
||||
### Local documentation
|
||||
|
||||
The installer also includes a copy of the documentation locally, so you can
|
||||
read it offline. On Linux or Mac, run `rustup doc` to open the local
|
||||
documentation in your browser. On Windows, the documentation is in a
|
||||
`share/doc` directory inside the directory where Rust was installed.
|
||||
read it offline. Run `rustup doc` to open the local documentation in your
|
||||
browser.
|
||||
|
@ -17,6 +17,8 @@ lives, but for this book, we'd suggest making a *projects* directory in your
|
||||
home directory and keeping all your projects there. Open a terminal and enter
|
||||
the following commands to make a directory for this particular project:
|
||||
|
||||
Linux and Mac:
|
||||
|
||||
```bash
|
||||
$ mkdir ~/projects
|
||||
$ cd ~/projects
|
||||
@ -24,9 +26,14 @@ $ mkdir hello_world
|
||||
$ cd hello_world
|
||||
```
|
||||
|
||||
> Note: If you’re on Windows and not using PowerShell, the `~` that represents
|
||||
> your home directory may not work.
|
||||
> Consult the documentation for your shell for more details.
|
||||
Windows:
|
||||
|
||||
```bash
|
||||
$ mkdir %USERPROFILE%\projects
|
||||
$ cd %USERPROFILE%\projects
|
||||
$ mkdir hello_world
|
||||
$ cd hello_world
|
||||
```
|
||||
|
||||
### Writing and Running a Rust Program
|
||||
|
||||
@ -54,10 +61,10 @@ $ ./main
|
||||
Hello, world!
|
||||
```
|
||||
|
||||
On Windows, just replace `main` with `main.exe`. Regardless of your operating
|
||||
system, you should see the string `Hello, world!` print to the terminal. If you
|
||||
did, then congratulations! You've officially written a Rust program. That makes
|
||||
you a Rust programmer! Welcome.
|
||||
On Windows, just replace `./main` with `.\main.exe`. Regardless of your
|
||||
operating system, you should see the string `Hello, world!` print to the
|
||||
terminal. If you did, then congratulations! You've officially written a Rust
|
||||
program. That makes you a Rust programmer! Welcome.
|
||||
|
||||
### Anatomy of a Rust Program
|
||||
|
||||
@ -131,8 +138,9 @@ main main.rs
|
||||
On Windows, you'd enter:
|
||||
|
||||
```bash
|
||||
$ dir
|
||||
main.exe main.rs
|
||||
$ dir /B # the /B option says to only show the file names
|
||||
main.exe
|
||||
main.rs
|
||||
```
|
||||
|
||||
This shows we have two files: the source code, with the `.rs` extension, and the
|
||||
@ -140,7 +148,7 @@ executable (`main.exe` on Windows, `main` everywhere else). All that's left to
|
||||
do from here is run the `main` or `main.exe` file, like this:
|
||||
|
||||
```bash
|
||||
$ ./main # or main.exe on Windows
|
||||
$ ./main # or .\main.exe on Windows
|
||||
```
|
||||
|
||||
If `main.rs` were your "Hello, world!" program, this would print `Hello,
|
||||
@ -195,11 +203,19 @@ Let's create a new project using Cargo and look at how it differs from our
|
||||
project in `hello_world`. Go back to your projects directory (or wherever you
|
||||
decided to put your code):
|
||||
|
||||
Linux and Mac:
|
||||
|
||||
```bash
|
||||
$ cd ~/projects
|
||||
```
|
||||
|
||||
And then run:
|
||||
Windows:
|
||||
|
||||
```bash
|
||||
$ cd %USERPROFILE%\projects
|
||||
```
|
||||
|
||||
And then on any operating system run:
|
||||
|
||||
```bash
|
||||
$ cargo new hello_cargo --bin
|
||||
@ -215,9 +231,9 @@ directory of the same name that we can then go into.
|
||||
If we list the files in the `hello_cargo` directory, we can see that Cargo has
|
||||
generated two files and one directory for us: a `Cargo.toml` and a `src`
|
||||
directory with a `main.rs` file inside. It has also initialized a new `git`
|
||||
repository in the `hello_cargo` directory for us; you can change this to use a
|
||||
different version control system, or no version control system, by using the
|
||||
`--vcs` flag.
|
||||
repository in the `hello_cargo` directory for us, along with a `.gitignore`
|
||||
file; you can change this to use a different version control system, or no
|
||||
version control system, by using the `--vcs` flag.
|
||||
|
||||
Open up `Cargo.toml` in your text editor of choice. It should look something
|
||||
like this:
|
||||
@ -294,10 +310,10 @@ $ cargo build
|
||||
```
|
||||
|
||||
This should have created an executable file in `target/debug/hello_cargo` (or
|
||||
`target/debug/hello_cargo.exe` on Windows), which you can run with this command:
|
||||
`target\debug\hello_cargo.exe` on Windows), which you can run with this command:
|
||||
|
||||
```bash
|
||||
$ ./target/debug/hello_cargo # or ./target/debug/hello_cargo.exe on Windows
|
||||
$ ./target/debug/hello_cargo # or .\target\debug\hello_cargo.exe on Windows
|
||||
Hello, world!
|
||||
```
|
||||
|
||||
@ -349,6 +365,10 @@ So a few more differences we've now seen:
|
||||
4. Instead of the result of the build being put in the same directory as our
|
||||
code, Cargo will put it in the `target/debug` directory.
|
||||
|
||||
The other advantage of using Cargo is that the commands are the same no matter
|
||||
what operating system you're on, so at this point we will no longer be
|
||||
providing specific instructions for Linux and Mac versus Windows.
|
||||
|
||||
### Building for Release
|
||||
|
||||
When your project is finally ready for release, you can use `cargo build
|
||||
|
@ -21,7 +21,6 @@ Let’s set up a new project. Go to your projects directory from the previous
|
||||
chapter, and create a new project using Cargo, like so:
|
||||
|
||||
```bash
|
||||
$ cd ~/projects
|
||||
$ cargo new guessing_game --bin
|
||||
$ cd guessing_game
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user