Skip to content

Commit 52b2635

Browse files
committed
Expand function call discussion a bit
1 parent ada9843 commit 52b2635

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

src/expressions.md

+43-27
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,9 @@ exact `self`-type of the left-hand-side is known, or dynamically dispatching if
220220
the left-hand-side expression is an indirect [trait
221221
object](types.html#trait-objects).
222222

223-
[UFCS](#unambiguous-function-call-syntax) allows method invocation using
224-
function-like syntax.
223+
The compiler sometimes cannot infer to which function or method a given call
224+
refers. These cases require a [more specific syntax.](#disambiguating-function-calls)
225+
for method and function invocation.
225226

226227
## Field expressions
227228

@@ -547,60 +548,75 @@ let x: i32 = add(1i32, 2i32);
547548
let pi: Result<f32, _> = "3.14".parse();
548549
```
549550

550-
Function calls may sometimes result in ambiguities about receiver or referent.
551-
See [UFCS](#unambiguous-function-call-syntax) for how to resolve this.
551+
### Disambiguating Function Calls
552552

553-
### Unambiguous Function Call Syntax (UFCS)
553+
Rust treats all function calls as sugar for a more explicit, fully-qualified
554+
syntax. Upon compilation, Rust will desugar all function calls into the explicit
555+
form. Rust may sometimes require you to qualify function calls with trait,
556+
depending on the ambiguity of a call in light of in-scope items.
557+
558+
> **Note**: In the past, the Rust community used the terms "Unambiguous
559+
> Function Call Syntax", "Universal Function Call Syntax", or "UFCS", in
560+
> documentation, issues, RFCs, and other community writings. However, the term
561+
> lacks descriptive power and potentially confuses the issue at hand. We mention
562+
> it here for searchability's sake.
554563
555564
Several situations often occur which result in ambiguities about the receiver or
556565
referent of method or associated function calls. These situations may include:
557566

558-
* Multiple in-scope traits define the same method for the same types
567+
* Multiple in-scope traits define methods with the same name for the same types
559568
* Auto-`deref` is undesirable; for example, distinguishing between methods on a
560569
smart pointer itself and the pointer's referent
561570
* Methods which take no arguments, like `default()`, and return properties of a
562571
type, like `size_of()`
563572

564-
The programmer may unambiguously refer to their desired method or function using
565-
Unambiguous Function Call Syntax (UFCS), specifying only as much type and path
566-
information as necessary.
573+
To resolve the ambiguity, the programmer may refer to their desired method or
574+
function using more specific paths, types, or traits.
567575

568576
For example,
569577

570578
```rust
571-
trait Foo {
572-
fn quux();
579+
trait Pretty {
580+
fn print(&self);
573581
}
574582

575-
trait Bar {
576-
fn quux();
583+
trait Ugly {
584+
fn print(&self);
577585
}
578586

579-
struct Faz;
580-
impl Foo for Faz {
581-
fn quux() {}
587+
struct Foo;
588+
impl Pretty for Foo {
589+
fn print(&self) {}
582590
}
583591

584-
struct Baz;
585-
impl Foo for Baz {
586-
fn quux() {}
592+
struct Bar;
593+
impl Pretty for Bar {
594+
fn print(&self) {}
587595
}
588-
impl Bar for Baz {
589-
fn quux() {}
596+
impl Ugly for Bar{
597+
fn print(&self) {}
590598
}
591599

592600
fn main() {
593-
// shorthand, only if unambiguous
594-
Faz::quux();
601+
let f = Foo;
602+
let b = Bar;
603+
604+
// we can do this because we only have one item called `print` for `Foo`s
605+
f.print();
606+
// more explicit, and, in the case of `Foo`, not necessary
607+
Foo::print(&f);
608+
// if you're not into the whole brevity thing
609+
<Foo as Pretty>::print(&f);
595610

596-
// Baz::quux(); // Error: multiple `quux` found
611+
// b.print(); // Error: multiple 'print' found
612+
// Bar::print(&b); // Still an error: multiple `print` found
597613

598-
// completely unambiguous; works if multiple in-scope traits define `quux`
599-
<Baz as Foo>::quux();
614+
// necessary because of in-scope items defining `print`
615+
<Bar as Pretty>::print(&b);
600616
}
601617
```
602618

603-
Refer to [RFC 132] for further details, motivations, and subtleties of syntax.
619+
Refer to [RFC 132] for further details and motivations.
604620

605621
[RFC 132]: https://github.com/rust-lang/rfcs/blob/master/text/0132-ufcs.md
606622

0 commit comments

Comments
 (0)