@@ -220,6 +220,10 @@ exact `self`-type of the left-hand-side is known, or dynamically dispatching if
220
220
the left-hand-side expression is an indirect [ trait
221
221
object] ( types.html#trait-objects ) .
222
222
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.
226
+
223
227
## Field expressions
224
228
225
229
A _ field expression_ consists of an expression followed by a single dot and an
@@ -544,6 +548,78 @@ let x: i32 = add(1i32, 2i32);
544
548
let pi : Result <f32 , _ > = " 3.14" . parse ();
545
549
```
546
550
551
+ ### Disambiguating Function Calls
552
+
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.
563
+
564
+ Several situations often occur which result in ambiguities about the receiver or
565
+ referent of method or associated function calls. These situations may include:
566
+
567
+ * Multiple in-scope traits define methods with the same name for the same types
568
+ * Auto-` deref ` is undesirable; for example, distinguishing between methods on a
569
+ smart pointer itself and the pointer's referent
570
+ * Methods which take no arguments, like ` default() ` , and return properties of a
571
+ type, like ` size_of() `
572
+
573
+ To resolve the ambiguity, the programmer may refer to their desired method or
574
+ function using more specific paths, types, or traits.
575
+
576
+ For example,
577
+
578
+ ``` rust
579
+ trait Pretty {
580
+ fn print (& self );
581
+ }
582
+
583
+ trait Ugly {
584
+ fn print (& self );
585
+ }
586
+
587
+ struct Foo ;
588
+ impl Pretty for Foo {
589
+ fn print (& self ) {}
590
+ }
591
+
592
+ struct Bar ;
593
+ impl Pretty for Bar {
594
+ fn print (& self ) {}
595
+ }
596
+ impl Ugly for Bar {
597
+ fn print (& self ) {}
598
+ }
599
+
600
+ fn main () {
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 );
610
+
611
+ // b.print(); // Error: multiple 'print' found
612
+ // Bar::print(&b); // Still an error: multiple `print` found
613
+
614
+ // necessary because of in-scope items defining `print`
615
+ <Bar as Pretty >:: print (& b );
616
+ }
617
+ ```
618
+
619
+ Refer to [ RFC 132] for further details and motivations.
620
+
621
+ [ RFC 132 ] : https://github.com/rust-lang/rfcs/blob/master/text/0132-ufcs.md
622
+
547
623
## Lambda expressions
548
624
549
625
A _ lambda expression_ (sometimes called an "anonymous function expression")
0 commit comments