Skip to content

Commit

Permalink
Backport fixes from NoStarch
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskrycho committed Jan 27, 2025
1 parent ad25d35 commit 907315f
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
let lucky_number = 7; // Im feeling lucky today
let lucky_number = 7; // I'm feeling lucky today
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() {
// Im feeling lucky today
// I'm feeling lucky today
let lucky_number = 7;
}
31 changes: 16 additions & 15 deletions listings/ch04-understanding-ownership/listing-04-04/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
fn main() {
let s1 = gives_ownership(); // gives_ownership moves its return
// value into s1
let s1 = gives_ownership(); // gives_ownership moves its return
// value into s1

let s2 = String::from("hello"); // s2 comes into scope
let s2 = String::from("hello"); // s2 comes into scope

let s3 = takes_and_gives_back(s2); // s2 is moved into
// takes_and_gives_back, which also
// moves its return value into s3
let s3 = takes_and_gives_back(s2); // s2 is moved into
// takes_and_gives_back, which also
// moves its return value into s3
} // Here, s3 goes out of scope and is dropped. s2 was moved, so nothing
// happens. s1 goes out of scope and is dropped.

fn gives_ownership() -> String { // gives_ownership will move its
// return value into the function
// that calls it
fn gives_ownership() -> String { // gives_ownership will move its
// return value into the function
// that calls it

let some_string = String::from("yours"); // some_string comes into scope

some_string // some_string is returned and
// moves out to the calling
// function
some_string // some_string is returned and
// moves out to the calling
// function
}

// This function takes a String and returns one
fn takes_and_gives_back(a_string: String) -> String { // a_string comes into
// scope
// This function takes a String and returns a String.
fn takes_and_gives_back(a_string: String) -> String {
// a_string comes into
// scope

a_string // a_string is returned and moves out to the calling function
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ fn first_word(s: &str) -> &str {
fn main() {
let my_string = String::from("hello world");

// `first_word` works on slices of `String`s, whether partial or whole
// `first_word` works on slices of `String`s, whether partial or whole.
let word = first_word(&my_string[0..6]);
let word = first_word(&my_string[..]);
// `first_word` also works on references to `String`s, which are equivalent
// to whole slices of `String`s
// to whole slices of `String`s.
let word = first_word(&my_string);

let my_string_literal = "hello world";

// `first_word` works on slices of string literals, whether partial or whole
// `first_word` works on slices of string literals, whether partial or
// whole.
let word = first_word(&my_string_literal[0..6]);
let word = first_word(&my_string_literal[..]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() {
let r1 = &s; // no problem
let r2 = &s; // no problem
println!("{r1} and {r2}");
// variables r1 and r2 will not be used after this point
// Variables r1 and r2 will not be used after this point.

let r3 = &mut s; // no problem
println!("{r3}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ fn dangle() -> &String { // dangle returns a reference to a String
let s = String::from("hello"); // s is a new String

&s // we return a reference to the String, s
} // Here, s goes out of scope, and is dropped. Its memory goes away.
} // Here, s goes out of scope, and is dropped, so its memory goes away.
// Danger!
// ANCHOR_END: here
// ANCHOR_END: here
2 changes: 0 additions & 2 deletions src/ch01-03-hello-cargo.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ name = "hello_cargo"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
```

Expand Down
2 changes: 1 addition & 1 deletion src/ch02-00-guessing-game-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ If `parse` is _not_ able to turn the string into a number, it will return an
`Err` value that contains more information about the error. The `Err` value
does not match the `Ok(num)` pattern in the first `match` arm, but it does
match the `Err(_)` pattern in the second arm. The underscore, `_`, is a
catchall value; in this example, we’re saying we want to match all `Err`
catch-all value; in this example, we’re saying we want to match all `Err`
values, no matter what information they have inside them. So the program will
execute the second arm’s code, `continue`, which tells the program to go to the
next iteration of the `loop` and ask for another guess. So, effectively, the
Expand Down
12 changes: 6 additions & 6 deletions src/ch03-02-data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ when it’s safe to assume the number is positive, it’s shown with no sign.
Signed numbers are stored using [two’s complement][twos-complement]<!-- ignore
--> representation.

Each signed variant can store numbers from -(2<sup>n - 1</sup>) to 2<sup>n -
1</sup> - 1 inclusive, where _n_ is the number of bits that variant uses. So an
`i8` can store numbers from -(2<sup>7</sup>) to 2<sup>7</sup> - 1, which equals
-128 to 127. Unsigned variants can store numbers from 0 to 2<sup>n</sup> - 1,
so a `u8` can store numbers from 0 to 2<sup>8</sup> - 1, which equals 0 to 255.
Each signed variant can store numbers from (2<sup>n 1</sup>) to 2<sup>n
1</sup> 1 inclusive, where _n_ is the number of bits that variant uses. So an
`i8` can store numbers from (2<sup>7</sup>) to 2<sup>7</sup> 1, which equals
128 to 127. Unsigned variants can store numbers from 0 to 2<sup>n</sup> 1,
so a `u8` can store numbers from 0 to 2<sup>8</sup> 1, which equals 0 to 255.

Additionally, the `isize` and `usize` types depend on the architecture of the
computer your program is running on, which is denoted in the table as “arch”:
Expand Down Expand Up @@ -120,7 +120,7 @@ some sort of collection.
>
> - Wrap in all modes with the `wrapping_*` methods, such as `wrapping_add`.
> - Return the `None` value if there is overflow with the `checked_*` methods.
> - Return the value and a boolean indicating whether there was overflow with
> - Return the value and a Boolean indicating whether there was overflow with
> the `overflowing_*` methods.
> - Saturate at the value’s minimum or maximum values with the `saturating_*`
> methods.
Expand Down
4 changes: 2 additions & 2 deletions src/ch03-04-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ comment continues until the end of the line. For comments that extend beyond a
single line, you’ll need to include `//` on each line, like this:

```rust
// So were doing something complicated here, long enough that we need
// So we're doing something complicated here, long enough that we need
// multiple lines of comments to do it! Whew! Hopefully, this comment will
// explain whats going on.
// explain what's going on.
```

Comments can also be placed at the end of lines containing code:
Expand Down

0 comments on commit 907315f

Please sign in to comment.