Skip to content

Commit

Permalink
Re-review of earlier chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
carols10cents committed Oct 20, 2022
1 parent a27da72 commit 6a8e75d
Show file tree
Hide file tree
Showing 28 changed files with 443 additions and 220 deletions.
16 changes: 8 additions & 8 deletions nostarch/chapter01.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ $ echo $PATH

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 the community page at
silly nickname we call ourselves) on the community page at
*https://www.rust-lang.org/community*.

### Updating and Uninstalling
Expand Down Expand Up @@ -304,7 +304,7 @@ fn main() {
```

These lines define a function named `main`. The `main` function is special: it
is always the first code that runs in every executable Rust program. Here, the
is always the first code that runs in every executable Rust program. Here, the
first line declares a function named `main` that has no parameters and returns
nothing. If there were parameters, they would go inside the parentheses `()`.

Expand Down Expand Up @@ -359,7 +359,7 @@ 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.

On Linux, macOS, and PowerShell on Windows, you can see the executable by
entering the `ls` command in your shell.
entering the `ls` command in your shell:

```
$ ls
Expand Down Expand Up @@ -405,7 +405,7 @@ If you’re more familiar with a dynamic language, such as Ruby, Python, or
JavaScript, you might not be used to compiling and running a program as
separate steps. Rust is an *ahead-of-time* *compiled* language, meaning you can
compile a program and give the executable to someone else, and they can run it
even without having Rust installed. If you give someone a *.rb*, *.py*, or
even without having Rust installed. If you give someone a *.rb*, *.py*, or
*.js* file, they need to have a Ruby, Python, or JavaScript implementation
installed (respectively). But in those languages, you only need one command to
compile and run your program. Everything is a trade-off in language design.
Expand Down Expand Up @@ -470,7 +470,7 @@ It has also initialized a new Git repository along with a *.gitignore* file.
Git files won’t be generated if you run `cargo` `new` within an existing Git
repository; you can override this behavior by using `cargo` `new` `--vcs=git`.

> NoteGit is a common version control system. You can change `cargo` `new` to
> NoteGit is a common version control system. You can change `cargo` `new` to
use a different version control system or no version control system by using
the `--vcs` flag. Run `cargo` `new` `--help` to see the available options.

Expand Down Expand Up @@ -505,16 +505,16 @@ edition = "2021"

Contents of *Cargo.toml* generated by `cargo` `new`

This file is in the *TOML* (*Tom’s* *Obvious,* *Minimal* *Language*) format,
This file is in the *TOML* (*Tom’s* *Obvious,* *Minimal* *Language*) format,
which is Cargo’s configuration format.

The first line, `[package]`, is a section heading that indicates that the
following statements are configuring a package. As we add more information to
this file, we’ll add other sections.

The next three lines set the configuration information Cargo needs to compile
your program: the name, the version, and the edition of Rust to use. We’ll
talk about the `edition` key in Appendix E.
your program: the name, the version, and the edition of Rust to use. We’ll talk
about the `edition` key in Appendix E.

The last line, `[dependencies]`, is the start of a section for you to list any
of your project’s dependencies. In Rust, packages of code are referred to as
Expand Down
46 changes: 23 additions & 23 deletions nostarch/chapter03.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ can find a list of the keywords in Appendix A.

## Variables and Mutability

As mentioned in “Storing Values with Variables” on page XX, by default
As mentioned in “Storing Values with Variables” on page XX, by default,
variables are immutable. This is one of many nudges Rust gives you to write
your code in a way that takes advantage of the safety and easy concurrency that
Rust offers. However, you still have the option to make your variables mutable.
Expand Down Expand Up @@ -244,15 +244,15 @@ program). Rust’s naming convention for constants is to use all uppercase with
underscores between words. The compiler is able to evaluate a limited set of
operations at compile time, which lets us choose to write out this value in a
way that’s easier to understand and verify, rather than setting this constant
to the value 10,800. See the Rust Reference’s section on constant evaluation at
*https://doc.rust-lang.org/reference/const_eval.html* for more information on
what operations can be used when declaring constants.
to the value `10,800`. See the Rust Reference’s section on constant evaluation
at *https://doc.rust-lang.org/reference/const_eval.html* for more information
on what operations can be used when declaring constants.

Constants are valid for the entire time a program runs, within the scope in
which they were declared. This property makes constants useful for values in
your application domain that multiple parts of the program might need to know
about, such as the maximum number of points any player of a game is allowed to
earn or the speed of light.
earn, or the speed of light.

Naming hardcoded values used throughout your program as constants is useful in
conveying the meaning of that value to future maintainers of the code. It also
Expand Down Expand Up @@ -641,7 +641,7 @@ Unmatched: BoxListBullet
Rust also has two primitive types for *floating-point numbers*, which are
numbers with decimal points. Rust’s floating-point types are `f32` and `f64`,
which are 32 bits and 64 bits in size, respectively. The default type is `f64`
because on modern CPUs it’s roughly the same speed as `f32` but is capable of
because on modern CPUs, it’s roughly the same speed as `f32` but is capable of
more precision. All floating-point types are signed.

Here’s an example that shows floating-point numbers in action:
Expand Down Expand Up @@ -1151,9 +1151,9 @@ fn main() {
```

This code compiles successfully. If you run this code using `cargo run` and
enter 0, 1, 2, 3, or 4, the program will print out the corresponding value at
that index in the array. If you instead enter a number past the end of the
array, such as 10, you’ll see output like this:
enter `0`, `1`, `2`, `3`, or `4`, the program will print out the corresponding
value at that index in the array. If you instead enter a number past the end of
the array, such as `10`, you’ll see output like this:

```
thread 'main' panicked at 'index out of bounds: the len is 5 but the index is
Expand Down Expand Up @@ -1269,8 +1269,8 @@ Another function.
```

The lines execute in the order in which they appear in the `main` function.
First, the “Hello, world!” message prints, and then `another_function` is
called and its message is printed.
First the “Hello, world!” message prints, and then `another_function` is called
and its message is printed.

### Parameters

Expand Down Expand Up @@ -1897,10 +1897,10 @@ discuss in “Publishing a Crate to Crates.io” on page XX.

## Control Flow

The ability to run some code depending on whether a condition is true and to
run some code repeatedly while a condition is true are basic building blocks in
most programming languages. The most common constructs that let you control the
flow of execution of Rust code are `if` expressions and loops.
The ability to run some code depending on whether a condition is `true` and to
run some code repeatedly while a condition is `true` are basic building blocks
in most programming languages. The most common constructs that let you control
the flow of execution of Rust code are `if` expressions and loops.

### if Expressions

Expand Down Expand Up @@ -1952,7 +1952,7 @@ fn main() {
All `if` expressions start with the keyword `if`, followed by a condition. In
this case, the condition checks whether or not the variable `number` has a
value less than 5. We place the block of code to execute if the condition is
true immediately after the condition inside curly brackets. Blocks of code
`true` immediately after the condition inside curly brackets. Blocks of code
associated with the conditions in `if` expressions are sometimes called *arms*,
just like the arms in `match` expressions that we discussed in “Comparing the
Guess to the Secret Number” on page XX.
Expand Down Expand Up @@ -2204,7 +2204,7 @@ When this program executes, it checks each `if` expression in turn and executes
the first body for which the condition evaluates to `true`. Note that even
though 6 is divisible by 2, we don’t see the output `number is divisible by 2`,
nor do we see the `number is not divisible by 4, 3, or 2` text from the `else`
block. That’s because Rust only executes the block for the first true
block. That’s because Rust only executes the block for the first `true`
condition, and once it finds one, it doesn’t even check the rest.

Using too many `else if` expressions can clutter your code, so if you have more
Expand Down Expand Up @@ -2396,7 +2396,7 @@ fn main() {

When we run this program, we’ll see `again!` printed over and over continuously
until we stop the program manually. Most terminals support the keyboard
shortcut ctrl-c to interrupt a program that is stuck in a continual loop. Give
shortcut ctrl-C to interrupt a program that is stuck in a continual loop. Give
it a try:

```
Expand Down Expand Up @@ -2435,7 +2435,7 @@ again!
^Cagain!
```

The symbol `^C` represents where you pressed ctrl-c. You may or may not see the
The symbol `^C` represents where you pressed ctrl-C. You may or may not see the
word `again!` printed after the `^C`, depending on where the code was in the
loop when it received the interrupt signal.

Expand Down Expand Up @@ -2516,7 +2516,7 @@ the loop. On every iteration of the loop, we add `1` to the `counter` variable,
and then check whether the `counter` is equal to `10`. When it is, we use the
`break` keyword with the value `counter * 2`. After the loop, we use a
semicolon to end the statement that assigns the value to `result`. Finally, we
print the value in `result`, which in this case is 20.
print the value in `result`, which in this case is `20`.

#### Loop Labels to Disambiguate Between Multiple Loops

Expand Down Expand Up @@ -2666,7 +2666,7 @@ End count = 2
#### Conditional Loops with while

A program will often need to evaluate a condition within a loop. While the
condition is true, the loop runs. When the condition ceases to be true, the
condition is `true`, the loop runs. When the condition ceases to be `true`, the
program calls `break`, stopping the loop. It’s possible to implement behavior
like this using a combination of `loop`, `if`, `else`, and `break`; you could
try that now in a program, if you’d like. However, this pattern is so common
Expand Down Expand Up @@ -2778,8 +2778,8 @@ Looping through each element of a collection using a `while` loop

Here, the code counts up through the elements in the array. It starts at index
`0`, and then loops until it reaches the final index in the array (that is,
when `index < 5` is no longer true). Running this code will print every element
in the array:
when `index < 5` is no longer `true`). Running this code will print every
element in the array:

```
$ cargo run
Expand Down
42 changes: 28 additions & 14 deletions nostarch/chapter04.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ features: borrowing, slices, and how Rust lays data out in memory.

## What Is Ownership?

*Ownership* is a set of rules that governs how a Rust program manages memory.
*Ownership* is a set of rules that govern how a Rust program manages memory.
All programs have to manage the way they use a computer’s memory while running.
Some languages have garbage collection that regularly looks for no-longer-used
memory as the program runs; in other languages, the programmer must explicitly
Expand Down Expand Up @@ -197,9 +197,9 @@ let s = String::from("hello");

The double colon `::` operator allows us to namespace this particular `from`
function under the `String` type rather than using some sort of name like
`string_from`. We’ll discuss this syntax more in “Method Syntax” on page XX and
when we talk about namespacing with modules in “Paths for Referring to an Item
in the Module Tree” on page XX.
`string_from`. We’ll discuss this syntax more in “Method Syntax” on page XX,
and when we talk about namespacing with modules in “Paths for Referring to an
Item in the Module Tree” on page XX.

This kind of string *can* be mutated:

Expand Down Expand Up @@ -378,7 +378,7 @@ of the memory safety bugs we mentioned previously. Freeing memory twice can
lead to memory corruption, which can potentially lead to security
vulnerabilities.

To ensure memory safety, after the line `let s2 =` `s1``;`, Rust considers `s1`
To ensure memory safety, after the line `let s2 = s1``;`, Rust considers `s1`
as no longer valid. Therefore, Rust doesn’t need to free anything when `s1`
goes out of scope. Check out what happens when you try to use `s1` after `s2`
is created; it won’t work:
Expand Down Expand Up @@ -423,7 +423,7 @@ error[E0382]: borrow of moved value: `s1`
```

```
does not implement the `Copy` trait
does not implement the `Copy` trait
```

```
Expand Down Expand Up @@ -464,7 +464,7 @@ In addition, there’s a design choice that’s implied by this: Rust will never
automatically create “deep” copies of your data. Therefore, any *automatic*
copying can be assumed to be inexpensive in terms of runtime performance.

#### Variables and Data Interacting With Clone
#### Variables and Data Interacting with Clone

If we *do* want to deeply copy the heap data of the `String`, not just the
stack data, we can use a common method called `clone`. We’ll discuss method
Expand Down Expand Up @@ -558,7 +558,9 @@ assigning a value to a variable. Passing a variable to a function will move or
copy, just as assignment does. Listing 4-3 has an example with some annotations
showing where variables go into and out of scope.

Filename: src/main.rs
```
// src/main.rs
```

```
fn main() {
Expand Down Expand Up @@ -665,7 +667,9 @@ Returning values can also transfer ownership. Listing 4-4 shows an example of a
function that returns some value, with similar annotations as those in Listing
4-3.

Filename: src/main.rs
```
// src/main.rs
```

```
fn main() {
Expand Down Expand Up @@ -1045,6 +1049,9 @@ reference

```
| ------- help: consider changing this to be a mutable
```

```
reference: `&mut String`
```

Expand All @@ -1054,6 +1061,9 @@ reference: `&mut String`

```
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `some_string` is a `&` reference, so
```

```
the data it refers to cannot be borrowed as mutable
```

Expand Down Expand Up @@ -1485,7 +1495,9 @@ is no value for it to be borrowed from
Let’s take a closer look at exactly what’s happening at each stage of our
`dangle` code:

Filename: src/main.rs
```
// src/main.rs
```

```
fn dangle() -> &String { // dangle returns a reference to a String
Expand Down Expand Up @@ -1655,7 +1667,9 @@ because it’s a separate value from the `String`, there’s no guarantee that i
will still be valid in the future. Consider the program in Listing 4-8 that
uses the `first_word` function from Listing 4-7.

Filename: src/main.rs
```
// src/main.rs
```

```
fn main() {
Expand Down Expand Up @@ -1749,16 +1763,16 @@ one more than the last position in the slice. Internally, the slice data
structure stores the starting position and the length of the slice, which
corresponds to `ending_index` minus `starting_index`. So, in the case of `let
world = &s[6..11];`, `world` would be a slice that contains a pointer to the
byte at index 6 of `s` with a length value of 5.
byte at index 6 of `s` with a length value of `5`.

Figure 4-6 shows this in a diagram.


Unmatched: GraphicSlug

Unmatched: CaptionLine
With Rust’s `..` range syntax, if you want to start at index zero, you
can drop the value before the two periods. In other words, these are equal:
With Rust’s `..` range syntax, if you want to start at index 0, you can
drop the value before the two periods. In other words, these are equal:

```
let s = String::from("hello");
Expand Down
Loading

0 comments on commit 6a8e75d

Please sign in to comment.