Created
March 3, 2021 01:53
-
-
Save hanslovsky/dc343b215e316311b898593d7a99fe56 to your computer and use it in GitHub Desktop.
Figuring how to pass iterators for custom traits as function parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Blub { | |
fn blub(&self) -> usize; | |
} | |
impl Blub for usize { | |
fn blub(&self) -> usize { | |
*self | |
} | |
} | |
fn some_fn<'a, I: Iterator<Item = &'a usize>>(iterator: I) { | |
for (idx, el) in iterator.enumerate() { | |
println!("{}: {}", idx, el); | |
} | |
} | |
fn some_fn2<'a, B: 'a + Blub, I: Iterator<Item = &'a B>>(iterator: I) { | |
for (idx, el) in iterator.enumerate() { | |
println!("{}: {}", idx, el.blub()); | |
} | |
} | |
fn main() { | |
println!("Hello, world!"); | |
let v: Vec<usize> = (0..3).collect(); | |
let a = [7, 7, 8]; | |
println!("func1"); | |
some_fn(v.iter()); | |
some_fn(a.iter()); | |
some_fn(v[..].iter()); | |
some_fn(a[..].iter()); | |
println!("func2"); | |
some_fn2(v.iter()); | |
some_fn2(a.iter()); | |
some_fn2(v[..].iter()); | |
some_fn2(a[..].iter()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment