{"record":{"id":"ffd0508542be0ee7","repo":"iced-rs/iced","slug":"the-subscription-closure-provided-is-not-non-captu","errorCode":null,"errorMessage":"The Subscription closure provided is not non-capturing. Closures given to Subscription::map or filter_map cannot capture external variables. If you need to capture state, consider using Subscription::with.","messagePattern":"The Subscription closure provided is not non-capturing\\. Closures given to Subscription::map or filter_map cannot capture external variables\\. If you need to capture state, consider using Subscription::with\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"futures/src/subscription.rs","lineNumber":510,"sourceCode":"    I: Hash + 'static,\n    F: FnOnce(&I, EventStream) -> S,\n    S: Stream<Item = T> + MaybeSend + 'static,\n{\n    type Output = T;\n\n    fn hash(&self, state: &mut Hasher) {\n        std::any::TypeId::of::<I>().hash(state);\n        self.data.hash(state);\n    }\n\n    fn stream(self: Box<Self>, input: EventStream) -> BoxStream<Self::Output> {\n        crate::boxed_stream((self.spawn)(&self.data, input))\n    }\n}\n\nconst fn check_zero_sized<T>() {\n    if std::mem::size_of::<T>() != 0 {\n        panic!(\n            \"The Subscription closure provided is not non-capturing. \\\n            Closures given to Subscription::map or filter_map cannot \\\n            capture external variables. If you need to capture state, \\\n            consider using Subscription::with.\"\n        );\n    }\n}\n","sourceCodeStart":492,"sourceCodeEnd":518,"githubUrl":"https://github.com/iced-rs/iced/blob/d146509d893945de5c304cfaf4301335eee278bd/futures/src/subscription.rs#L492-L518","documentation":"This compile-time (const-evaluated) check panics when a closure passed to `Subscription::map` or `filter_map` captures environment variables, making it non-zero-sized. Non-capturing closures are required so subscriptions can be compared for equality across `view` calls; capturing closures would make subscription identity unstable. The message points to `Subscription::with` for stateful cases.","triggerScenarios":"`subscription.map(|m| ...)` where the closure body references a variable from the enclosing scope (e.g. an id, a channel sender, config value), or `filter_map` with a capturing predicate. The panic surfaces at runtime when the closure type is instantiated through the const fn.","commonSituations":"Forwarding a `ModuleId`/entity id into a mapped subscription; capturing a `Sender` to route messages; migrating code that previously built closures capturing state after upgrading to an iced version that enforces non-capturing closures.","solutions":["Move captured data into the subscription itself using `Subscription::with` (or the recipe/`batch` APIs) instead of the closure.","Make the closure truly non-capturing: pass needed data through the subscription message payload and remap in `update`.","Recreate the needed value inside the closure (e.g. recompute a constant) so nothing is captured.","Restructure with `Subscription::run` + a stateful recipe when long-lived state is required."],"exampleFix":"// before\nlet id = self.id;\nsubscription.map(move |event| { /* uses id */ })\n// after\nSubscription::with(id, move |id, events| {\n    events.map(move |event| { /* id available here */ })\n})","handlingStrategy":"validation","validationCode":"// Assert the closure is zero-sized (non-capturing) before use:\nfn assert_non_capturing<F>() {\n    assert_eq!(std::mem::size_of::<F>(), 0, \"closure captures variables\");\n}","typeGuard":"fn is_non_capturing<F>(_: &F) -> bool {\n    std::mem::size_of::<F>() == 0\n}","tryCatchPattern":"// Panic occurs at subscription construction; wrap in catch_unwind during development\nguarded_sub = std::panic::catch_unwind(|| subscription.map(no_capture_fn));","preventionTips":["Never reference outer variables inside `Subscription::map`/`filter_map` closures.","Use `Subscription::with` whenever data must flow into a subscription.","Keep subscription mapping functions as free functions or fn items where possible.","Run subscription-heavy screens in debug builds to catch the const panic early."],"tags":["rust","closures","subscriptions","iced","runtime-panic"],"backgroundTag":"invalid-argument-value","analyzedSha":"d146509d893945de5c304cfaf4301335eee278bd","analyzedAt":"2026-09-11T16:54:14.229Z","contentChangedAt":"2026-09-11T16:54:14.229Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}