Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. struct Container {
  2. elems: Vec<u8>,
  3. }
  4.  
  5. impl Container {
  6. // Since we're just indexing, it would be nice to re-use CIter here
  7. fn iter_mut<'a>(&'a mut self) -> CIter<'a> {
  8. CIter {
  9. reference: self,
  10. position: 0,
  11. }
  12. }
  13. }
  14.  
  15. struct CIter<'a> {
  16. reference: &'a mut Container,
  17. position: usize,
  18. }
  19.  
  20. /*impl<'a> Iterator for CIter<'a> {
  21. type Item = &'a mut u8;
  22.  
  23. fn next(&'a mut self) -> Option<&'a mut u8> {
  24. let item = self.reference.elems.get_mut(self.position);
  25. self.position += 1;
  26. item
  27. }
  28. }*/
  29.  
  30. impl<'a> CIter<'a> {
  31. fn next(&'a mut self) -> Option<&'a mut u8> {
  32. let item = self.reference.elems.get_mut(self.position);
  33. self.position += 1;
  34. item
  35. }
  36. }
  37.  
  38. fn main() {
  39. for elem in (Container { elems: vec![0, 1, 2, 3, 4, 5] }).iter_mut().next() {
  40. println!("{}", elem);
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement