Guest User

Untitled

a guest
Jun 25th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. use std::iter::Iterator;
  2.  
  3. trait LineIter<'a> {
  4. type Iter: Iterator<Item = &'a mut String>;
  5.  
  6. fn line_iter(&'a mut self) -> Self::Iter;
  7. }
  8.  
  9. struct Page<'a> {
  10. lines: &'a mut Vec<String>,
  11. }
  12.  
  13. impl<'a, 'b> LineIter<'a> for Page<'b>
  14. where
  15. 'a: 'b,
  16. {
  17. type Iter = std::slice::IterMut<'a, String>;
  18.  
  19. fn line_iter(&'a mut self) -> Self::Iter {
  20. self.lines.iter_mut()
  21. }
  22. }
  23.  
  24. fn main() {
  25. let mut lines = Vec::new();
  26. lines.push("first line".to_string());
  27. lines.push("second line".to_string());
  28. lines.push("third line".to_string());
  29.  
  30. let mut page = Page { lines: &mut lines };
  31.  
  32. for line in page.line_iter() {
  33. line.push('!');
  34. }
  35.  
  36. for line in page.line_iter() {
  37. println!("{}", line);
  38. }
  39. }
Add Comment
Please, Sign In to add comment