How to iterate over `R: 'a + AsRef<[T]>`

I want to make a function that accepts anything that can be converted to a slice. Also, I could walk through this slice by doing for i in slice.len() but it's nice to iterate over it. Also, I don't want to consume the type, just walk thtough it.

pub fn s<'a, T, R: 'a + AsRef<[T]>>(elements: &R){
    for e in elements {
        
    }
}

fn main() {
    let b = vec![0; 3];
    s(&b);
}

Is there a way?

 pub fn s<'a, T, R: 'a + AsRef<[T]>>(elements: &R){
-    for e in elements {
+    for e in elements.as_ref() {
        

(Incidentally though, AsRef is usually so you can take ownership or not, so you would take an R. If you don't want to take ownership, I think just taking &[T] and letting your users call .as_ref() themselves is more common. Also your 'a is unused.)

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.