Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. /// Only needed to prevent lifetime elision
  2. struct Context {
  3. start: usize,
  4. end: usize,
  5. }
  6.  
  7. impl Context {
  8. fn new(start: usize, end: usize) -> Self {
  9. Self { start, end }
  10. }
  11. }
  12.  
  13. /// Some kind of genralized ArrayIter as you want.
  14. struct SliceWrapper<'a, T>(&'a [T]);
  15.  
  16. impl<'a, T> SliceWrapper<'a, T> {
  17. fn with_context(i: &'a [T], c: &Context) -> SliceWrapper<'a, T> {
  18. SliceWrapper( &i[c.start..c.end] )
  19. }
  20. }
  21.  
  22. fn main() {
  23. let mut array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  24. let c = Context::new(2, 4);
  25. let sw = SliceWrapper::with_context(&array, &c);
  26.  
  27. // this fails because array is still borrowed immutable by sw
  28. // array[4] = 15;
  29.  
  30. for number in sw.0.iter() {
  31. print!("{},", number);
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement