Skip to content

Commit 710ff42

Browse files
committed
Fix hidelines syntax change.
1 parent ad376e7 commit 710ff42

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

src/ch04-02-references-and-borrowing.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ References are **non-owning pointers**, because they do not own the data they po
7575
The previous examples using boxes and strings have not shown how Rust "follows" a pointer to its data. For example, the `println!` macro has mysteriously worked for both owned strings of type `String`, and for string references of type `&String`. The underlying mechanism is the **dereference** operator, written with an asterisk (`*`). For example, here's a program that uses dereferences in a few different ways:
7676

7777
```aquascope,interpreter
78-
#fn main() {
78+
# fn main() {
7979
let mut x: Box<i32> = Box::new(1);
8080
let a: i32 = *x; // *x reads the heap value, so a = 1
8181
*x += 1; // *x on the left-side modifies the heap value,
@@ -86,15 +86,15 @@ let b: i32 = **r1; // two dereferences get us to the heap value
8686
8787
let r2: &i32 = &*x; // r2 points to the heap value directly
8888
let c: i32 = *r2;`[]` // so only one dereference is needed to read it
89-
#}
89+
# }
9090
```
9191

9292
Observe the difference between `r1` pointing to `x` on the stack, and `r2` pointing to the heap value `2`.
9393

9494
You probably won't see the dereference operator very often when you read Rust code. Rust implicitly inserts dereferences and references in certain cases, such as calling a method with the dot operator. For example, this program shows two equivalent ways of calling the [`i32::abs`](https://doc.rust-lang.org/std/primitive.i32.html#method.abs) (absolute value) and [`str::len`](https://doc.rust-lang.org/std/primitive.str.html#method.len) (string length) functions:
9595

9696
```rust,ignore
97-
#fn main() {
97+
# fn main() {
9898
let x: Box<i32> = Box::new(-1);
9999
let x_abs1 = i32::abs(*x); // explicit dereference
100100
let x_abs2 = x.abs(); // implicit dereference
@@ -109,7 +109,7 @@ let s = String::from("Hello");
109109
let s_len1 = str::len(&s); // explicit reference
110110
let s_len2 = s.len(); // implicit reference
111111
assert_eq!(s_len1, s_len2);
112-
#}
112+
# }
113113
```
114114

115115
This example shows implicit conversions in three ways:

src/ch04-03-fixing-ownership-errors.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,34 +284,34 @@ In sum, **if a value does not own heap data, then it can be copied without a mov
284284
So if we have a vector of non-`Copy` types like `String`, then how do we safely get access to an element of the vector? Here's a few different ways to safely do so. First, you can avoid taking ownership of the string and just use an immutable reference:
285285
286286
```rust,ignore
287-
#fn main() {
287+
# fn main() {
288288
let v: Vec<String> = vec![String::from("Hello world")];
289289
let s_ref: &String = &v[0];
290290
println!("{s_ref}!");
291-
#}
291+
# }
292292
```
293293
294294
Second, you can clone the data if you want to get ownership of the string while leaving the vector alone:
295295

296296
```rust,ignore
297-
#fn main() {
297+
# fn main() {
298298
let v: Vec<String> = vec![String::from("Hello world")];
299299
let mut s: String = v[0].clone();
300300
s.push('!');
301301
println!("{s}");
302-
#}
302+
# }
303303
```
304304

305305
Finally, you can use a method like [`Vec::remove`] to move the string out of the vector:
306306

307307
```rust,ignore
308-
#fn main() {
308+
# fn main() {
309309
let mut v: Vec<String> = vec![String::from("Hello world")];
310310
let mut s: String = v.remove(0);
311311
s.push('!');
312312
println!("{s}");
313313
assert!(v.len() == 0);
314-
#}
314+
# }
315315
```
316316

317317

@@ -424,24 +424,24 @@ error[E0502]: cannot borrow `a[_]` as immutable because it is also borrowed as m
424424
Again, **this program is safe.** For cases like these, Rust often provides a function in the standard library that can work around the borrow checker. For example, we could use [`slice::split_at_mut`][split_at_mut]:
425425

426426
```rust,ignore
427-
#fn main() {
427+
# fn main() {
428428
let mut a = [0, 1, 2, 3];
429429
let (a_l, a_r) = a.split_at_mut(2);
430430
let x = &mut a_l[1];
431431
let y = &a_r[0];
432432
*x += *y;
433-
#}
433+
# }
434434
```
435435

436436
You might wonder, but how is `split_at_mut` implemented? In some Rust libraries, especially core types like `Vec` or `slice`, you will often find **`unsafe` blocks**. `unsafe` blocks allow the use of "raw" pointers, which are not checked for safety by the borrow checker. For example, we could use an unsafe block to accomplish our task:
437437

438438
```rust,ignore
439-
#fn main() {
439+
# fn main() {
440440
let mut a = [0, 1, 2, 3];
441441
let x = &mut a[1] as *mut i32;
442442
let y = &a[2] as *const i32;
443443
unsafe { *x += *y; } // DO NOT DO THIS unless you know what you're doing!
444-
#}
444+
# }
445445
```
446446

447447
Unsafe code is sometimes necessary to work around the limitations of the borrow checker. As a general strategy, let's say the borrow checker rejects a program you think is actually safe. Then you should look for standard library functions (like `split_at_mut`) that contain `unsafe` blocks which solve your problem. We will discuss unsafe code further in [Chapter 20][unsafe]. For now, just be aware that unsafe code is how Rust implements certain otherwise-impossible patterns.

src/ch18-02-trait-objects.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,19 +231,19 @@ Rust just needs to know the type of a single element in the vector to infer `T`.
231231
an empty vector causes a type inference error:
232232

233233
```rust,ignore,does_not_compile
234-
#fn main() {
234+
# fn main() {
235235
let v = vec![];
236236
// error[E0282]: type annotations needed for `Vec<T>`
237-
#}
237+
# }
238238
```
239239

240240
But adding an element enables Rust to infer the type of the vector:
241241

242242
```rust,ignore
243-
#fn main() {
243+
# fn main() {
244244
let v = vec!["Hello world"];
245245
// ok, v : Vec<&str>
246-
#}
246+
# }
247247
```
248248

249249
Type inference is trickier for trait objects. For example, say we tried to factor

0 commit comments

Comments
 (0)