Why `Vec` is allowed in function signatures

⚓ Rust    📅 2025-10-27    👤 surdeus    👁️ 5      

surdeus

I know why Vec<impl Fn> is not allowed in let bindings because the size of impl Fn is unknown at compile time - the compiler needs to know how much memory is required to be allocated for this array.
But why is it allowed in function signatures?

E.g.

fn run_funs(funs: Vec<impl Fn()>) {
    for f in funs {
        f();
    }
}

fn f1() {
    println!("in f1()");
}

fn f2() {
    println!("in f2()");
}

fn main() {
    run_funs(vec![f1, f2]);
}

(play ground)

Does this vec become a heterogeneous array but somehow doesn't require its items having a uniformed structure, like a pointer?

4 posts - 4 participants

Read full topic

🏷️ Rust_feed