diff options
| author | Alisdair Owens <awo101@zepler.net> | 2015-08-10 16:48:01 +0100 |
|---|---|---|
| committer | Alisdair Owens <awo101@zepler.net> | 2015-08-10 16:48:53 +0100 |
| commit | 78d28336feb9f46ac5929023f45acdbb3f3379e5 (patch) | |
| tree | 81417a20afe8b8fa4d0128bcf332f7fbcb4601fc | |
| parent | d83a0fdf7da8b93b21de6ff2809e81a69447a9a5 (diff) | |
add info about cell types to diagnostic message E0387
| -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/ "## } |
