I have something which looks similar to this (simplified it)
struct MyStruct<'a> {
pub add_and_return_last_two: Box<dyn FnMut(&[u32]) -> &'a [u32] + 'a>,
}
impl<'a> MyStruct<'a> {
pub fn new() -> Self {
let mut vec = vec![0u32, 2u32];
Self {
add_and_return_last_two: Box::new(move |add| {
vec.extend_from_slice(add);
let len = vec.len();
&vec[len - 2..len]
}),
}
}
}
I get this error
captured variable cannot escape FnMut closure body FnMut closures only have access to their captured variables while they are executing... ...therefore, they cannot allow references to captured variables to escape
How can I expose a “part” of the vector which lives long as MyStruct
, feels unnecessary to wrap it in Box
since I’m ok with it dying with MyStruct