File tree Expand file tree Collapse file tree 2 files changed +5
-9
lines changed
src/idiomatic/leveraging-the-type-system
borrow-checker-invariants Expand file tree Collapse file tree 2 files changed +5
-9
lines changed Original file line number Diff line number Diff line change @@ -4,9 +4,8 @@ minutes: 10
44
55# Using the Borrow checker to enforce Invariants
66
7- The logic of the borrow checker, while tied to "memory ownership", can be
8- abstracted away from this central use case to model other problems and prevent
9- API misuse.
7+ The borrow checker, while added to enforce memory ownership, can be
8+ leveraged model other problems and prevent API misuse.
109
1110``` rust,editable
1211// Doors can be open or closed, and you need the right key to lock or unlock
@@ -66,9 +65,7 @@ fn main() {
6665 wrong key the door is still closed (here represented as an error) and the key
6766 persists regardless.
6867
69- - The rules of the borrow checker exist to prevent developers from accessing,
70- changing, and holding onto data in memory in unpredictable ways without being
71- so restrictive that it would prevent _ writing software_ . The underlying
68+ - The rules of the borrow checker exist to prevent memory safety bugs. However, the underlying
7269 logical system does not "know" what memory is. All it does is enforce a
7370 specific set of rules of how different operations affect what later operations
7471 are possible.
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ fn main() {
3232
3333<details >
3434
35- - What if we want to be able to guarantee that a value can only be used once?
35+ - Problem: How can we guarantee a value is used only once?
3636
3737- Motivation: A nonce is a piece of random, unique data used in cryptographic
3838 protocols to prevent replay attacks.
@@ -46,8 +46,7 @@ fn main() {
4646- Rust has an obvious tool for "Once you use this, you can't use it anymore":
4747 Using a value as an _ owned argument_ .
4848
49- - Highlight: the ` encrypt ` function takes references for ` key ` and ` data ` but
50- not ` nonce `
49+ - Highlight: the ` encrypt ` function takes ` nonce ` by value (an owned argument), but ` key ` and ` data ` by reference.
5150
5251- By keeping constructors private and not implementing clone/copy for a type,
5352 making the interior type opaque (as per the newtype pattern), we can prevent
You can’t perform that action at this time.
0 commit comments