diff options
| -rw-r--r-- | src/librustc_borrowck/diagnostics.rs | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/librustc_borrowck/diagnostics.rs b/src/librustc_borrowck/diagnostics.rs index 79abd08bcb1..b641dfd7d40 100644 --- a/src/librustc_borrowck/diagnostics.rs +++ b/src/librustc_borrowck/diagnostics.rs @@ -125,12 +125,28 @@ The problem here is that foo is defined as accepting a parameter of type `Fn`. Closures passed into foo will thus be inferred to be of type `Fn`, meaning that they capture their context immutably. -The solution is to capture the data mutably. This can be done by defining `foo` -to take FnMut rather than Fn: +If the definition of `foo` is under your control, the simplest solution is to +capture the data mutably. This can be done by defining `foo` to take FnMut +rather than Fn: ``` fn foo<F: FnMut()>(f: F) { } ``` + +Alternatively, we can consider using the `Cell` and `RefCell` types to achieve +interior mutability through a shared reference. Our example's `mutable` function +could be redefined as below: + +``` +fn mutable() { + let x = std::cell::Cell::new(0u32); + foo(|| x.set(2)); +} +``` + +You can read more about cell types in the API documentation: + +https://doc.rust-lang.org/std/cell/ "## } |
