@@ -6,13 +6,13 @@ Rust's functional roots allow for it to be more expressive in the type system
66than many other languages, and turn many kinds of programming problems into
77"static typing" problems. A key part of this idea is the way generic types work.
88
9- In C++ and Java, for example, generic types a meta-programming construct for the
10- compiler. A ` Vec<int> ` and ` Vec<char> ` in C++ are just two different copies of
9+ In C++ and Java, for example, generic types are a meta-programming construct for
10+ the compiler. ` Vec<int> ` and ` Vec<char> ` in C++ are just two different copies of
1111the same boilerplate code for a ` Vec ` type, with two different types filled in.
1212
1313In Rust, the generic type parameter creates what is known as a "type class
1414constraint", and each value used by an end user * actually changes the type of
15- each instatiation* . In other words, ` Vec<usize> ` and ` Vec<char> ` * are two
15+ each instatiation* . In other words, ` Vec<usize> ` and ` Vec<char> ` * are two
1616different types* .
1717
1818This is why ` impl ` blocks must specify generic parameters: different ones can
@@ -102,9 +102,9 @@ fn main() {
102102Why those chances to panic? Because there are * state invariants* here:
103103
1041041 . ` set_param ` can only be called in an "initial" state.
105- 1 . ` init ` must be called before any scripts are compiled.
106- 1 . ` exec ` can only be called in a "loaded" or "ready" state.
107- 1 . ` init ` must be called * exactly once* .
105+ 2 . ` init ` must be called before any scripts are compiled.
106+ 3 . ` exec ` can only be called in a "loaded" or "ready" state.
107+ 4 . ` init ` must be called * exactly once* .
108108
109109It would be possible to add these to the ` Error ` types instead of panicking.
110110However, this solution is suboptimal. Not only would users of the code
@@ -117,7 +117,9 @@ if it were misused. After all, every user's program contains the invalid call
117117order in the logic itself.
118118
119119In Rust, this is actually possible! The solution is to * change the type* in
120- order to enforce the invariants. How? With a private generic parameter.
120+ order to enforce the invariants.
121+
122+ How? With a private generic parameter.
121123
122124Here is what that looks like:
123125
@@ -227,7 +229,7 @@ fn main() {
227229```
228230
229231They would get a syntax error. The type ` Interpreter<Loaded> ` does not
230- implement set param , only the type ` Interpreter<Init> ` does.
232+ implement ` set_param() ` , only the type ` Interpreter<Init> ` does.
231233
232234## Disadvantages
233235
@@ -236,7 +238,7 @@ transitions, an `InvalidState` enum value in an error type might be simpler.
236238
237239## Alternatives
238240
239- There are a number of simpler state machines, however, that have their own patterns:
241+ There are a number of simpler state machines that have their own patterns:
240242
2412431 . If the state transition is during construction/finalizing of an object, see
242244[ Builder Pattern] ( ../patterns/creational/builder.md ) .
0 commit comments