Hacker News new | ask | show | jobs
by kragen 1056 days ago
ruby is closer to what i meant because you can't add methods to rust's iterator, can you? but people add stuff to enumerable all the time
1 comments

You can!

    trait MyIterHelpers: Iterator {
        fn dance(&self) {
            println!("wheee");
        }
    }
    
    // And tell rust that all Iterators are also MyIterHelpers.
    impl<I: Iterator> MyIterHelpers for I {}
The one caveat is that using it in a different context will need a use crate::MyIterHelpers; line, so the namespace isn't polluted.
neat, i didn't know that was possible