]> git.proxmox.com Git - rustc.git/blame - src/doc/reference/src/expressions/call-expr.md
New upstream version 1.50.0+dfsg1
[rustc.git] / src / doc / reference / src / expressions / call-expr.md
CommitLineData
ea8adc8c
XL
1# Call expressions
2
8faf50e0
XL
3> **<sup>Syntax</sup>**\
4> _CallExpression_ :\
5> &nbsp;&nbsp; [_Expression_] `(` _CallParams_<sup>?</sup> `)`
6>
7> _CallParams_ :\
8> &nbsp;&nbsp; [_Expression_]&nbsp;( `,` [_Expression_] )<sup>\*</sup> `,`<sup>?</sup>
ff7c6d11 9
ea8adc8c
XL
10A _call expression_ consists of an expression followed by a parenthesized
11expression-list. It invokes a function, providing zero or more input variables.
12If the function eventually returns, then the expression completes. For
416331ca 13[non-function types](../types/function-item.md), the expression f(...) uses
2c00a5a8
XL
14the method on one of the [`std::ops::Fn`], [`std::ops::FnMut`] or
15[`std::ops::FnOnce`] traits, which differ in whether they take the type by
16reference, mutable reference, or take ownership respectively. An automatic
17borrow will be taken if needed. Rust will also automatically dereference `f` as
18required. Some examples of call expressions:
ea8adc8c
XL
19
20```rust
21# fn add(x: i32, y: i32) -> i32 { 0 }
22let three: i32 = add(1i32, 2i32);
23let name: &'static str = (|| "Rust")();
24```
25
26## Disambiguating Function Calls
27
f035d41b
XL
28Rust treats all function calls as sugar for a more explicit, [fully-qualified
29syntax]. Upon compilation, Rust will desugar all function calls into the explicit
ea8adc8c
XL
30form. Rust may sometimes require you to qualify function calls with trait,
31depending on the ambiguity of a call in light of in-scope items.
32
33> **Note**: In the past, the Rust community used the terms "Unambiguous
34> Function Call Syntax", "Universal Function Call Syntax", or "UFCS", in
35> documentation, issues, RFCs, and other community writings. However, the term
36> lacks descriptive power and potentially confuses the issue at hand. We mention
37> it here for searchability's sake.
38
39Several situations often occur which result in ambiguities about the receiver or
40referent of method or associated function calls. These situations may include:
41
42* Multiple in-scope traits define methods with the same name for the same types
43* Auto-`deref` is undesirable; for example, distinguishing between methods on a
44 smart pointer itself and the pointer's referent
f035d41b
XL
45* Methods which take no arguments, like [`default()`], and return properties of a
46 type, like [`size_of()`]
ea8adc8c
XL
47
48To resolve the ambiguity, the programmer may refer to their desired method or
49function using more specific paths, types, or traits.
50
51For example,
52
53```rust
54trait Pretty {
55 fn print(&self);
56}
57
58trait Ugly {
59 fn print(&self);
60}
61
62struct Foo;
63impl Pretty for Foo {
64 fn print(&self) {}
65}
66
67struct Bar;
68impl Pretty for Bar {
69 fn print(&self) {}
70}
fc512014 71impl Ugly for Bar {
ea8adc8c
XL
72 fn print(&self) {}
73}
74
75fn main() {
76 let f = Foo;
77 let b = Bar;
78
79 // we can do this because we only have one item called `print` for `Foo`s
80 f.print();
81 // more explicit, and, in the case of `Foo`, not necessary
82 Foo::print(&f);
83 // if you're not into the whole brevity thing
84 <Foo as Pretty>::print(&f);
85
86 // b.print(); // Error: multiple 'print' found
87 // Bar::print(&b); // Still an error: multiple `print` found
88
89 // necessary because of in-scope items defining `print`
90 <Bar as Pretty>::print(&b);
91}
92```
93
94Refer to [RFC 132] for further details and motivations.
95
96[RFC 132]: https://github.com/rust-lang/rfcs/blob/master/text/0132-ufcs.md
416331ca 97[_Expression_]: ../expressions.md
f035d41b
XL
98[`default()`]: ../../std/default/trait.Default.html#tymethod.default
99[`size_of()`]: ../../std/mem/fn.size_of.html
100[`std::ops::FnMut`]: ../../std/ops/trait.FnMut.html
101[`std::ops::FnOnce`]: ../../std/ops/trait.FnOnce.html
102[`std::ops::Fn`]: ../../std/ops/trait.Fn.html
103[fully-qualified syntax]: ../paths.md#qualified-paths