Stuart Cook
ea4e037dd7
Rollup merge of #133149 - estebank:niko-rustnation, r=wesleywiser
...
Provide more context on `Fn` closure modifying binding
When modifying a binding from inside of an `Fn` closure, point at the binding definition and suggest using an `std::sync` type that would allow the code to compile.
```
error[E0594]: cannot assign to `counter`, as it is a captured variable in a `Fn` closure
--> f703.rs:6:9
|
4 | let mut counter = 0;
| ----------- `counter` declared here, outside the closure
5 | let x: Box<dyn Fn()> = Box::new(|| {
| -- in this closure
6 | counter += 1;
| ^^^^^^^^^^^^ cannot assign
|
= help: consider using `std::sync::atomic::AtomicI32` instead, which allows for multiple threads to access and modify the value
```
This provides more context on where the binding being modified was declared, and more importantly, guides newcomers towards `std::sync` when encountering cases like these.
When the requirement comes from an argument of a local function, we already tell the user that they might want to change from `Fn` to `FnMut`:
```
error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure
--> $DIR/borrow-immutable-upvar-mutation.rs:21:27
|
LL | fn to_fn<A: std::marker::Tuple, F: Fn<A>>(f: F) -> F {
| - change this to accept `FnMut` instead of `Fn`
...
LL | let mut x = 0;
| ----- `x` declared here, outside the closure
LL | let _f = to_fn(|| x = 42);
| ----- -- ^^^^^^ cannot assign
| | |
| | in this closure
| expects `Fn` instead of `FnMut`
|
= help: consider using `std::sync::atomic::AtomicI32` instead, which allows for multiple threads to access and modify the value
```
We might want to avoid the `help` in this case.
_Inspired by a [part of Niko's keynote at RustNation UK 2024](https://youtu.be/04gTQmLETFI?si=dgJL2OJRtuShkxdD&t=600 )._
2025-11-04 13:44:47 +11:00
..
2025-09-28 20:55:35 +00:00
2025-08-15 18:52:12 +00:00
2025-10-15 15:47:44 +00:00
2025-04-22 12:07:53 +02:00
2025-07-30 04:07:19 +00:00
2025-10-31 20:45:33 +00:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2025-04-26 14:48:30 +09:00
2025-08-19 21:27:10 +02:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2025-04-08 23:06:31 +03:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2024-04-11 16:41:41 +00:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2025-02-11 19:24:07 +00:00
2025-02-11 19:24:07 +00:00
2025-02-11 19:24:07 +00:00
2023-01-11 09:32:08 +00:00
2024-07-26 14:41:56 -04:00
2023-01-11 09:32:08 +00:00
2024-04-24 22:21:13 +00:00
2023-01-11 09:32:08 +00:00
2025-11-03 20:26:18 +00:00
2024-02-16 20:02:50 +00:00
2023-10-05 01:04:41 +00:00
2024-04-12 04:46:31 +00:00
2024-04-12 04:46:31 +00:00
2024-04-12 04:46:31 +00:00
2023-01-11 09:32:08 +00:00
2023-01-15 19:46:20 +00:00
2023-01-11 09:32:08 +00:00
2024-04-12 04:46:31 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2024-02-08 08:10:43 +00:00
2024-02-08 08:10:43 +00:00
2024-04-24 08:05:29 +00:00
2024-04-24 08:05:29 +00:00
2024-07-04 05:36:34 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-01-15 19:46:20 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-01-15 19:46:20 +00:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2024-04-24 08:05:29 +00:00
2025-01-31 11:04:50 +00:00
2025-01-31 11:04:50 +00:00
2025-01-31 11:04:50 +00:00
2025-01-31 11:04:50 +00:00
2023-08-06 21:20:15 +02:00
2023-08-06 21:20:15 +02:00
2024-03-23 16:14:42 +01:00
2025-10-13 15:26:22 +01:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2025-07-25 18:34:10 +00:00
2023-01-11 09:32:08 +00:00
2024-04-24 22:21:15 +00:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2024-07-26 14:41:56 -04:00
2024-07-26 14:41:56 -04:00
2024-07-26 14:41:56 -04:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2024-07-26 14:41:56 -04:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2023-05-08 17:58:01 -03:00
2023-11-24 19:15:52 +01:00
2023-05-08 17:58:01 -03:00
2023-11-24 19:15:52 +01:00
2023-05-08 17:58:01 -03:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2024-04-07 17:38:07 -03:00
2024-04-07 17:38:07 -03:00
2024-04-07 17:38:07 -03:00
2024-04-07 17:38:07 -03:00
2024-04-07 17:38:07 -03:00
2024-04-07 17:38:07 -03:00
2023-01-11 09:32:08 +00:00
2024-04-24 22:21:13 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2024-07-04 05:36:34 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2025-08-08 15:15:09 +00:00
2025-08-08 15:15:09 +00:00
2025-08-08 15:15:09 +00:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2024-04-24 08:05:29 +00:00
2025-08-19 21:27:10 +02:00
2025-04-08 23:06:31 +03:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2025-07-16 12:35:44 +00:00
2025-07-16 12:35:44 +00:00
2025-10-31 19:23:33 +00:00
2025-10-31 19:23:33 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2025-08-06 23:59:47 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2024-07-26 14:41:56 -04:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2024-04-11 16:41:41 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-06-18 04:14:43 +08:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2025-08-14 12:50:07 -04:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-03-10 21:18:41 -04:00
2024-02-16 20:02:50 +00:00
2025-06-04 10:40:04 -04:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-03-20 13:00:34 -04:00
2024-03-20 13:00:34 -04:00
2023-05-08 17:58:01 -03:00
2023-05-08 17:58:01 -03:00
2023-05-08 17:58:01 -03:00
2023-12-15 06:59:42 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-11-28 14:32:45 -08:00
2025-06-04 10:40:04 -04:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-01-15 19:46:20 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2025-04-08 23:06:31 +03:00
2025-01-06 16:12:11 -08:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-05-03 15:56:59 +02:00
2024-05-03 15:56:59 +02:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2024-04-24 08:05:29 +00:00
2024-07-21 22:32:29 -04:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2024-04-21 15:43:43 -03:00
2024-04-21 15:43:43 -03:00
2024-04-21 20:10:12 -04:00
2025-05-24 23:31:07 +02:00
2024-04-21 20:10:12 -04:00
2025-05-24 23:31:07 +02:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-05-05 22:40:05 +12:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2025-01-06 16:08:29 -08:00
2023-01-11 09:32:08 +00:00
2025-01-06 16:08:29 -08:00
2024-02-16 20:02:50 +00:00
2025-01-06 16:08:29 -08:00
2025-01-06 16:08:29 -08:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-09-13 14:10:56 +03:00
2024-09-13 14:10:56 +03:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2024-08-17 12:43:25 -04:00
2023-02-16 11:42:35 -03:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2025-01-06 16:08:29 -08:00
2025-01-06 16:08:29 -08:00
2025-03-06 12:53:35 +01:00
2025-03-06 12:53:35 +01:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2024-02-16 20:02:50 +00:00
2025-01-06 16:12:11 -08:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-07-10 20:09:26 +00:00
2023-01-11 09:32:08 +00:00
2025-09-28 20:55:35 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2024-04-03 21:02:47 +02:00
2024-04-03 21:02:47 +02:00
2024-04-03 23:16:27 +02:00
2024-04-10 20:36:14 +00:00
2024-07-26 14:41:56 -04:00
2024-07-26 14:41:56 -04:00
2023-01-11 09:32:08 +00:00
2023-01-15 19:46:20 +00:00
2023-01-11 09:32:08 +00:00
2024-04-15 21:36:52 -04:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-03-14 17:19:40 +01:00
2025-08-22 13:38:22 +02:00
2023-01-11 09:32:08 +00:00
2024-11-25 20:29:04 -08:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2025-05-02 18:45:28 +00:00
2025-05-02 18:45:28 +00:00
2024-02-14 11:00:30 +00:00
2025-02-15 12:18:30 +08:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2025-01-06 16:08:29 -08:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2024-12-12 23:36:27 +00:00
2025-05-27 22:19:56 +08:00
2025-05-27 22:19:56 +08:00
2025-05-27 22:19:56 +08:00
2023-01-11 09:32:08 +00:00
2023-12-07 23:00:46 -05:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2023-01-11 09:32:08 +00:00
2025-01-06 16:12:11 -08:00
2023-01-11 09:32:08 +00:00
2025-08-14 17:43:39 +02:00
2024-03-24 10:05:27 +01:00
2024-02-16 20:02:50 +00:00
2024-02-16 20:02:50 +00:00
2023-11-24 19:15:52 +01:00
2024-02-16 20:02:50 +00:00
2023-01-11 09:32:08 +00:00
2023-11-24 19:15:52 +01:00
2023-01-11 09:32:08 +00:00
2025-01-06 16:08:29 -08:00