Skip to content

Wildcard pattern in function parameters. #848

Open
@OnlyLys

Description

@OnlyLys

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions