For n arrays, you can implement it with Vec , as shown below:
use std::ops::Index; struct VecSlice<'a, T: 'a>(Vec<&'a [T]>); impl<'a, T> Index<usize> for VecSlice<'a, T> { type Output = T; fn index(&self, mut index: usize) -> &T { for slice in self.0.iter() { if index < slice.len() { return &slice[index]; } else { index -= slice.len(); } } panic!("out of bound"); } }
And then refer to it as an array, just stay in the box.
fn main() { let a1 = [0, 1, 2]; let a2 = [7, 8, 9]; let a = VecSlice(vec!(&a1, &a2)); println!("{}", a[4]); }
Will print
8
WiSaGaN
source share