@@ -220,8 +220,9 @@ 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
- [ 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.
225
226
226
227
## Field expressions
227
228
@@ -547,60 +548,75 @@ let x: i32 = add(1i32, 2i32);
547
548
let pi : Result <f32 , _ > = " 3.14" . parse ();
548
549
```
549
550
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
552
552
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.
554
563
555
564
Several situations often occur which result in ambiguities about the receiver or
556
565
referent of method or associated function calls. These situations may include:
557
566
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
559
568
* Auto-` deref ` is undesirable; for example, distinguishing between methods on a
560
569
smart pointer itself and the pointer's referent
561
570
* Methods which take no arguments, like ` default() ` , and return properties of a
562
571
type, like ` size_of() `
563
572
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.
567
575
568
576
For example,
569
577
570
578
``` rust
571
- trait Foo {
572
- fn quux ( );
579
+ trait Pretty {
580
+ fn print ( & self );
573
581
}
574
582
575
- trait Bar {
576
- fn quux ( );
583
+ trait Ugly {
584
+ fn print ( & self );
577
585
}
578
586
579
- struct Faz ;
580
- impl Foo for Faz {
581
- fn quux ( ) {}
587
+ struct Foo ;
588
+ impl Pretty for Foo {
589
+ fn print ( & self ) {}
582
590
}
583
591
584
- struct Baz ;
585
- impl Foo for Baz {
586
- fn quux ( ) {}
592
+ struct Bar ;
593
+ impl Pretty for Bar {
594
+ fn print ( & self ) {}
587
595
}
588
- impl Bar for Baz {
589
- fn quux ( ) {}
596
+ impl Ugly for Bar {
597
+ fn print ( & self ) {}
590
598
}
591
599
592
600
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 );
595
610
596
- // Baz::quux(); // Error: multiple `quux` found
611
+ // b.print(); // Error: multiple 'print' found
612
+ // Bar::print(&b); // Still an error: multiple `print` found
597
613
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 );
600
616
}
601
617
```
602
618
603
- Refer to [ RFC 132] for further details, motivations, and subtleties of syntax .
619
+ Refer to [ RFC 132] for further details and motivations .
604
620
605
621
[ RFC 132 ] : https://github.com/rust-lang/rfcs/blob/master/text/0132-ufcs.md
606
622
0 commit comments