reference icon indicating copy to clipboard operation
reference copied to clipboard

Wildcard pattern in function parameters.

Open OnlyLys opened this issue 5 years ago • 2 comments

I noticed that the section on wildcard patterns says that:

Unlike identifier patterns, it does not copy, move or borrow the value it matches.

However for a function parameter, any arguments that match the wildcard are still consumed by the function:

fn main() {
    let s = String::from("Hello");
    f(s);

    // Won't compile since `s` was moved. 
    println!("{}", s);
}

fn f(_: String) {}

One explanation I had in my head was that function arguments are moved into the function's "stack frame" first, before being bound to parameters. So even though _ does not bind to s, s will still be moved into the function's scope and thus be "consumed".

However, I wasn't able to find any documentation of argument passing in the reference.

I'm willing to do any write-ups to update the documentation if someone can point me at the right direction.

OnlyLys avatar Jul 16 '20 09:07 OnlyLys