I have a function:
fn take_iter<
LABEL: std::fmt::Debug,
DATA: std::fmt::Debug,
SAMPLE: IntoIterator<Item = DATA>,
I: Iterator<Item = (SAMPLE, LABEL)>,
>(
iter: I,
) {
for (sample, label) in iter {
print!("sample {:.?}: ", label);
for val in sample {
print!("{:.?} ", val);
}
println!();
}
}
Example usage:
let a: Vec<(Vec<u8>, u16)> = vec![(vec![1, 2, 3, 4], 2), (vec![1, 2, 3, 4], 4)];
take_iter(a.into_iter());
Prints:
sample 2: 1 2 3 4
sample 4: 1 2 3 4
My goal is to adapt this function to take the values by reference rather than by value:
let a: Vec<(Vec<u8>, u16)> = vec![(vec![1, 2, 3, 4], 2), (vec![1, 2, 3, 4], 4)];
take_iter(a.iter());
My current attempt (Rust playground) is:
fn take_iter<
LABEL: std::fmt::Debug,
DATA: std::fmt::Debug,
SAMPLE: IntoIterator<Item = DATA>,
K: std::ops::Deref<Target = (SAMPLE, LABEL)>,
I: Iterator<Item = K>,
>(
iter: I,
) {
for t in iter {
let (sample, label) = &*t;
print!("sample {:.?}: ", label);
for val in sample.iter() {
print!("{:.?} ", val);
}
println!();
}
}
This gives the error:
error[E0599]: no method named `iter` found for reference `&SAMPLE` in the current scope
--> src/main.rs:30:27
|
30 | for val in sample.iter() {
| ^^^^ method not found in `&SAMPLE`
What am I doing wrong here?