Wildcard pattern in function parameters.

Open
#848 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
documentation

Research direction

Start with the linked wildcard patterns section and investigate the Reference's treatment of function argument passing, which the issue says is currently undocumented. Done means clarifying why a wildcard function parameter still consumes its argument and adding a focused documentation update or write-up.

Written by the indexing model from the issue text.

Description

A-patterns

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.

Dominant language
Rust
Stars
1.6k
Forks
607
Avg merge
1d 1h
Merged PRs (30d)
12

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rust-lang/reference

All issues in rust-lang/reference

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.