Skip to content

Commit 6999e89

Browse files
committed
Expand function call discussion a bit
1 parent ada9843 commit 6999e89

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

src/expressions.md

+40-25
Original file line numberDiff line numberDiff line change
@@ -547,60 +547,75 @@ let x: i32 = add(1i32, 2i32);
547547
let pi: Result<f32, _> = "3.14".parse();
548548
```
549549

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

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

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

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.
572+
To resolve the ambiguity, the programmer may refer to their desired method or
573+
function using more specific paths, types, or traits.
567574

568575
For example,
569576

570577
```rust
571-
trait Foo {
572-
fn quux();
578+
trait Pretty {
579+
fn print(&self);
573580
}
574581

575-
trait Bar {
576-
fn quux();
582+
trait Ugly {
583+
fn print(&self);
577584
}
578585

579-
struct Faz;
580-
impl Foo for Faz {
581-
fn quux() {}
586+
struct Foo;
587+
impl Pretty for Foo {
588+
fn print(&self) {}
582589
}
583590

584-
struct Baz;
585-
impl Foo for Baz {
586-
fn quux() {}
591+
struct Bar;
592+
impl Pretty for Bar {
593+
fn print(&self) {}
587594
}
588-
impl Bar for Baz {
589-
fn quux() {}
595+
impl Ugly for Bar{
596+
fn print(&self) {}
590597
}
591598

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

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

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

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

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

0 commit comments

Comments
 (0)