mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 19:24:32 -06:00
@@ -8,16 +8,11 @@ how and why Rust encourages us to favor immutability, and why we might want to
|
||||
opt out of that.
|
||||
|
||||
Variable bindings being immutable means that once a value is bound, you can't
|
||||
change that value. To illustrate this, let's generate a new project with Cargo.
|
||||
Open a terminal, and navigate to the directory you want to store your projects
|
||||
in. From there, run these commands:
|
||||
change that value. To illustrate this, let's generate a new project in your
|
||||
projects directory called `bindings` by using `cargo new --bin bindings`.
|
||||
|
||||
```bash
|
||||
$ cargo new --bin bindings
|
||||
$ cd bindings
|
||||
```
|
||||
|
||||
Then open `src/main.rs` and replace its code with the following:
|
||||
Then, in your new `bindings` directory, open `src/main.rs` and replace its code
|
||||
with the following:
|
||||
|
||||
Filename: src/main.rs
|
||||
|
||||
|
||||
@@ -36,17 +36,9 @@ called from inside the `main()` function. Note that we defined
|
||||
have defined it before as well. Rust doesn’t care where you define your
|
||||
functions, only that they are defined somewhere.
|
||||
|
||||
Let’s start a new project to explore functions further. Open a terminal, and
|
||||
navigate to the directory you're keeping your projects in. From there, use
|
||||
Cargo to generate a new project, as follows:
|
||||
|
||||
```bash
|
||||
$ cargo new --bin functions
|
||||
$ cd functions
|
||||
```
|
||||
|
||||
Place the `another_function()` example in a file named `src/main.rs` and run
|
||||
it. You should see the following output:
|
||||
Let’s start a new binary project named `functions` so that we can explore
|
||||
further. Place the `another_function()` example in `src/main.rs` and run it.
|
||||
You should see the following output:
|
||||
|
||||
```bash
|
||||
$ cargo run
|
||||
|
||||
@@ -12,16 +12,8 @@ An `if` expression allows us to branch our code depending on conditions. We
|
||||
provide a condition and then say, "If this condition is met, then run this
|
||||
block of code. If the condition is not met, do not run this block of code."
|
||||
|
||||
Let’s make a new project to explore `if`. Navigate to your projects directory,
|
||||
and use Cargo to make a new project called `branches`:
|
||||
|
||||
```bash
|
||||
$ cargo new --bin branches
|
||||
$ cd branches
|
||||
```
|
||||
|
||||
Write this sample program using `if` and save it in the *branches* directory in
|
||||
`src/main.rs`:
|
||||
Let’s make a new project to explore `if`, called `branches`. In `src/main.rs`,
|
||||
put:
|
||||
|
||||
Filename: src/main.rs
|
||||
|
||||
@@ -244,13 +236,7 @@ us exactly where to find the problem in our program.
|
||||
It’s often useful to be able to execute a block of code more than one time. For
|
||||
this, Rust has several constructs called *loops*. A loop runs through the code
|
||||
inside it to the end and then starts immediately back at the beginning. To try
|
||||
out loops, let’s make a new project. Navigate to your *projects* folder and use
|
||||
Cargo to make a new project:
|
||||
|
||||
```bash
|
||||
$ cargo new --bin loops
|
||||
$ cd loops
|
||||
```
|
||||
out loops, let’s make a new project called `loops`.
|
||||
|
||||
There are three kinds of loops in Rust: `loop`, `while`, and `for`. Let’s dig
|
||||
in.
|
||||
|
||||
@@ -12,15 +12,9 @@ Let’s write a program which calculates the distance between two points.
|
||||
We’ll start off with single variable bindings, and then refactor it to
|
||||
use `struct`s instead.
|
||||
|
||||
Let’s make a new project with Cargo:
|
||||
|
||||
```bash
|
||||
$ cargo new --bin points
|
||||
$ cd points
|
||||
```
|
||||
|
||||
Here’s a short program which calculates the distance between two points. Put
|
||||
it into your `src/main.rs`:
|
||||
Let’s make a new project with Cargo called `points`. Here’s a short program
|
||||
which calculates the distance between two points to put into this project's
|
||||
`src/main.rs`:
|
||||
|
||||
Filename: src/main.rs
|
||||
|
||||
|
||||
Reference in New Issue
Block a user