File tree Expand file tree Collapse file tree 2 files changed +19
-3
lines changed
trunk/src/librustc_borrowck Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Original file line number Diff line number Diff line change 1
1
---
2
- refs/heads/master: d83a0fdf7da8b93b21de6ff2809e81a69447a9a5
2
+ refs/heads/master: 78d28336feb9f46ac5929023f45acdbb3f3379e5
3
3
refs/heads/snap-stage3: 1af31d4974e33027a68126fa5a5a3c2c6491824f
4
4
refs/heads/try: b53c0f93eedcdedd4fd89bccc5a3a09d1c5cd23e
5
5
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
Original file line number Diff line number Diff line change @@ -125,12 +125,28 @@ The problem here is that foo is defined as accepting a parameter of type `Fn`.
125
125
Closures passed into foo will thus be inferred to be of type `Fn`, meaning that
126
126
they capture their context immutably.
127
127
128
- The solution is to capture the data mutably. This can be done by defining `foo`
129
- to take FnMut rather than Fn:
128
+ If the definition of `foo` is under your control, the simplest solution is to
129
+ capture the data mutably. This can be done by defining `foo` to take FnMut
130
+ rather than Fn:
130
131
131
132
```
132
133
fn foo<F: FnMut()>(f: F) { }
133
134
```
135
+
136
+ Alternatively, we can consider using the `Cell` and `RefCell` types to achieve
137
+ interior mutability through a shared reference. Our example's `mutable` function
138
+ could be redefined as below:
139
+
140
+ ```
141
+ fn mutable() {
142
+ let x = std::cell::Cell::new(0u32);
143
+ foo(|| x.set(2));
144
+ }
145
+ ```
146
+
147
+ You can read more about cell types in the API documentation:
148
+
149
+ https://doc.rust-lang.org/std/cell/
134
150
"##
135
151
136
152
}
You can’t perform that action at this time.
0 commit comments