]>
Commit | Line | Data |
---|---|---|
ea8adc8c XL |
1 | # Call expressions |
2 | ||
8faf50e0 XL |
3 | > **<sup>Syntax</sup>**\ |
4 | > _CallExpression_ :\ | |
5 | > [_Expression_] `(` _CallParams_<sup>?</sup> `)` | |
6 | > | |
7 | > _CallParams_ :\ | |
8 | > [_Expression_] ( `,` [_Expression_] )<sup>\*</sup> `,`<sup>?</sup> | |
ff7c6d11 | 9 | |
ea8adc8c XL |
10 | A _call expression_ consists of an expression followed by a parenthesized |
11 | expression-list. It invokes a function, providing zero or more input variables. | |
12 | If the function eventually returns, then the expression completes. For | |
416331ca | 13 | [non-function types](../types/function-item.md), the expression f(...) uses |
2c00a5a8 XL |
14 | the 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 | |
16 | reference, mutable reference, or take ownership respectively. An automatic | |
17 | borrow will be taken if needed. Rust will also automatically dereference `f` as | |
18 | required. Some examples of call expressions: | |
ea8adc8c XL |
19 | |
20 | ```rust | |
21 | # fn add(x: i32, y: i32) -> i32 { 0 } | |
22 | let three: i32 = add(1i32, 2i32); | |
23 | let name: &'static str = (|| "Rust")(); | |
24 | ``` | |
25 | ||
26 | ## Disambiguating Function Calls | |
27 | ||
f035d41b XL |
28 | Rust treats all function calls as sugar for a more explicit, [fully-qualified |
29 | syntax]. Upon compilation, Rust will desugar all function calls into the explicit | |
ea8adc8c XL |
30 | form. Rust may sometimes require you to qualify function calls with trait, |
31 | depending 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 | ||
39 | Several situations often occur which result in ambiguities about the receiver or | |
40 | referent 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 | |
48 | To resolve the ambiguity, the programmer may refer to their desired method or | |
49 | function using more specific paths, types, or traits. | |
50 | ||
51 | For example, | |
52 | ||
53 | ```rust | |
54 | trait Pretty { | |
55 | fn print(&self); | |
56 | } | |
57 | ||
58 | trait Ugly { | |
59 | fn print(&self); | |
60 | } | |
61 | ||
62 | struct Foo; | |
63 | impl Pretty for Foo { | |
64 | fn print(&self) {} | |
65 | } | |
66 | ||
67 | struct Bar; | |
68 | impl Pretty for Bar { | |
69 | fn print(&self) {} | |
70 | } | |
fc512014 | 71 | impl Ugly for Bar { |
ea8adc8c XL |
72 | fn print(&self) {} |
73 | } | |
74 | ||
75 | fn 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 | ||
94 | Refer 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 |