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