]> git.proxmox.com Git - rustc.git/blame - src/doc/book/nostarch/chapter19-new-function-pointer-text.md
New upstream version 1.36.0+dfsg1
[rustc.git] / src / doc / book / nostarch / chapter19-new-function-pointer-text.md
CommitLineData
532ac7d7
XL
1Please add this text at the end of the Function Pointers section, just before
2the Returning Closures section starts, so at the end of page 447 and before
3page 448.
4
5---
6
7We have another useful pattern that exploits an implementation detail of tuple
8structs and tuple-struct enum variants. These types use `()` as initializer
9syntax, which looks like a function call. The initializers are actually
10implemented as functions returning an instance that’s constructed from their
11arguments. We can use these initializer functions as function pointers that
12implement the closure traits, which means we can specify the initializer
13functions as arguments for methods that take closures, like so:
14
15```
16enum Status {
17 Value(u32),
18 Stop,
19}
20
21let list_of_statuses: Vec<Status> =
22 (0u32..20)
23 .map(Status::Value)
24 .collect();
25```
26
27Here we create `Status::Value` instances using each `u32` value in the range
28that `map` is called on by using the initializer function of `Status::Value`.
29Some people prefer this style, and some people prefer to use closures. They
30compile to the same code, so use whichever style is clearer to you.
31