Skip to content

Commit 78d2833

Browse files
committed
add info about cell types to diagnostic message E0387
1 parent d83a0fd commit 78d2833

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/librustc_borrowck/diagnostics.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,28 @@ The problem here is that foo is defined as accepting a parameter of type `Fn`.
125125
Closures passed into foo will thus be inferred to be of type `Fn`, meaning that
126126
they capture their context immutably.
127127
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:
130131
131132
```
132133
fn foo<F: FnMut()>(f: F) { }
133134
```
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/
134150
"##
135151

136152
}

0 commit comments

Comments
 (0)