-
Notifications
You must be signed in to change notification settings - Fork 544
Open
Labels
A-patternsArea: PatternsArea: Patterns
Description
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
Labels
A-patternsArea: PatternsArea: Patterns
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
ChrisCho-H commentedon Aug 7, 2024
How's the status of this issue? If the wildcard pattern exists in function parameter with the type of data which requires allocation into heap memory, it's ownership is moved.
mattheww commentedon Aug 7, 2024
For what it's worth, the Reference's current description of how argument passing works is under "Function body" in
functions.md
: