Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Only needed to prevent lifetime elision
- struct Context {
- start: usize,
- end: usize,
- }
- impl Context {
- fn new(start: usize, end: usize) -> Self {
- Self { start, end }
- }
- }
- /// Some kind of genralized ArrayIter as you want.
- struct SliceWrapper<'a, T>(&'a [T]);
- impl<'a, T> SliceWrapper<'a, T> {
- fn with_context(i: &'a [T], c: &Context) -> SliceWrapper<'a, T> {
- SliceWrapper( &i[c.start..c.end] )
- }
- }
- fn main() {
- let mut array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- let c = Context::new(2, 4);
- let sw = SliceWrapper::with_context(&array, &c);
- // this fails because array is still borrowed immutable by sw
- // array[4] = 15;
- for number in sw.0.iter() {
- print!("{},", number);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement