Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- struct MyCollection(Vec<i32>);
- impl MyCollection {
- fn new() -> MyCollection {
- MyCollection(Vec::new())
- }
- fn add(&mut self, elem: i32) {
- self.0.push(elem);
- }
- fn my_filter(&self) -> impl Iterator<Item=&i32> {
- self.0.iter().filter(|&n| *n > 0)
- }
- fn my_map(&self) -> impl Iterator<Item=i32> {
- self.0.iter().map(|n| n * 2)
- }
- }
- fn main() {
- let mut c = MyCollection::new();
- c.add(0);
- c.add(1);
- c.add(2);
- c.add(3);
- c.add(4);
- c.add(5);
- for n in c.my_filter().map(|&n| n * 2) {
- println!("{}", n);
- }
- for n in c.my_map().filter(|&n| n > 0) {
- println!("{}", n);
- }
- }
Add Comment
Please, Sign In to add comment