Is IntoIterator a zero cost generic?

⚓ rust    📅 2025-06-14    👤 surdeus    👁️ 3      

surdeus

Hi folks,

I wonder if I pass Vec<String> into the following functions:

fn non_generic_fn(s: Vec<String>) -> Vec<String> {
    s
}
fn generic_fn<V, T>(s: V) -> Vec<String>
where
    V: IntoIterator<Item = String>,
{
    s.into_iter().collect()
}
fn even_more_generic_fn<V, T>(s: V) -> Vec<String>
where
    V: IntoIterator<Item = T>,
    T: Into<String>,
{
    s.into_iter().map(|s| s.into()).collect()
}

will I get any performance penalties for the generic versions? Or is the compiler clever enough to understand that I'm already passing Vec and therefore nothing to do?

Thank you.

4 posts - 3 participants

Read full topic

🏷️ rust_feed