Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::iter::Iterator;
- trait LineIter<'a> {
- type Iter: Iterator<Item = &'a mut String>;
- fn line_iter(&'a mut self) -> Self::Iter;
- }
- struct Page<'a> {
- lines: &'a mut Vec<String>,
- }
- impl<'a, 'b> LineIter<'a> for Page<'b>
- where
- 'a: 'b,
- {
- type Iter = std::slice::IterMut<'a, String>;
- fn line_iter(&'a mut self) -> Self::Iter {
- self.lines.iter_mut()
- }
- }
- fn main() {
- let mut lines = Vec::new();
- lines.push("first line".to_string());
- lines.push("second line".to_string());
- lines.push("third line".to_string());
- let mut page = Page { lines: &mut lines };
- for line in page.line_iter() {
- line.push('!');
- }
- for line in page.line_iter() {
- println!("{}", line);
- }
- }
Add Comment
Please, Sign In to add comment