Skip to content

Commit e77f35a

Browse files
committed
update original
1 parent fec85cf commit e77f35a

7 files changed

Lines changed: 43 additions & 36 deletions

File tree

rust-by-example/src/flow_control/for.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn main() {
8080
data is provided. Once the collection has been consumed it is no longer
8181
available for reuse as it has been 'moved' within the loop.
8282

83-
```rust,editable,ignore,mdbook-runnable
83+
```rust,editable
8484
fn main() {
8585
let names = vec!["Bob", "Frank", "Ferris"];
8686
@@ -91,8 +91,9 @@ fn main() {
9191
}
9292
}
9393
94-
println!("names: {:?}", names);
95-
// FIXME ^ Comment out this line
94+
// `names` has been 'moved' and can no longer be used.
95+
// Try uncommenting the line below to see the compiler error:
96+
// println!("names: {:?}", names);
9697
}
9798
```
9899

rust-by-example/src/fn/closures/closure_examples/iter_find.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ fn main() {
4242
let array1 = [1, 2, 3];
4343
let array2 = [4, 5, 6];
4444
45-
// `array1.iter()` yields `&i32`
45+
// `array1.iter()` yields `&i32`, and `find` passes `&Item` to the
46+
// predicate. Since `Item = &i32`, the closure argument has type `&&i32`.
4647
println!("Find 2 in array1: {:?}", array1.iter().find(|&&x| x == 2));
47-
// `array2.into_iter()` yields `i32`
48+
// `array2.into_iter()` yields `i32` (since Rust 2021 edition), and
49+
// `find` passes `&Item` to the predicate. Since `Item = i32`, the
50+
// closure argument has type `&i32`.
4851
println!("Find 2 in array2: {:?}", array2.into_iter().find(|&x| x == 2));
4952
}
5053
```

rust-by-example/src/generics/new_types.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,50 @@
33
The `newtype` idiom gives compile time guarantees that the right type of value is supplied
44
to a program.
55

6-
For example, an age verification function that checks age in years, *must* be given
7-
a value of type `Years`.
6+
For example, a function that measures distance in miles, *must* be given
7+
a value of type `Miles`.
88

99
```rust, editable
10-
struct Years(i64);
10+
struct Miles(f64);
1111
12-
struct Days(i64);
12+
struct Kilometers(f64);
1313
14-
impl Years {
15-
pub fn to_days(&self) -> Days {
16-
Days(self.0 * 365)
14+
impl Miles {
15+
pub fn to_kilometers(&self) -> Kilometers {
16+
Kilometers(self.0 * 1.609344)
1717
}
1818
}
1919
20-
impl Days {
21-
/// truncates partial years
22-
pub fn to_years(&self) -> Years {
23-
Years(self.0 / 365)
20+
impl Kilometers {
21+
pub fn to_miles(&self) -> Miles {
22+
Miles(self.0 / 1.609344)
2423
}
2524
}
2625
27-
fn is_adult(age: &Years) -> bool {
28-
age.0 >= 18
26+
fn is_a_marathon(distance: &Miles) -> bool {
27+
distance.0 >= 26.2
2928
}
3029
3130
fn main() {
32-
let age = Years(25);
33-
let age_days = age.to_days();
34-
println!("Is an adult? {}", is_adult(&age));
35-
println!("Is an adult? {}", is_adult(&age_days.to_years()));
36-
// println!("Is an adult? {}", is_adult(&age_days));
31+
let distance = Miles(30.0);
32+
let distance_km = distance.to_kilometers();
33+
println!("Is a marathon? {}", is_a_marathon(&distance));
34+
println!("Is a marathon? {}", is_a_marathon(&distance_km.to_miles()));
35+
// println!("Is a marathon? {}", is_a_marathon(&distance_km));
3736
}
3837
```
3938

40-
Uncomment the last print statement to observe that the type supplied must be `Years`.
39+
Uncomment the last print statement to observe that the type supplied must be `Miles`.
4140

4241
To obtain the `newtype`'s value as the base type, you may use the tuple or destructuring syntax like so:
4342

4443
```rust, editable
45-
struct Years(i64);
44+
struct Miles(f64);
4645
4746
fn main() {
48-
let years = Years(42);
49-
let years_as_primitive_1: i64 = years.0; // Tuple
50-
let Years(years_as_primitive_2) = years; // Destructuring
47+
let distance = Miles(42.0);
48+
let distance_as_primitive_1: f64 = distance.0; // Tuple
49+
let Miles(distance_as_primitive_2) = distance; // Destructuring
5150
}
5251
```
5352

rust-by-example/src/hello/print.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ for these types. To print text for custom types, more steps are required.
8787
Implementing the `fmt::Display` trait automatically implements the
8888
[`ToString`] trait which allows us to [convert] the type to [`String`][string].
8989

90-
In *line 43*, `#[allow(dead_code)]` is an [attribute] which only applies to the module after it.
90+
In *line 43*, `#[allow(dead_code)]` is an [attribute] which only applies to the item after it.
9191

9292
### Activities
9393

rust-by-example/src/hello/print/print_display.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ fn main() {
9494
println!("Display: {}", point);
9595
println!("Debug: {:?}", point);
9696
97-
// Error. Both `Debug` and `Display` were implemented, but `{:b}`
98-
// requires `fmt::Binary` to be implemented. This will not work.
97+
// The following line would not compile: both `Debug` and `Display`
98+
// were implemented, but `{:b}` requires `fmt::Binary` to be
99+
// implemented, which it hasn't been for `Point2D`.
99100
// println!("What does Point2D look like in binary: {:b}?", point);
100101
}
101102
```

rust-by-example/src/hello/print/print_display/testcase_list.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ struct List(Vec<i32>);
2424
2525
impl fmt::Display for List {
2626
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27-
// Extract the value using tuple indexing,
28-
// and create a reference to `vec`.
27+
// Create a reference to the Vec<i32> stored in the List struct.
2928
let vec = &self.0;
3029
3130
write!(f, "[")?;

rust-by-example/src/trait/drop.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,19 @@ impl TempFile {
7676
}
7777
7878
// When TempFile is dropped:
79-
// 1. First, our drop implementation will remove the file's name from the filesystem.
80-
// 2. Then, File's drop will close the file, removing its underlying content from the disk.
79+
// 1. First, our custom drop implementation runs. The file is still open at this point,
80+
// but we can remove it from the filesystem by path.
81+
// 2. Then, after our drop returns, Rust automatically drops each field,
82+
// so File's drop runs and closes the file handle.
8183
impl Drop for TempFile {
8284
fn drop(&mut self) {
85+
// Note: the File is still open here — field destructors run after this method.
8386
if let Err(e) = std::fs::remove_file(&self.path) {
8487
eprintln!("Failed to remove temporary file: {}", e);
8588
}
8689
println!("> Dropped temporary file: {:?}", self.path);
87-
// File's drop is implicitly called here because it is a field of this struct.
90+
// After this method returns, Rust will drop each field (including `file`),
91+
// which closes the underlying file handle.
8892
}
8993
}
9094

0 commit comments

Comments
 (0)