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