Rust提取代码至独立函数的生命周期问题求助
Rust生命周期问题:提取函数后借用检查器报错
我正尝试将一段原本可正常编译的代码拆分为独立函数,但遇到了*借用检查器(borrowck)*的生命周期相关错误。原代码在单一函数中能正常运行,拆分后却无法通过编译。
trait Item<'s: 'i, 'i>: Sized + 'i { type Slice: 's; fn get<'a: 'i>(slice: &'a mut Self::Slice) -> Self where 's: 'a; } impl<'s: 'i, 'i> Item<'s, 'i> for &'i mut u32 { type Slice = &'s mut [u32]; fn get<'a: 'i>(slice: &'a mut Self::Slice) -> Self where 's: 'a { &mut slice[0] } } // 这段代码无法运行(已尝试HRTB但仍无法正常编译) /* fn extracted<'a: 'i, 's: 'i, 'i, I: Item<'s, 'i>>(slice: &'a mut I::Slice) { for x in 0..4 { let func = I::get(slice); } } */ fn main() { let mut array = [0, 0, 0, 0u32]; let mut slice = array.as_mut_slice(); let mutable = &mut slice; for x in 0..10 { let value = <&mut u32>::get(mutable); } }
我已测试过相关建议,确认问题与Copy trait无关。
内容的提问来源于stack exchange,提问作者Jed037
相关产品推荐
相关产品推荐

