Skip to content

Commit 96c65b6

Browse files
MhmRhmwillcrichton
authored andcommitted
Fix missing code from ch04-02-references-and-borrowing.md
The code for adding the `default` parameter to the example for "Data Must Outlive All Of Its References" sub-section was missing. Signed-off-by: Mohammad Rahimi <rahimi.mhmmd@gmail.com>
1 parent ebbb65b commit 96c65b6

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,16 @@ This snippet introduces a new kind of permission, the flow permission @Perm{flow
392392

393393
Unlike the @Perm{read}@Perm{write}@Perm{own} permissions, @Perm{flow} does not change throughout the body of a function. A reference has the @Perm{flow} permission if it's allowed to be used (that is, to *flow*) in a particular expression. For example, let's say we change `first` to a new function `first_or` that includes a `default` parameter:
394394

395+
```rust,ignore
396+
fn first_or(strings: &Vec<String>, default: &String) -> &String {
397+
if strings.is_empty() {
398+
default
399+
} else {
400+
&strings[0]
401+
}
402+
}
403+
```
404+
395405
This function no longer compiles, because the expressions `&strings[0]` and `default` lack the necessary @Perm{flow} permission to be returned. But why? Rust gives the following error:
396406

397407
```text

0 commit comments

Comments
 (0)