Hey all, I'm a hobby game developer and new to rust (I have only ever used it for Bevy). I took inspiration from the official Bevy examples to create a main menu using SpawnIter. However, I wanted extract the closures into their own functions. So I created create_button_bundle , which uses SpawnIter to iterate over a list of ButtonInfos and calls create_menu_bundle for each element.
However, this leads to the lifetime error below. The passed in ButtonInfo has shorter lifetime than static, but I don't see why it needs to have static lifetime. The information in each ButtonInfo is cloned into the Bundle, so how is the data escaping create_menu_bundle? The suggested fix from rustc --explain E0521 is to not type annotate my closures, but I am not using closures.
Any tips, either to fix this issue, or to restructure my code, would be appreciated.
--> src/main.rs:100:17
|
82 | button_infos: &[ButtonInfo],
| ------------ - let's call the lifetime of this reference `'1`
| |
| `button_infos` is a reference that is only valid in the function body
...
100 | / Children::spawn(SpawnIter(
101 | | button_infos.into_iter().map(create_button_bundle)
102 | | )),
| | ^
| | |
| |__________________`button_infos` escapes the function body here
| argument requires that `'1` must outlive `'static`
For more information about this error, try `rustc --explain E0521`.