SHARE
TWEET

Untitled

a guest Sep 16th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top